aboutsummaryrefslogtreecommitdiff
path: root/lout/container.cc
diff options
context:
space:
mode:
authorSebastian Geerken <devnull@localhost>2013-03-05 14:28:34 +0100
committerSebastian Geerken <devnull@localhost>2013-03-05 14:28:34 +0100
commit09cbe1a36c6ca2762d2e49f173db7ee4fb683448 (patch)
treeaaf322ad1388f2c8a507d1c6a51779c8dd025f24 /lout/container.cc
parent98465834b8b0e7c4a563e651a51befd2a2ab06ac (diff)
parente844f4119a6a5b144b18af4bcc841c816f575159 (diff)
Merge with main repo.
Diffstat (limited to 'lout/container.cc')
-rw-r--r--lout/container.cc64
1 files changed, 60 insertions, 4 deletions
diff --git a/lout/container.cc b/lout/container.cc
index deeede57..5e5eda73 100644
--- a/lout/container.cc
+++ b/lout/container.cc
@@ -190,16 +190,72 @@ void Vector::remove(int pos)
*/
void Vector::sort()
{
- qsort(array, numElements, sizeof(Object*), misc::Comparable::compareFun);
+ qsort (array, numElements, sizeof(Object*), Comparable::compareFun);
}
-
/**
- * \bug Not implemented.
+ * \brief Use binary search to find an element in a sorted vector.
+ *
+ * If "mustExist" is true, only exact matches are found; otherwise, -1
+ * is returned. If it is false, the position of the next greater
+ * element is returned, or, if the key is the greatest element, the
+ * size of the array. (This is the value which can be used for
+ * insertion; see insertSortet()).
*/
+int Vector::bsearch(Object *key, bool mustExist)
+{
+ // The case !mustExist is not handled by bsearch(3), so here is a
+ // new implementation.
+
+ int high = numElements - 1, low = 0;
+
+ while (true) {
+ int index = (low + high) / 2;
+ int c = ((Comparable*) key)->compareTo ((Comparable*)array[index]);
+ if (c == 0)
+ return index;
+ else {
+ if (low >= high) {
+ if (mustExist)
+ return -1;
+ else
+ return c > 0 ? index + 1 : index;
+ }
+
+ if (c < 0)
+ high = index - 1;
+ else
+ low = index + 1;
+ }
+ }
+
+
+ /*
+ void *result = ::bsearch (&key, array, numElements, sizeof (Object*),
+ Comparable::compareFun);
+ if (result)
+ return (Object**)result - array;
+ else
+ return -1;
+ */
+}
+
+Object *Vector::VectorIterator::getNext()
+{
+ if (index < vector->numElements - 1)
+ index++;
+
+ return index < vector->numElements ? vector->array[index] : NULL;
+}
+
+bool Vector::VectorIterator::hasNext()
+{
+ return index < vector->numElements - 1;
+}
+
Collection0::AbstractIterator* Vector::createIterator()
{
- return NULL;
+ return new VectorIterator(this);
}
// ------------