2 (c) 1995--2006 Han-Wen Nienhuys
4 Distributed under GNU GPL
16 /// copy a bare (C-)array from #src# to #dest# sized #count#
17 template<class T> void arrcpy (T *dest, T const *src, int count);
20 Scaleable array/stack template, for a type T with default constructor.
23 This template implements a scaleable vector. With (or without) range
24 checking. The type T should have a default constructor. It is
25 best suited for simple types, such as int, double or String, it
26 provides a paranoidly safe replacement for the new T[int] construct.
28 You should \bf{never} store pointers to objects in an Array (since
29 the array may be relocated without the pointer knowing it).
31 It uses stack terminology, (push, pop, top), and can be used as a stack.
37 /// maximum length of array.
43 /// stretch or shrink array.
44 void remax (int newmax)
46 T *newarr = new T[newmax];
47 size_ = (newmax < size_) ? newmax : size_;
48 arrcpy (newarr, array_, size_);
69 /// POST: size () == 0
79 arrcpy (array_, tp, n);
83 { array_ = 0; max_ = 0; size_ = 0; }
85 // ugh, get around gcc 2.8.1 ice; see bezier.cc
92 /** set the size_ to #s#.
94 Warning: contents are unspecified */
97 if (s > max_) remax (s);
104 /// return a "new"ed copy of array
107 T *Tarray = new T[size_];
108 arrcpy (Tarray, array_, size_);
112 T const *accesses () const
116 void operator = (Array const &src)
118 set_size (src.size_);
119 arrcpy (array_, src.array_, size_);
121 Array (Array const &src)
123 array_ = src.copys ();
124 max_ = size_ = src.size_;
127 /// tighten array size_.
128 void tighten_maxsize ()
136 T &operator [] (int i)
141 T const &operator [] (int i) const
146 T &elem_ref (int i) const
148 assert (i >= 0 && i < size_);
149 return ((T *)array_)[i];
157 /// add to the end of array
161 remax (2 * max_ + 1);
163 // T::operator= (T &) is called here. Safe to use with automatic
167 /// remove and return last entry
170 assert (!is_empty ());
172 set_size (size () - 1);
175 /// access last entry
178 return (*this)[size_ - j - 1];
180 /// return last entry
181 T top (int j = 0) const
183 return (*this)[size_ - j - 1];
186 T &boundary (int dir, int idx)
192 return elem_ref (idx);
194 T boundary (int dir, int idx) const
202 void swap (int i, int j)
205 (*this)[i] = (*this)[j];
208 bool is_empty () const
211 void insert (T k, int j);
213 remove i-th element, and return it.
221 void unordered_del (int i)
223 elem_ref (i) = top ();
224 set_size (size () -1);
228 assert (i >= 0&& i < size_);
229 arrcpy (array_ + i, array_ + i + 1, size_ - i - 1);
233 void sort (int (*compare) (T const &, T const &),
234 int lower = -1, int upper = -1);
235 void concat (Array<T> const &src)
238 set_size (size_ + src.size_);
239 arrcpy (array_ + s, src.array_, src.size_);
241 Array<T> slice (int lower, int upper) const;
246 int default_compare (T const &a, T const &b)