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)
50 #define VPOS ((vsize) -1)
53 #if HAVE_STL_DATA_METHOD
55 #else /* !HAVE_STL_DATA_METHOD */
56 #define vector __vector
62 /* Interface without pointer arithmetic (iterator) semantics. */
63 template<typename T, typename A=std::allocator<T> >
64 class vector : public __vector<T, A>
67 typedef typename __vector<T>::iterator iterator;
68 typedef typename __vector<T>::const_iterator const_iterator;
70 vector<T, A> () : __vector<T, A> ()
74 vector<T, A> (const_iterator b, const_iterator e) : __vector<T, A> (b, e)
93 #endif /* !HAVE_STL_DATA_METHOD */
97 boundary (vector<T> const &v, int dir, vsize i)
100 return v[dir == -1 ? i : v.size () - 1 - i];
105 boundary (vector<T> &v, int dir, vsize i)
108 return v[dir == -1 ? i : v.size () - 1 - i];
113 back (vector<T> const &v, vsize i)
115 return v[v.size () - i - 1];
120 back (vector<T> &v, vsize i)
122 return v[v.size () - i - 1];
127 concat (vector<T> &v, vector<T> const& w)
129 v.insert (v.end (), w.begin (), w.end ());
134 binary_search_bounds (vector<T> const &table,
135 T const &key, int (*compare) (T const &, T const &),
148 cmp = (*lo + *hi) / 2;
150 result = (*compare) (key, table[cmp]);
157 while (*hi - *lo > 1);
162 binary_search_bounds (vector<T*> const &table,
163 T const *key, int (*compare) (T *const &, T *const &),
173 cmp = (*lo + *hi) / 2;
175 result = (*compare) ((T *) key, table[cmp]);
182 while (*hi - *lo > 1);
186 /* FIXME: what if COMPARE is named: int operator == (T const&, T const&),
187 wouldn't that work for most uses of BINARY_SEARCH?
191 binary_search (vector<T> const &v,
192 T const &key, int (*compare) (T const &, T const &),
193 vsize b=0, vsize e=VPOS)
197 typename vector<T>::const_iterator i = find (v.begin () + b,
201 return i - v.begin ();
204 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
207 binary_search (vector<T> const &table,
209 int (*compare) (T const &, T const &),
219 binary_search_bounds (table, key, compare, &lo, &hi);
221 if (! (*compare) (key, table[lo]))
233 /* FIXME: the COMPARE functionality is broken? */
236 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
237 vsize lower=VPOS, vsize upper=VPOS)
239 typename vector<T>::iterator b = v.begin ();
240 typename vector<T>::iterator e = v.begin ();
246 sort (b + lower, e + upper, compare);
251 template<typename T> void
252 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
253 vsize lower=VPOS, vsize upper=VPOS)
258 upper = v.size () - 1;
260 if (upper == VPOS || lower >= upper)
262 swap (v[lower], v[(lower + upper) / 2]);
264 for (vsize i = lower +1; i <= upper; i++)
265 if (compare (v[i], v[lower]) < 0)
266 swap (v[++last], v[i]);
267 swap (v[lower], v[last]);
268 vector_sort (v, compare, lower, last - 1);
269 vector_sort (v, compare, last + 1, upper);
275 reverse (vector<T> &v)
277 // CHECKME: for a simple vector, like vector<int>, this should
279 reverse (v.begin (), v.end ());
286 v.erase (unique (v.begin (), v.end ()), v.end ());
290 typename vector<T>::const_iterator
291 find (vector<T> const &v, T const &key)
293 return find (v.begin (), v.end (), key);
296 #if HAVE_BOOST_LAMBDA
297 #include <boost/lambda/lambda.hpp>
298 using namespace boost::lambda;
301 junk_pointers (vector<T> &v)
303 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
308 template<typename T> struct del : public unary_function<T, void>
310 void operator() (T x)
319 junk_pointers (vector<T> &v)
322 for_each (v.begin (), v.end (), del<T> ());
325 #endif /* HAVE_BOOST_LAMBDA */
327 #endif /* STD_VECTOR_HH */