2 std-vector.hh -- declare std::vector
4 source file of the GNU LilyPond music typesetter
6 (c) 2006 Jan Nieuwenhuizen <janneke@gnu.org>
12 #include <algorithm> // reverse, sort
15 /* Also declare vector, in the wrong way. */
26 #define vector __vector
38 /* Interface without pointer arithmetic (iterator) semantics. */
40 class vector : public __vector<T>
43 typedef typename __vector<T>::iterator iterator;
44 typedef typename __vector<T>::const_iterator const_iterator;
46 vector<T> () : __vector<T> ()
50 vector<T> (const_iterator b, const_iterator e) : __vector<T> (b, e)
59 return __vector<T>::begin () + n;
67 return __vector<T>::begin () + n;
73 __vector<T>::insert (this->iter (i), k);
77 insert (vector<T> &v, vsize i)
79 __vector<T>::insert (iter (i), v.begin (), v.end ());
83 concat (vector<T> const &v)
85 __vector<T>::insert (this->end (), v.begin (), v.end ());
88 /* Flower-Array compatibility. */
90 boundary (int dir, vsize i) const
100 boundary (int dir, vsize i)
104 return this->top (i);
121 #if 1 // FIXME, silly, but keep for s/r
123 elem_ref (vsize i) const
139 // FIXME, no algorithm for this?
140 T *p = new T[this->size ()];
141 for (vsize i = 0; i < this->size (); i++)
160 // CHECKME: for a simple vector, like vector<int>, this should
162 ::std::reverse (this->begin (), this->end ());
165 void swap (vsize i, vsize j)
168 (*this)[i] = (*this)[j];
175 return (*this)[this->size () - i - 1];
181 return (*this)[this->size () - i - 1];
188 // binary_search (std::vector<T> const &v,
189 binary_search (vector<T> const &v,
190 T const &key, int (*compare) (T const &, T const &),
191 vsize b=0, vsize e=VPOS)
194 typename vector<T>::const_iterator i = find (v.iter (b), v.iter (e), key);
196 return i - v.begin ();
199 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
202 binary_search_bounds (vector<T> const &table,
203 T const &key, int (*compare) (T const &, T const &),
213 cmp = (*lo + *hi) / 2;
215 result = (*compare) (key, table[cmp]);
222 while (*hi - *lo > 1);
227 binary_search (vector<T> const &table,
228 T const &key, int (*compare) (T const &, T const &),
235 binary_search_bounds (table, key, compare, &lo, &hi);
237 if (! (*compare) (key, table[lo]))
247 /* FIXME: the simple test works, but lily barfs. */
250 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
251 vsize lower=VPOS, vsize upper=VPOS)
253 typename vector<T>::iterator b = v.begin ();
254 typename vector<T>::iterator e = v.begin ();
260 ::std::sort (b + lower, e + upper, compare);
264 template<typename T> void
265 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
266 vsize lower=VPOS, vsize upper=VPOS)
271 upper = v.size () - 1;
273 if (upper == VPOS || lower >= upper)
275 v.swap (lower, (lower + upper) / 2);
277 for (vsize i = lower +1; i <= upper; i++)
278 if (compare (v[i], v[lower]) < 0)
280 v.swap (lower, last);
281 vector_sort (v, compare, lower, last - 1);
282 vector_sort (v, compare, last + 1, upper);
291 #else /* ! STD_VECTOR */
310 #endif /* STD_VECTOR */
313 int default_compare (T const &a, T const &b)
326 #endif /* STD_VECTOR_HH */