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_ULongVector_cpp)
00056 #define ALIZE_ULongVector_cpp
00057
00058 #include <new>
00059 #include <math.h>
00060 #include <memory.h>
00061 #include <cstdlib>
00062 #include <iostream>
00063 #include <fstream>
00064 #include "ULongVector.h"
00065 #include "alizeString.h"
00066 #include "Exception.h"
00067
00068 using namespace alize;
00069 using namespace std;
00070
00071
00072 ULongVector::ULongVector(unsigned long capacity, unsigned long size)
00073 :Object(), _size(0), _capacity(capacity!=0?capacity:1),
00074 _array(createArray()) { setSize(size); }
00075
00076 ULongVector::ULongVector(const ULongVector& v)
00077 :Object(), _size(v._size), _capacity(v._size!=0?v._size:1),
00078 _array(createArray())
00079 { memcpy(_array, v._array, _size*sizeof(_array[0])); }
00080
00081 const ULongVector& ULongVector::operator=(const ULongVector& v)
00082 {
00083 if (this->isSameObject(v))
00084 return v;
00085 assert(_array != NULL);
00086 _size = v._size;
00087 if (_capacity < _size)
00088 {
00089 delete[] _array;
00090 _capacity = _size!=0?_size:1;
00091 _array = createArray();
00092 }
00093 memcpy(_array, v._array, _size*sizeof(_array[0]));
00094 return *this;
00095 }
00096
00097 bool ULongVector::operator==(const ULongVector& v) const
00098 {
00099 if (_size != v._size)
00100 return false;
00101 for (unsigned long i=0; i<_size; i++)
00102 {
00103 if (_array[i] != v._array[i])
00104 return false;
00105 }
00106 return true;
00107 }
00108
00109 bool ULongVector::operator!=(const ULongVector& v) const
00110 { return !(*this == v); }
00111
00112 ULongVector::_type* ULongVector::createArray() const
00113 {
00114 assert(_capacity != 0);
00115 _type* p = new (std::nothrow) _type[_capacity];
00116 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00117 return p;
00118 }
00119
00120 ULongVector::_type& ULongVector::operator[](unsigned long i)
00121 {
00122 assertIsInBounds(__FILE__, __LINE__, i, _size);
00123 assert(_array != NULL);
00124 return _array[i];
00125 }
00126
00127 ULongVector::_type ULongVector::operator[](unsigned long i) const
00128 {
00129 assertIsInBounds(__FILE__, __LINE__, i, _size);
00130 assert(_array != NULL);
00131 return _array[i];
00132 }
00133
00134 ULongVector& ULongVector::addValue(_type element)
00135 {
00136 assert(_array != NULL);
00137 if (_size == _capacity)
00138 {
00139 _capacity += _capacity;
00140 _type* oldArray = _array;
00141 _array = createArray();
00142 memcpy(_array, oldArray, _size*sizeof(_array[0]));
00143 delete[] oldArray;
00144 }
00145 _array[_size] = element;
00146 _size++;
00147 return *this;
00148 }
00149
00150 void ULongVector::addValue(const ULongVector& v)
00151 {
00152 for (unsigned long i=0; i<v._size; i++)
00153 { addValue(v._array[i]); }
00154
00155 }
00156
00157 void ULongVector::setSize(unsigned long size)
00158 {
00159 assert(_array != NULL);
00160 if (size > _capacity)
00161 {
00162 unsigned long oldSize = _size;
00163 _size = size;
00164 _capacity = _size;
00165 _type* oldArray = _array;
00166 _array = createArray();
00167 memcpy(_array, oldArray, oldSize*sizeof(_array[0]));
00168 delete[] oldArray;
00169 }
00170 else
00171 _size = size;
00172 }
00173
00174 int ULongVector::compare(const void *s1, const void *s2)
00175 {
00176 if (*((_type*)s1) > *((_type*)s2))
00177 return 1;
00178 if (*((_type*)s1) < *((_type*)s2))
00179 return -1;
00180 return 0;
00181 }
00182
00183 void ULongVector::ascendingSort() const
00184 {
00185 assert(_array != NULL);
00186 qsort(_array, _size, sizeof(_type), compare);
00187 }
00188
00189 void ULongVector::removeValues(unsigned long first, unsigned long last)
00190 {
00191 if (last>_size-1)
00192 last = _size-1;
00193 if (first > last)
00194 return;
00195 last++;
00196 assert(_array != NULL);
00197 for (; last<_size; first++, last++)
00198 { _array[first] = _array[last]; }
00199 _size -= last-first;
00200 }
00201
00202 ULongVector::_type* ULongVector::getArray() const { return _array; }
00203
00204 void ULongVector::clear() { _size = 0; }
00205
00206 unsigned long ULongVector::size() const { return _size; }
00207
00208 String ULongVector::getClassName() const { return "ULongVector"; }
00209
00210 String ULongVector::toString() const
00211 {
00212 String s = Object::toString() + " size = " + String::valueOf(_size);
00213 for (unsigned long i=0; i<_size; i++)
00214 {
00215 s += " \n [" + String::valueOf(i) + "] = "
00216 + String::valueOf(_array[i]);
00217 }
00218 return s;
00219 }
00220
00221 ULongVector::~ULongVector() { delete[] _array; }
00222
00223 void ULongVector::setAllValues(unsigned long u) {
00224 for (unsigned long i=0; i< _size; i++)
00225 _array[i] = u;
00226 }
00227
00228 void ULongVector::save(const FileName& f) {
00229 try {
00230 ofstream outputMat(f.c_str(),ios::out|ios::binary);
00231 if(!outputMat)
00232 throw IOException("Cannot open file", __FILE__, __LINE__,f);
00233 outputMat.write((char*)&_size,sizeof(_size));
00234 outputMat.write((char*)_array,_size*sizeof(unsigned long));
00235 outputMat.close();
00236 }
00237 catch (Exception& e) {cout << e.toString().c_str() << endl;}
00238 }
00239
00240 void ULongVector::load(const FileName& f) {
00241 try {
00242 ifstream inputVect(f.c_str(),ios::in|ios::binary);
00243 if(!inputVect)
00244 throw IOException("Cannot open file", __FILE__, __LINE__,f);
00245 inputVect.read((char*)&_size,sizeof(_size));
00246 if (_capacity != 0) delete[] _array;
00247 _array = new unsigned long[_size];
00248 if (!_array)
00249 throw Exception("Memory allocation exception", __FILE__, __LINE__);
00250 _capacity=_size;
00251 inputVect.read((char*)_array,_size*sizeof(unsigned long));
00252 inputVect.close();
00253 }
00254 catch (Exception& e) {cout << e.toString().c_str() << endl;}
00255 }
00256
00257
00258 #endif // ALIZE_ULongVector_cpp