00001 #ifndef Impala_Core_Vector_VectorSet_h
00002 #define Impala_Core_Vector_VectorSet_h
00003
00004 #include <vector>
00005 #include "Core/Array/Arrays.h"
00006 #include "Core/Vector/VectorTem.h"
00007 #include "Core/Array/ReadRaw.h"
00008 #include "Core/Array/WriteRaw.h"
00009 #include "Core/Array/SetPart.h"
00010
00011 namespace Impala
00012 {
00013 namespace Core
00014 {
00015 namespace Vector
00016 {
00017
00018
00019
00020
00021
00022
00023 template <class ArrayT>
00024 class VectorSet
00025 {
00026 public:
00027
00028 typedef typename ArrayT::StorType ElemT;
00029 typedef VectorTem<ElemT> VectorT;
00030
00031 typedef VectorTem<ElemT> ColElemType;
00032
00033
00034
00035
00036 VectorSet(bool hasConstVecSize, int vecSize, int capacity)
00037 {
00038 mHasConstVecSize = hasConstVecSize;
00039 mVecSize = vecSize;
00040 mCapacity = capacity;
00041 mLast = 0;
00042 mFirst = 0;
00043 mLength = 0;
00044 mStorage = new ArrayT(mVecSize, mCapacity, 0, 0);
00045 if (!mHasConstVecSize)
00046 {
00047 mFirst = new Array::Array2dScalarInt32(mCapacity, 1, 0, 0);
00048 mLength = new Array::Array2dScalarInt32(mCapacity, 1, 0, 0);
00049 }
00050 }
00051
00052 VectorSet(const VectorSet& vs)
00053 {
00054 mHasConstVecSize = vs.mHasConstVecSize;
00055 mVecSize = vs.mVecSize;
00056 mCapacity = vs.mCapacity;
00057 mLast = 0;
00058 mFirst = 0;
00059 mLength = 0;
00060 mStorage = new ArrayT(mVecSize, mCapacity, 0, 0);
00061 if (!mHasConstVecSize)
00062 {
00063 mFirst = new Array::Array2dScalarInt32(mCapacity, 1, 0, 0);
00064 mLength = new Array::Array2dScalarInt32(mCapacity, 1, 0, 0);
00065 }
00066 }
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 virtual
00077 ~VectorSet()
00078 {
00079 if (mFirst)
00080 delete mFirst;
00081 if (mLength)
00082 delete mLength;
00083 delete mStorage;
00084 }
00085
00086 int
00087 Size() const
00088 {
00089 return mLast;
00090 }
00091
00092 void
00093 SetSize(int nr)
00094 {
00095 mLast = nr;
00096 }
00097
00098 int
00099 Capacity() const
00100 {
00101 return mCapacity;
00102 }
00103
00104 bool
00105 HasConstVectorSize() const
00106 {
00107 return mHasConstVecSize;
00108 }
00109
00110
00111
00112 VectorT
00113 GetVector(int id, bool wrapper) const
00114 {
00115 if (mHasConstVecSize)
00116 return VectorT(mVecSize, mStorage->CPB(0, id), wrapper);
00117 int first = mFirst->Value(id, 0);
00118 int length = mLength->Value(id, 0);
00119 return VectorT(length, mStorage->CPB(first, 0), wrapper);
00120 }
00121
00122 void
00123 AddVector(const VectorT& vec)
00124 {
00125
00126
00127 if (mLast == mCapacity)
00128 Reserve(Impala::Max(2*mCapacity, 1), true);
00129 if (!mHasConstVecSize)
00130 {
00131 int lastVec = mLast - 1;
00132 int first = 0;
00133 if (mLast != 0)
00134 first = mFirst->Value(lastVec, 0) + mLength->Value(lastVec, 0);
00135 *(mFirst->CPB(mLast, 0)) = first;
00136 *(mLength->CPB(mLast, 0)) = vec.Size();
00137 }
00138 GetVector(mLast++, true) = vec;
00139 }
00140
00141 void
00142 AddVector(Real64* data, int nrElem)
00143 {
00144 VectorT vec(nrElem, data, true);
00145 AddVector(vec);
00146 }
00147
00148
00149
00150
00151 int
00152 AllocateVector(int nr)
00153 {
00154 if (!mHasConstVecSize)
00155 return -1;
00156 if (mLast + nr >= mCapacity)
00157 Reserve(Impala::Max(2*mCapacity,mCapacity+nr), true);
00158 int idx = mLast;
00159 SetSize(mLast + nr);
00160 return idx;
00161 }
00162
00163
00164 VectorT
00165 Get(int i) const
00166 {
00167 return GetVector(i, false);
00168 }
00169
00170
00171 void
00172 Set(int i, VectorT v)
00173 {
00174 GetVector(i, true) = v;
00175 }
00176
00177
00178 bool
00179 Valid() const
00180 {
00181 return true;
00182 }
00183
00184 bool
00185 Reserve(int newCapacity, bool copyData)
00186 {
00187 if (newCapacity == mCapacity)
00188 return true;
00189 int common = (newCapacity > mCapacity) ? mCapacity : newCapacity;
00190 mCapacity = newCapacity;
00191 ArrayT* newStorage = new ArrayT(mVecSize, mCapacity, 0, 0);
00192 if (newStorage == 0)
00193 {
00194 std::cout << "VectorSet: could not resize, no memory" << std::endl;
00195 return false;
00196 }
00197 Array::Array2dScalarInt32* newFirst;
00198 Array::Array2dScalarInt32* newLength;
00199 if (!mHasConstVecSize)
00200 {
00201 newFirst = new Array::Array2dScalarInt32(mCapacity, 1, 0, 0);
00202 newLength = new Array::Array2dScalarInt32(mCapacity, 1, 0, 0);
00203 if ((newFirst == 0) || (newLength == 0))
00204 {
00205 std::cout << "VectorSet: could not resize, no memory" << std::endl;
00206 return false;
00207 }
00208 }
00209 if (copyData)
00210 {
00211 Array::SetPart(newStorage, mStorage, 0, 0, mVecSize, common, 0, 0);
00212 if (!mHasConstVecSize)
00213 {
00214 Array::SetPart(newFirst, mFirst, 0, 0, common, 1, 0, 0);
00215 Array::SetPart(newLength, mLength, 0, 0, common, 1, 0, 0);
00216 }
00217 }
00218 delete mStorage;
00219 mStorage = newStorage;
00220 if (!mHasConstVecSize)
00221 {
00222 delete mFirst;
00223 delete mLength;
00224 mFirst = newFirst;
00225 mLength = newLength;
00226 }
00227 if (mLast > mCapacity)
00228 mLast = mCapacity;
00229 return true;
00230 }
00231
00232 virtual void
00233 Dump(int from = 0, int to = -1)
00234 {
00235 Dump(from, to, std::cout);
00236 }
00237
00238 virtual void
00239 Dump(int from, int to, std::ostream& os)
00240 {
00241 if (to == -1)
00242 to = Size();
00243 if (to < from)
00244 to = from;
00245 os << "Dumping VectorSet from " << from << " to " << to
00246 << " (set size=" << Size() << ", capacity=" << Capacity()
00247 << ")" << std::endl;
00248 for (int i=from ; i<to ; i++)
00249 os << " " << i << " = " << GetVector(i, true) << std::endl;
00250 os << std::endl;
00251 }
00252
00253
00254
00255 ArrayT*
00256 GetStorage()
00257 {
00258 return mStorage;
00259 }
00260
00261 const ArrayT*
00262 GetStorage() const
00263 {
00264 return mStorage;
00265 }
00266
00267 void
00268 SetStorage(ArrayT* storage)
00269 {
00270 if (mStorage)
00271 delete mStorage;
00272 mStorage = storage;
00273 if (mHasConstVecSize)
00274 {
00275 mVecSize = mStorage->CW();
00276 mCapacity = mStorage->CH();
00277 }
00278 else
00279 {
00280 std::cout << "SetStorage: warning: only works on const size" << std::endl;
00281 }
00282 }
00283
00284 ElemT*
00285 GetVectorData(int id)
00286 {
00287 if (mHasConstVecSize)
00288 return mStorage->CPB(0, id);
00289 int first = mFirst->Value(id, 0);
00290 return mStorage->CPB(first, 0);
00291 }
00292
00293 const ElemT*
00294 GetVectorData(int id) const
00295 {
00296 if (mHasConstVecSize)
00297 return mStorage->CPB(0, id);
00298 int first = mFirst->Value(id, 0);
00299 return mStorage->CPB(first, 0);
00300 }
00301
00302 int
00303 GetVectorLength(int id) const
00304 {
00305 if (mHasConstVecSize)
00306 return mVecSize;
00307 return mLength->Value(id, 0);
00308 }
00309
00310 private:
00311
00312 bool mHasConstVecSize;
00313 int mVecSize;
00314 int mCapacity;
00315 int mLast;
00316
00317 ArrayT* mStorage;
00318
00319
00320 Array::Array2dScalarInt32* mFirst;
00321 Array::Array2dScalarInt32* mLength;
00322
00323 };
00324
00325 typedef VectorSet<Array::Array2dScalarReal64> ColumnVectorSet;
00326
00327 }
00328 }
00329 }
00330
00331 #endif