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> /* find, reverse, sort */
13 #include <functional> /* unary_function */
17 #include <boost/lambda/lambda.hpp>
21 int default_compare (T const &a, T const &b)
32 int default_compare (T *const &a, T *const &b)
43 /* Also declare vector, in the wrong way. */
54 #if HAVE_STL_DATA_METHOD
56 #else /* !HAVE_STL_DATA_METHOD */
57 #define vector __vector
61 /* Interface without pointer arithmetic (iterator) semantics. */
63 class vector : public __vector<T>
66 typedef typename __vector<T>::iterator iterator;
67 typedef typename __vector<T>::const_iterator const_iterator;
69 vector<T> () : __vector<T> ()
73 vector<T> (const_iterator b, const_iterator e) : __vector<T> (b, e)
90 /* FIXME: it appears that choosing this function is broken when stl
91 has no data () member too... */
94 binary_search_bounds (vector<T*> const &table,
95 T const *key, int (*compare) (T *const &, T *const &),
105 cmp = (*lo + *hi) / 2;
107 result = (*compare) ((T *) key, table[cmp]);
114 while (*hi - *lo > 1);
118 #endif /* !HAVE_STL_DATA_METHOD */
124 typedef size_t vsize;
125 #define VPOS UINT_MAX
130 boundary (vector<T> const &v, int dir, vsize i)
133 return v[dir == -1 ? i : v.size () - 1 - i];
138 boundary (vector<T> &v, int dir, vsize i)
141 return v[dir == -1 ? i : v.size () - 1 - i];
146 back (vector<T> const &v, vsize i)
148 return v[v.size () - i - 1];
153 back (vector<T> &v, vsize i)
155 return v[v.size () - i - 1];
160 concat (vector<T> &v, vector<T> const& w)
162 v.insert (v.end (), w.begin (), w.end ());
167 binary_search_bounds (vector<T> const &table,
168 T const &key, int (*compare) (T const &, T const &),
181 cmp = (*lo + *hi) / 2;
183 result = (*compare) (key, table[cmp]);
190 while (*hi - *lo > 1);
194 /* FIXME: what if COMPARE is named: int operator == (T const&, T const&),
195 wouldn't that work for most uses of BINARY_SEARCH?
199 binary_search (vector<T> const &v,
200 T const &key, int (*compare) (T const &, T const &),
201 vsize b=0, vsize e=VPOS)
205 typename vector<T>::const_iterator i = find (v.begin () + b,
209 return i - v.begin ();
212 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
215 binary_search (vector<T> const &table,
217 int (*compare) (T const &, T const &),
227 binary_search_bounds (table, key, compare, &lo, &hi);
229 if (! (*compare) (key, table[lo]))
241 /* FIXME: the COMPARE functionality is broken? */
244 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
245 vsize lower=VPOS, vsize upper=VPOS)
247 typename vector<T>::iterator b = v.begin ();
248 typename vector<T>::iterator e = v.begin ();
254 ::std::sort (b + lower, e + upper, compare);
259 template<typename T> void
260 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
261 vsize lower=VPOS, vsize upper=VPOS)
266 upper = v.size () - 1;
268 if (upper == VPOS || lower >= upper)
270 swap (v[lower], v[(lower + upper) / 2]);
272 for (vsize i = lower +1; i <= upper; i++)
273 if (compare (v[i], v[lower]) < 0)
274 swap (v[++last], v[i]);
275 swap (v[lower], v[last]);
276 vector_sort (v, compare, lower, last - 1);
277 vector_sort (v, compare, last + 1, upper);
283 reverse (vector<T> &v)
285 // CHECKME: for a simple vector, like vector<int>, this should
287 ::std::reverse (v.begin (), v.end ());
294 v.erase (unique (v.begin (), v.end ()), v.end ());
298 typename vector<T>::const_iterator
299 find (vector<T> const &v, T const &key)
301 return ::std::find (v.begin (), v.end (), key);
304 #if HAVE_BOOST_LAMBDA
305 #include <boost/lambda/lambda.hpp>
306 using namespace boost::lambda;
309 junk_pointers (vector<T> &v)
311 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
316 template<typename T> struct del : public unary_function<T, void>
318 void operator() (T x)
327 junk_pointers (vector<T> &v)
330 ::std::for_each (v.begin (), v.end (), del<T> ());
333 #endif /* HAVE_BOOST_LAMBDA */
337 #else /* ! STD_VECTOR */
358 #endif /* !STD_VECTOR */
362 #endif /* STD_VECTOR_HH */