Home || Visual Search || Applications || Architecture || Important Messages || OGL || Src

ColumnTem.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Column_ColumnTem_h
00002 #define Impala_Core_Column_ColumnTem_h
00003 
00004 #include <iostream>
00005 
00006 #include "Basis/ILog.h"
00007 
00008 namespace Impala
00009 {
00010 namespace Core
00011 {
00012 namespace Column
00013 {
00014 
00015 
00019 template <class ElemT>
00020 class ColumnTem
00021 {
00022 public:
00023 
00024     typedef ElemT ColElemType;
00025 
00027     ColumnTem()
00028     {
00029         mCapacity = 0;
00030         mData = 0;
00031     }
00032 
00034     ColumnTem(int n)
00035     {
00036         mCapacity = n;
00037         mData = new ElemT[mCapacity];
00038     }
00039 
00041     ColumnTem(const ColumnTem& c)
00042     {
00043         mCapacity = c.mCapacity;
00044         mData = new ElemT[mCapacity];
00045         for (int i=0 ; i<mCapacity ; i++)
00046             mData[i] = c.mData[i];
00047     }
00048 
00050     virtual
00051     ~ColumnTem()
00052     {
00053         if (mData)
00054             delete [] mData; 
00055     }
00056 
00058     bool
00059     Valid() const
00060     {
00061         return true;
00062     }
00063 
00066     int
00067     Capacity() const
00068     { 
00069         return mCapacity; 
00070     }
00071     int
00072     Size() const
00073     { 
00074         return mCapacity; 
00075     }
00077 
00079     bool
00080     Reserve(int newCapacity, bool copyData)
00081     {
00082         if (newCapacity == mCapacity)
00083             return true;
00084         int common = (newCapacity > mCapacity) ? mCapacity : newCapacity;
00085         mCapacity = newCapacity;
00086         ElemT* newData = new ElemT[mCapacity];
00087         if (newData == 0)
00088         {
00089             ILOG_ERROR("ColumnTem: could not resize, no memory");
00090             return false;
00091         }
00092         if (copyData)
00093             for (int i=0 ; i<common ; i++)
00094                 newData[i] = mData[i];
00095         delete [] mData;
00096         mData = newData;
00097         return true;
00098     }
00099 
00101     ColumnTem&
00102     operator=(const ColumnTem& c)
00103     {
00104         if (this != &c)
00105         {
00106             if (mCapacity != c.mCapacity)
00107             {
00108                 delete [] mData;
00109                 mCapacity = c.mCapacity;
00110                 mData = new ElemT[mCapacity];
00111             }
00112             for (int i=0 ; i<mCapacity ; i++)
00113                 mData[i] = c.mData[i];
00114         }
00115         return *this;
00116     }
00117 
00119     ElemT&
00120     operator[](int i)
00121     { 
00122         //CheckIndex(i, "operator_array");
00123         return mData[i];
00124     }
00125 
00126     ElemT&
00127     Elem(int i)
00128     { 
00129         //CheckIndex(i, "Elem");
00130         return mData[i];
00131     }
00132 
00133     ElemT
00134     Get(int i) const
00135     { 
00136         //CheckIndex(i, "Get");
00137         return mData[i];
00138     }
00139 
00140     void
00141     Set(int i, ElemT e)
00142     {
00143         //CheckIndex(i, "Set");
00144         mData[i] = e;
00145     }
00146 
00147     // direct access, use with care
00148 
00149     ElemT*
00150     GetData()
00151     { 
00152         return mData;
00153     }
00154 
00155     void
00156     SetData(ElemT* data, int nrElem, bool copy = true)
00157     {
00158         if (mData)
00159             delete mData;
00160         if (copy)
00161         {
00162             mData = new ElemT[nrElem];
00163             size_t numBytes = nrElem * sizeof(ElemT);
00164             memcpy(mData, (void*) data, numBytes);
00165         }
00166         else
00167             mData = data;
00168         mCapacity = nrElem;
00169     }
00170 
00171     void
00172     Print(int from=0, int to=-1)
00173     {
00174         if (to < 0 || to > mCapacity)
00175             to = mCapacity - 1;
00176         for (int i=from ; i<to ; ++i)
00177             std::cout << mData[i] << std::endl;
00178         std::cout << std::endl;
00179     }
00180 
00181 private:
00182 
00183     void
00184     CheckIndex(int index, String tag = "CheckIndex") const
00185     {
00186         if (index >= mCapacity)
00187             ILOG_ERROR("CheckIndex: [" << tag << "] Index out of range: " <<
00188                        index);
00189     }
00190 
00191     int    mCapacity; // number of elements
00192     ElemT* mData;     // data pointer
00193 
00194     ILOG_VAR_DEC;
00195 
00196 };
00197 
00198 ILOG_VAR_INIT_TEMPL_1(ColumnTem, ElemT, Impala.Core.Column);
00199 
00200 } // namespace Column
00201 } // namespace Core
00202 } // namespace Impala
00203 
00204 #endif

Generated on Thu Jan 13 09:04:22 2011 for ImpalaSrc by  doxygen 1.5.1