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 #endif /* !HAVE_STL_DATA_METHOD */
102 boundary (vector<T> const &v, int dir, vsize i)
105 return v[dir == -1 ? i : v.size () - 1 - i];
110 boundary (vector<T> &v, int dir, vsize i)
113 return v[dir == -1 ? i : v.size () - 1 - i];
118 back (vector<T> const &v, vsize i)
120 return v[v.size () - i - 1];
125 back (vector<T> &v, vsize i)
127 return v[v.size () - i - 1];
132 concat (vector<T> &v, vector<T> const& w)
134 v.insert (v.end (), w.begin (), w.end ());
139 binary_search_bounds (vector<T> const &table,
140 T const &key, int (*compare) (T const &, T const &),
153 cmp = (*lo + *hi) / 2;
155 result = (*compare) (key, table[cmp]);
162 while (*hi - *lo > 1);
168 // binary_search (std::vector<T> const &v,
169 binary_search (vector<T> const &v,
170 T const &key, int (*compare) (T const &, T const &),
171 vsize b=0, vsize e=VPOS)
176 typename vector<T>::const_iterator i = find (v.begin () + b,
177 v.begin () + e, key);
179 return i - v.begin ();
182 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
185 binary_search (vector<T> const &table,
187 int (*compare) (T const &, T const &),
197 binary_search_bounds (table, key, compare, &lo, &hi);
199 if (! (*compare) (key, table[lo]))
211 /* FIXME: the COMPARE functionality is broken? */
214 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
215 vsize lower=VPOS, vsize upper=VPOS)
217 typename vector<T>::iterator b = v.begin ();
218 typename vector<T>::iterator e = v.begin ();
224 ::std::sort (b + lower, e + upper, compare);
229 template<typename T> void
230 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
231 vsize lower=VPOS, vsize upper=VPOS)
236 upper = v.size () - 1;
238 if (upper == VPOS || lower >= upper)
240 swap (v[lower], v[(lower + upper) / 2]);
242 for (vsize i = lower +1; i <= upper; i++)
243 if (compare (v[i], v[lower]) < 0)
244 swap (v[++last], v[i]);
245 swap (v[lower], v[last]);
246 vector_sort (v, compare, lower, last - 1);
247 vector_sort (v, compare, last + 1, upper);
253 reverse (vector<T> &v)
255 // CHECKME: for a simple vector, like vector<int>, this should
257 ::std::reverse (v.begin (), v.end ());
264 v.erase (unique (v.begin (), v.end ()), v.end ());
268 typename vector<T>::const_iterator
269 find (vector<T> const &v, T const &key)
271 return ::std::find (v.begin (), v.end (), key);
274 #if HAVE_BOOST_LAMBDA
275 #include <boost/lambda/lambda.hpp>
276 using namespace boost::lambda;
279 junk_pointers (vector<T> &v)
281 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
286 template<typename T> struct del : public unary_function<T, void>
288 void operator() (T x)
297 junk_pointers (vector<T> &v)
300 ::std::for_each (v.begin (), v.end (), del<T> ());
303 #endif /* HAVE_BOOST_LAMBDA */
307 #else /* ! STD_VECTOR */
328 #endif /* !STD_VECTOR */
332 #endif /* STD_VECTOR_HH */