00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __SEAL_VECTOR_H__
00030 #define __SEAL_VECTOR_H__
00031
00032 #include "list.h"
00033 #include "randaccess.h"
00034 #include "dataptr.h"
00035 #include "exception.h"
00036
00037 #define DEFAULT_VECTOR_CAPACITY 11
00038
00039 namespace seal
00040 {
00041
00051 template <typename T>
00052 class Vector : public List<T>, public RandomAccess<T>
00053 {
00054 public:
00055
00058 class Iterator : public AbstractIterator<T>
00059 {
00060
00063 friend class Vector<T>;
00064
00065 public:
00066
00069 Iterator(void) : AbstractIterator<T>(), _firstIndex(0), _lastIndex(0), _iterIndex(-1), _size(0) {}
00070
00075 virtual T next(void) throw (OutOfBoundsException);
00076
00080 virtual bool hasNext(void);
00081
00084 virtual void reset(void);
00085
00086 private:
00087
00090 Iterator(int firstIndex, int lastIndex, unsigned int size, T *dataPtr)
00091 : AbstractIterator<T>(), _firstIndex(firstIndex), _lastIndex(lastIndex), _size(size), _dataPtr(dataPtr)
00092 { _iterIndex = _firstIndex-1; }
00093
00094 int _firstIndex, _lastIndex, _iterIndex;
00095 unsigned int _size;
00096
00097 T *_dataPtr;
00098 };
00099
00102 Vector(void);
00103
00106 Vector(unsigned int length);
00107
00110 Vector(const Vector<T> &v);
00111
00114 virtual ~Vector();
00115
00118 virtual unsigned int size(void) const { return *_sizePtr; }
00119
00122 virtual bool isEmpty(void) const { return (*_sizePtr == 0); }
00123
00127 virtual AbstractIterator<T>* iteratorPtr(void) const
00128 {
00129 typename Vector<T>::Iterator *iterPtr = new typename Vector<T>::Iterator(*_firstPtr, *_lastPtr, *_sizePtr, _dataPtr->data);
00130 return iterPtr;
00131 }
00132
00135 typename Vector<T>::Iterator iterator(void) const
00136 {
00137 return typename Vector<T>::Iterator(*_firstPtr, *_lastPtr, *_sizePtr, _dataPtr->data);
00138 }
00139
00142 virtual void purge(void);
00143
00146 inline virtual const T& operator[](int index) const throw (OutOfRangeException<int>);
00147
00150 inline virtual T& operator[](int index) throw (OutOfRangeException<int>);
00151
00155 void operator=(const Vector<T> &v);
00156
00161 virtual void pushBack(const T element);
00162
00165 virtual void popBack(void);
00166
00171 virtual void pushFront(const T element);
00172
00175 virtual void popFront(void);
00176
00184 virtual void insert(const T element, unsigned int index);
00185
00192 virtual void remove(unsigned int index);
00193
00196 virtual const T& first(void) const throw (EmptyCollectionException);
00197
00200 virtual const T& last(void) const throw (EmptyCollectionException);
00201
00204 virtual T& first(void) throw (EmptyCollectionException);
00205
00208 virtual T& last(void) throw(EmptyCollectionException);
00209
00210 private:
00211
00212 unsigned int *_firstPtr, *_lastPtr, *_capacityPtr;
00213 unsigned int *_refCountPtr;
00214 unsigned int *_sizePtr;
00215 DataPtr<T> *_dataPtr;
00216 };
00217
00218
00222
00223 template <typename T>
00224 Vector<T>::Vector(void)
00225 : List<T>(), RandomAccess<T>()
00226 {
00227 _capacityPtr = new unsigned int; *_capacityPtr = DEFAULT_VECTOR_CAPACITY;
00228 _sizePtr = new unsigned int; *_sizePtr = 0;
00229 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00230
00231 _firstPtr = new unsigned int; *_firstPtr = DEFAULT_VECTOR_CAPACITY/2;
00232 _lastPtr = new unsigned int; *_lastPtr = DEFAULT_VECTOR_CAPACITY/2;
00233
00234 _dataPtr = new DataPtr<T>();
00235 _dataPtr->data = new T [DEFAULT_VECTOR_CAPACITY];
00236 }
00237
00238 template <typename T>
00239 Vector<T>::Vector(unsigned int length)
00240 : List<T>(), RandomAccess<T>()
00241 {
00242 _capacityPtr = new unsigned int; *_capacityPtr = 2*length;
00243 _sizePtr = new unsigned int; *_sizePtr = length;
00244 _refCountPtr = new unsigned int; *_refCountPtr = 1;
00245
00246 _firstPtr = new unsigned int; *_firstPtr = length/2;
00247 _lastPtr = new unsigned int; *_lastPtr = *_firstPtr + length - 1;
00248
00249 _dataPtr = new DataPtr<T>();
00250 _dataPtr->data = new T [*_capacityPtr];
00251 }
00252
00253 template <typename T>
00254 Vector<T>::Vector(const Vector<T> &v)
00255 : List<T>(), RandomAccess<T>(),
00256 _firstPtr(v._firstPtr),
00257 _lastPtr(v._lastPtr),
00258 _capacityPtr(v._capacityPtr),
00259 _refCountPtr(v._refCountPtr),
00260 _sizePtr(v._sizePtr),
00261 _dataPtr(v._dataPtr)
00262 {
00263 (*_refCountPtr)++;
00264 }
00265
00266 template <typename T>
00267 Vector<T>::~Vector()
00268 {
00269 if (*_refCountPtr <= 1)
00270 {
00271 delete _firstPtr;
00272 delete _lastPtr;
00273 delete _capacityPtr;
00274 delete _sizePtr;
00275 delete _refCountPtr;
00276
00277 delete [] _dataPtr->data;
00278 delete _dataPtr;
00279 }
00280 else
00281 (*_refCountPtr)--;
00282 }
00283
00284 template <typename T>
00285 void Vector<T>::purge(void)
00286 {
00287 *_sizePtr = 0;
00288 *_firstPtr = *_capacityPtr / 2;
00289 *_lastPtr = *_firstPtr;
00290 }
00291
00292 template <typename T>
00293 const T& Vector<T>::operator[](int index) const throw (OutOfRangeException<int>)
00294 {
00295 if (*_sizePtr == 0)
00296 throw OutOfRangeException<int>(-1, -1, 0);
00297 else if (index < 0 || index >= *_sizePtr)
00298 throw OutOfRangeException<int>(0, *_sizePtr-1, index);
00299 else
00300 return _dataPtr->data[*_firstPtr + index];
00301 }
00302
00303 template <typename T>
00304 T& Vector<T>::operator[](int index) throw (OutOfRangeException<int>)
00305 {
00306 if (*_sizePtr == 0)
00307 throw OutOfRangeException<int>(-1, -1, 0);
00308 else if (index < 0 || index >= *_sizePtr)
00309 throw OutOfRangeException<int>(0, *_sizePtr-1, index);
00310 else
00311 return _dataPtr->data[*_firstPtr + index];
00312 }
00313
00314 template <typename T>
00315 void Vector<T>::pushBack(const T element)
00316 {
00317
00318
00319 if (*_sizePtr > 0)
00320 (*_lastPtr)++;
00321
00322
00323
00324 (*_sizePtr)++;
00325
00326
00327
00328 if (*_lastPtr < *_capacityPtr)
00329 {
00330 _dataPtr->data[*_lastPtr] = element;
00331 }
00332 else
00333 {
00334
00335
00336
00337
00338
00339 (*_capacityPtr) *= 2;
00340
00341
00342 T *newData = new T [*_capacityPtr];
00343
00344
00345
00346 for (int i = *_firstPtr; i < *_lastPtr; i++)
00347 {
00348 newData[i] = _dataPtr->data[i];
00349 }
00350
00351
00352 newData[*_lastPtr] = element;
00353
00354
00355
00356 delete [] _dataPtr->data;
00357 _dataPtr->data = newData;
00358 }
00359
00360 }
00361
00362 template <typename T>
00363 void Vector<T>::popBack(void)
00364 {
00365
00366 if (*_sizePtr == 0)
00367 return;
00368
00369
00370
00371 (*_sizePtr)--;
00372
00373
00374
00375 if (*_sizePtr > 0)
00376 (*_lastPtr)--;
00377
00378
00379 if (*_sizePtr < (*_capacityPtr)/4 && *_capacityPtr >= 3*DEFAULT_VECTOR_CAPACITY/2)
00380 {
00381 (*_capacityPtr) /= 2;
00382 T *newData = new T [*_capacityPtr];
00383
00384 int newFirst = (*_capacityPtr)/2 - (*_sizePtr)/2;
00385 for (int i = *_firstPtr, j = newFirst; i <= *_lastPtr; i++, j++)
00386 newData[j] = _dataPtr->data[i];
00387
00388 *_firstPtr = newFirst;
00389 *_lastPtr = newFirst + *_sizePtr - 1;
00390
00391 delete [] _dataPtr->data;
00392 _dataPtr->data = newData;
00393 }
00394 }
00395
00396 template <typename T>
00397 void Vector<T>::pushFront(const T element)
00398 {
00399
00400 (*_sizePtr)++;
00401
00402 if (*_firstPtr > 0)
00403 {
00404 if (*_sizePtr > 1)
00405 (*_firstPtr)--;
00406
00407 _dataPtr->data[*_firstPtr] = element;
00408 }
00409 else
00410 {
00411 if (*_sizePtr == 1)
00412 {
00413 _dataPtr->data[*_firstPtr] = element;
00414 }
00415 else
00416 {
00417
00418
00419
00420 (*_capacityPtr) *= 2;
00421 T *newData = new T [*_capacityPtr];
00422
00423
00424 int newFirst = (*_capacityPtr)/2 - (*_sizePtr)/2;
00425 for (int i = *_firstPtr, j = newFirst+1; i <= *_lastPtr; i++, j++)
00426 {
00427 newData[j] = _dataPtr->data[i];
00428 }
00429
00430 newData[newFirst] = element;
00431 *_firstPtr = newFirst;
00432 *_lastPtr = newFirst + *_sizePtr - 1;
00433
00434
00435 delete [] _dataPtr->data;
00436
00437
00438 _dataPtr->data = newData;
00439 }
00440 }
00441
00442 }
00443
00444 template <typename T>
00445 void Vector<T>::popFront(void)
00446 {
00447
00448 if (*_sizePtr == 0)
00449 return;
00450
00451
00452
00453 (*_sizePtr)--;
00454
00455
00456
00457 if (*_sizePtr > 0)
00458 (*_firstPtr)++;
00459
00460
00461 if (*_sizePtr < (*_capacityPtr)/4 && *_capacityPtr > 3*DEFAULT_VECTOR_CAPACITY/2)
00462 {
00463
00464 (*_capacityPtr) /= 2;
00465 T *newData = new T [*_capacityPtr];
00466
00467
00468
00469 int newFirst = (*_capacityPtr)/2 - (*_sizePtr)/2;
00470 for (int i = *_firstPtr, j= newFirst; i <= *_lastPtr; i++, j++)
00471 {
00472 newData[j] = _dataPtr->data[i];
00473 }
00474
00475
00476 *_firstPtr = newFirst;
00477 *_lastPtr = newFirst + *_sizePtr - 1;
00478
00479
00480 delete [] _dataPtr->data;
00481 _dataPtr->data = newData;
00482 }
00483
00484 }
00485
00486 template <typename T>
00487 void Vector<T>::insert(const T element, unsigned int index)
00488 {
00489 if (index == 0)
00490 pushFront(element);
00491 else if (index >= *_sizePtr)
00492 pushBack(element);
00493 else
00494 {
00495 unsigned int size = *_sizePtr;
00496 pushBack(this->operator[](size - 1));
00497 for (int i = size-1; i > index; i--)
00498 {
00499 this->operator[](i) = this->operator[](i-1);
00500 }
00501
00502 this->operator[](index) = element;
00503 }
00504 }
00505
00506 template <typename T>
00507 void Vector<T>::remove(unsigned int index)
00508 {
00509 if (index == 0)
00510 popFront();
00511 else if (index == (*_sizePtr - 1))
00512 popBack();
00513 else if (index >= *_sizePtr)
00514 return;
00515 else
00516 {
00517 for (int i = index; i < *_sizePtr-1; i++)
00518 {
00519 this->operator[](i) = this->operator[](i+1);
00520 }
00521
00522 popBack();
00523 }
00524 }
00525
00526 template <typename T>
00527 void Vector<T>::operator=(const Vector<T> &v)
00528 {
00529 if (_refCountPtr == v._refCountPtr)
00530 return;
00531
00532 if (*_refCountPtr > 1)
00533 {
00534 (*_refCountPtr)--;
00535
00536 _firstPtr = v._firstPtr;
00537 _lastPtr = v._lastPtr;
00538 _capacityPtr = v._capacityPtr;
00539 _sizePtr = v._sizePtr;
00540 _refCountPtr = v._refCountPtr;
00541 _dataPtr = v._dataPtr;
00542
00543 (*_refCountPtr)++;
00544 }
00545 else
00546 {
00547 delete _firstPtr;
00548 delete _lastPtr;
00549 delete _capacityPtr;
00550 delete _sizePtr;
00551 delete _refCountPtr;
00552
00553 delete [] _dataPtr->data;
00554 delete _dataPtr;
00555
00556 _firstPtr = v._firstPtr;
00557 _lastPtr = v._lastPtr;
00558 _capacityPtr = v._capacityPtr;
00559 _sizePtr = v._sizePtr;
00560 _refCountPtr = v._refCountPtr;
00561 _dataPtr = v._dataPtr;
00562
00563 (*_refCountPtr)++;
00564 }
00565 }
00566
00567 template <typename T>
00568 const T& Vector<T>::first(void) const throw (EmptyCollectionException)
00569 {
00570 if (*_sizePtr == 0)
00571 throw EmptyCollectionException();
00572
00573 return _dataPtr->data[*_firstPtr];
00574 }
00575
00576 template <typename T>
00577 T& Vector<T>::first(void) throw (EmptyCollectionException)
00578 {
00579 if (*_sizePtr == 0)
00580 throw EmptyCollectionException();
00581
00582 return _dataPtr->data[*_firstPtr];
00583 }
00584
00585 template <typename T>
00586 const T& Vector<T>::last(void) const throw (EmptyCollectionException)
00587 {
00588 if (*_sizePtr == 0)
00589 throw EmptyCollectionException();
00590
00591 return _dataPtr->data[*_lastPtr];
00592 }
00593
00594 template <typename T>
00595 T& Vector<T>::last(void) throw (EmptyCollectionException)
00596 {
00597 if (*_sizePtr == 0)
00598 throw EmptyCollectionException();
00599
00600 return _dataPtr->data[*_lastPtr];
00601 }
00602
00606
00607 template <typename T>
00608 bool Vector<T>::Iterator::hasNext()
00609 {
00610 if (_size > 0 && _iterIndex < _lastIndex && _iterIndex >= _firstIndex - 1)
00611 return true;
00612 else
00613 return false;
00614 }
00615
00616 template <typename T>
00617 T Vector<T>::Iterator::next() throw (OutOfBoundsException)
00618 {
00619 if (_size > 0 && _iterIndex < _lastIndex && _iterIndex >= _firstIndex - 1)
00620 {
00621 _iterIndex++;
00622 return _dataPtr[_iterIndex];
00623 }
00624 else
00625 throw OutOfBoundsException();
00626 }
00627
00628 template <typename T>
00629 void Vector<T>::Iterator::reset()
00630 {
00631 _iterIndex = _firstIndex - 1;
00632 }
00633
00634 }
00635
00636 #endif // __SEAL_VECTOR_H__
00637