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_RealVector_h)
00056 #define ALIZE_RealVector_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 #include <new>
00069 #include <math.h>
00070 #include <memory.h>
00071 #include <cstdlib>
00072 #include "alizeString.h"
00073 #include "Exception.h"
00074
00075 namespace alize
00076 {
00082
00083 template <class T> class ALIZE_API RealVector : public Object
00084 {
00085 ;
00086
00087 friend class TestDoubleVector;
00088 friend class TestFloatVector;
00089 friend class TestRealVector;
00090
00091 public:
00092
00099 explicit RealVector(unsigned long capacity = 0, unsigned long size = 0)
00100 :Object(), _size(0), _capacity(capacity!=0?capacity:1),
00101 _array(createArray())
00102 {
00103 setSize(size);
00104 }
00105
00106 RealVector(const RealVector<T>& v)
00107 :Object(), _size(v._size), _capacity(v._size!=0?v._size:1),
00108 _array(createArray())
00109 {
00110 memcpy(_array, v._array, _size*sizeof(_array[0]));
00111 }
00112
00113 static RealVector<T>& create(unsigned long capacity = 0,
00114 unsigned long size = 0)
00115 {
00116 RealVector<T>* p = new (std::nothrow) RealVector<T>(capacity, size);
00117 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00118 return *p;
00119 }
00120
00121 const RealVector<T>& operator=(const RealVector<T>& v)
00122 {
00123 if (this->isSameObject(v))
00124 return v;
00125 assert(_array != NULL);
00126 _size = v._size;
00127 if (_capacity < _size)
00128 {
00129 delete[] _array;
00130 _capacity = _size!=0?_size:1;
00131 _array = createArray();
00132 }
00133 memcpy(_array, v._array, _size*sizeof(_array[0]));
00134 return *this;
00135 }
00136
00137 const RealVector<T>& operator+=(const RealVector<T>& v)
00138 {
00139 if (_size != v._size)
00140 throw Exception("Mismatch vector sizes", __FILE__, __LINE__);
00141 for (unsigned long i=0; i<_size; i++)
00142 _array[i] += v._array[i];
00143 return *this;
00144 }
00145 const RealVector<T>& operator-=(const RealVector<T>& v)
00146 {
00147 if (_size != v._size)
00148 throw Exception("Mismatch vector sizes", __FILE__, __LINE__);
00149 for (unsigned long i=0; i<_size; i++)
00150 _array[i] -= v._array[i];
00151 return *this;
00152 }
00153
00154 bool operator==(const RealVector<T>& v) const
00155 {
00156 if (_size != v._size)
00157 return false;
00158 for (unsigned long i=0; i<_size; i++)
00159 {
00160 if (_array[i] != v._array[i])
00161 return false;
00162 }
00163 return true;
00164 }
00165
00166 bool operator!=(const RealVector<T>& v) const
00167 {
00168 return !(*this == v);
00169 }
00170
00171 virtual ~RealVector()
00172 {
00173 delete[] _array;
00174 }
00175
00176 unsigned long size() const
00177 {
00178 return _size;
00179 }
00180
00181 void clear()
00182 {
00183 _size = 0;
00184 }
00185
00191 void setSize(const unsigned long size,
00192 const bool updateCapacity = false)
00193 {
00194 assert(_array != NULL);
00195 if ((size > _capacity) || (size < _capacity && updateCapacity))
00196 {
00197 unsigned long oldSize = _size;
00198 _size = size;
00199 _capacity = _size;
00200 if (_capacity == 0)
00201 _capacity = 1;
00202 T* oldArray = _array;
00203 _array = createArray();
00204 memcpy(_array, oldArray, (size>oldSize?oldSize:size)*sizeof(_array[0]));
00205 delete[] oldArray;
00206
00207
00208 }
00209 else
00210 _size = size;
00211 }
00212
00216 void addValue(T v)
00217 {
00218 assert(_array != NULL);
00219 if (_size == _capacity)
00220 {
00221 _capacity += _capacity;
00222 T* oldArray = _array;
00223 _array = createArray();
00224 memcpy(_array, oldArray, _size*sizeof(_array[0]));
00225 delete[] oldArray;
00226 }
00227 _array[_size] = v;
00228 _size++;
00229 }
00230
00234 void addValue(const RealVector<T>& v)
00235 {
00236 for (unsigned long i=0; i<v._size; i++)
00237 {
00238 addValue(v._array[i]);
00239 }
00240
00241 }
00242
00247 void setValues(const RealVector<T>& v)
00248 {
00249 if (_size != v._size)
00250 throw Exception("Cannot set values : vector size mismatch ",
00251 __FILE__, __LINE__);
00252 memcpy(_array, v._array, _size*sizeof(_array[0]));
00253 }
00254
00258 void setAllValues(T v)
00259 {
00260 for (unsigned long i=0; i< _size; i++)
00261 _array[i] = v;
00262 }
00263
00267 T computeSum() const
00268 {
00269 T s = 0.0;
00270 for (unsigned long i=0; i<_size; i++)
00271 s += _array[i];
00272 return s;
00273 }
00274
00279 const RealVector<T>& operator*=(double s)
00280 {
00281 for (unsigned long i=0; i<_size; i++)
00282 _array[i] *= (T)s;
00283 return *this;
00284 }
00285
00291 T& operator[](unsigned long i)
00292 {
00293 assertIsInBounds(__FILE__, __LINE__, i, _size);
00294 assert(_array != NULL);
00295 return _array[i];
00296 }
00297
00300 T operator[](unsigned long i) const
00301 {
00302 assertIsInBounds(__FILE__, __LINE__, i, _size);
00303 assert(_array != NULL);
00304 return _array[i];
00305 }
00306
00309 void ascendingSort() const
00310 {
00311 assert(_array != NULL);
00312 qsort(_array, _size, sizeof(T), compare);
00313 }
00314
00319 unsigned long getIndexOfLargestValue()
00320 {
00321 if (_size == 0)
00322 throw Exception("Empty vector : cannot find the largest value", __FILE__, __LINE__);
00323 unsigned long i, maxI = 0;
00324 T v = _array[maxI];
00325
00326 for (i=1; i<_size; i++)
00327 if (_array[i] > v)
00328 {
00329 maxI = i;
00330 v = _array[maxI];
00331 }
00332 return maxI;
00333 }
00334
00339 T* getArray() const
00340 {
00341 return _array;
00342 }
00343
00344 virtual String getClassName() const
00345 {
00346 return "RealVector";
00347 }
00348
00349 virtual String toString() const
00350 {
00351 String s = Object::toString()
00352 + "\n size = " + String::valueOf(size());
00353 for (unsigned long i=0; i<size(); i++)
00354 s += "\n value[" + String::valueOf(i) + "] = "
00355 + String::valueOf(operator[](i));
00356 return s;
00357 }
00358
00359 private:
00360
00361 unsigned long _size;
00362 unsigned long _capacity;
00363 T* _array;
00364
00365 T* createArray() const
00366 {
00367 assert(_capacity != 0);
00368 T* p = new (std::nothrow) T[_capacity];
00369 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00370 return p;
00371 }
00372 static int compare(const void* s1, const void* s2)
00373 {
00374 if (*((T*)s1) > *((T*)s2))
00375 return 1;
00376 if (*((T*)s1) < *((T*)s2))
00377 return -1;
00378 return 0;
00379 }
00380
00381
00382 };
00383
00384 typedef RealVector<double> DoubleVector;
00385 typedef RealVector<float> FloatVector;
00386
00387 #if defined(_WIN32)
00388 template class RealVector<double>;
00389 template class RealVector<float>;
00390 #endif
00391
00392 }
00393
00394 #endif // ALIZE_RealVector_h