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.
43 /// maximum length of array.
49 /// stretch or shrink array.
50 void remax (vsize newmax)
52 T *newarr = new T[newmax];
53 size_ = (newmax < size_) ? newmax : size_;
54 arrcpy (newarr, array_, size_);
63 /* std::vector interface */
65 typedef T const* const_iterator;
74 Array (Array const &src)
76 array_ = src.copys ();
77 max_ = size_ = src.size_;
80 Array (const_iterator begin, const_iterator end);
82 T const &back () const
84 return (*this)[size_ - 1];
89 return (*this)[size_ - 1];
100 resize (size () - 1);
108 /** set the size_ to #s#.
110 Warning: contents are unspecified */
111 void resize (vsize s)
145 return data () + size_;
151 return data () + size_;
160 /* std::vector uses unchecked variant for [] */
161 T &operator [] (vsize i)
166 /* std::vector uses unchecked variant for [] */
167 T const &operator [] (vsize i) const
175 vsize i = p - data ();
177 assert (i >= 0 && i < size_);
181 arrcpy (array_ + i, array_ + i + 1, size_ - i - 1);
189 /* Flower intererface */
194 /** report the size_.
199 Array (T *tp, vsize n)
203 arrcpy (array_, tp, n);
206 // ugh, get around gcc 2.8.1 ice; see bezier.cc
213 /// tighten array size_.
214 void tighten_maxsize ()
224 /// return a "new"ed copy of array
227 T *Tarray = new T[size_];
228 arrcpy (Tarray, array_, size_);
232 T const *accesses () const
236 void operator = (Array const &src)
239 arrcpy (array_, src.array_, size_);
245 T &elem_ref (vsize i) const
248 assert (i >= 0 && i < size_);
252 return ((T *)array_)[i];
255 T elem (vsize i) const
260 /// add to the end of array
264 remax (2 * max_ + 1);
266 // T::operator= (T &) is called here. Safe to use with automatic
271 /// remove and return last entry
276 resize (size () - 1);
281 /// access last entry
284 return (*this)[size_ - j - 1];
286 /// return last entry
287 T top (vsize j) const
289 return (*this)[size_ - j - 1];
292 T &boundary (int dir, vsize idx)
298 return elem_ref (idx);
300 T boundary (int dir, vsize idx) const
308 void swap (vsize i, vsize j)
311 (*this)[i] = (*this)[j];
315 void insert (T k, vsize j);
317 void unordered_del (vsize i)
319 elem_ref (i) = back ();
322 void concat (Array<T> const &src)
325 resize (size_ + src.size_);
326 arrcpy (array_ + s, src.array_, src.size_);