56 template <
typename TYPE>
61 VECTOR(
int initialCapacity);
77 const TYPE&
eraseAt(
unsigned int pos);
79 bool remove(
const TYPE& element);
83 int replaceAll(
const TYPE& find,
const TYPE& replace);
85 void fill(
const TYPE& fillElement);
88 unsigned int size()
const;
90 void reserve(
unsigned int size);
95 TYPE&
at(
const unsigned int i);
97 const TYPE&
operator[](
const unsigned int i )
const;
101 void changeCapacity(
unsigned int newSize);
102 TYPE* shiftLeftFromPosition(
unsigned int pos);
103 TYPE* shiftRightFromPosition(
unsigned int pos);
105 unsigned int mGrowthRate;
106 unsigned int mVectorCapacity;
107 unsigned int mVectorSize;
136 template <
typename TYPE>
141 template <
typename TYPE>
145 changeCapacity(initialCapacity);
148 template <
typename TYPE>
155 template <
typename TYPE>
165 for(
unsigned int i = 0; i < copy.
size(); i++)
167 *(this->mpObjPtrs[i]) = copy[i];
173 template <
typename TYPE>
176 for(
unsigned int i=0; i < mVectorCapacity; i++) {
183 template <
typename TYPE>
186 return (mVectorSize > 0) ? *mpObjPtrs[--mVectorSize] : mNullItem;
190 template <
typename TYPE>
196 template <
typename TYPE>
199 if(mVectorSize >= mVectorCapacity)
201 changeCapacity( (mVectorCapacity + mGrowthRate) );
203 *mpObjPtrs[mVectorSize++] = element;
206 template <
typename TYPE>
209 if(mVectorSize >= mVectorCapacity)
211 changeCapacity( (mVectorCapacity + mGrowthRate) );
215 if( mVectorSize++ >= 1)
217 mpObjPtrs[0] = shiftRightFromPosition(0);
221 *mpObjPtrs[0] = element;
224 template <
typename TYPE>
230 template <
typename TYPE>
233 return (*
this)[mVectorSize-1];
236 template <
typename TYPE>
242 template <
typename TYPE>
245 return mVectorCapacity;
248 template <
typename TYPE>
251 changeCapacity(theSize);
254 template <
typename TYPE>
258 mGrowthRate = factor;
261 template <
typename TYPE>
264 for(
unsigned int i = 0; i < mVectorSize; i++) {
265 if(*mpObjPtrs[i] == find) {
272 template <
typename TYPE>
276 if(elementNumber < mVectorSize && elementNumber >= 0)
279 item = (mpObjPtrs[mVectorSize-1] = shiftLeftFromPosition(elementNumber));
282 return 0 == item ? mNullItem : *item;
285 template <
typename TYPE>
289 const bool found = (index >= 0);
296 template <
typename TYPE>
300 int itemsRemoved = 0;
301 while(
remove(element)) { itemsRemoved++; }
305 template <
typename TYPE>
309 const bool found = (index >= 0);
312 *mpObjPtrs[index] = replaceWith;
318 template <
typename TYPE>
321 int itemsReplaced = 0;
322 for(
unsigned int i = 0; i < mVectorSize; i++) {
323 if(*mpObjPtrs[i] == find) {
324 *mpObjPtrs[i] = replaceWith;
328 return itemsReplaced;
331 template <
typename TYPE>
334 for(
unsigned int i = 0; i < mVectorCapacity; i++) {
335 *mpObjPtrs[i] = fillElement;
337 mVectorSize = mVectorCapacity;
340 template <
typename TYPE>
343 for(
unsigned int i = mVectorSize; i < mVectorCapacity; i++) {
344 *mpObjPtrs[i] = fillElement;
346 mVectorSize = mVectorCapacity;
349 template <
typename TYPE>
355 template <
typename TYPE>
358 return (0 == mVectorSize);
361 template <
typename TYPE>
364 for(
unsigned int i = 0; i < (mVectorSize/2); i++)
366 TYPE* temp = mpObjPtrs[i];
367 mpObjPtrs[i] = mpObjPtrs[ (mVectorSize-1-i) ];
368 mpObjPtrs[ (mVectorSize-1-i) ] = temp;
372 template <
typename TYPE>
378 mpObjPtrs[0] = shiftRightFromPosition(0);
383 template <
typename TYPE>
389 mpObjPtrs[ (mVectorSize-1) ] = shiftLeftFromPosition(0);
394 template <
typename TYPE>
400 template <
typename TYPE>
403 return ( i >= 0 && i < mVectorSize) ? *mpObjPtrs[i] : mNullItem;
406 template <
typename TYPE>
409 return ( i >= 0 && i < mVectorSize) ? *mpObjPtrs[i] : mNullItem;
415 template <
typename TYPE>
418 if(newSize < mVectorCapacity)
424 TYPE **newData =
new TYPE*[ (newSize) ];
425 for(
unsigned int i = 0; i < newSize; i++) {
426 newData[i] =
new TYPE();
432 for(
unsigned int i = 0; i < mVectorSize; i++)
434 newData[i] = mpObjPtrs[i];
443 mpObjPtrs = (TYPE**)realloc(mpObjPtrs,
sizeof(TYPE*)*newSize);
446 for(
unsigned int i = mVectorSize; i < newSize; i++) {
447 mpObjPtrs[i] =
new TYPE();
451 mVectorCapacity = newSize;
454 template <
typename TYPE>
457 TYPE* leftMostItem = mpObjPtrs[pos];
458 if(mVectorSize > 1 && (mVectorSize-pos) > 1)
461 for(
unsigned int i = pos; i < (mVectorSize-1); i++)
463 mpObjPtrs[i] = mpObjPtrs[i+1];
469 template <
typename TYPE>
473 TYPE* rightMostItem = mpObjPtrs[mVectorSize-1];
476 for(
unsigned int i = mVectorSize-1; i > pos; i--)
478 mpObjPtrs[i] = mpObjPtrs[i-1];
480 return rightMostItem;
void setGrowthFactor(int factor)
Changes the size the vector grows by.
Definition: vector.hpp:255
void fillUnused(const TYPE &fillElement)
Fills the unused capacity of the vector with the given fillElement.
Definition: vector.hpp:341
Definition: vector.hpp:57
int getFirstIndexOf(const TYPE &find)
Definition: vector.hpp:262
void reserve(unsigned int size)
Reserves the memory for the vector up front.
Definition: vector.hpp:249
VECTOR()
Default Constructor.
Definition: vector.hpp:137
const TYPE & rotateLeft()
Rotates the vector left by 1 and.
Definition: vector.hpp:373
bool remove(const TYPE &element)
Removes the first Vector Element match from this vector,.
Definition: vector.hpp:286
void operator+=(const TYPE &item)
+= Operator which is same as push_back() of an item
Definition: vector.hpp:98
const TYPE & rotateRight()
Rotates the vector right by 1 and.
Definition: vector.hpp:384
const TYPE & pop_back()
Pops & returns the last element from the vector. (FAST)
Definition: vector.hpp:184
bool isEmpty()
Definition: vector.hpp:356
int replaceAll(const TYPE &find, const TYPE &replace)
Replaces all instances of "find" and replaces it with "replace".
Definition: vector.hpp:319
const TYPE & eraseAt(unsigned int pos)
Erases the element at pos and returns it. All elements are shifted left from this pos...
Definition: vector.hpp:273
VECTOR & operator=(const VECTOR ©)
=Operator to copy the vector.
Definition: vector.hpp:156
TYPE & operator[](const unsigned int i)
[] Operator for Left-hand-side.
Definition: vector.hpp:401
const TYPE & back()
Definition: vector.hpp:231
const TYPE & pop_front()
Pops & returns the first(oldest) element of the vector (index 0). (SLOW)
Definition: vector.hpp:191
bool replace(const TYPE &find, const TYPE &replace)
Replaces the first element "find" and replaces it with "replace".
Definition: vector.hpp:306
int removeAll(const TYPE &element)
Removes all Vector Elements that match the given element,.
Definition: vector.hpp:297
TYPE & at(const unsigned int i)
Access element at given index.
Definition: vector.hpp:395
const TYPE & front()
Definition: vector.hpp:225
unsigned int capacity() const
Definition: vector.hpp:243
void clear()
Clears the entire vector.
Definition: vector.hpp:350
void fill(const TYPE &fillElement)
Fills the entire vector capacity with the given fillElement.
Definition: vector.hpp:332
~VECTOR()
Destructor of the vector.
Definition: vector.hpp:174
void reverse()
Reverses the order of the vector contents.
Definition: vector.hpp:362
void push_front(const TYPE &element)
Pushes the element at the 1st location (index 0). (SLOW)
Definition: vector.hpp:207
void push_back(const TYPE &element)
Pushes the element to the end of the vector. (FAST)
Definition: vector.hpp:197
unsigned int size() const
Definition: vector.hpp:237