2 std-vector.hh -- declare vector
4 source file of the GNU LilyPond music typesetter
6 (c) 2006--2009 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 #if HAVE_BOOST_LAMBDA_LAMBDA_HPP
30 #include <boost/lambda/lambda.hpp>
34 int default_compare (T const &a, T const &b)
45 int default_compare (T *const &a, T *const &b)
60 #define VPOS ((vsize) -1)
63 #if HAVE_STL_DATA_METHOD
65 #else /* !HAVE_STL_DATA_METHOD */
66 #define vector __vector
72 /* Interface without pointer arithmetic (iterator) semantics. */
73 template<typename T, typename A=std::allocator<T> >
74 class vector : public __vector<T, A>
77 typedef typename __vector<T>::iterator iterator;
78 typedef typename __vector<T>::const_iterator const_iterator;
80 vector<T, A> () : __vector<T, A> ()
84 vector<T, A> (vector<T, A> const& v) : __vector<T, A> (v)
88 vector<T, A> (const_iterator b, const_iterator e) : __vector<T, A> (b, e)
105 } /* namespace std */
107 #endif /* !HAVE_STL_DATA_METHOD */
111 boundary (vector<T> const &v, int dir, vsize i)
114 return v[dir == -1 ? i : v.size () - 1 - i];
119 boundary (vector<T> &v, int dir, vsize i)
122 return v[dir == -1 ? i : v.size () - 1 - i];
127 back (vector<T> const &v, vsize i)
129 return v[v.size () - i - 1];
134 back (vector<T> &v, vsize i)
136 return v[v.size () - i - 1];
141 concat (vector<T> &v, vector<T> const& w)
143 v.insert (v.end (), w.begin (), w.end ());
146 template<typename T, typename Compare>
148 lower_bound (vector<T> const &v,
151 vsize b=0, vsize e=VPOS)
155 typename vector<T>::const_iterator i = lower_bound (v.begin () + b,
160 return i - v.begin ();
163 template<typename T, typename Compare>
165 upper_bound (vector<T> const &v,
168 vsize b=0, vsize e=VPOS)
173 typename vector<T>::const_iterator i = upper_bound (v.begin () + b,
178 return i - v.begin ();
181 template<typename T, typename Compare>
183 binary_search (vector<T> const &v,
185 Compare less=less<T> (),
186 vsize b=0, vsize e=VPOS)
188 vsize lb = lower_bound (v, key, less, b, e);
190 if (lb == v.size () || less (key, v[lb]))
195 template<typename T, typename Compare>
197 vector_sort (vector<T> &v,
199 vsize b=0, vsize e=VPOS)
204 sort (v.begin () + b, v.begin () + e, less);
209 reverse (vector<T> &v)
211 // CHECKME: for a simple vector, like vector<int>, this should
213 reverse (v.begin (), v.end ());
220 v.erase (unique (v.begin (), v.end ()), v.end ());
224 typename vector<T>::const_iterator
225 find (vector<T> const &v, T const &key)
227 return find (v.begin (), v.end (), key);
230 #if HAVE_BOOST_LAMBDA_LAMBDA_HPP
231 #include <boost/lambda/lambda.hpp>
232 using namespace boost::lambda;
235 junk_pointers (vector<T> &v)
237 for_each (v.begin (), v.end (), (delete _1, _1 = 0));
242 template<typename T> struct del : public unary_function<T, void>
244 void operator() (T x)
253 junk_pointers (vector<T> &v)
256 for_each (v.begin (), v.end (), del<T> ());
259 #endif /* HAVE_BOOST_LAMBDA */
261 vector<string> string_split (string str, char c);
262 string string_join (vector<string> const &strs, string infix);
264 #define iterof(i,s) typeof((s).begin()) i((s).begin())
266 #endif /* STD_VECTOR_HH */