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 concat (vector<T> const &v)
58 __vector<T>::insert (this->end (), v.begin (), v.end ());
62 boundary (int dir, vsize i) const
72 boundary (int dir, vsize i)
85 /* FIXME: forget array? */
86 /* this->resize (0); */
93 // CHECKME: for a simple vector, like vector<int>, this should
95 ::std::reverse (this->begin (), this->end ());
98 void swap (vsize i, vsize j)
101 (*this)[i] = (*this)[j];
108 return (*this)[this->size () - i - 1];
114 return (*this)[this->size () - i - 1];
121 // binary_search (std::vector<T> const &v,
122 binary_search (vector<T> const &v,
123 T const &key, int (*compare) (T const &, T const &),
124 vsize b=0, vsize e=VPOS)
127 typename vector<T>::const_iterator i = find (v.iter (b), v.iter (e), key);
129 return i - v.begin ();
132 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
135 binary_search_bounds (vector<T> const &table,
136 T const &key, int (*compare) (T const &, T const &),
146 cmp = (*lo + *hi) / 2;
148 result = (*compare) (key, table[cmp]);
155 while (*hi - *lo > 1);
160 binary_search (vector<T> const &table,
161 T const &key, int (*compare) (T const &, T const &),
168 binary_search_bounds (table, key, compare, &lo, &hi);
170 if (! (*compare) (key, table[lo]))
180 /* FIXME: the simple test works, but lily barfs. */
183 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
184 vsize lower=VPOS, vsize upper=VPOS)
186 typename vector<T>::iterator b = v.begin ();
187 typename vector<T>::iterator e = v.begin ();
193 ::std::sort (b + lower, e + upper, compare);
197 template<typename T> void
198 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
199 vsize lower=VPOS, vsize upper=VPOS)
204 upper = v.size () - 1;
206 if (upper == VPOS || lower >= upper)
208 v.swap (lower, (lower + upper) / 2);
210 for (vsize i = lower +1; i <= upper; i++)
211 if (compare (v[i], v[lower]) < 0)
213 v.swap (lower, last);
214 vector_sort (v, compare, lower, last - 1);
215 vector_sort (v, compare, last + 1, upper);
224 #else /* ! STD_VECTOR */
243 #endif /* STD_VECTOR */
246 int default_compare (T const &a, T const &b)
259 #endif /* STD_VECTOR_HH */