Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if !defined(ALIZE_RefVector_h)
00056 #define ALIZE_RefVector_h
00057
00058 #if defined(_WIN32)
00059 #if defined(ALIZE_EXPORTS)
00060 #define ALIZE_API __declspec(dllexport)
00061 #else
00062 #define ALIZE_API __declspec(dllimport)
00063 #endif
00064 #else
00065 #define ALIZE_API
00066 #endif
00067
00068 #if defined(_WIN32)
00069 #pragma warning( disable : 4150) // disable warning 4150 due to virtual SegCluster functions
00070 #endif
00071
00072
00073 #include "Object.h"
00074 #include <new>
00075 #include <memory.h>
00076 #include "DoubleSquareMatrix.h"
00077 #include "Exception.h"
00078 #include "Feature.h"
00079
00080 namespace alize
00081 {
00090
00091 template <class T> class ALIZE_API RefVector : public Object
00092 {
00093 friend class TestRefVector;
00094
00095 public:
00096
00104 explicit RefVector(unsigned long capacity = 1)
00105 :Object(), _size(0), _capacity(capacity!=0?capacity:1),
00106 _array(createArray()) {}
00107
00108 RefVector(const RefVector<T>& v)
00109 :Object(), _size(v._size), _capacity(v._size!=0?v._size:1),
00110 _array(createArray())
00111 { memcpy(_array, v._array, _size*sizeof(_array[0])); }
00112
00113 const RefVector<T>& operator=(const RefVector<T>& v)
00114 {
00115 if (this != &v)
00116 {
00117 assert(_array != NULL);
00118 _size = v._size;
00119 if (_capacity < _size)
00120 {
00121 delete[] _array;
00122 _capacity = _size!=0?_size:1;
00123 _array = createArray();
00124 }
00125 memcpy(_array, v._array, _size*sizeof(_array[0]));
00126 }
00127 return *this;
00128 }
00129
00130 virtual ~RefVector() { delete[] _array; }
00131
00132 void clear() { _size = 0; }
00133
00139 void setCapacity(unsigned long newCapacity)
00140 {
00141 T** oldArray = _array;
00142 _capacity = newCapacity!=0?newCapacity:1;
00143 _array = createArray();
00144 memcpy(_array, oldArray, _size*sizeof(_array[0]));
00145 delete[] oldArray;
00146 if (_size > _capacity)
00147 _size = _capacity;
00148 }
00149
00150 unsigned long size() const { return _size; }
00151 bool isEmpty() const{ return _size == 0; }
00152
00157 unsigned long addObject(T& o)
00158 {
00159 assert(_array != NULL);
00160 if (_size == _capacity)
00161 {
00162 _capacity += _capacity;
00163 T** oldArray = _array;
00164 _array = createArray();
00165 memcpy(_array, oldArray, _size*sizeof(_array[0]));
00166 delete[] oldArray;
00167 }
00168 _array[_size] = &o;
00169 _size++;
00170 return _size-1;
00171 }
00172
00178 unsigned long addObject(T& o, unsigned long i)
00179 {
00180
00181 if (i>=_size)
00182 return addObject(o);
00183 addObject(getObject(_size-1));
00184 for (long t=_size-3; t>=(long)i; t--)
00185 setObject(getObject(t), t+1);
00186 setObject(o, i);
00187 return i;
00188 }
00189
00196 void setObject(T& o, unsigned long index) const
00197 {
00198 assertIsInBounds(__FILE__, __LINE__, index, _size);
00199 assert(_array != NULL);
00200 _array[index] = &o;
00201 }
00202
00208 T& getObject(unsigned long index) const
00209 {
00210 assertIsInBounds(__FILE__, __LINE__, index, _size);
00211 assert(_array != NULL);
00212 return *_array[index];
00213 }
00214
00215 T& operator[](unsigned long index)
00216 {
00217 assertIsInBounds(__FILE__, __LINE__, index, _size);
00218 assert(_array != NULL);
00219 return *_array[index];
00220 }
00221 const T& operator[](unsigned long index) const
00222 {
00223 assertIsInBounds(__FILE__, __LINE__, index, _size);
00224 assert(_array != NULL);
00225 return *_array[index];
00226 }
00227
00233 unsigned long getObjectIndex(const T& o) const
00234 {
00235 for (unsigned long i=0; i< _size; i++)
00236 if (_array[i] == &o)
00237 return i;
00238 throw Exception("Object not found", __FILE__, __LINE__);
00239 return 0;
00240 }
00241
00248 void removeObjects(unsigned long first, unsigned long last,
00249 DeleteFlag fl = DO_NOT_DELETE)
00250 {
00251 if (last>_size-1)
00252 last = _size-1;
00253 if (first > last)
00254 return;
00255 assert(_array != NULL);
00256 if (fl == DELETE)
00257 for (unsigned long i=first; i<=last; i++)
00258 delete _array[i];
00259 last++;
00260 for (; last<_size; first++, last++)
00261 _array[first] = _array[last];
00262 _size -= last-first;
00263 }
00264
00269 T& removeObject(unsigned long i)
00270 {
00271 T& o = getObject(i);
00272 for (; i+1<_size; i++)
00273 _array[i] = _array[i+1];
00274 _size--;
00275 return o;
00276 }
00277
00284 const T& removeObject(const T& o)
00285 {
00286 for (unsigned long i=0; i<_size; i++)
00287 {
00288 if (_array[i] == &o)
00289 {
00290 for (i++; i<_size; i++)
00291 _array[i-1] = _array[i];
00292 _size--;
00293 return o;
00294 }
00295 }
00296 throw Exception("Object not found in the vector", __FILE__, __LINE__);
00297 }
00298
00303 T** getArray() const { return _array; }
00304
00305 void deleteAllObjects(unsigned long first = 0)
00306 {
00307
00308 for (unsigned long i=first; i<_size; i++)
00309 delete _array[i];
00310 _size = first<_size?first:_size;
00311
00312 }
00313
00314 virtual String getClassName() const { return "RefVector"; }
00315
00316 private:
00317
00318 unsigned long _size;
00319 unsigned long _capacity;
00320 T** _array;
00321
00322 T** createArray()
00323 {
00324 assert(_capacity != 0);
00325 T** p = new (std::nothrow) T*[_capacity];
00326 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00327 return p;
00328 }
00329
00330
00331 };
00332
00333 typedef RefVector<Object> ObjectRefVector;
00334
00335 #if defined(_WIN32)
00336 template class RefVector<Object>;
00337 template class RefVector<DoubleSquareMatrix>;
00338 template class RefVector<DoubleVector>;
00339 template class RefVector<SegCluster>;
00340 template class RefVector<MixtureGDStat>;
00341 template class RefVector<Feature>;
00342 #endif
00343
00344 }
00345
00346 #endif // ALIZE_RefVector_h
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356