2 std-vector.hh -- declare 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 */
19 #include <boost/lambda/lambda.hpp>
23 int default_compare (T const &a, T const &b)
34 int default_compare (T *const &a, T *const &b)
49 #define VPOS ((vsize) -1)
52 #if HAVE_STL_DATA_METHOD
54 #else /* !HAVE_STL_DATA_METHOD */
55 #define vector __vector
61 /* Interface without pointer arithmetic (iterator) semantics. */
62 template<typename T, typename A=std::allocator<T> >
63 class vector : public __vector<T, A>
66 typedef typename __vector<T>::iterator iterator;
67 typedef typename __vector<T>::const_iterator const_iterator;
69 vector<T, A> () : __vector<T, A> ()
73 vector<T, A> (vector<T, A> const& v) : __vector<T, A> (v)
77 vector<T, A> (const_iterator b, const_iterator e) : __vector<T, A> (b, e)
96 #endif /* !HAVE_STL_DATA_METHOD */
100 boundary (vector<T> const &v, int dir, vsize i)
103 return v[dir == -1 ? i : v.size () - 1 - i];
108 boundary (vector<T> &v, int dir, vsize i)
111 return v[dir == -1 ? i : v.size () - 1 - i];
116 back (vector<T> const &v, vsize i)
118 return v[v.size () - i - 1];
123 back (vector<T> &v, vsize i)
125 return v[v.size () - i - 1];
130 concat (vector<T> &v, vector<T> const& w)
132 v.insert (v.end (), w.begin (), w.end ());
135 template<typename T, typename Compare>
137 lower_bound (vector<T> const &v,
140 vsize b=0, vsize e=VPOS)
144 typename vector<T>::const_iterator i = lower_bound (v.begin () + b,
149 return i - v.begin ();
152 template<typename T, typename Compare>
154 upper_bound (vector<T> const &v,
157 vsize b=0, vsize e=VPOS)
162 typename vector<T>::const_iterator i = upper_bound (v.begin () + b,
167 return i - v.begin ();
170 template<typename T, typename Compare>
172 binary_search (vector<T> const &v,
174 Compare less=less<T> (),
175 vsize b=0, vsize e=VPOS)
177 vsize lb = lower_bound (v, key, less, b, e);
179 if (lb == v.size () || less (key, v[lb]))
185 /* FIXME: the COMPARE functionality is broken? */
188 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
189 vsize lower=VPOS, vsize upper=VPOS)
191 typename vector<T>::iterator b = v.begin ();
192 typename vector<T>::iterator e = v.begin ();
198 sort (b + lower, e + upper, compare);
203 template<typename T> void
204 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
205 vsize lower=VPOS, vsize upper=VPOS)
210 upper = v.size () - 1;
212 if (upper == VPOS || lower >= upper)
214 swap (v[lower], v[(lower + upper) / 2]);
216 for (vsize i = lower +1; i <= upper; i++)
217 if (compare (v[i], v[lower]) < 0)
218 swap (v[++last], v[i]);
219 swap (v[lower], v[last]);
220 vector_sort (v, compare, lower, last - 1);
221 vector_sort (v, compare, last + 1, upper);
227 reverse (vector<T> &v)
229 // CHECKME: for a simple vector, like vector<int>, this should
231 reverse (v.begin (), v.end ());
238 v.erase (unique (v.begin (), v.end ()), v.end ());
242 typename vector<T>::const_iterator
243 find (vector<T> const &v, T const &key)
245 return find (v.begin (), v.end (), key);
248 #if HAVE_BOOST_LAMBDA
249 #include <boost/lambda/lambda.hpp>
250 using namespace boost::lambda;
253 junk_pointers (vector<T> &v)
255 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
260 template<typename T> struct del : public unary_function<T, void>
262 void operator() (T x)
271 junk_pointers (vector<T> &v)
274 for_each (v.begin (), v.end (), del<T> ());
277 #endif /* HAVE_BOOST_LAMBDA */
279 vector<string> string_split (string str, char c);
281 #define iterof(i,s) typeof((s).begin()) i((s).begin())
283 #endif /* STD_VECTOR_HH */