diff options
Diffstat (limited to '')
| -rw-r--r-- | src/array.h | 240 | 
1 files changed, 228 insertions, 12 deletions
| diff --git a/src/array.h b/src/array.h index 093ece2..9233875 100644 --- a/src/array.h +++ b/src/array.h | |||
| @@ -13,6 +13,7 @@ | |||
| 13 | 13 | ||
| 14 | namespace Bu | 14 | namespace Bu | 
| 15 | { | 15 | { | 
| 16 | subExceptionDecl( ArrayException ) | ||
| 16 | /** | 17 | /** | 
| 17 | * Array type container, just like a normal array only flexible and keeps | 18 | * Array type container, just like a normal array only flexible and keeps | 
| 18 | * track of your memory for you. | 19 | * track of your memory for you. | 
| @@ -41,10 +42,14 @@ namespace Bu | |||
| 41 | iSize( 0 ), | 42 | iSize( 0 ), | 
| 42 | iCapacity( 0 ) | 43 | iCapacity( 0 ) | 
| 43 | { | 44 | { | 
| 44 | // for( Link *pCur = src.pFirst; pCur; pCur = pCur->pNext ) | 45 | } | 
| 45 | // { | 46 | |
| 46 | // append( *pCur->pValue ); | 47 | Array( long iSetCap ) : | 
| 47 | // } | 48 | pData( NULL ), | 
| 49 | iSize( 0 ), | ||
| 50 | iCapacity( 0 ) | ||
| 51 | { | ||
| 52 | setCapacity( iSetCap ); | ||
| 48 | } | 53 | } | 
| 49 | 54 | ||
| 50 | ~Array() | 55 | ~Array() | 
| @@ -53,33 +58,244 @@ namespace Bu | |||
| 53 | } | 58 | } | 
| 54 | 59 | ||
| 55 | /** | 60 | /** | 
| 56 | * Clear the data from the list. | 61 | * Clear the array. | 
| 57 | */ | 62 | */ | 
| 58 | void clear() | 63 | void clear() | 
| 59 | { | 64 | { | 
| 65 | if( pData ) | ||
| 66 | { | ||
| 67 | for( int j = 0; j < iSize; j++ ) | ||
| 68 | { | ||
| 69 | va.destroy( &pData[j] ); | ||
| 70 | } | ||
| 71 | va.deallocate( pData, iCapacity ); | ||
| 72 | pData = NULL; | ||
| 73 | } | ||
| 74 | iSize = 0; | ||
| 75 | iCapacity = 0; | ||
| 60 | } | 76 | } | 
| 61 | 77 | ||
| 62 | //operator | 78 | void append( const value &rVal ) | 
| 79 | { | ||
| 80 | if( iSize == iCapacity ) | ||
| 81 | { | ||
| 82 | setCapacity( iCapacity + inc ); | ||
| 83 | } | ||
| 84 | |||
| 85 | va.construct( &pData[iSize++], rVal ); | ||
| 86 | } | ||
| 87 | |||
| 88 | //operator | ||
| 89 | value &operator[]( long iIndex ) | ||
| 90 | { | ||
| 91 | if( iIndex < 0 || iIndex >= iSize ) | ||
| 92 | throw ArrayException( | ||
| 93 | "Index %d out of range 0:%d", iIndex, iSize ); | ||
| 94 | |||
| 95 | return pData[iIndex]; | ||
| 96 | } | ||
| 97 | |||
| 98 | const value &operator[]( long iIndex ) const | ||
| 99 | { | ||
| 100 | if( iIndex < 0 || iIndex >= iSize ) | ||
| 101 | throw ArrayException( | ||
| 102 | "Index %d out of range 0:%d", iIndex, iSize ); | ||
| 103 | |||
| 104 | return pData[iIndex]; | ||
| 105 | } | ||
| 63 | 106 | ||
| 64 | /** | 107 | /** | 
| 65 | * Get the current size of the list. | 108 | * Get the current size of the array. | 
| 66 | *@returns (int) The current size of the list. | 109 | *@returns The current size of the array. | 
| 67 | */ | 110 | */ | 
| 68 | int getSize() const | 111 | long getSize() const | 
| 69 | { | 112 | { | 
| 70 | return iSize; | 113 | return iSize; | 
| 71 | } | 114 | } | 
| 72 | 115 | ||
| 73 | int getCapacity() const | 116 | /** | 
| 117 | * Get the capacity of the array. This number will grow as data is | ||
| 118 | * added, and is mainly for the curious, it doesn't really determine | ||
| 119 | * much for the end user. | ||
| 120 | *@returns The current capacity of the array. | ||
| 121 | */ | ||
| 122 | long getCapacity() const | ||
| 74 | { | 123 | { | 
| 75 | return iCapacity; | 124 | return iCapacity; | 
| 76 | } | 125 | } | 
| 77 | 126 | ||
| 127 | /** | ||
| 128 | * Change the capacity of the array, very useful if you know you'll be | ||
| 129 | * adding a large amount of already counted items to the array, makes | ||
| 130 | * the appending much faster afterwords. | ||
| 131 | *@param iNewLen The new capacity of the array. | ||
| 132 | *@todo Set this up so it can reduce the size of the array as well as | ||
| 133 | * make it bigger. | ||
| 134 | */ | ||
| 135 | void setCapacity( long iNewLen ) | ||
| 136 | { | ||
| 137 | if( iNewLen < iCapacity ) return; | ||
| 138 | value *pNewData = va.allocate( iNewLen ); | ||
| 139 | if( pData ) | ||
| 140 | { | ||
| 141 | for( int j = 0; j < iSize; j++ ) | ||
| 142 | { | ||
| 143 | va.construct( &pNewData[j], pData[j] ); | ||
| 144 | va.destroy( &pData[j] ); | ||
| 145 | } | ||
| 146 | va.deallocate( pData, iCapacity ); | ||
| 147 | } | ||
| 148 | pData = pNewData; | ||
| 149 | iCapacity = iNewLen; | ||
| 150 | } | ||
| 151 | |||
| 152 | typedef struct iterator | ||
| 153 | { | ||
| 154 | friend class Array<value, inc, valuealloc>; | ||
| 155 | private: | ||
| 156 | iterator( MyType &src, long iPos=0 ) : | ||
| 157 | src( src ), | ||
| 158 | iPos( iPos ) | ||
| 159 | { | ||
| 160 | } | ||
| 161 | |||
| 162 | MyType &src; | ||
| 163 | long iPos; | ||
| 164 | |||
| 165 | public: | ||
| 166 | iterator operator++( int ) | ||
| 167 | { | ||
| 168 | if( iPos < 0 ) | ||
| 169 | throw ArrayException( | ||
| 170 | "Cannot increment iterator past end of array."); | ||
| 171 | iPos++; | ||
| 172 | if( iPos >= src.getSize() ) | ||
| 173 | iPos = -1; | ||
| 174 | return *this; | ||
| 175 | } | ||
| 176 | |||
| 177 | iterator operator++() | ||
| 178 | { | ||
| 179 | if( iPos >= 0 ) | ||
| 180 | iPos++; | ||
| 181 | if( iPos >= src.getSize() ) | ||
| 182 | iPos = -1; | ||
| 183 | return *this; | ||
| 184 | } | ||
| 185 | |||
| 186 | bool operator==( const iterator &oth ) | ||
| 187 | { | ||
| 188 | return iPos == oth.iPos; | ||
| 189 | } | ||
| 190 | |||
| 191 | bool operator!=( const iterator &oth ) | ||
| 192 | { | ||
| 193 | return iPos != oth.iPos; | ||
| 194 | } | ||
| 195 | |||
| 196 | iterator operator=( const iterator &oth ) | ||
| 197 | { | ||
| 198 | if( &src != &oth.src ) | ||
| 199 | throw ArrayException( | ||
| 200 | "Cannot mix iterators from different array objects."); | ||
| 201 | iPos = oth.iPos; | ||
| 202 | } | ||
| 203 | |||
| 204 | value &operator*() | ||
| 205 | { | ||
| 206 | if( iPos < 0 ) | ||
| 207 | throw ArrayException( | ||
| 208 | "Cannot dereference finished iterator."); | ||
| 209 | return src[iPos]; | ||
| 210 | } | ||
| 211 | } iterator; | ||
| 212 | |||
| 213 | typedef struct const_iterator | ||
| 214 | { | ||
| 215 | friend class Array<value, inc, valuealloc>; | ||
| 216 | private: | ||
| 217 | const_iterator( const MyType &src, long iPos=0 ) : | ||
| 218 | src( src ), | ||
| 219 | iPos( iPos ) | ||
| 220 | { | ||
| 221 | } | ||
| 222 | |||
| 223 | const MyType &src; | ||
| 224 | long iPos; | ||
| 225 | |||
| 226 | public: | ||
| 227 | const_iterator operator++( int ) | ||
| 228 | { | ||
| 229 | if( iPos < 0 ) | ||
| 230 | throw ArrayException( | ||
| 231 | "Cannot increment iterator past end of array."); | ||
| 232 | iPos++; | ||
| 233 | if( iPos >= src.getSize() ) | ||
| 234 | iPos = -1; | ||
| 235 | return *this; | ||
| 236 | } | ||
| 237 | |||
| 238 | const_iterator operator++() | ||
| 239 | { | ||
| 240 | if( iPos >= 0 ) | ||
| 241 | iPos++; | ||
| 242 | if( iPos >= src.getSize() ) | ||
| 243 | iPos = -1; | ||
| 244 | return *this; | ||
| 245 | } | ||
| 246 | |||
| 247 | bool operator==( const const_iterator &oth ) | ||
| 248 | { | ||
| 249 | return iPos == oth.iPos; | ||
| 250 | } | ||
| 251 | |||
| 252 | bool operator!=( const const_iterator &oth ) | ||
| 253 | { | ||
| 254 | return iPos != oth.iPos; | ||
| 255 | } | ||
| 256 | |||
| 257 | const_iterator operator=( const const_iterator &oth ) | ||
| 258 | { | ||
| 259 | if( &src != &oth.src ) | ||
| 260 | throw ArrayException( | ||
| 261 | "Cannot mix iterators from different array objects."); | ||
| 262 | iPos = oth.iPos; | ||
| 263 | } | ||
| 264 | |||
| 265 | const value &operator*() const | ||
| 266 | { | ||
| 267 | if( iPos < 0 ) | ||
| 268 | throw ArrayException( | ||
| 269 | "Cannot dereference finished iterator."); | ||
| 270 | return src[iPos]; | ||
| 271 | } | ||
| 272 | } const_iterator; | ||
| 273 | |||
| 274 | iterator begin() | ||
| 275 | { | ||
| 276 | return iterator( *this ); | ||
| 277 | } | ||
| 278 | |||
| 279 | const_iterator begin() const | ||
| 280 | { | ||
| 281 | return const_iterator( *this ); | ||
| 282 | } | ||
| 283 | |||
| 284 | iterator end() | ||
| 285 | { | ||
| 286 | return iterator( *this, -1 ); | ||
| 287 | } | ||
| 288 | |||
| 289 | const_iterator end() const | ||
| 290 | { | ||
| 291 | return const_iterator( *this, -1 ); | ||
| 292 | } | ||
| 293 | |||
| 78 | private: | 294 | private: | 
| 79 | valuealloc va; | 295 | valuealloc va; | 
| 80 | value *pData; | 296 | value *pData; | 
| 81 | int iSize; | 297 | long iSize; | 
| 82 | int iCapacity; | 298 | long iCapacity; | 
| 83 | }; | 299 | }; | 
| 84 | } | 300 | } | 
| 85 | 301 | ||
