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;
45 vector<T> () : __vector<T> ()
49 vector<T> (iterator const b, iterator const e) : __vector<T> (b, e)
53 vector<T> (vsize b, vsize e) : __vector<T> (iter (b), iter (e))
57 iterator iter (vsize n)
61 return __vector<T>::begin () + n;
64 iterator const iter (vsize n) const
68 return __vector<T>::begin () + n;
74 __vector<T>::insert (this->iter (i), k);
78 insert (vector<T> &v, vsize i)
80 __vector<T>::insert (iter (i), v.begin (), v.end ());
84 concat (vector<T> const &v)
86 __vector<T>::insert (this->end (), v.begin (), v.end ());
89 /* Flower-Array compatibility. */
91 boundary (int dir, vsize i) const
101 boundary (int dir, vsize i)
105 return this->top (i);
122 #if 1 // FIXME, silly, but keep for s/r
124 elem_ref (vsize i) const
140 // FIXME, no algorithm for this?
141 T *p = new T[this->size ()];
142 for (vsize i = 0; i < this->size (); i++)
161 // CHECKME: for a simple vector, like vector<int>, this should
163 ::std::reverse (this->begin (), this->end ());
167 slice (vsize b, vsize e) const
169 return vector<T> (b, e);
173 sort (int vsize=VPOS, vsize b=VPOS, vsize e=VPOS)
175 ::std::sort (iter (b), iter(e));
179 sort (int (*compare) (T const &, T const &), int b=-1, int e=-1)
181 ::std::sort (iter (b), iter(e), compare);
184 void swap (vsize i, vsize j)
187 (*this)[i] = (*this)[j];
194 return (*this)[this->size () - i - 1];
200 return (*this)[this->size () - i - 1];
207 // binary_search (std::vector<T> const &v,
208 binary_search (vector<T> const &v,
209 T const &key, int (*compare) (T const &, T const &),
210 vsize b=0, vsize e=VPOS)
213 vector<T>::iterator const i = find (v.iter (b), v.iter (e), key);
215 return i - v.begin ();
218 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
221 binary_search_bounds (vector<T> const &table,
222 T const &key, int (*compare) (T const &, T const &),
232 cmp = (*lo + *hi) / 2;
234 result = (*compare) (key, table[cmp]);
241 while (*hi - *lo > 1);
246 binary_search (vector<T> const &table,
247 T const &key, int (*compare) (T const &, T const &),
254 binary_search_bounds (table, key, compare, &lo, &hi);
256 if (! (*compare) (key, table[lo]))
268 #else /* ! STD_VECTOR */
287 #endif /* STD_VECTOR */
291 #endif /* STD_VECTOR_HH */