2 std-vector.hh -- declare vector
4 source file of the GNU LilyPond music typesetter
6 (c) 2006 Jan Nieuwenhuizen <janneke@gnu.org>
15 leads to dubious crashes - libstdc++ bug?
18 #define _GLIBCXX_DEBUG 1
22 #include <algorithm> /* find, reverse, sort */
23 #include <functional> /* unary_function */
29 #include <boost/lambda/lambda.hpp>
33 int default_compare (T const &a, T const &b)
44 int default_compare (T *const &a, T *const &b)
59 #define VPOS ((vsize) -1)
62 #if HAVE_STL_DATA_METHOD
64 #else /* !HAVE_STL_DATA_METHOD */
65 #define vector __vector
71 /* Interface without pointer arithmetic (iterator) semantics. */
72 template<typename T, typename A=std::allocator<T> >
73 class vector : public __vector<T, A>
76 typedef typename __vector<T>::iterator iterator;
77 typedef typename __vector<T>::const_iterator const_iterator;
79 vector<T, A> () : __vector<T, A> ()
83 vector<T, A> (vector<T, A> const& v) : __vector<T, A> (v)
87 vector<T, A> (const_iterator b, const_iterator e) : __vector<T, A> (b, e)
104 } /* namespace std */
106 #endif /* !HAVE_STL_DATA_METHOD */
110 boundary (vector<T> const &v, int dir, vsize i)
113 return v[dir == -1 ? i : v.size () - 1 - i];
118 boundary (vector<T> &v, int dir, vsize i)
121 return v[dir == -1 ? i : v.size () - 1 - i];
126 back (vector<T> const &v, vsize i)
128 return v[v.size () - i - 1];
133 back (vector<T> &v, vsize i)
135 return v[v.size () - i - 1];
140 concat (vector<T> &v, vector<T> const& w)
142 v.insert (v.end (), w.begin (), w.end ());
145 template<typename T, typename Compare>
147 lower_bound (vector<T> const &v,
150 vsize b=0, vsize e=VPOS)
154 typename vector<T>::const_iterator i = lower_bound (v.begin () + b,
159 return i - v.begin ();
162 template<typename T, typename Compare>
164 upper_bound (vector<T> const &v,
167 vsize b=0, vsize e=VPOS)
172 typename vector<T>::const_iterator i = upper_bound (v.begin () + b,
177 return i - v.begin ();
180 template<typename T, typename Compare>
182 binary_search (vector<T> const &v,
184 Compare less=less<T> (),
185 vsize b=0, vsize e=VPOS)
187 vsize lb = lower_bound (v, key, less, b, e);
189 if (lb == v.size () || less (key, v[lb]))
195 /* FIXME: the COMPARE functionality is broken? */
198 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
199 vsize lower=VPOS, vsize upper=VPOS)
201 typename vector<T>::iterator b = v.begin ();
202 typename vector<T>::iterator e = v.begin ();
208 sort (b + lower, e + upper, compare);
213 template<typename T> void
214 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
215 vsize lower=VPOS, vsize upper=VPOS)
220 upper = v.size () - 1;
222 if (upper == VPOS || lower >= upper)
224 swap (v[lower], v[(lower + upper) / 2]);
226 for (vsize i = lower +1; i <= upper; i++)
227 if (compare (v[i], v[lower]) < 0)
228 swap (v[++last], v[i]);
229 swap (v[lower], v[last]);
230 vector_sort (v, compare, lower, last - 1);
231 vector_sort (v, compare, last + 1, upper);
237 reverse (vector<T> &v)
239 // CHECKME: for a simple vector, like vector<int>, this should
241 reverse (v.begin (), v.end ());
248 v.erase (unique (v.begin (), v.end ()), v.end ());
252 typename vector<T>::const_iterator
253 find (vector<T> const &v, T const &key)
255 return find (v.begin (), v.end (), key);
258 #if HAVE_BOOST_LAMBDA
259 #include <boost/lambda/lambda.hpp>
260 using namespace boost::lambda;
263 junk_pointers (vector<T> &v)
265 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
270 template<typename T> struct del : public unary_function<T, void>
272 void operator() (T x)
281 junk_pointers (vector<T> &v)
284 for_each (v.begin (), v.end (), del<T> ());
287 #endif /* HAVE_BOOST_LAMBDA */
289 vector<string> string_split (string str, char c);
291 #define iterof(i,s) typeof((s).begin()) i((s).begin())
293 #endif /* STD_VECTOR_HH */