00001 #ifndef EP_VECTOR_H_
00002 #define EP_VECTOR_H_
00003
00004 #include <utility/malloc.h>
00005 #include <algorithm.h>
00006
00010 const unsigned int ADAPTION_RATE = 4;
00011
00012 enum VecType {
00013 GROWABLE,
00014 ADAPTABLE,
00015 STATIC
00016 };
00017
00021 template <class Heritor, class Contained>
00022 struct VectorPrivate {
00023 VectorPrivate(unsigned int s, unsigned int c) : size(s), capacity(c) {
00024 elements = (Contained*) malloc(sizeof(Contained) * capacity);
00025 }
00026
00030 inline void pushBack(const Contained& contained) { static_cast<Heritor&>(*this).pushBack(contained); }
00034 inline Contained& popBack() { return static_cast<Heritor&>(*this).popBack(); }
00035
00039 void enlarge() {
00040 capacity *= ADAPTION_RATE;
00041 Contained* newElements = (Contained*) malloc(
00042 sizeof(Contained) * capacity);
00043
00044 copy(&elements[0], &elements[size], newElements);
00045 free(elements);
00046
00047 elements = newElements;
00048 }
00049
00053 void minimize() {
00054 capacity /= ADAPTION_RATE;
00055 Contained* newElements = (Contained*) malloc(
00056 sizeof(Contained) * capacity);
00057
00058 copy(&elements[0], &elements[size], newElements);
00059 free(elements);
00060
00061 elements = newElements;
00062 }
00063
00067 Contained* elements;
00071 unsigned int size;
00075 unsigned int capacity;
00076 };
00077
00081 template <VecType tp, class Contained>
00082 struct VecBehaviour :
00083 public VectorPrivate<VecBehaviour<tp, Contained>, Contained > {};
00084
00088 template<class Contained>
00089 struct VecBehaviour<GROWABLE, Contained> :
00090 public VectorPrivate<VecBehaviour<GROWABLE, Contained>, Contained> {
00091
00092 inline VecBehaviour() : VectorPrivate<VecBehaviour<GROWABLE, Contained>, Contained>(0, 1) {}
00093 inline VecBehaviour(unsigned int s, unsigned int c) : VectorPrivate<VecBehaviour<GROWABLE, Contained>, Contained>(s, c) {}
00094
00095 inline void pushBack(const Contained& contained) {
00096 if (this->size == this->capacity) this->enlarge();
00097 this->elements[this->size++] = contained;
00098 }
00099
00100 inline Contained& popBack() {
00101 --this->size;
00102 return this->elements[this->size];
00103 }
00104 };
00105
00109 template<class Contained>
00110 struct VecBehaviour<ADAPTABLE, Contained> :
00111 public VectorPrivate<VecBehaviour<ADAPTABLE, Contained>, Contained> {
00112
00113 inline VecBehaviour() : VectorPrivate<VecBehaviour<ADAPTABLE, Contained>, Contained>(0, 1) {}
00114 inline VecBehaviour(unsigned int s, unsigned int c) : VectorPrivate<VecBehaviour<ADAPTABLE, Contained>, Contained>(s, c) {}
00115
00116 inline void pushBack(const Contained& contained) {
00117 if (this->size == this->capacity) this->enlarge();
00118 this->elements[this->size++] = contained;
00119 }
00120
00121 inline Contained& popBack() {
00122 --this->size;
00123 if (this->size <= this->capacity/ADAPTION_RATE) this->minimize();
00124 return this->elements[this->size];
00125 }
00126 };
00127
00131 template<class Contained>
00132 struct VecBehaviour<STATIC, Contained> :
00133 public VectorPrivate<VecBehaviour<STATIC, Contained>, Contained> {
00134
00135 inline VecBehaviour() : VectorPrivate<VecBehaviour<STATIC, Contained>, Contained>(0, 1) {}
00136 inline VecBehaviour(unsigned int s, unsigned int c) : VectorPrivate<VecBehaviour<STATIC, Contained>, Contained>(s, c) {}
00137
00138 inline void pushBack(const Contained& contained) {
00139 this->elements[this->size++] = contained;
00140 }
00141
00142 inline Contained& popBack() {
00143 --this->size;
00144 return this->elements[this->size];
00145 }
00146 };
00147
00152 template <class Contained, VecType tp = GROWABLE>
00153 class Vector {
00154 public:
00155 template<class ItTp>
00156 struct Incrementor {
00161 Incrementor(ItTp* p) : pointer(p) {}
00162
00168 inline Incrementor operator+(int sum) {
00169 pointer += sum;
00170 return Incrementor(pointer);
00171 }
00172
00173
00179 inline Incrementor operator++(int) {
00180 return Incrementor(pointer++);
00181 }
00182
00186 inline void operator++() {
00187 ++pointer;
00188 }
00189
00193 inline void operator--() {
00194 --pointer;
00195 }
00196
00197
00203 inline Incrementor operator--(int) {
00204 return Incrementor(pointer--);
00205 }
00206
00211 inline ItTp& operator*() const {
00212 return *pointer;
00213 }
00214
00221 inline bool operator==(const Incrementor& comparable) const {
00222 return (comparable.pointer == pointer);
00223 }
00224
00231 inline bool operator!=(const Incrementor& comparable) const {
00232 return !operator==(comparable);
00233 }
00234
00238 ItTp* pointer;
00239 };
00240
00241 typedef Incrementor<Contained> Iterator;
00242 typedef Incrementor<const Contained> ConstIterator;
00243
00247 Vector() : _p() {}
00248
00253 Vector(unsigned int capacity) : _p(0, capacity) {}
00254
00258 ~Vector() {
00259 free(_p.elements);
00260 }
00261
00266 inline void pushBack(const Contained& contained) {
00267 _p.pushBack(contained);
00268 }
00269
00273 inline Contained& popBack() {
00274 return _p.popBack();
00275 }
00276
00281 inline Contained& getFront() const {
00282 return _p.elements[0];
00283 }
00284
00289 inline Contained& getBack() const {
00290 return _p.elements[_p.size - 1];
00291 }
00292
00293 template<class IteratorTp>
00294 Contained& remove(const IteratorTp& it) {
00295 Contained* element = &*it;
00296
00297 if (element == &getBack()) return popBack();
00298
00299 shift(element, _p.elements[_p.size], -1);
00300 }
00301
00306 inline unsigned int capacity() const {
00307 return _p.capacity;
00308 }
00309
00314 inline unsigned int size() const {
00315 return _p.size;
00316 }
00317
00322 inline bool isEmpty() const {
00323 return (size() == 0);
00324 }
00325
00330 Iterator begin() {
00331 return Iterator(&_p.elements[0]);
00332 }
00333
00338 Iterator end() {
00339 return begin() + _p.size;
00340 }
00341
00346 ConstIterator begin() const {
00347 return Iterator(&_p.elements[0]);
00348 }
00349
00354 ConstIterator end() const {
00355 return begin() + _p.size;
00356 }
00357
00358 protected:
00359 VecBehaviour<tp, Contained> _p;
00360 };
00361
00362 #endif