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_LKVector_cpp)
00056 #define ALIZE_LKVector_cpp
00057
00058 #include <new>
00059 #include <math.h>
00060 #include <memory.h>
00061 #include <cstdlib>
00062 #include "LKVector.h"
00063 #include "alizeString.h"
00064 #include "Exception.h"
00065
00066 using namespace alize;
00067
00068
00069 LKVector::LKVector(unsigned long capacity, unsigned long size)
00070 :Object(), _capacity(capacity!=0?capacity:1), _size(0),
00071 _array(createArray()) { setSize(size); }
00072
00073
00074
00075 LKVector::LKVector(const LKVector& obj){
00076 _capacity = obj._capacity;
00077 _size = 0;
00078 if (_capacity > 0) {
00079 _array = createArray();
00080
00081 for(unsigned long i = 0; i < obj._size; i++) {
00082 addValue(obj[i]);
00083 }
00084 }
00085 sumNonTopDistribWeights = obj.sumNonTopDistribWeights;
00086 sumNonTopDistribLK = obj.sumNonTopDistribLK;
00087 topDistribsCount = obj.topDistribsCount;
00088 }
00089
00090
00091 const LKVector& LKVector::operator=(const LKVector& obj){
00092 _capacity = obj._capacity;
00093 _size = 0;
00094 if (_capacity > 0) {
00095 _array = createArray();
00096 for(unsigned long i = 0; i < obj._size; i++) {
00097 addValue(obj[i]);
00098 }
00099 }
00100 sumNonTopDistribWeights = obj.sumNonTopDistribWeights;
00101 sumNonTopDistribLK = obj.sumNonTopDistribLK;
00102 topDistribsCount = obj.topDistribsCount;
00103 return *this;
00104 }
00105
00106 LKVector::type* LKVector::createArray() const
00107 {
00108 assert(_capacity != 0);
00109 type* p = new (std::nothrow) type[_capacity];
00110 assertMemoryIsAllocated(p, __FILE__, __LINE__);
00111 return p;
00112 }
00113
00114 LKVector::type& LKVector::operator[](unsigned long i)
00115 {
00116 assertIsInBounds(__FILE__, __LINE__, i, _size);
00117 assert(_array != NULL);
00118 return _array[i];
00119 }
00120
00121 const LKVector::type& LKVector::operator[](unsigned long i) const
00122 {
00123 assertIsInBounds(__FILE__, __LINE__, i, _size);
00124 assert(_array != NULL);
00125 return _array[i];
00126 }
00127
00128 void LKVector::addValue(type element)
00129 {
00130 assert(_array != NULL);
00131 if (_size == _capacity)
00132 {
00133 _capacity += _capacity;
00134 type* oldArray = _array;
00135 _array = createArray();
00136 memcpy(_array, oldArray, _size*sizeof(_array[0]));
00137 delete[] oldArray;
00138 }
00139 _array[_size] = element;
00140 _size++;
00141 }
00142
00143 void LKVector::pack(unsigned long size)
00144 {
00145 unsigned long oldSize = _size;
00146 _size = size;
00147 _capacity = _size;
00148 type* oldArray = _array;
00149 _array = createArray();
00150 unsigned long s = oldSize;
00151 if (oldSize > size) {
00152 s = size;
00153 }
00154 memcpy(_array, oldArray, s*sizeof(_array[0]));
00155 delete[] oldArray;
00156 }
00157
00158 void LKVector::setSize(unsigned long size)
00159 {
00160 assert(_array != NULL);
00161 if (size > _capacity)
00162 {
00163 unsigned long oldSize = _size;
00164 _size = size;
00165 _capacity = _size;
00166 type* oldArray = _array;
00167 _array = createArray();
00168 memcpy(_array, oldArray, oldSize*sizeof(_array[0]));
00169 delete[] oldArray;
00170 }
00171 else
00172 _size = size;
00173 }
00174
00175 int LKVector::compare(const void *s1, const void *s2)
00176 {
00177 if (((type*)s1)->lk > ((type*)s2)->lk)
00178 return -1;
00179 if (((type*)s1)->lk < ((type*)s2)->lk)
00180 return 1;
00181 return 0;
00182 }
00183
00184 void LKVector::descendingSort() const
00185 {
00186 assert(_array != NULL);
00187 qsort(_array, _size, sizeof(type), compare);
00188 }
00189
00190 LKVector::type* LKVector::getArray() const { return _array; }
00191
00192 void LKVector::clear() { _size = 0; }
00193
00194 unsigned long LKVector::size() const { return _size; }
00195
00196 String LKVector::getClassName() const { return "LKVector"; }
00197
00198 String LKVector::toString() const
00199 {
00200 String s = Object::toString() + "Size = " + String::valueOf(_size);
00201 for (unsigned long i=0; i<_size; i++)
00202 {
00203 s += "\n idx = " + String::valueOf(_array[i].idx)
00204 + " lk = " + String::valueOf(_array[i].lk);
00205 }
00206
00207 return s;
00208 }
00209
00210 LKVector::~LKVector() { delete[] _array; }
00211
00212
00213 #endif // ALIZE_LKVector_cpp