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
53 /* Interface without pointer arithmetic (iterator) semantics. */
55 class vector : public __vector<T>
58 typedef typename __vector<T>::iterator iterator;
59 typedef typename __vector<T>::const_iterator const_iterator;
61 vector<T> () : __vector<T> ()
65 vector<T> (const_iterator b, const_iterator e) : __vector<T> (b, e)
82 /* FIXME: it appears that choosing this function is broken when stl
83 has no data () member too... */
86 binary_search_bounds (vector<T*> const &table,
87 T const *key, int (*compare) (T *const &, T *const &),
97 cmp = (*lo + *hi) / 2;
99 result = (*compare) ((T *) key, table[cmp]);
106 while (*hi - *lo > 1);
110 #endif /* !HAVE_STL_DATA_METHOD */
116 typedef size_t vsize;
117 #define VPOS UINT_MAX
122 boundary (vector<T> const &v, int dir, vsize i)
125 return v[dir == -1 ? i : v.size () - 1 - i];
130 boundary (vector<T> &v, int dir, vsize i)
133 return v[dir == -1 ? i : v.size () - 1 - i];
138 back (vector<T> const &v, vsize i)
140 return v[v.size () - i - 1];
145 back (vector<T> &v, vsize i)
147 return v[v.size () - i - 1];
152 concat (vector<T> &v, vector<T> const& w)
154 v.insert (v.end (), w.begin (), w.end ());
159 binary_search_bounds (vector<T> const &table,
160 T const &key, int (*compare) (T const &, T const &),
173 cmp = (*lo + *hi) / 2;
175 result = (*compare) (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 ::std::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 ::std::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 ::std::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 ::std::for_each (v.begin (), v.end (), del<T> ());
325 #endif /* HAVE_BOOST_LAMBDA */
331 #endif /* STD_VECTOR_HH */