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)
55 concat (vector<T> const &v)
57 __vector<T>::insert (this->end (), v.begin (), v.end ());
60 /* Flower-Array compatibility. */
62 boundary (int dir, vsize i) const
72 boundary (int dir, vsize i)
93 #if 1 // FIXME, silly, but keep for s/r
95 elem_ref (vsize i) const
111 // FIXME, no algorithm for this?
112 T *p = new T[this->size ()];
113 for (vsize i = 0; i < this->size (); i++)
132 // CHECKME: for a simple vector, like vector<int>, this should
134 ::std::reverse (this->begin (), this->end ());
137 void swap (vsize i, vsize j)
140 (*this)[i] = (*this)[j];
147 return (*this)[this->size () - i - 1];
153 return (*this)[this->size () - i - 1];
160 // binary_search (std::vector<T> const &v,
161 binary_search (vector<T> const &v,
162 T const &key, int (*compare) (T const &, T const &),
163 vsize b=0, vsize e=VPOS)
166 typename vector<T>::const_iterator i = find (v.iter (b), v.iter (e), key);
168 return i - v.begin ();
171 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
174 binary_search_bounds (vector<T> const &table,
175 T const &key, int (*compare) (T const &, T const &),
185 cmp = (*lo + *hi) / 2;
187 result = (*compare) (key, table[cmp]);
194 while (*hi - *lo > 1);
199 binary_search (vector<T> const &table,
200 T const &key, int (*compare) (T const &, T const &),
207 binary_search_bounds (table, key, compare, &lo, &hi);
209 if (! (*compare) (key, table[lo]))
219 /* FIXME: the simple test works, but lily barfs. */
222 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
223 vsize lower=VPOS, vsize upper=VPOS)
225 typename vector<T>::iterator b = v.begin ();
226 typename vector<T>::iterator e = v.begin ();
232 ::std::sort (b + lower, e + upper, compare);
236 template<typename T> void
237 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
238 vsize lower=VPOS, vsize upper=VPOS)
243 upper = v.size () - 1;
245 if (upper == VPOS || lower >= upper)
247 v.swap (lower, (lower + upper) / 2);
249 for (vsize i = lower +1; i <= upper; i++)
250 if (compare (v[i], v[lower]) < 0)
252 v.swap (lower, last);
253 vector_sort (v, compare, lower, last - 1);
254 vector_sort (v, compare, last + 1, upper);
263 #else /* ! STD_VECTOR */
282 #endif /* STD_VECTOR */
285 int default_compare (T const &a, T const &b)
298 #endif /* STD_VECTOR_HH */