/usr/include/shogun/lib/DynamicObjectArray.h is in libshogun-dev 1.1.0-4ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | /*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 1999-2009 Soeren Sonnenburg
* Written (W) 2011 Heiko Strathmann
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#ifndef _DYNAMIC_OBJECT_ARRAY_H_
#define _DYNAMIC_OBJECT_ARRAY_H_
#include <shogun/base/SGObject.h>
#include <shogun/base/DynArray.h>
#include <shogun/base/Parameter.h>
namespace shogun
{
/** @brief Template Dynamic array class that creates an array that can
* be used like a list or an array.
*
* It grows and shrinks dynamically, while elements can be accessed
* via index. It only stores CSGObject pointers, which ARE automagically
* SG_REF'd/deleted.
*
* Note that this array is generic, but only takes pointers to objects which
* implement the CSGObject interface, so only put these in here.
* T specifies the type of the pointers
*/
template<class T>class CDynamicObjectArray :public CSGObject
{
public:
/** constructor
*
* @param p_resize_granularity resize granularity
*/
CDynamicObjectArray(int32_t p_resize_granularity=128)
: CSGObject()
{
CSGObject*** casted_array=(CSGObject***)&m_array.array;
m_parameters->add_vector(casted_array, &m_array.num_elements, "array",
"Memory for dynamic array.");
m_parameters->add(&m_array.last_element_idx, "last_element_idx",
"Element with largest index.");
m_parameters->add(&m_array.resize_granularity, "resize_granularity",
"shrink/grow step size.");
}
virtual ~CDynamicObjectArray() { unref_all(); }
/** set the resize granularity
*
* @param g new granularity
* @return what has been set (minimum is 128)
*/
inline int32_t set_granularity(int32_t g)
{ return m_array.set_granularity(g); }
/** get number of elements
*
* @return number of elements
*/
inline int32_t get_num_elements() const
{
return m_array.get_num_elements();
}
/** get array element at index
*
* (does NOT do bounds checking)
*
* @param index index
* @return array element at index
*/
inline T* get_element(int32_t index) const
{
T* element=m_array.get_element(index);
CSGObject* casted=cast_to_sgobject(element);
SG_REF(casted);
return element;
}
/** get array element at index
*
* (does bounds checking)
*
* @param index index
* @return array element at index
*/
inline T* get_element_safe(int32_t index) const
{
T* element=m_array.get_element_safe(index);
CSGObject* casted=(CSGObject*)element;
SG_REF(casted);
return element;
}
/** set array element at index
*
* @param element element to set
* @param index index
* @return if setting was successful
*/
inline bool set_element(T* element, int32_t index)
{
CSGObject* casted=cast_to_sgobject(element);
CSGObject* old=(CSGObject*)m_array.get_element(index);
bool success=m_array.set_element(element, index);
if (success)
{
SG_REF(casted);
SG_UNREF(old);
}
/* ref before unref to prevent deletion if new=old */
return success;
}
/** insert array element at index
*
* @param element element to insert
* @param index index
* @return if setting was successful
*/
inline bool insert_element(T* element, int32_t index)
{
CSGObject* casted=cast_to_sgobject(element);
bool success=m_array.insert_element(element, index);
if (success)
SG_REF(casted);
return success;
}
/** append array element to the end of array
*
* @param element element to append
* @return if setting was successful
*/
inline bool append_element(T* element)
{
CSGObject* casted=cast_to_sgobject(element);
bool success=m_array.append_element(element);
if (success)
SG_REF(casted);
return success;
}
/** STD VECTOR compatible. Append array element to the end
* of array.
*
* @param element element to append
*/
inline void push_back(T* element)
{
CSGObject* casted=cast_to_sgobject(element);
SG_REF(casted);
m_array.push_back(element);
}
/** STD VECTOR compatible. Delete array element at the end
* of array.
*/
inline void pop_back()
{
CSGObject* element=(CSGObject*)m_array.back();
SG_UNREF(element);
m_array.pop_back();
}
/** STD VECTOR compatible. Return array element at the end
* of array.
*
* @return element at the end of array
*/
inline T* back() const
{
T* element=m_array.back();
CSGObject* casted=(CSGObject*)element;
SG_REF(casted);
return element;
}
/** find first occurence of array element and return its index
* or -1 if not available
*
* @param element element to search for
* @return index of element or -1
*/
inline int32_t find_element(T* element) const
{
return m_array.find_element(element);
}
/** delete array element at idx
* (does not call SG_FREE() or the like)
*
* @param idx index
* @return if deleting was successful
*/
inline bool delete_element(int32_t idx)
{
CSGObject* element=(CSGObject*)m_array.get_element(idx);
SG_UNREF(element);
return m_array.delete_element(idx);
}
/** clear the array (with zeros) */
inline void clear_array()
{
unref_all();
m_array.clear_array();
}
/** operator overload for array assignment
*
* @param orig original array
* @return new array
*/
inline CDynamicObjectArray<T>& operator=(CDynamicObjectArray<T>& orig)
{
/* SG_REF all new elements (implicitly) */
for (index_t i=0; i<orig.get_num_elements(); ++i)
orig.get_element(i);
/* unref after adding to avoid possible deletion */
unref_all();
/* copy pointer DynArray */
m_array=orig.m_array;
return *this;
}
/** @return underlying array of pointers */
inline T** get_array() const { return m_array.get_array(); }
/** shuffles the array */
inline void shuffle() { m_array.shuffle(); }
/** @return object name */
inline virtual const char* get_name() const
{ return "DynamicObjectArray"; }
private:
/** de-reference all elements of this array once */
inline void unref_all()
{
/* SG_UNREF all my elements */
for (index_t i=0; i<m_array.get_num_elements(); ++i)
{
CSGObject* element=(CSGObject*)m_array.get_element(i);
SG_UNREF(element);
}
}
/** Dynamically casts the given element to a CSGObject, if non-NULL.
* Used for all method that have some input elements that are to be put
* into the array */
inline CSGObject* cast_to_sgobject(T* element) const
{
if (!element)
return NULL;
CSGObject* casted=dynamic_cast<CSGObject*>(element);
if (!casted)
{
SG_ERROR("Generic type of CDynamicObjectArray is not of type "
"CSGObject!\n");
}
return casted;
}
private:
DynArray<T*> m_array;
};
}
#endif /* _DYNAMIC_OBJECT_ARRAY_H_ */
|