2 std-vector.hh -- declare 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 */
19 #include <boost/lambda/lambda.hpp>
23 int default_compare (T const &a, T const &b)
34 int default_compare (T *const &a, T *const &b)
53 #if HAVE_STL_DATA_METHOD
55 #else /* !HAVE_STL_DATA_METHOD */
56 #define vector __vector
62 /* Interface without pointer arithmetic (iterator) semantics. */
64 class vector : public __vector<T>
67 typedef typename __vector<T>::iterator iterator;
68 typedef typename __vector<T>::const_iterator const_iterator;
70 vector<T> () : __vector<T> ()
74 vector<T> (const_iterator b, const_iterator e) : __vector<T> (b, e)
91 /* FIXME: it appears that choosing this function is broken when stl
92 has no data () member too... */
95 binary_search_bounds (vector<T*> const &table,
96 T const *key, int (*compare) (T *const &, T *const &),
106 cmp = (*lo + *hi) / 2;
108 result = (*compare) ((T *) key, table[cmp]);
115 while (*hi - *lo > 1);
117 } /* namespace std */
119 #endif /* !HAVE_STL_DATA_METHOD */
123 boundary (vector<T> const &v, int dir, vsize i)
126 return v[dir == -1 ? i : v.size () - 1 - i];
131 boundary (vector<T> &v, int dir, vsize i)
134 return v[dir == -1 ? i : v.size () - 1 - i];
139 back (vector<T> const &v, vsize i)
141 return v[v.size () - i - 1];
146 back (vector<T> &v, vsize i)
148 return v[v.size () - i - 1];
153 concat (vector<T> &v, vector<T> const& w)
155 v.insert (v.end (), w.begin (), w.end ());
160 binary_search_bounds (vector<T> const &table,
161 T const &key, int (*compare) (T const &, T const &),
174 cmp = (*lo + *hi) / 2;
176 result = (*compare) (key, table[cmp]);
183 while (*hi - *lo > 1);
187 /* FIXME: what if COMPARE is named: int operator == (T const&, T const&),
188 wouldn't that work for most uses of BINARY_SEARCH?
192 binary_search (vector<T> const &v,
193 T const &key, int (*compare) (T const &, T const &),
194 vsize b=0, vsize e=VPOS)
198 typename vector<T>::const_iterator i = find (v.begin () + b,
202 return i - v.begin ();
205 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
208 binary_search (vector<T> const &table,
210 int (*compare) (T const &, T const &),
220 binary_search_bounds (table, key, compare, &lo, &hi);
222 if (! (*compare) (key, table[lo]))
234 /* FIXME: the COMPARE functionality is broken? */
237 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
238 vsize lower=VPOS, vsize upper=VPOS)
240 typename vector<T>::iterator b = v.begin ();
241 typename vector<T>::iterator e = v.begin ();
247 sort (b + lower, e + upper, compare);
252 template<typename T> void
253 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
254 vsize lower=VPOS, vsize upper=VPOS)
259 upper = v.size () - 1;
261 if (upper == VPOS || lower >= upper)
263 swap (v[lower], v[(lower + upper) / 2]);
265 for (vsize i = lower +1; i <= upper; i++)
266 if (compare (v[i], v[lower]) < 0)
267 swap (v[++last], v[i]);
268 swap (v[lower], v[last]);
269 vector_sort (v, compare, lower, last - 1);
270 vector_sort (v, compare, last + 1, upper);
276 reverse (vector<T> &v)
278 // CHECKME: for a simple vector, like vector<int>, this should
280 reverse (v.begin (), v.end ());
287 v.erase (unique (v.begin (), v.end ()), v.end ());
291 typename vector<T>::const_iterator
292 find (vector<T> const &v, T const &key)
294 return find (v.begin (), v.end (), key);
297 #if HAVE_BOOST_LAMBDA
298 #include <boost/lambda/lambda.hpp>
299 using namespace boost::lambda;
302 junk_pointers (vector<T> &v)
304 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
309 template<typename T> struct del : public unary_function<T, void>
311 void operator() (T x)
320 junk_pointers (vector<T> &v)
323 for_each (v.begin (), v.end (), del<T> ());
326 #endif /* HAVE_BOOST_LAMBDA */
328 #endif /* STD_VECTOR_HH */