2 (c) 1995--2006 Han-Wen Nienhuys
4 Distributed under GNU GPL
10 #error array.hh is obsolete, use std-vector.hh
21 #define index_assert(i) (assert (i >= 0 && i < size_));
23 #define index_assert(i) (assert (i != VPOS && i < size_));
27 /// copy a bare (C-)array from #src# to #dest# sized #count#
28 template<class T> void arrcpy (T *dest, T const *src, vsize count);
31 Scaleable array/stack template, for a type T with default constructor.
34 This template implements a scaleable vector. With (or without) range
35 checking. The type T should have a default constructor. It is
36 best suited for simple types, such as int, double or String, it
37 provides a paranoidly safe replacement for the new T[int] construct.
39 You should \bf{never} store pointers to objects in an Array (since
40 the array may be relocated without the pointer knowing it).
42 It uses stack terminology, (push, pop, top), and can be used as a stack.
49 /// maximum length of array.
55 /// stretch or shrink array.
56 void remax (vsize newmax)
58 T *newarr = new T[newmax];
59 size_ = (newmax < size_) ? newmax : size_;
60 arrcpy (newarr, array_, size_);
69 /* std::vector interface */
71 typedef T const* const_iterator;
80 Array (Array const &src)
82 array_ = src.copys ();
83 max_ = size_ = src.size_;
86 Array (const_iterator begin, const_iterator end);
88 T const &back () const
90 return (*this)[size_ - 1];
95 return (*this)[size_ - 1];
106 resize (size () - 1);
114 /** set the size_ to #s#.
116 Warning: contents are unspecified */
117 void resize (vsize s)
151 return data () + size_;
157 return data () + size_;
178 T &operator [] (vsize i)
183 T const &operator [] (vsize i) const
191 vsize i = p - data ();
193 arrcpy (array_ + i, array_ + i + 1, size_ - i - 1);
199 insert (iterator b, T k)
201 vsize j = b - array_;
204 for (vsize i = size_ - 1; i > j; i--)
205 array_[i] = array_[i - 1];
210 insert (iterator pos, const_iterator b, const_iterator e)
212 vsize j = pos - array_;
215 for (vsize i = size_ - 1; i > j + k; i--)
216 array_[i] = array_[i - k];
217 for (vsize i = j; i < j + k; i++)
218 array_[i] = b[i - j];
221 /// add to the end of array
225 remax (2 * max_ + 1);
227 // T::operator= (T &) is called here. Safe to use with automatic
233 /* Flower intererface */
238 /** report the size_.
243 Array (T *tp, vsize n)
247 arrcpy (array_, tp, n);
250 // ugh, get around gcc 2.8.1 ice; see bezier.cc
257 /// tighten array size_.
258 void tighten_maxsize ()
268 /// return a "new"ed copy of array
271 T *Tarray = new T[size_];
272 arrcpy (Tarray, array_, size_);
276 T const *accesses () const
280 void operator = (Array const &src)
283 arrcpy (array_, src.array_, size_);
286 void unordered_del (vsize i)
295 back (Array<T> const &v, vsize i)
297 return v[v.size () - i - 1];
302 back (Array<T> &v, vsize i)
304 return v[v.size () - i - 1];
309 boundary (Array<T> const &v, int dir, vsize i)
312 return v[dir == 1 ? i : v.size () - 1 - i];
317 boundary (Array<T> &v, int dir, vsize i)
320 return v[dir == 1 ? i : v.size () - 1 - i];
325 reverse (Array<T> &v)
327 vsize h = v.size () / 2;
328 for (vsize i = 0, j = v.size () - 1; i < h; i++, j--)