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*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_p_, size_);
61 assert (max_ >= size_ && size_ >=0);
62 if (max_) assert (array_p_);
83 arrcpy (array_p_, tp, n);
87 { array_p_ = 0; max_ =0; size_ =0; }
89 // ugh, get around gcc 2.8.1 ice; see bezier.cc
97 /** set the size_ to #s#.
99 Warning: contents are unspecified */
100 void set_size (int s)
102 if (s >= max_) remax (s);
107 { delete[] array_p_; }
109 /// return a "new"ed copy of array
110 T* copy_array() const
112 T* Tarray = new T[size_];
113 arrcpy (Tarray, array_p_, size_);
121 void operator=(Array const & src)
123 set_size (src.size_);
124 arrcpy (array_p_,src.array_p_, size_);
126 Array (Array const & src)
128 array_p_ = src.copy_array();
129 max_ = size_ = src.size_;
132 /// tighten array size_.
137 T * remove_array_p () {
146 T &operator[] (int i)
151 T const & operator[] (int i) const
156 T &elem (int i) const
158 assert (i >=0&&i<size_);
159 return ((T*)array_p_)[i];
162 /// add to the end of array
168 // T::operator=(T &) is called here. Safe to use with automatic
170 array_p_[size_++] = x;
172 /// remove and return last entry
180 /// access last entry
183 return (*this)[size_-j-1];
185 /// return last entry
186 T top (int j=0) const
188 return (*this)[size_-j-1];
192 void swap (int i,int j)
195 (*this)[i]=(*this)[j];
201 void insert (T k, int j);
203 remove i-th element, and return it.
211 void unordered_del (int i)
215 set_size (size() -1);
219 assert (i >=0&& i < size_);
220 arrcpy (array_p_+i, array_p_+i+1, size_-i-1);
224 void sort (int (*compare)(T const&,T const&),
225 int lower = -1, int upper = -1);
226 void concat (Array<T> const &src)
229 set_size (size_ + src.size_);
230 arrcpy (array_p_+s,src.array_p_, src.size_);
232 Array<T> slice (int lower, int upper)
234 assert (lower >= 0 && lower <=upper&& upper <= size_);
238 arrcpy (r.array_p_, array_p_ + lower, s);