2 (c) 1995--2005 Han-Wen Nienhuys
4 Distributed under GNU GPL
15 /// copy a bare (C-)array from #src# to #dest# sized #count#
16 template<class T> void arrcpy (T *dest, T const *src, int count);
19 Scaleable array/stack template, for a type T with default constructor.
22 This template implements a scaleable vector. With (or without) range
23 checking. The type T should have a default constructor. It is
24 best suited for simple types, such as int, double or String, it
25 provides a paranoidly safe replacement for the new T[int] construct.
27 You should \bf{never} store pointers to objects in an Array (since
28 the array may be relocated without the pointer knowing it).
30 It uses stack terminology, (push, pop, top), and can be used as a stack.
36 /// maximum length of array.
42 /// stretch or shrink array.
43 void remax (int newmax)
45 T *newarr = new T[newmax];
46 size_ = (newmax < size_) ? newmax : size_;
47 arrcpy (newarr, array_, size_);
68 /// POST: size () == 0
78 arrcpy (array_, tp, n);
82 { array_ = 0; max_ = 0; size_ = 0; }
84 // ugh, get around gcc 2.8.1 ice; see bezier.cc
91 /** set the size_ to #s#.
93 Warning: contents are unspecified */
96 if (s > max_) remax (s);
103 /// return a "new"ed copy of array
106 T *Tarray = new T[size_];
107 arrcpy (Tarray, array_, size_);
111 T const *accesses () const
115 void operator = (Array const &src)
117 set_size (src.size_);
118 arrcpy (array_, src.array_, size_);
120 Array (Array const &src)
122 array_ = src.copys ();
123 max_ = size_ = src.size_;
126 /// tighten array size_.
127 void tighten_maxsize ()
135 T &operator [] (int i)
140 T const &operator [] (int i) const
145 T &elem_ref (int i) const
147 assert (i >= 0 && i < size_);
148 return ((T *)array_)[i];
156 /// add to the end of array
160 remax (2 * max_ + 1);
162 // T::operator= (T &) is called here. Safe to use with automatic
166 /// remove and return last entry
169 assert (!is_empty ());
171 set_size (size () - 1);
174 /// access last entry
177 return (*this)[size_ - j - 1];
179 /// return last entry
180 T top (int j = 0) const
182 return (*this)[size_ - j - 1];
185 T &boundary (int dir, int idx)
191 return elem_ref (idx);
193 T boundary (int dir, int idx) const
201 void swap (int i, int j)
204 (*this)[i] = (*this)[j];
207 bool is_empty () const
210 void insert (T k, int j);
212 remove i-th element, and return it.
220 void unordered_del (int i)
222 elem_ref (i) = top ();
223 set_size (size () -1);
227 assert (i >= 0&& i < size_);
228 arrcpy (array_ + i, array_ + i + 1, size_ - i - 1);
232 void sort (int (*compare) (T const &, T const &),
233 int lower = -1, int upper = -1);
234 void concat (Array<T> const &src)
237 set_size (size_ + src.size_);
238 arrcpy (array_ + s, src.array_, src.size_);
240 Array<T> slice (int lower, int upper) const;
245 int default_compare (T const &a, T const &b)