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)
49 #define VPOS ((vsize) -1)
52 #if HAVE_STL_DATA_METHOD
54 #else /* !HAVE_STL_DATA_METHOD */
55 #define vector __vector
61 /* Interface without pointer arithmetic (iterator) semantics. */
62 template<typename T, typename A=std::allocator<T> >
63 class vector : public __vector<T, A>
66 typedef typename __vector<T>::iterator iterator;
67 typedef typename __vector<T>::const_iterator const_iterator;
69 vector<T, A> () : __vector<T, A> ()
73 vector<T, A> (vector<T, A> const& v) : __vector<T, A> (v)
77 vector<T, A> (const_iterator b, const_iterator e) : __vector<T, A> (b, e)
96 #endif /* !HAVE_STL_DATA_METHOD */
100 boundary (vector<T> const &v, int dir, vsize i)
103 return v[dir == -1 ? i : v.size () - 1 - i];
108 boundary (vector<T> &v, int dir, vsize i)
111 return v[dir == -1 ? i : v.size () - 1 - i];
116 back (vector<T> const &v, vsize i)
118 return v[v.size () - i - 1];
123 back (vector<T> &v, vsize i)
125 return v[v.size () - i - 1];
130 concat (vector<T> &v, vector<T> const& w)
132 v.insert (v.end (), w.begin (), w.end ());
137 binary_search_bounds (vector<T> const &table,
138 T const &key, int (*compare) (T const &, T const &),
151 cmp = (*lo + *hi) / 2;
153 result = (*compare) (key, table[cmp]);
160 while (*hi - *lo > 1);
165 binary_search_bounds (vector<T*> const &table,
166 T const *key, int (*compare) (T *const &, T *const &),
176 cmp = (*lo + *hi) / 2;
178 result = (*compare) ((T *) key, table[cmp]);
185 while (*hi - *lo > 1);
189 /* FIXME: what if COMPARE is named: int operator == (T const&, T const&),
190 wouldn't that work for most uses of BINARY_SEARCH?
194 binary_search (vector<T> const &v,
195 T const &key, int (*compare) (T const &, T const &),
196 vsize b=0, vsize e=VPOS)
200 typename vector<T>::const_iterator i = find (v.begin () + b,
204 return i - v.begin ();
207 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
210 binary_search (vector<T> const &table,
212 int (*compare) (T const &, T const &),
222 binary_search_bounds (table, key, compare, &lo, &hi);
224 if (! (*compare) (key, table[lo]))
236 /* FIXME: the COMPARE functionality is broken? */
239 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
240 vsize lower=VPOS, vsize upper=VPOS)
242 typename vector<T>::iterator b = v.begin ();
243 typename vector<T>::iterator e = v.begin ();
249 sort (b + lower, e + upper, compare);
254 template<typename T> void
255 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
256 vsize lower=VPOS, vsize upper=VPOS)
261 upper = v.size () - 1;
263 if (upper == VPOS || lower >= upper)
265 swap (v[lower], v[(lower + upper) / 2]);
267 for (vsize i = lower +1; i <= upper; i++)
268 if (compare (v[i], v[lower]) < 0)
269 swap (v[++last], v[i]);
270 swap (v[lower], v[last]);
271 vector_sort (v, compare, lower, last - 1);
272 vector_sort (v, compare, last + 1, upper);
278 reverse (vector<T> &v)
280 // CHECKME: for a simple vector, like vector<int>, this should
282 reverse (v.begin (), v.end ());
289 v.erase (unique (v.begin (), v.end ()), v.end ());
293 typename vector<T>::const_iterator
294 find (vector<T> const &v, T const &key)
296 return find (v.begin (), v.end (), key);
299 #if HAVE_BOOST_LAMBDA
300 #include <boost/lambda/lambda.hpp>
301 using namespace boost::lambda;
304 junk_pointers (vector<T> &v)
306 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
311 template<typename T> struct del : public unary_function<T, void>
313 void operator() (T x)
322 junk_pointers (vector<T> &v)
325 for_each (v.begin (), v.end (), del<T> ());
328 #endif /* HAVE_BOOST_LAMBDA */
330 vector<string> string_split (string str, char c);
332 #endif /* STD_VECTOR_HH */