2 (c) Han-Wen Nienhuys 1995,96,97
4 Distributed under GNU GPL
11 /// copy a bare (C-)array from #src# to #dest# sized #count#
13 inline void arrcpy (T*dest, T*src, int count) {
14 for (int i_shadows_local=0; i_shadows_local < count ; i_shadows_local++)
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.
39 /// maximum length of array.
45 /// stretch or shrink array.
46 void remax (int newmax)
48 T* newarr = new T[newmax];
49 size_ = (newmax < size_) ? newmax : size_;
50 arrcpy (newarr, array_p_, size_);
62 assert (max_ >= size_ && size_ >=0);
63 if (max_) assert (array_p_);
77 { array_p_ = 0; max_ =0; size_ =0; }
80 /** set the size_ to #s#.
82 Warning: contents are unspecified */
85 if (s >= max_) remax (s);
90 { delete[] array_p_; }
92 /// return a "new"ed copy of array
95 T* Tarray = new T[size_];
96 arrcpy (Tarray, array_p_, size_);
104 void operator=(Array const & src)
106 set_size (src.size_);
107 arrcpy (array_p_,src.array_p_, size_);
109 Array (Array const & src)
111 array_p_ = src.copy_array();
112 max_ = size_ = src.size_;
115 /// tighten array size_.
120 T * remove_array_p () {
129 T &operator[] (int i)
134 T const & operator[] (int i) const
139 T &elem (int i) const
141 assert (i >=0&&i<size_);
142 return ((T*)array_p_)[i];
145 /// add to the end of array
151 // T::operator=(T &) is called here. Safe to use with automatic
153 array_p_[size_++] = x;
155 /// remove and return last entry
163 /// access last entry
166 return (*this)[size_-j-1];
168 /// return last entry
169 T top (int j=0) const
171 return (*this)[size_-j-1];
175 void swap (int i,int j)
178 (*this)[i]=(*this)[j];
183 void insert (T k, int j)
185 assert (j >=0 && j<= size_);
187 for (int i=size_-1; i > j; i--)
188 array_p_[i] = array_p_[i-1];
192 remove i-th element, and return it.
200 void unordered_del (int i)
204 set_size (size() -1);
208 assert (i >=0&& i < size_);
209 arrcpy (array_p_+i, array_p_+i+1, size_-i-1);
213 void sort (int (*compare)(T const&,T const&),
214 int lower = -1, int upper = -1)
223 swap (lower, (lower+upper)/2);
225 for (int i= lower +1; i <= upper; i++)
226 if (compare (array_p_[i], array_p_[lower]) < 0)
229 sort (compare, lower, last-1);
230 sort (compare, last+1, upper);
232 void concat (Array<T> const &src)
235 set_size (size_ + src.size_);
236 arrcpy (array_p_+s,src.array_p_, src.size_);
238 Array<T> slice (int lower, int upper)
240 assert (lower >= 0 && lower <=upper&& upper <= size_);
244 arrcpy (r.array_p_, array_p_ + lower, s);
250 for (int i =0,j = size_-1; i < h; i++,j--)