diff options
author | Sebastian Geerken <devnull@localhost> | 2013-04-15 10:23:58 +0200 |
---|---|---|
committer | Sebastian Geerken <devnull@localhost> | 2013-04-15 10:23:58 +0200 |
commit | 92116e9be3fbca3c5614b661a2b8e99d466d3819 (patch) | |
tree | 22a58a4ff2e7a9d956ebc184427d123194069a10 | |
parent | 1790b0b654f49d11aefc1900442fe350415af77e (diff) |
Container::bsearch with a variable range to search.
-rw-r--r-- | lout/container.cc | 7 | ||||
-rw-r--r-- | lout/container.hh | 11 |
2 files changed, 14 insertions, 4 deletions
diff --git a/lout/container.cc b/lout/container.cc index 77f0e710..5f3be123 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -203,14 +203,15 @@ void Vector::sort(Comparator *comparator) * size of the array. (This is the value which can be used for * insertion; see insertSortet()). */ -int Vector::bsearch(Object *key, bool mustExist, Comparator *comparator) +int Vector::bsearch(Object *key, bool mustExist, int start, int end, + Comparator *comparator) { // The case !mustExist is not handled by bsearch(3), so here is a // new implementation. - if (numElements == 0) + if (start >= end) return mustExist ? -1 : 0; - int high = numElements - 1, low = 0; + int low = start, high = end; while (true) { int index = (low + high) / 2; diff --git a/lout/container.hh b/lout/container.hh index 70d443f6..9180b9e0 100644 --- a/lout/container.hh +++ b/lout/container.hh @@ -148,8 +148,12 @@ public: inline int size() { return numElements; } void clear(); void sort(object::Comparator *comparator = &object::standardComparator); - int bsearch(Object *key, bool mustExist, + int bsearch(Object *key, bool mustExist, int start, int end, object::Comparator *comparator = &object::standardComparator); + inline int bsearch(Object *key, bool mustExist, + object::Comparator *comparator = + &object::standardComparator) + { return bsearch (key, mustExist, 0, size () - 1, comparator); } }; @@ -421,6 +425,11 @@ public: inline void sort(object::Comparator *comparator = &object::standardComparator) { ((untyped::Vector*)this->base)->sort(comparator); } + inline int bsearch(T *key, bool mustExist, int start, int end, + object::Comparator *comparator = + &object::standardComparator) + { return ((untyped::Vector*)this->base)->bsearch(key, mustExist, start, end, + comparator); } inline int bsearch(T *key, bool mustExist, object::Comparator *comparator = &object::standardComparator) |