00001 #ifndef __TCollection__
00002 #define __TCollection__
00003
00004
00005
00006 #include "TPointerCollection.hpp"
00007 #include "../Exceptions/CException.hpp"
00008 using Exponent::Exceptions::CException;
00009
00010
00011 namespace Exponent
00012 {
00013 namespace Collections
00014 {
00036 template<class TypeName> class TCollection : public CCountedObject
00037 {
00039 EXPONENT_CLASS_DECLARATION;
00042
00043
00044 public:
00045
00046
00047
00052 TCollection(const long size);
00053
00058 TCollection(const TCollection<TypeName> &other);
00059
00063 TCollection();
00064
00068 ~TCollection();
00069
00070
00071
00077 TCollection &operator = (const TCollection<TypeName> &other);
00078
00084 TCollection &operator = (const TypeName &element);
00085
00091 TypeName *operator [] (const long index);
00092
00093
00094
00100 TypeName elementAtIndex(const long index) const;
00101
00107 TypeName *elementPointerAtIndex(const long index);
00108
00114 void addElementAtIndex(const TypeName &value, const long index);
00115
00116
00117
00125 long getIndexOfObject(const TypeName &object);
00126
00134 bool isObjectInArray(const TypeName &object);
00135
00136
00137
00143 void initialise(const long size);
00144
00149 long getArraySize() const;
00150
00154 void clearArray();
00155
00156
00157
00162 void contract(const long contractBy);
00163
00168 void expand(const long expandBy);
00169
00170
00171
00177 void swapIndexes(const long index1, const long index2);
00178
00183 void setAllElementsTo(const TypeName &value);
00184
00191 void copyElements(TypeName *buffer, const long startIndex, const long size) const;
00192
00198 void copyBuffer(const TypeName *buffer, const long size);
00199
00200
00201
00206 const TypeName *getInternalBuffer() const { return m_array; }
00207
00212 TypeName *getMutableInternalBuffer() { return m_array; }
00213
00214
00215
00216 protected:
00217
00218
00219
00223 void freePointers();
00224
00225
00226
00227 TypeName *m_array;
00228 long m_arraySize;
00229 };
00230
00233 EXPONENT_TEMPLATE_CLASS_IMPLEMENTATION(TCollection<TypeName>, TypeName, CCountedObject);
00234
00235
00236 template<class TypeName> TCollection<TypeName>::TCollection(const long size) : m_array(NULL), m_arraySize(0)
00237 {
00238
00239 NULL_POINTER(m_array);
00240 m_arraySize = 0;
00241
00242
00243 this->initialise(size);
00244 }
00245
00246
00247 template<class TypeName> TCollection<TypeName>::TCollection(const TCollection<TypeName> &other) : m_array(NULL), m_arraySize(0)
00248 {
00249
00250 NULL_POINTER(m_array);
00251 m_arraySize = 0;
00252
00253
00254 *this = other;
00255 }
00256
00257
00258 template<class TypeName> TCollection<TypeName>::TCollection() : m_array(NULL), m_arraySize(0)
00259 {
00260 NULL_POINTER(m_array);
00261 m_arraySize = 0;
00262 }
00263
00264
00265 template<class TypeName> TCollection<TypeName>::~TCollection()
00266 {
00267 this->freePointers();
00268 }
00269
00270
00271 template<class TypeName> TCollection<TypeName> &TCollection<TypeName>::operator = (const TCollection<TypeName> &other)
00272 {
00273
00274 if (this != &other)
00275 {
00276
00277 if (other.m_arraySize != m_arraySize)
00278 {
00279
00280 this->freePointers();
00281
00282
00283 this->initialise(other.m_arraySize);
00284 }
00285
00286
00287 other.copyElements(m_array, 0, m_arraySize);
00288 }
00289
00290
00291 return *this;
00292 }
00293
00294
00295 template<class TypeName> TCollection<TypeName> &TCollection<TypeName>::operator = (const TypeName &element)
00296 {
00297 this->setAllElementsTo(element);
00298 return *this;
00299 }
00300
00301
00302 template<class TypeName> TypeName *TCollection<TypeName>::operator [] (const long index)
00303 {
00304 return this->elementPointerAtIndex(index);
00305 }
00306
00307
00308 template<class TypeName> TypeName TCollection<TypeName>::elementAtIndex(const long index) const
00309 {
00310
00311
00312
00313
00314
00315 return m_array[index];
00316
00317
00318
00319
00320 }
00321
00322
00323 template<class TypeName> TypeName *TCollection<TypeName>::elementPointerAtIndex(const long index)
00324 {
00325
00326 if (m_array && index >= 0 && index < m_arraySize)
00327 {
00328 return &m_array[index];
00329 }
00330
00331
00332 return NULL;
00333 }
00334
00335
00336 template<class TypeName> void TCollection<TypeName>::addElementAtIndex(const TypeName &value, const long index)
00337 {
00338
00339 if (m_array && index >= 0 && index < m_arraySize)
00340 {
00341 m_array[index] = value;
00342 }
00343 }
00344
00345
00346 template<class TypeName> long TCollection<TypeName>::getIndexOfObject(const TypeName &object)
00347 {
00348
00349 if (m_array)
00350 {
00351
00352 for (long i = 0; i < m_arraySize; i++)
00353 {
00354
00355 if (m_array[i] == object)
00356 {
00357 return i;
00358 }
00359 }
00360 }
00361
00362
00363 return TPointerCollection<TypeName>::TPOINTERCOLLECTION_FAILED_TO_FIND_POINTER;
00364 }
00365
00366
00367 template<class TypeName> bool TCollection<TypeName>::isObjectInArray(const TypeName &object)
00368 {
00369 return (this->getIndexOfObject(object) != TPointerCollection<TypeName>::TPOINTERCOLLECTION_FAILED_TO_FIND_POINTER);
00370 }
00371
00372
00373 template<class TypeName> void TCollection<TypeName>::initialise(const long size)
00374 {
00375
00376 if (size != m_arraySize)
00377 {
00378
00379 this->freePointers();
00380
00381
00382 m_arraySize = size;
00383
00384
00385 m_array = new TypeName[m_arraySize];
00386 }
00387 }
00388
00389
00390 template<class TypeName> long TCollection<TypeName>::getArraySize() const
00391 {
00392 return m_arraySize;
00393 }
00394
00395
00396 template<class TypeName> void TCollection<TypeName>::clearArray()
00397 {
00398 this->freePointers();
00399 }
00400
00401
00402 template<class TypeName> void TCollection<TypeName>::contract(const long contractBy)
00403 {
00404
00405 if (m_arraySize - contractBy <= 0)
00406 {
00407 throw CException("Conraction causes array to be <= 0 in size", "template<class TypeName> void TCollection<TypeName>::contract(const long)");
00408 }
00409
00410
00411 const long newSize = m_arraySize - contractBy;
00412
00413
00414 TypeName *temp = new TypeName[newSize];
00415
00416
00417 this->copyElements(temp, 0, newSize);
00418
00419
00420 this->freePointers();
00421
00422
00423 m_array = temp;
00424 m_arraySize = newSize;
00425 }
00426
00427
00428 template<class TypeName> void TCollection<TypeName>::expand(const long expandBy)
00429 {
00430
00431 const long newSize = m_arraySize + expandBy;
00432
00433
00434 TypeName *temp = new TypeName[newSize];
00435
00436
00437 this->copyElements(temp, 0, m_arraySize);
00438
00439
00440 this->freePointers();
00441
00442
00443 m_array = temp;
00444 m_arraySize = newSize;
00445 }
00446
00447
00448 template<class TypeName> void TCollection<TypeName>::swapIndexes(const long index1, const long index2)
00449 {
00450
00451 if (m_array && index1 >= 0 && index2 >= 0 && index1 < m_arraySize && index2 < m_arraySize)
00452 {
00453
00454 TypeName object = m_array[index1];
00455
00456
00457 m_array[index1] = m_array[index2];
00458
00459
00460 m_array[index2] = object;
00461 }
00462 }
00463
00464
00465 template<class TypeName> void TCollection<TypeName>::setAllElementsTo(const TypeName &value)
00466 {
00467
00468 if (m_array)
00469 {
00470
00471 for (long i = 0; i < m_arraySize; i++)
00472 {
00473
00474 m_array[i] = value;
00475 }
00476 }
00477 }
00478
00479
00480 template<class TypeName> void TCollection<TypeName>::copyElements(TypeName *buffer, const long startIndex, const long size) const
00481 {
00482
00483 if (m_array && buffer && startIndex >= 0 && startIndex < m_arraySize && startIndex + size <= m_arraySize)
00484 {
00485
00486 for (long i = startIndex; i < size; i++)
00487 {
00488 buffer[i] = m_array[i];
00489 }
00490 }
00491 }
00492
00493
00494 template<class TypeName> void TCollection<TypeName>::copyBuffer(const TypeName *buffer, const long size)
00495 {
00496
00497 if (buffer)
00498 {
00499
00500 if (size != m_arraySize)
00501 {
00502
00503 this->freePointers();
00504
00505
00506 this->initialise(size);
00507 }
00508
00509
00510 for (long i = 0; i < size; i++)
00511 {
00512 m_array[i] = buffer[i];
00513 }
00514
00515 for (long i = 0; i < this->getArraySize(); i++)
00516 {
00517 if (m_array[i] != buffer[i])
00518 {
00519 float a = 1;
00520 a =2;
00521 }
00522 }
00523 }
00524 }
00525
00526
00527 template<class TypeName> void TCollection<TypeName>::freePointers()
00528 {
00529 FREE_ARRAY_POINTER(m_array);
00530 m_arraySize = 0;
00531 }
00532
00534 }
00535 }
00536 #endif // End of TCollection.hpp