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; }
79 // ugh, get around gcc 2.8.1 ice; see bezier.cc
87 /** set the size_ to #s#.
89 Warning: contents are unspecified */
92 if (s >= max_) remax (s);
97 { delete[] array_p_; }
99 /// return a "new"ed copy of array
100 T* copy_array() const
102 T* Tarray = new T[size_];
103 arrcpy (Tarray, array_p_, size_);
111 void operator=(Array const & src)
113 set_size (src.size_);
114 arrcpy (array_p_,src.array_p_, size_);
116 Array (Array const & src)
118 array_p_ = src.copy_array();
119 max_ = size_ = src.size_;
122 /// tighten array size_.
127 T * remove_array_p () {
136 T &operator[] (int i)
141 T const & operator[] (int i) const
146 T &elem (int i) const
148 assert (i >=0&&i<size_);
149 return ((T*)array_p_)[i];
152 /// add to the end of array
158 // T::operator=(T &) is called here. Safe to use with automatic
160 array_p_[size_++] = x;
162 /// remove and return last entry
170 /// access last entry
173 return (*this)[size_-j-1];
175 /// return last entry
176 T top (int j=0) const
178 return (*this)[size_-j-1];
182 void swap (int i,int j)
185 (*this)[i]=(*this)[j];
190 void insert (T k, int j)
192 assert (j >=0 && j<= size_);
194 for (int i=size_-1; i > j; i--)
195 array_p_[i] = array_p_[i-1];
199 remove i-th element, and return it.
207 void unordered_del (int i)
211 set_size (size() -1);
215 assert (i >=0&& i < size_);
216 arrcpy (array_p_+i, array_p_+i+1, size_-i-1);
220 void sort (int (*compare)(T const&,T const&),
221 int lower = -1, int upper = -1)
230 swap (lower, (lower+upper)/2);
232 for (int i= lower +1; i <= upper; i++)
233 if (compare (array_p_[i], array_p_[lower]) < 0)
236 sort (compare, lower, last-1);
237 sort (compare, last+1, upper);
239 void concat (Array<T> const &src)
242 set_size (size_ + src.size_);
243 arrcpy (array_p_+s,src.array_p_, src.size_);
245 Array<T> slice (int lower, int upper)
247 assert (lower >= 0 && lower <=upper&& upper <= size_);
251 arrcpy (r.array_p_, array_p_ + lower, s);
257 for (int i =0,j = size_-1; i < h; i++,j--)