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
)
54 /* Flower-Array compatibility. */
56 boundary (int dir
, vsize i
) const
66 boundary (int dir
, vsize i
)
79 /* FIXME: forget array? */
80 /* this->resize (0); */
87 // CHECKME: for a simple vector, like vector<int>, this should
89 ::std::reverse (this->begin (), this->end ());
92 void swap (vsize i
, vsize j
)
95 (*this)[i
] = (*this)[j
];
102 return (*this)[this->size () - i
- 1];
108 return (*this)[this->size () - i
- 1];
115 // binary_search (std::vector<T> const &v,
116 binary_search (vector
<T
> const &v
,
117 T
const &key
, int (*compare
) (T
const &, T
const &),
118 vsize b
=0, vsize e
=VPOS
)
121 typename vector
<T
>::const_iterator i
= find (v
.iter (b
), v
.iter (e
), key
);
123 return i
- v
.begin ();
126 #else // c&p from array.icc; cannot easily use stl_algo:find b.o. compare func.
129 binary_search_bounds (vector
<T
> const &table
,
130 T
const &key
, int (*compare
) (T
const &, T
const &),
140 cmp
= (*lo
+ *hi
) / 2;
142 result
= (*compare
) (key
, table
[cmp
]);
149 while (*hi
- *lo
> 1);
154 binary_search (vector
<T
> const &table
,
155 T
const &key
, int (*compare
) (T
const &, T
const &),
162 binary_search_bounds (table
, key
, compare
, &lo
, &hi
);
164 if (! (*compare
) (key
, table
[lo
]))
174 /* FIXME: the simple test works, but lily barfs. */
177 vector_sort (vector
<T
> &v
, int (*compare
) (T
const &, T
const &),
178 vsize lower
=VPOS
, vsize upper
=VPOS
)
180 typename vector
<T
>::iterator b
= v
.begin ();
181 typename vector
<T
>::iterator e
= v
.begin ();
187 ::std::sort (b
+ lower
, e
+ upper
, compare
);
191 template<typename T
> void
192 vector_sort (vector
<T
> &v
, int (*compare
) (T
const &, T
const &),
193 vsize lower
=VPOS
, vsize upper
=VPOS
)
198 upper
= v
.size () - 1;
200 if (upper
== VPOS
|| lower
>= upper
)
202 v
.swap (lower
, (lower
+ upper
) / 2);
204 for (vsize i
= lower
+1; i
<= upper
; i
++)
205 if (compare (v
[i
], v
[lower
]) < 0)
207 v
.swap (lower
, last
);
208 vector_sort (v
, compare
, lower
, last
- 1);
209 vector_sort (v
, compare
, last
+ 1, upper
);
218 #else /* ! STD_VECTOR */
237 #endif /* STD_VECTOR */
240 int default_compare (T
const &a
, T
const &b
)
253 #endif /* STD_VECTOR_HH */