diff options
author | Sebastian Geerken <devnull@localhost> | 2014-02-21 18:41:47 +0100 |
---|---|---|
committer | Sebastian Geerken <devnull@localhost> | 2014-02-21 18:41:47 +0100 |
commit | 90aa934e73733080b372bbaeecde63b15a62c4e5 (patch) | |
tree | 58b4a8f04a179d013b1d92605a1308cc972f5c2f | |
parent | 276e8bbb2b443f32856891dfeec5ddbf71827489 (diff) |
RTFL stuff.
-rw-r--r-- | lout/Makefile.am | 3 | ||||
-rw-r--r-- | lout/container.cc | 73 |
2 files changed, 44 insertions, 32 deletions
diff --git a/lout/Makefile.am b/lout/Makefile.am index bef9696e..f2219360 100644 --- a/lout/Makefile.am +++ b/lout/Makefile.am @@ -1,5 +1,6 @@ AM_CPPFLAGS = \ - -I$(top_srcdir) + -I$(top_srcdir) \ + -DCUR_WORKING_DIR='"@BASE_CUR_WORKING_DIR@/lout"' noinst_LIBRARIES = liblout.a diff --git a/lout/container.cc b/lout/container.cc index 908df4ae..0e06722e 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -22,6 +22,7 @@ #include "container.hh" #include "misc.hh" +#include "debug.hh" namespace lout { @@ -209,41 +210,51 @@ int Vector::bsearch(Object *key, bool mustExist, int start, int end, // The case !mustExist is not handled by bsearch(3), so here is a // new implementation. - if (start > end) - return mustExist ? -1 : start; - - int low = start, high = end; - - while (true) { - int index = (low + high) / 2; - int c = comparator->compare (key, array[index]); - if (c == 0) - return index; - else { - if (low >= high) { - if (mustExist) - return -1; + DBG_OBJ_MSGF ("container", 0, + "<b>bsearch</b> (<i>key</i>, %s, %d, %d, <i>comparator</i>", + mustExist ? "true" : "false", start, end); + DBG_OBJ_MSG_START (); + + int result; + + if (start > end) { + DBG_OBJ_MSG ("container", 1, "empty"); + result = mustExist ? -1 : start; + } else { + int low = start, high = end; + bool found = false; + + while (!found) { + int index = (low + high) / 2; + int c = comparator->compare (key, array[index]); + DBG_OBJ_MSGF ("container", 1, + "searching within %d and %d; compare key with #%d => %d", + low, start, index, c); + if (c == 0) { + found = true; + result = index; + } else { + if (low >= high) { + if (mustExist) { + found = true; + result = -1; + } else { + found = true; + result = c > 0 ? index + 1 : index; + } + } + + if (c < 0) + high = index - 1; else - return c > 0 ? index + 1 : index; + low = index + 1; } - - if (c < 0) - high = index - 1; - else - low = index + 1; } - } - + } - /* - Comparator::compareFunComparator = comparator; - void *result = ::bsearch (&key, array, numElements, sizeof (Object*), - Comparable::compareFun); - if (result) - return (Object**)result - array; - else - return -1; - */ + DBG_OBJ_MSGF ("container", 1, "result = %d", result); + DBG_OBJ_MSG_END (); + return result; } Object *Vector::VectorIterator::getNext() |