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)
54 /* Flower-Array compatibility. */
56 boundary (int dir, vsize i) const
66 boundary (int dir, vsize i)
78 return (*this)[this->size () - i - 1];
84 return (*this)[this->size () - i - 1];
91 // binary_search (std::vector<T> const &v,
92 binary_search (vector<T> const &v,
93 T const &key, int (*compare) (T const &, T const &),
94 vsize b=0, vsize e=VPOS)
97 typename vector<T>::const_iterator i = find (v.iter (b), v.iter (e), key);
99 return i - v.begin ();
102 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
105 binary_search_bounds (vector<T> const &table,
106 T const &key, int (*compare) (T const &, T const &),
116 cmp = (*lo + *hi) / 2;
118 result = (*compare) (key, table[cmp]);
125 while (*hi - *lo > 1);
130 binary_search (vector<T> const &table,
131 T const &key, int (*compare) (T const &, T const &),
138 binary_search_bounds (table, key, compare, &lo, &hi);
140 if (! (*compare) (key, table[lo]))
150 /* FIXME: the simple test works, but lily barfs. */
153 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
154 vsize lower=VPOS, vsize upper=VPOS)
156 typename vector<T>::iterator b = v.begin ();
157 typename vector<T>::iterator e = v.begin ();
163 ::std::sort (b + lower, e + upper, compare);
177 template<typename T> void
178 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
179 vsize lower=VPOS, vsize upper=VPOS)
184 upper = v.size () - 1;
186 if (upper == VPOS || lower >= upper)
188 swap (v[lower], v[(lower + upper) / 2]);
190 for (vsize i = lower +1; i <= upper; i++)
191 if (compare (v[i], v[lower]) < 0)
192 swap (v[++last], v[i]);
193 swap (v[lower], v[last]);
194 vector_sort (v, compare, lower, last - 1);
195 vector_sort (v, compare, last + 1, upper);
200 reverse (vector<T> &v)
202 // CHECKME: for a simple vector, like vector<int>, this should
204 ::std::reverse (v.begin (), v.end ());
213 #else /* ! STD_VECTOR */
232 #endif /* STD_VECTOR */
235 int default_compare (T const &a, T const &b)
248 #endif /* STD_VECTOR_HH */