2 (c) Han-Wen Nienhuys 1995,96,97
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.
38 /// maximum length of array.
44 /// stretch or shrink array.
45 void remax (int newmax)
47 T* newarr = new T[newmax];
48 size_ = (newmax < size_) ? newmax : size_;
49 arrcpy (newarr, array_, size_);
70 /// POST: size () == 0
80 arrcpy (array_, tp, n);
84 { array_ = 0; max_ =0; size_ =0; }
86 // ugh, get around gcc 2.8.1 ice; see bezier.cc
94 /** set the size_ to #s#.
96 Warning: contents are unspecified */
99 if (s > max_) remax (s);
106 /// return a "new"ed copy of array
109 T* Tarray = new T[size_];
110 arrcpy (Tarray, array_, size_);
114 T const *accesses () const
118 void operator= (Array const & src)
120 set_size (src.size_);
121 arrcpy (array_,src.array_, size_);
123 Array (Array const & src)
125 array_ = src.copys ();
126 max_ = size_ = src.size_;
129 /// tighten array size_.
130 void tighten_maxsize () {
137 T &operator[] (int i)
142 T const & operator[] (int i) const
147 T &elem_ref (int i) const
149 assert (i >=0&&i<size_);
150 return ((T*)array_)[i];
158 /// add to the end of array
164 // T::operator= (T &) is called here. Safe to use with automatic
168 /// remove and return last entry
173 set_size (size ()-1);
176 /// access last entry
179 return (*this)[size_-j-1];
181 /// return last entry
182 T top (int j=0) const
184 return (*this)[size_-j-1];
187 T& boundary (int dir, int idx)
193 return elem_ref (idx);
195 T boundary (int dir, int idx) const
203 void swap (int i,int j)
206 (*this)[i]= (*this)[j];
212 void insert (T k, int j);
214 remove i-th element, and return it.
222 void unordered_del (int i)
224 elem_ref (i) = top ();
225 set_size (size () -1);
229 assert (i >=0&& i < size_);
230 arrcpy (array_+i, array_+i+1, size_-i-1);
234 void sort (int (*compare) (T const&,T const&),
235 int lower = -1, int upper = -1);
236 void concat (Array<T> const &src)
239 set_size (size_ + src.size_);
240 arrcpy (array_+s,src.array_, src.size_);
242 Array<T> slice (int lower, int upper) const;