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