2 (c) 1995--2006 Han-Wen Nienhuys
4 Distributed under GNU GPL
10 #error array.hh is obsolete, use std-vector.hh
21 /// copy a bare (C-)array from #src# to #dest# sized #count#
22 template<class T> void arrcpy (T *dest, T const *src, int count);
25 Scaleable array/stack template, for a type T with default constructor.
28 This template implements a scaleable vector. With (or without) range
29 checking. The type T should have a default constructor. It is
30 best suited for simple types, such as int, double or String, it
31 provides a paranoidly safe replacement for the new T[int] construct.
33 You should \bf{never} store pointers to objects in an Array (since
34 the array may be relocated without the pointer knowing it).
36 It uses stack terminology, (push, pop, top), and can be used as a stack.
42 /// maximum length of array.
48 /// stretch or shrink array.
49 void remax (vsize newmax)
51 T *newarr = new T[newmax];
52 size_ = (newmax < size_) ? newmax : size_;
53 arrcpy (newarr, array_, size_);
62 /* std::vector interface */
70 Array (Array const &src)
72 array_ = src.copys ();
73 max_ = size_ = src.size_;
76 T const &back () const
78 return (*this)[size_ - 1];
83 return (*this)[size_ - 1];
102 /** set the size_ to #s#.
104 Warning: contents are unspecified */
105 void resize (vsize s)
116 /** report the size_.
121 /// POST: size () == 0
127 Array (T *tp, vsize n)
131 arrcpy (array_, tp, n);
134 // ugh, get around gcc 2.8.1 ice; see bezier.cc
141 /// tighten array size_.
142 void tighten_maxsize ()
152 /// return a "new"ed copy of array
155 T *Tarray = new T[size_];
156 arrcpy (Tarray, array_, size_);
160 T const *accesses () const
164 void operator = (Array const &src)
167 arrcpy (array_, src.array_, size_);
173 T &operator [] (vsize i)
178 T const &operator [] (vsize i) const
183 T &elem_ref (vsize i) const
186 assert (i >= 0 && i < size_);
190 return ((T *)array_)[i];
193 T elem (vsize i) const
198 /// add to the end of array
202 remax (2 * max_ + 1);
204 // T::operator= (T &) is called here. Safe to use with automatic
209 /// remove and return last entry
214 resize (size () - 1);
219 /// access last entry
222 return (*this)[size_ - j - 1];
224 /// return last entry
225 T top (vsize j) const
227 return (*this)[size_ - j - 1];
230 T &boundary (int dir, vsize idx)
236 return elem_ref (idx);
238 T boundary (int dir, vsize idx) const
246 void swap (vsize i, vsize j)
249 (*this)[i] = (*this)[j];
253 void insert (T k, vsize j);
255 remove i-th element, and return it.
263 void unordered_del (vsize i)
265 elem_ref (i) = back ();
271 assert (i >= 0 && i < size_);
275 arrcpy (array_ + i, array_ + i + 1, size_ - i - 1);
279 void sort (int (*compare) (T const &, T const &),
280 vsize lower=VPOS, vsize upper=VPOS);
281 void concat (Array<T> const &src)
284 resize (size_ + src.size_);
285 arrcpy (array_ + s, src.array_, src.size_);
287 Array<T> slice (vsize lower, vsize upper) const;
292 int default_compare (T const &a, T const &b)