smallvec_type.hpp

Go to the documentation of this file.
00001 /* $Id: smallvec_type.hpp 15447 2009-02-11 07:17:39Z peter1138 $ */
00002 
00005 #ifndef SMALLVEC_TYPE_HPP
00006 #define SMALLVEC_TYPE_HPP
00007 
00008 #include "alloc_func.hpp"
00009 #include "math_func.hpp"
00010 
00021 template <typename T, uint S>
00022 class SmallVector {
00023 protected:
00024   T *data;       
00025   uint items;    
00026   uint capacity; 
00027 
00028 public:
00029   SmallVector() : data(NULL), items(0), capacity(0) { }
00030 
00031   ~SmallVector()
00032   {
00033     free(this->data);
00034   }
00035 
00039   FORCEINLINE void Clear()
00040   {
00041     /* In fact we just reset the item counter avoiding the need to
00042      * probably reallocate the same amount of memory the list was
00043      * previously using. */
00044     this->items = 0;
00045   }
00046 
00050   void Reset()
00051   {
00052     this->items = 0;
00053     this->capacity = 0;
00054     free(data);
00055     data = NULL;
00056   }
00057 
00061   FORCEINLINE void Compact()
00062   {
00063     uint capacity = Align(this->items, S);
00064     if (capacity >= this->capacity) return;
00065 
00066     this->capacity = capacity;
00067     this->data = ReallocT(this->data, this->capacity);
00068   }
00069 
00073   FORCEINLINE T *Append()
00074   {
00075     if (this->items == this->capacity) {
00076       this->capacity += S;
00077       this->data = ReallocT(this->data, this->capacity);
00078     }
00079 
00080     return &this->data[this->items++];
00081   }
00082 
00089   FORCEINLINE const T *Find(const T &item) const
00090   {
00091     const T *pos = this->Begin();
00092     const T *end = this->End();
00093     while (pos != end && *pos != item) pos++;
00094     return pos;
00095   }
00096 
00103   FORCEINLINE T *Find(const T &item)
00104   {
00105     T *pos = this->Begin();
00106     const T *end = this->End();
00107     while (pos != end && *pos != item) pos++;
00108     return pos;
00109   }
00110 
00117   FORCEINLINE bool Contains(const T &item) const
00118   {
00119     return this->Find(item) != this->End();
00120   }
00121 
00126   FORCEINLINE void Erase(T *item)
00127   {
00128     assert(item >= this->Begin() && item < this->End());
00129     *item = this->data[--this->items];
00130   }
00131 
00138   FORCEINLINE bool Include(const T &item)
00139   {
00140     bool is_member = this->Contains(item);
00141     if (!is_member) *this->Append() = item;
00142     return is_member;
00143   }
00144 
00148   FORCEINLINE uint Length() const
00149   {
00150     return this->items;
00151   }
00152 
00158   FORCEINLINE const T *Begin() const
00159   {
00160     return this->data;
00161   }
00162 
00168   FORCEINLINE T *Begin()
00169   {
00170     return this->data;
00171   }
00172 
00178   FORCEINLINE const T *End() const
00179   {
00180     return &this->data[this->items];
00181   }
00182 
00188   FORCEINLINE T *End()
00189   {
00190     return &this->data[this->items];
00191   }
00192 
00199   FORCEINLINE const T *Get(uint index) const
00200   {
00201     return &this->data[index];
00202   }
00203 
00210   FORCEINLINE T *Get(uint index)
00211   {
00212     return &this->data[index];
00213   }
00214 
00221   FORCEINLINE const T &operator[](uint index) const
00222   {
00223     return this->data[index];
00224   }
00225 
00232   FORCEINLINE T &operator[](uint index)
00233   {
00234     return this->data[index];
00235   }
00236 };
00237 
00238 
00249 template <typename T, uint S>
00250 class AutoFreeSmallVector : public SmallVector<T, S> {
00251 public:
00252   ~AutoFreeSmallVector()
00253   {
00254     this->Clear();
00255   }
00256 
00260   FORCEINLINE void Clear()
00261   {
00262     for (uint i = 0; i < this->items; i++) {
00263       free(this->data[i]);
00264     }
00265 
00266     this->items = 0;
00267   }
00268 };
00269 
00270 #endif /* SMALLVEC_TYPE_HPP */

Generated on Mon Jun 8 23:04:03 2009 for OpenTTD by  doxygen 1.5.6