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)
4 #ifndef STL_ARRAY_AS_VECTOR
5 #define STL_ARRAY_AS_VECTOR
8 #pragma warning(disable: 4786)
11 // #include <vector.h>
19 class TArrayAsVector
: public vector
<T
> {
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
;
26 TArrayAsVector(size_type upper
,
32 { vector
<T
>::reserve(upper
-lower
+ 1);}
35 { // This call is unnecessary? (Paul Brannan 5/7/98)
36 // vector<T>::~vector( );
39 int Add(const T
& item
)
40 { if(!growable
&& vector
<T
>::size( ) == vector
<T
>::capacity( ))
43 insert(vector
<T
>::end( ), item
);
46 int AddAt(const T
& item
, size_type index
)
48 ((vector
<T
>::size( ) == vector
<T
>::capacity( )) ||
49 (ZeroBase(index
> vector
<T
>::capacity( )) )))
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
); }
56 { insert(vector
<T
>::begin( ) + ZeroBase(index
), item
); }
60 size_type
ArraySize( )
61 { return vector
<T
>::capacity( ); }
63 size_type
BoundBase(size_type location
) const
64 { if(location
== UINT_MAX
)
67 return location
+ lowerbound
; }
68 void Detach(size_type index
)
69 { erase(vector
<T
>::begin( ) + ZeroBase(index
)); }
71 void Detach(const T
& item
)
72 { Destroy(Find(item
)); }
74 void Destroy(size_type index
)
75 { erase(vector
<T
>::begin( ) + ZeroBase(index
)); }
77 void Destroy(const T
& item
)
78 { Destroy(Find(item
)); }
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( )));
89 size_type
GetItemsInContainer( )
90 { return vector
<T
>::size( ); }
92 void Grow(size_type index
)
93 { if( index
< lowerbound
)
94 Reallocate(ArraySize( ) + (index
-
96 else if( index
>= BoundBase(vector
<T
>::size( )))
97 Reallocate(ZeroBase(index
) ); }
99 int HasMember(const T
& item
)
100 { if(Find(item
) != INT_MAX
)
106 { return vector
<T
>::empty( ); }
111 if(vector
<T
>::size( ) == vector
<T
>::capacity( ))
116 size_type
LowerBound( )
117 { return lowerbound
; }
119 T
& operator[] (size_type index
)
121 operator[](ZeroBase(index
)); }
123 const T
& operator[] (size_type index
) const
125 operator[](ZeroBase(index
)); }
132 void Reallocate(size_type sz
,
133 size_type offset
= 0)
135 insert(vector
<T
>::begin( ), offset
, T( ));
136 vector
<T
>::reserve(sz
);
137 erase(vector
<T
>::end( ) - offset
, vector
<T
>::end( )); }
139 void RemoveEntry(size_type index
)
142 void SetData(size_type index
, const T
& item
)
143 { (*this)[index
] = item
; }
145 size_type
UpperBound( )
146 { return BoundBase(vector
<T
>::capacity( )) - 1; }
148 size_type
ZeroBase(size_type index
) const
149 { return index
- lowerbound
; }
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?