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. */
38 boundary (vector<T> const &v, int dir, vsize i)
41 return v[dir == 1 ? i : v.size () - 1 - i];
46 boundary (vector<T> &v, int dir, vsize i)
49 return v[dir == 1 ? i : v.size () - 1 - i];
54 back (vector<T> const &v, vsize i)
56 return v[v.size () - i - 1];
61 back (vector<T> &v, vsize i)
63 return v[v.size () - i - 1];
69 // binary_search (std::vector<T> const &v,
70 binary_search (vector<T> const &v,
71 T const &key, int (*compare) (T const &, T const &),
72 vsize b=0, vsize e=VPOS)
75 typename vector<T>::const_iterator i = find (v.iter (b), v.iter (e), key);
77 return i - v.begin ();
80 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
83 binary_search_bounds (vector<T> const &table,
84 T const &key, int (*compare) (T const &, T const &),
94 cmp = (*lo + *hi) / 2;
96 result = (*compare) (key, table[cmp]);
103 while (*hi - *lo > 1);
108 binary_search (vector<T> const &table,
109 T const &key, int (*compare) (T const &, T const &),
116 binary_search_bounds (table, key, compare, &lo, &hi);
118 if (! (*compare) (key, table[lo]))
128 /* FIXME: the simple test works, but lily barfs. */
131 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
132 vsize lower=VPOS, vsize upper=VPOS)
134 typename vector<T>::iterator b = v.begin ();
135 typename vector<T>::iterator e = v.begin ();
141 ::std::sort (b + lower, e + upper, compare);
155 template<typename T> void
156 vector_sort (vector<T> &v, int (*compare) (T const &, T const &),
157 vsize lower=VPOS, vsize upper=VPOS)
162 upper = v.size () - 1;
164 if (upper == VPOS || lower >= upper)
166 swap (v[lower], v[(lower + upper) / 2]);
168 for (vsize i = lower +1; i <= upper; i++)
169 if (compare (v[i], v[lower]) < 0)
170 swap (v[++last], v[i]);
171 swap (v[lower], v[last]);
172 vector_sort (v, compare, lower, last - 1);
173 vector_sort (v, compare, last + 1, upper);
178 reverse (vector<T> &v)
180 // CHECKME: for a simple vector, like vector<int>, this should
182 ::std::reverse (v.begin (), v.end ());
191 #else /* ! STD_VECTOR */
210 #endif /* STD_VECTOR */
213 int default_compare (T const &a, T const &b)
226 #endif /* STD_VECTOR_HH */