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)
45 #if HAVE_STL_DATA_METHOD
47 #else /* !HAVE_STL_DATA_METHOD */
48 #define vector __vector
60 /* Interface without pointer arithmetic (iterator) semantics. */
62 class vector : public __vector<T>
65 typedef typename __vector<T>::iterator iterator;
66 typedef typename __vector<T>::const_iterator const_iterator;
68 vector<T> () : __vector<T> ()
72 vector<T> (const_iterator b, const_iterator e) : __vector<T> (b, e)
89 /* FIXME: it appears that choosing this function is broken when stl
90 has no data () member too... */
93 binary_search_bounds (vector<T*> const &table,
94 T const *key, int (*compare) (T *const &, T *const &),
104 cmp = (*lo + *hi) / 2;
106 result = (*compare) ((T *) key, table[cmp]);
113 while (*hi - *lo > 1);
117 #endif /* !HAVE_STL_DATA_METHOD */
123 typedef size_t vsize;
124 #define VPOS UINT_MAX
129 boundary (vector<T> const &v, int dir, vsize i)
132 return v[dir == -1 ? i : v.size () - 1 - i];
137 boundary (vector<T> &v, int dir, vsize i)
140 return v[dir == -1 ? i : v.size () - 1 - i];
145 back (vector<T> const &v, vsize i)
147 return v[v.size () - i - 1];
152 back (vector<T> &v, vsize i)
154 return v[v.size () - i - 1];
159 concat (vector<T> &v, vector<T> const& w)
161 v.insert (v.end (), w.begin (), w.end ());
166 binary_search_bounds (vector<T> const &table,
167 T const &key, int (*compare) (T const &, T const &),
180 cmp = (*lo + *hi) / 2;
182 result = (*compare) (key, table[cmp]);
189 while (*hi - *lo > 1);
193 /* FIXME: what if COMPARE is named: int operator == (T const&, T const&),
194 wouldn't that work for most uses of BINARY_SEARCH?
198 binary_search (vector<T> const &v,
199 T const &key, int (*compare) (T const &, T const &),
200 vsize b=0, vsize e=VPOS)
204 typename vector<T>::const_iterator i = find (v.begin () + b,
208 return i - v.begin ();
211 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
214 binary_search (vector<T> const &table,
216 int (*compare) (T const &, T const &),
226 binary_search_bounds (table, key, compare, &lo, &hi);
228 if (! (*compare) (key, table[lo]))
240 /* FIXME: the COMPARE functionality is broken? */
243 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
244 vsize lower=VPOS, vsize upper=VPOS)
246 typename vector<T>::iterator b = v.begin ();
247 typename vector<T>::iterator e = v.begin ();
253 ::std::sort (b + lower, e + upper, compare);
258 template<typename T> void
259 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
260 vsize lower=VPOS, vsize upper=VPOS)
265 upper = v.size () - 1;
267 if (upper == VPOS || lower >= upper)
269 swap (v[lower], v[(lower + upper) / 2]);
271 for (vsize i = lower +1; i <= upper; i++)
272 if (compare (v[i], v[lower]) < 0)
273 swap (v[++last], v[i]);
274 swap (v[lower], v[last]);
275 vector_sort (v, compare, lower, last - 1);
276 vector_sort (v, compare, last + 1, upper);
282 reverse (vector<T> &v)
284 // CHECKME: for a simple vector, like vector<int>, this should
286 ::std::reverse (v.begin (), v.end ());
293 v.erase (unique (v.begin (), v.end ()), v.end ());
297 typename vector<T>::const_iterator
298 find (vector<T> const &v, T const &key)
300 return ::std::find (v.begin (), v.end (), key);
303 #if HAVE_BOOST_LAMBDA
304 #include <boost/lambda/lambda.hpp>
305 using namespace boost::lambda;
308 junk_pointers (vector<T> &v)
310 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
315 template<typename T> struct del : public unary_function<T, void>
317 void operator() (T x)
326 junk_pointers (vector<T> &v)
329 ::std::for_each (v.begin (), v.end (), del<T> ());
332 #endif /* HAVE_BOOST_LAMBDA */
338 #endif /* STD_VECTOR_HH */