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> // reverse, sort
15 /* Also declare vector, in the wrong way. */
26 #define vector __vector
38 /* Interface without pointer arithmetic (iterator) semantics. */
40 class vector : public __vector<T>
43 typedef typename __vector<T>::iterator iterator;
44 typedef typename __vector<T>::const_iterator const_iterator;
46 vector<T> () : __vector<T> ()
50 vector<T> (const_iterator b, const_iterator e) : __vector<T> (b, e)
59 return __vector<T>::begin () + n;
67 return __vector<T>::begin () + n;
73 __vector<T>::insert (this->iter (i), k);
77 insert (vector<T> &v, vsize i)
79 __vector<T>::insert (iter (i), v.begin (), v.end ());
83 concat (vector<T> const &v)
85 __vector<T>::insert (this->end (), v.begin (), v.end ());
88 /* Flower-Array compatibility. */
90 boundary (int dir, vsize i) const
100 boundary (int dir, vsize i)
104 return this->top (i);
121 #if 1 // FIXME, silly, but keep for s/r
123 elem_ref (vsize i) const
139 // FIXME, no algorithm for this?
140 T *p = new T[this->size ()];
141 for (vsize i = 0; i < this->size (); i++)
160 // CHECKME: for a simple vector, like vector<int>, this should
162 ::std::reverse (this->begin (), this->end ());
166 sort (int vsize=VPOS, vsize b=VPOS, vsize e=VPOS)
168 ::std::sort (iter (b), iter(e));
172 sort (int (*compare) (T const &, T const &), int b=-1, int e=-1)
174 ::std::sort (iter (b), iter(e), compare);
177 void swap (vsize i, vsize j)
180 (*this)[i] = (*this)[j];
187 return (*this)[this->size () - i - 1];
193 return (*this)[this->size () - i - 1];
200 // binary_search (std::vector<T> const &v,
201 binary_search (vector<T> const &v,
202 T const &key, int (*compare) (T const &, T const &),
203 vsize b=0, vsize e=VPOS)
206 typename vector<T>::const_iterator i = find (v.iter (b), v.iter (e), key);
208 return i - v.begin ();
211 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
214 binary_search_bounds (vector<T> const &table,
215 T const &key, int (*compare) (T const &, T const &),
225 cmp = (*lo + *hi) / 2;
227 result = (*compare) (key, table[cmp]);
234 while (*hi - *lo > 1);
239 binary_search (vector<T> const &table,
240 T const &key, int (*compare) (T const &, T const &),
247 binary_search_bounds (table, key, compare, &lo, &hi);
249 if (! (*compare) (key, table[lo]))
261 #else /* ! STD_VECTOR */
280 #endif /* STD_VECTOR */
284 #endif /* STD_VECTOR_HH */