BWAPI
Aiur/include/BWTA/RectangleArray.h
Go to the documentation of this file.
00001 #pragma once
00002 
00003 namespace BWTA
00004 {
00008   template <class Type>
00009   class RectangleArray
00010    {
00011      public :
00017        RectangleArray(unsigned int width = 1, unsigned int height = 1, Type* data = NULL);
00019        RectangleArray(const RectangleArray<Type>& rectangleArray);
00021        ~RectangleArray(void);
00026        unsigned int getWidth(void) const;
00031        unsigned int getHeight(void) const;
00038        Type* getItem(unsigned int x, unsigned int y);
00039        inline Type* operator[](int i) { return this->getColumn(i); }
00040        inline Type const * const operator[](int i) const {return this->getColumn(i); }
00047        void setItem(unsigned int x, unsigned int y, Type *item);
00048        void resize(unsigned int width, unsigned int height);
00049        void printToFile(FILE* f);
00050        void saveToFile(const std::string& fileName);
00052        void setTo(const Type& value);
00053        void setBorderTo(const Type& value);
00054      private :
00055        bool owner;
00057        unsigned int width;
00059        unsigned int height;
00061        Type *data;
00063        Type **columns;
00068        Type getData(unsigned int index);
00074        Type *getColumn(unsigned int index);
00080        const Type *getColumn(unsigned int index) const;
00085        void setWidth(unsigned int width);
00090        void setHeight(unsigned int height);       
00091    };
00092   //---------------------------------------------- CONSTRUCTOR -----------------------------------------------
00093   template <class Type>
00094   RectangleArray<Type>::RectangleArray(unsigned int width, unsigned int height, Type* data)
00095   {
00096     this->setWidth(width);
00097     this->setHeight(height);
00098     this->owner = (data == NULL);
00099     if (this->owner)
00100       this->data = new Type[this->getWidth()*this->getHeight()];
00101     else
00102       this->data = data;
00103 
00104     columns = new Type*[this->getWidth()];
00105     unsigned int i = 0;
00106     for (unsigned int position = 0;i < width; i ++,position += height)
00107       columns[i] = &this->data[position];
00108   }
00109   //---------------------------------------------- CONSTRUCTOR -----------------------------------------------
00110   template <class Type>
00111   RectangleArray<Type>::RectangleArray(const RectangleArray<Type>& rectangleArray)
00112   :owner(true)
00113   {
00114     this->setWidth(rectangleArray.getWidth());
00115     this->setHeight(rectangleArray.getHeight());
00116     this->data = new Type[this->getWidth()*this->getHeight()];
00117     columns = new Type*[this->getWidth()];
00118     
00119     unsigned int i = 0;
00120     for (unsigned int position = 0;i < width; i ++,position += height)
00121       columns[i] = &data[position];
00122     memcpy(this->data, rectangleArray.data, sizeof(Type)*this->getWidth()*this->getHeight());
00123   }
00124   //----------------------------------------------- DESTRUCTOR -----------------------------------------------
00125   template <class Type>
00126   RectangleArray<Type>::~RectangleArray(void)
00127   {
00128      delete [] columns;
00129      if (this->owner)
00130        delete [] data;
00131   }
00132   //----------------------------------------------- GET WIDTH ------------------------------------------------
00133   template <class Type>
00134   unsigned int RectangleArray<Type>::getWidth(void) const
00135   {
00136     return this->width;
00137   }
00138   //----------------------------------------------- SET WIDTH ------------------------------------------------
00139   template <class Type>
00140   void RectangleArray<Type>::setWidth(unsigned int width)
00141   {
00142     this->width = width;
00143   }
00144   //----------------------------------------------- GET HEIGHT -----------------------------------------------
00145   template <class Type>
00146   unsigned int RectangleArray<Type>::getHeight(void) const
00147   {
00148     return this->height;
00149   }
00150   //----------------------------------------------- SET HEIGHT -----------------------------------------------
00151   template <class Type>
00152   void RectangleArray<Type>::setHeight(unsigned int height)
00153   {
00154     this->height = height;
00155   }
00156   //------------------------------------------------ GET ITEM ------------------------------------------------
00157   template <class Type>
00158   Type* RectangleArray<Type>::getItem(unsigned int x, unsigned int y)
00159   {
00160     return this->getColumn(x)[y];
00161   }
00162   //------------------------------------------------ SET ITEM ------------------------------------------------
00163   template <class Type>
00164   void RectangleArray<Type>::setItem(unsigned int x, unsigned int y, Type* item)
00165   {
00166     this->getColumn(x)[y] = item;
00167   }
00168   //------------------------------------------------ GET LINE ------------------------------------------------
00169   template <class Type>
00170   Type* RectangleArray<Type>::getColumn(unsigned int index)
00171   {
00172     return columns[index];
00173   }
00174   //------------------------------------------------ GET LINE ------------------------------------------------
00175   template <class Type>
00176   const Type* RectangleArray<Type>::getColumn(unsigned int index) const
00177   {
00178     return columns[index];
00179   }
00180   //------------------------------------------------- RESIZE -------------------------------------------------
00181   template <class Type>
00182   void RectangleArray<Type>::resize(unsigned int width, unsigned int height)
00183   {
00184     if (this->getWidth() == width &&
00185         this->getHeight() == height)
00186       return;
00187 
00188     delete [] this->columns;
00189     delete [] this->data;  
00190 
00191     this->setWidth(width);
00192     this->setHeight(height);
00193 
00194     this->data = new Type[this->width * this->height];
00195 
00196     this->columns = new Type*[this->width];
00197     unsigned int i = 0;
00198     for (unsigned int position = 0;i < this->width; i ++,position += this->height)
00199       columns[i] = &data[position];
00200   }
00201   //--------------------------------------------- PRINT TO FILE ----------------------------------------------
00202   template <class Type>
00203   void RectangleArray<Type>::printToFile(FILE* f)
00204   {
00205     for (unsigned int y = 0; y < this->getHeight(); y++)
00206     {
00207       for (unsigned int x = 0; x < this->getWidth(); x++)
00208       {
00209         char ch = this->getColumn(x)[y];
00210         fprintf_s(f, "%c", ch);
00211       }
00212       fprintf_s(f, "\n");
00213     }
00214   }
00215   //---------------------------------------------- SAVE TO FILE ----------------------------------------------
00216   template <class Type>
00217   void RectangleArray<Type>::saveToFile(const std::string& fileName)
00218   {
00219     FILE* f = fopen(fileName.c_str(), "wt");
00220     if (!f)
00221       exit(1);
00222     this->printToFile(f);
00223     fclose(f);
00224   }
00225   //------------------------------------------------- SET TO -------------------------------------------------
00226   template <class Type>
00227   void RectangleArray<Type>::setTo(const Type& value)
00228   {
00229     for (unsigned int i = 0; i < this->getWidth()*this->getHeight(); i++)
00230       this->data[i] = value;
00231   }
00232   //--------------------------------------------- SET BORDER TO ----------------------------------------------
00233   template <class Type>
00234   void RectangleArray<Type>::setBorderTo(const Type& value)
00235   {
00236     for (unsigned int i = 0; i < this->width; i++)
00237     {
00238       this->getColumn(i)[0] = value;
00239       this->getColumn(i)[this->height - 1] = value;
00240     }
00241     for (unsigned int i = 0; i < this->height; i++)
00242     {
00243       this->getColumn(0)[i] = value;
00244       this->getColumn(this->width - 1)[i] = value;
00245     }    
00246   }
00247   //----------------------------------------------------------------------------------------------------------
00248 }
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines