00001 /* 00002 * File: fixedarray.h 00003 * Summary: Fixed size 2D vector class that asserts if you do something bad. 00004 * Written by: Jesse Jones 00005 */ 00006 00007 #ifndef FIXARY_H 00008 #define FIXARY_H 00009 00010 #include "fixedvector.h" 00011 00012 // ========================================================================== 00013 // class FixedArray 00014 // ========================================================================== 00015 template <class TYPE, int WIDTH, int HEIGHT> class FixedArray { 00016 00017 //----------------------------------- 00018 // Types 00019 // 00020 public: 00021 typedef TYPE value_type; 00022 typedef TYPE& reference; 00023 typedef const TYPE& const_reference; 00024 typedef TYPE* pointer; 00025 typedef const TYPE* const_pointer; 00026 00027 typedef unsigned long size_type; 00028 typedef long difference_type; 00029 00030 // operator[] should return one of these to avoid breaking 00031 // client code (if inlining is on there won't be a speed hit) 00032 typedef FixedVector<TYPE, HEIGHT> Column; 00033 00034 //----------------------------------- 00035 // Initialization/Destruction 00036 // 00037 public: 00038 ~FixedArray() {} 00039 00040 FixedArray() {} 00041 00042 FixedArray(TYPE def) 00043 { 00044 init(def); 00045 } 00046 00047 //----------------------------------- 00048 // API 00049 // 00050 public: 00051 // ----- Size ----- 00052 bool empty() const { return WIDTH == 0 || HEIGHT == 0; } 00053 int size() const { return WIDTH*HEIGHT; } 00054 int width() const { return WIDTH; } 00055 int height() const { return HEIGHT; } 00056 00057 // ----- Access ----- 00058 Column& operator[](unsigned long index) { return mData[index]; } 00059 const Column& operator[](unsigned long index) const { 00060 return mData[index]; 00061 } 00062 00063 template<class Indexer> TYPE& operator () (const Indexer &i) { 00064 return mData[i.x][i.y]; 00065 } 00066 00067 template<class Indexer> const TYPE& operator () (const Indexer &i) const { 00068 return mData[i.x][i.y]; 00069 } 00070 00071 void init(const TYPE& def) { 00072 for (int i = 0; i < WIDTH; ++i) 00073 mData[i].init(def); 00074 } 00075 00076 protected: 00077 FixedVector<Column, WIDTH> mData; 00078 }; 00079 00080 // A fixed array centered around the origin. 00081 template <class TYPE, int RADIUS> class SquareArray { 00082 //----------------------------------- 00083 // Types 00084 // 00085 public: 00086 typedef TYPE value_type; 00087 typedef TYPE& reference; 00088 typedef const TYPE& const_reference; 00089 typedef TYPE* pointer; 00090 typedef const TYPE* const_pointer; 00091 00092 typedef unsigned long size_type; 00093 typedef long difference_type; 00094 00095 //----------------------------------- 00096 // Initialization/Destruction 00097 // 00098 public: 00099 ~SquareArray() {} 00100 00101 SquareArray() {} 00102 00103 SquareArray(TYPE def) 00104 { 00105 init(def); 00106 } 00107 00108 //----------------------------------- 00109 // API 00110 // 00111 public: 00112 // ----- Size ----- 00113 bool empty() const { return data.empty(); } 00114 int size() const { return data.size(); } 00115 int width() const { return data.width(); } 00116 int height() const { return data.height(); } 00117 00118 // ----- Access ----- 00119 template<class Indexer> TYPE& operator () (const Indexer &i) { 00120 return data[i.x+RADIUS][i.y+RADIUS]; 00121 } 00122 00123 template<class Indexer> const TYPE& operator () (const Indexer &i) const { 00124 return data[i.x+RADIUS][i.y+RADIUS]; 00125 } 00126 00127 void init(const TYPE& def) { 00128 data.init(def); 00129 } 00130 00131 protected: 00132 FixedArray<TYPE, 2*RADIUS+1, 2*RADIUS+1> data; 00133 }; 00134 00135 #endif // FIXARY_H