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 int (*compare) (T *const&, T *const&);
46 vector<T> () : __vector<T> ()
50 vector<T> (iterator const b, iterator const e) : __vector<T> (b, e)
54 vector<T> (vsize b, vsize e) : __vector<T> (iter (b), iter (e))
58 iterator iter (vsize n)
62 return __vector<T>::begin () + n;
65 iterator const iter (vsize n) const
69 return __vector<T>::begin () + n;
75 __vector<T>::insert (this->iter (i), k);
79 insert (vector<T> &v, vsize i)
81 __vector<T>::insert (iter (i), v.begin (), v.end ());
85 concat (vector<T> const &v)
87 __vector<T>::insert (this->end (), v.begin (), v.end ());
90 /* Flower-Array compatibility. */
92 boundary (int dir, vsize i) const
102 boundary (int dir, vsize i)
106 return this->top (i);
114 return *__vector<T>::erase (iter (i));
129 #if 1 // FIXME, silly, but keep for s/r
131 elem_ref (vsize i) const
147 // FIXME, no algorithm for this?
148 T *p = new T[this->size ()];
149 for (vsize i = 0; i < this->size (); i++)
168 // CHECKME: for a simple vector, like vector<int>, this should
170 ::std::reverse (this->begin (), this->end ());
174 slice (vsize b, vsize e) const
176 return vector<T> (b, e);
180 sort (int vsize=VPOS, vsize b=VPOS, vsize e=VPOS)
182 ::std::sort (iter (b), iter(e));
186 sort (int (*compare) (T const &, T const &), int b=-1, int e=-1)
188 ::std::sort (iter (b), iter(e), compare);
191 void swap (vsize i, vsize j)
194 (*this)[i] = (*this)[j];
201 return (*this)[this->size () - i - 1];
207 return (*this)[this->size () - i - 1];
214 // binary_search (std::vector<T> const &v,
215 binary_search (vector<T> const &v,
216 T const &key, int (*compare) (T const &, T const &),
217 vsize b=0, vsize e=VPOS)
220 vector<T>::iterator const i = find (v.iter (b), v.iter (e), key);
222 return i - v.begin ();
225 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
228 binary_search_bounds (vector<T> const &table,
229 T const &key, int (*compare) (T const &, T const &),
239 cmp = (*lo + *hi) / 2;
241 result = (*compare) (key, table[cmp]);
248 while (*hi - *lo > 1);
253 binary_search (vector<T> const &table,
254 T const &key, int (*compare) (T const &, T const &),
261 binary_search_bounds (table, key, compare, &lo, &hi);
263 if (! (*compare) (key, table[lo]))
274 #else /* ! STD_VECTOR */
289 #endif /* STD_VECTOR */
293 #endif /* STD_VECTOR_HH */