2 std-vector.hh -- declare vector
4 source file of the GNU LilyPond music typesetter
6 (c) 2006--2007 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 */
28 #if HAVE_BOOST_LAMBDA_LAMBDA_HPP
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]))
194 template<typename T, typename Compare>
196 vector_sort (vector<T> &v,
198 vsize b=0, vsize e=VPOS)
203 sort (v.begin () + b, v.begin () + e, less);
208 reverse (vector<T> &v)
210 // CHECKME: for a simple vector, like vector<int>, this should
212 reverse (v.begin (), v.end ());
219 v.erase (unique (v.begin (), v.end ()), v.end ());
223 typename vector<T>::const_iterator
224 find (vector<T> const &v, T const &key)
226 return find (v.begin (), v.end (), key);
229 #if HAVE_BOOST_LAMBDA_LAMBDA_HPP
230 #include <boost/lambda/lambda.hpp>
231 using namespace boost::lambda;
234 junk_pointers (vector<T> &v)
236 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
241 template<typename T> struct del : public unary_function<T, void>
243 void operator() (T x)
252 junk_pointers (vector<T> &v)
255 for_each (v.begin (), v.end (), del<T> ());
258 #endif /* HAVE_BOOST_LAMBDA */
260 vector<string> string_split (string str, char c);
261 string string_join (vector<string> const &strs, string infix);
263 #define iterof(i,s) typeof((s).begin()) i((s).begin())
265 #endif /* STD_VECTOR_HH */