1 // This is the STL wrapper for classlib/arrays.h from Borland's web site 2 // It has been modified to be compatible with vc++ (Paul Branann 5/7/98) 3 4 #ifndef STL_ARRAY_AS_VECTOR 5 #define STL_ARRAY_AS_VECTOR 6 7 #ifdef _MSC_VER 8 #pragma warning(disable: 4786) 9 #endif 10 11 // #include <vector.h> 12 // #include <algo.h> 13 #include <vector> 14 #include <algorithm> 15 #include <limits.h> 16 using namespace std; 17 18 template <class T> 19 class TArrayAsVector : public vector<T> { 20 private: 21 const unsigned int growable; 22 typedef size_t size_type; 23 typedef typename vector<T>::const_iterator const_iterator; 24 const size_type lowerbound; 25 public: 26 TArrayAsVector(size_type upper, 27 size_type lower = 0, 28 int delta = 0) : 29 vector<T>( ), 30 growable(delta), 31 lowerbound(lower) 32 { vector<T>::reserve(upper-lower + 1);} 33 34 ~TArrayAsVector( ) 35 { // This call is unnecessary? (Paul Brannan 5/7/98) 36 // vector<T>::~vector( ); 37 } 38 39 int Add(const T& item) 40 { if(!growable && vector<T>::size( ) == vector<T>::capacity( )) 41 return 0; 42 else 43 insert(vector<T>::end( ), item); 44 return 1; } 45 46 int AddAt(const T& item, size_type index) 47 { if(!growable && 48 ((vector<T>::size( ) == vector<T>::capacity( )) || 49 (ZeroBase(index > vector<T>::capacity( )) ))) 50 return 0; 51 if(ZeroBase(index) > vector<T>::capacity( )) // out of bounds 52 { insert(vector<T>::end( ), 53 ZeroBase(index) - vector<T>::size( ), T( )); 54 insert(vector<T>::end( ), item); } 55 else 56 { insert(vector<T>::begin( ) + ZeroBase(index), item); } 57 return 1; 58 } 59 60 size_type ArraySize( ) 61 { return vector<T>::capacity( ); } 62 63 size_type BoundBase(size_type location) const 64 { if(location == UINT_MAX) 65 return INT_MAX; 66 else 67 return location + lowerbound; } 68 void Detach(size_type index) 69 { erase(vector<T>::begin( ) + ZeroBase(index)); } 70 71 void Detach(const T& item) 72 { Destroy(Find(item)); } 73 74 void Destroy(size_type index) 75 { erase(vector<T>::begin( ) + ZeroBase(index)); } 76 77 void Destroy(const T& item) 78 { Destroy(Find(item)); } 79 80 size_type Find(const T& item) const 81 { const_iterator location = find(vector<T>::begin( ), 82 vector<T>::end( ), item); 83 if(location != vector<T>::end( )) 84 return BoundBase(size_type(location - 85 vector<T>::begin( ))); 86 else 87 return INT_MAX; } 88 89 size_type GetItemsInContainer( ) 90 { return vector<T>::size( ); } 91 92 void Grow(size_type index) 93 { if( index < lowerbound ) 94 Reallocate(ArraySize( ) + (index - 95 lowerbound)); 96 else if( index >= BoundBase(vector<T>::size( ))) 97 Reallocate(ZeroBase(index) ); } 98 99 int HasMember(const T& item) 100 { if(Find(item) != INT_MAX) 101 return 1; 102 else 103 return 0; } 104 105 int IsEmpty( ) 106 { return vector<T>::empty( ); } 107 108 int IsFull( ) 109 { if(growable) 110 return 0; 111 if(vector<T>::size( ) == vector<T>::capacity( )) 112 return 1; 113 else 114 return 0; } 115 116 size_type LowerBound( ) 117 { return lowerbound; } 118 119 T& operator[] (size_type index) 120 { return vector<T>:: 121 operator[](ZeroBase(index)); } 122 123 const T& operator[] (size_type index) const 124 { return vector<T>:: 125 operator[](ZeroBase(index)); } 126 127 void Flush( ) 128 { 129 vector<T>::clear(); 130 } 131 132 void Reallocate(size_type sz, 133 size_type offset = 0) 134 { if(offset) 135 insert(vector<T>::begin( ), offset, T( )); 136 vector<T>::reserve(sz); 137 erase(vector<T>::end( ) - offset, vector<T>::end( )); } 138 139 void RemoveEntry(size_type index) 140 { Detach(index); } 141 142 void SetData(size_type index, const T& item) 143 { (*this)[index] = item; } 144 145 size_type UpperBound( ) 146 { return BoundBase(vector<T>::capacity( )) - 1; } 147 148 size_type ZeroBase(size_type index) const 149 { return index - lowerbound; } 150 151 // The assignment operator is not inherited (Paul Brannan 5/25/98) 152 TArrayAsVector& operator=(const TArrayAsVector& v) { 153 vector<T>::operator=(v); 154 // should growable and lowerbound be copied as well? 155 return *this; 156 } 157 158 }; 159 160 #endif 161