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. */
65 boundary (vector<T> const &v, int dir, vsize i)
68 return v[dir == -1 ? i : v.size () - 1 - i];
73 boundary (vector<T> &v, int dir, vsize i)
76 return v[dir == -1 ? i : v.size () - 1 - i];
81 back (vector<T> const &v, vsize i)
83 return v[v.size () - i - 1];
88 back (vector<T> &v, vsize i)
90 return v[v.size () - i - 1];
95 concat (vector<T> &v, vector<T> const& w)
97 v.insert (v.end (), w.begin (), w.end ());
102 binary_search_bounds (vector<T> const &table,
103 T const &key, int (*compare) (T const &, T const &),
116 cmp = (*lo + *hi) / 2;
118 result = (*compare) (key, table[cmp]);
125 while (*hi - *lo > 1);
130 binary_search_bounds (vector<T> const &table,
131 T const *key, int (*compare) (T *const &, T *const &),
141 cmp = (*lo + *hi) / 2;
143 result = (*compare) ((T *) key, table[cmp]);
150 while (*hi - *lo > 1);
156 // binary_search (std::vector<T> const &v,
157 binary_search (vector<T> const &v,
158 T const &key, int (*compare) (T const &, T const &),
159 vsize b=0, vsize e=VPOS)
164 typename vector<T>::const_iterator i = find (v.begin () + b,
165 v.begin () + e, key);
167 return i - v.begin ();
170 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
173 binary_search (vector<T> const &table,
175 int (*compare) (T const &, T const &),
185 binary_search_bounds (table, key, compare, &lo, &hi);
187 if (! (*compare) (key, table[lo]))
199 /* FIXME: the COMPARE functionality is broken? */
202 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
203 vsize lower=VPOS, vsize upper=VPOS)
205 typename vector<T>::iterator b = v.begin ();
206 typename vector<T>::iterator e = v.begin ();
212 ::std::sort (b + lower, e + upper, compare);
217 template<typename T> void
218 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
219 vsize lower=VPOS, vsize upper=VPOS)
224 upper = v.size () - 1;
226 if (upper == VPOS || lower >= upper)
228 swap (v[lower], v[(lower + upper) / 2]);
230 for (vsize i = lower +1; i <= upper; i++)
231 if (compare (v[i], v[lower]) < 0)
232 swap (v[++last], v[i]);
233 swap (v[lower], v[last]);
234 vector_sort (v, compare, lower, last - 1);
235 vector_sort (v, compare, last + 1, upper);
241 reverse (vector<T> &v)
243 // CHECKME: for a simple vector, like vector<int>, this should
245 ::std::reverse (v.begin (), v.end ());
252 v.erase (unique (v.begin (), v.end ()), v.end ());
256 typename vector<T>::const_iterator
257 find (vector<T> const &v, T const &key)
259 return ::std::find (v.begin (), v.end (), key);
262 #if HAVE_BOOST_LAMBDA
263 #include <boost/lambda/lambda.hpp>
264 using namespace boost::lambda;
267 junk_pointers (vector<T> &v)
269 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
274 template<typename T> struct del : public unary_function<T, void>
276 void operator() (T x)
285 junk_pointers (vector<T> &v)
288 ::std::for_each (v.begin (), v.end (), del<T> ());
291 #endif /* HAVE_BOOST_LAMBDA */
295 #else /* ! STD_VECTOR */
313 #endif /* STD_VECTOR */
320 #endif /* STD_VECTOR_HH */