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);
131 // binary_search (std::vector<T> const &v,
132 binary_search (vector<T> const &v,
133 T const &key, int (*compare) (T const &, T const &),
134 vsize b=0, vsize e=VPOS)
139 typename vector<T>::const_iterator i = find (v.begin () + b,
140 v.begin () + e, key);
142 return i - v.begin ();
145 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
148 binary_search (vector<T> const &table,
150 int (*compare) (T const &, T const &),
160 binary_search_bounds (table, key, compare, &lo, &hi);
162 if (! (*compare) (key, table[lo]))
174 /* FIXME: the COMPARE functionality is broken? */
177 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
178 vsize lower=VPOS, vsize upper=VPOS)
180 typename vector<T>::iterator b = v.begin ();
181 typename vector<T>::iterator e = v.begin ();
187 ::std::sort (b + lower, e + upper, compare);
192 template<typename T> void
193 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
194 vsize lower=VPOS, vsize upper=VPOS)
199 upper = v.size () - 1;
201 if (upper == VPOS || lower >= upper)
203 swap (v[lower], v[(lower + upper) / 2]);
205 for (vsize i = lower +1; i <= upper; i++)
206 if (compare (v[i], v[lower]) < 0)
207 swap (v[++last], v[i]);
208 swap (v[lower], v[last]);
209 vector_sort (v, compare, lower, last - 1);
210 vector_sort (v, compare, last + 1, upper);
216 reverse (vector<T> &v)
218 // CHECKME: for a simple vector, like vector<int>, this should
220 ::std::reverse (v.begin (), v.end ());
227 v.erase (unique (v.begin (), v.end ()), v.end ());
231 typename vector<T>::const_iterator
232 find (vector<T> const &v, T const &key)
234 return ::std::find (v.begin (), v.end (), key);
237 #if HAVE_BOOST_LAMBDA
238 #include <boost/lambda/lambda.hpp>
239 using namespace boost::lambda;
242 junk_pointers (vector<T> &v)
244 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
249 template<typename T> struct del : public unary_function<T, void>
251 void operator() (T x)
260 junk_pointers (vector<T> &v)
263 ::std::for_each (v.begin (), v.end (), del<T> ());
266 #endif /* HAVE_BOOST_LAMBDA */
270 #else /* ! STD_VECTOR */
291 #endif /* !STD_VECTOR */
295 #endif /* STD_VECTOR_HH */