diff options
author | Eugene Sandulenko | 2016-05-22 22:58:38 +0200 |
---|---|---|
committer | Eugene Sandulenko | 2016-05-22 22:59:41 +0200 |
commit | a6fd06074da4720c77e83dca90d22c88a8dbe0b8 (patch) | |
tree | 055a08c010077492ccaa0ae835cd2134e0e14b18 | |
parent | 018828ba90bd60c457ad8d5e699226646aaeed42 (diff) | |
download | scummvm-rg350-a6fd06074da4720c77e83dca90d22c88a8dbe0b8.tar.gz scummvm-rg350-a6fd06074da4720c77e83dca90d22c88a8dbe0b8.tar.bz2 scummvm-rg350-a6fd06074da4720c77e83dca90d22c88a8dbe0b8.zip |
COMMON: Fix SortedArray implementation.
Had to use a modified bsearch for finding the nearest element.
-rw-r--r-- | common/array.h | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/common/array.h b/common/array.h index 320b367809..869d79b68b 100644 --- a/common/array.h +++ b/common/array.h @@ -378,7 +378,7 @@ public: * Inserts element at the sorted position. */ void insert(const T &element) { - T *where = (T *)bsearch(element, this->front(), this->_size, sizeof(T), _comparator); + T *where = (T *)bsearchMin(element, this->front(), this->_size, sizeof(T), _comparator); insert(where, element); } @@ -406,7 +406,29 @@ public: error("Operation not allowed with SortedArray"); } +private: + // Based on code Copyright (C) 2008-2009 Ksplice, Inc. + // Author: Tim Abbott <tabbott@ksplice.com> + // Licensed under GPLv2+ + void *bsearchMin(const void *key, const void *base, uint num, uint size, + int (*cmp)(const void *key, const void *elt)) { + uint start = 0, end = num; + int result; + + while (start < end) { + uint mid = start + (end - start) / 2; + + result = cmp(key, (byte *)base + mid * size); + if (result < 0) + end = mid; + else if (result > 0) + start = mid + 1; + else + return (void *)((byte *)base + mid * size); + } + return (void *)((byte *)base + start * size); + } private: int (*_comparator)(const void *, const void *); |