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