lilypond-1.3.18
[lilypond.git] / flower / include / parray.hh
bloba9baeab836c58c7f80c8f4c11c30e380906b24e1
1 /*
2 parray.hh -- declare Pointer_array
4 source file of the Flower Library
6 (c) 1997--1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
10 #ifndef PARRAY_HH
11 #define PARRAY_HH
13 #include "array.hh"
15 /**
16 an array of pointers.
18 TODO
19 should init to 0.
21 template<class T>
22 class Link_array : private Array<void *>
24 static int default_compare (T *const& p1, T *const&p2) {
25 /* can't do p1 -p2, since T might be an incomplete type */
26 if (p1 < p2)
27 return -1 ;
28 if (p2 < p1)
29 return 1;
30 return 0;
32 Link_array (Array<void*> v)
33 :Array<void*> (v)
36 public:
37 Link_array()
40 Link_array(T * const *tp, int n)
41 : Array<void*> ((void **)tp, n)
45 Link_array (Link_array<T> const &src)
46 : Array<void*> (src)
49 /// access element
50 T *elem (int i) const
52 return elem_ref (i);
54 T *&elem_ref (int i) const
56 return (T*&) Array<void*>::elem_ref (i);
59 /// access element
60 T* &operator[] (int i)
62 return (T*&) Array<void*>::elem_ref (i);
64 /// access element
65 T *const operator[] (int i) const
67 return (T *const) Array<void*>::elem (i);
69 T *pop ()
71 return (T*) Array<void*>::pop ();
73 void insert (T *t, int i)
75 Array<void*>::insert (t, i);
77 void push (T* t)
79 Array<void*>::push (t);
81 /// return last entry
82 T* top (int j=0) const
84 return (T*) Array<void*>::top (j);
86 T *& top (int i=0)
88 return (T*&) Array<void*>::top (i);
90 void substitute (T *old, T*new_l)
92 int i;
93 while ((i = find_i (old)) >=0)
94 if (new_l)
95 elem_ref (i) =new_l;
96 else
97 del (i);
99 void unordered_substitute (T* old, T * new_l)
101 int i;
102 while ((i = find_i (old)) >=0)
103 if (new_l)
104 elem_ref (i) =new_l;
105 else {
106 unordered_del (i);
110 void default_sort() {
111 sort (default_compare);
113 // quicksort.
114 void sort (int (*compare)(T *const&,T *const&),
115 int lower = -1, int upper = -1);
117 void uniq() {
118 Link_array<T> l_arr;
119 for (int i=0; i < size(); i++)
120 if (!i || elem (i-1) != elem (i))
121 l_arr.push (elem (i));
122 *this = l_arr;
124 Array<void*>::del;
125 Array<void*>::unordered_del;
126 Array<void*>::size;
127 Array<void*>::clear;
128 Array<void*>::set_size;
129 Array<void*>::empty;
130 Array<void*>::reverse;
131 Array<void*>::tighten_maxsize;
133 T *& boundary (int d, int i)
135 assert (d);
136 if (d == 1)
137 return top (i);
138 else
139 return elem_ref (i);
141 T * boundary (int d, int i)const
143 assert (d);
144 if (d == 1)
145 return top (i);
146 else
147 return elem_ref (i);
151 T ** access_array () const
153 return (T**) Array<void*>::access_array();
155 T * get (int i)
157 return (T*) Array<void*>::get (i);
159 Link_array<T>
160 slice(int l,int u)
162 return Array<void*>::slice (l,u);
164 void concat (Link_array<T> const &a2)
166 Array<void*>::concat (a2);
168 int find_i (T const * t) const {
169 for (int i=0; i < size(); i++)
170 if (elem (i) == t)
171 return i;
172 return -1;
174 T *find_l (T const *t) const
176 int i = find_i (t);
177 if (i >= 0)
178 return elem (i);
179 else
180 return 0;
185 template<class T, class V>
186 Link_array<T>
187 typecast_array (Link_array<V> const &a, T * /* dummy */ )
189 Link_array<T> ret;
190 for (int i=a.size (); i-- ; )
191 ret.push (dynamic_cast<T*> (a[i])); // ugh?
192 return ret;
197 template<class T> inline void
198 Link_array<T>::sort (int (*compare)(T *const&,T *const&),
199 int lower = -1, int upper = -1)
201 if (lower < 0)
203 lower = 0 ;
204 upper = size () - 1;
206 if (lower >= upper)
207 return;
208 swap (lower, (lower+upper)/2);
209 int last = lower;
210 for (int i= lower +1; i <= upper; i++)
211 if (compare (elem (i), elem(lower)) < 0)
212 swap (++last,i);
213 swap (lower, last);
214 sort (compare, lower, last-1);
215 sort (compare, last+1, upper);
218 template<class T>
219 void
220 junk_pointer_array (Link_array<T> &a)
222 for (int i=0; i < a.size (); i++)
224 delete a[i];
226 a.clear ();
232 lookup with binsearch, return tokencode.
234 template<class T>
236 binsearch_array (Array<T> const &arr, T t, int (*compare)(T const&,T const&))
238 int lo;
239 int hi;
240 int cmp;
241 int result;
242 lo = 0;
243 hi = maxkey;
245 /* binary search */
248 cmp = (lo + hi) / 2;
250 result = compare (t, arr[cmp]);
252 if (result < 0)
253 hi = cmp;
254 else
255 lo = cmp;
257 while (hi - lo > 1);
258 if (!compare (t, arr[lo]))
259 return lo;
260 else
261 return -1; /* not found */
265 template<class T>
267 binsearch_link_array (Link_array<T> const &arr, T *t,
268 int (*compare)(T *const&,T *const&))
270 int lo;
271 int hi;
272 int cmp;
273 int result;
274 lo = 0;
275 hi = arr.size();
277 if (hi == 0)
278 return -1;
280 /* binary search */
283 cmp = (lo + hi) / 2;
285 result = compare (t, arr[cmp]);
287 if (result < 0)
288 hi = cmp;
289 else
290 lo = cmp;
292 while (hi - lo > 1);
293 if (!compare (t, arr[lo]))
294 return lo;
295 else
296 return -1; /* not found */
300 #endif // PARRAY_HH