summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorZach Reizner <zach297@gmail.com>2015-05-18 16:17:19 -0700
committerZach Reizner <zach297@gmail.com>2015-05-18 17:29:51 -0700
commitc8f1682e076db24a2c431d1a8438b000a45184b2 (patch)
treea6ce308096c5b53dd61796225db74df88bd8a2e5 /include
parentf64e040896c27df7c283e8dfba4b6f267b5b96d9 (diff)
downloadflatbuffers-c8f1682e076db24a2c431d1a8438b000a45184b2.tar.gz
flatbuffers-c8f1682e076db24a2c431d1a8438b000a45184b2.tar.bz2
flatbuffers-c8f1682e076db24a2c431d1a8438b000a45184b2.zip
Use std::bsearch in LookupByKey for binary search
Diffstat (limited to 'include')
-rw-r--r--include/flatbuffers/flatbuffers.h42
1 files changed, 21 insertions, 21 deletions
diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h
index f77b13ee..8f0671bc 100644
--- a/include/flatbuffers/flatbuffers.h
+++ b/include/flatbuffers/flatbuffers.h
@@ -305,28 +305,17 @@ public:
}
template<typename K> return_type LookupByKey(K key) const {
- auto span = size();
- uoffset_t start = 0;
- // Perform binary search for key.
- while (span) {
- // Compare against middle element of current span.
- auto middle = span / 2;
- auto table = Get(start + middle);
- auto comp = table->KeyCompareWithValue(key);
- if (comp > 0) {
- // Greater than. Adjust span and try again.
- span = middle;
- } else if (comp < 0) {
- // Less than. Adjust span and try again.
- middle++;
- start += middle;
- span -= middle;
- } else {
- // Found element.
- return table;
- }
+ std::size_t count = size();
+ void *search_result = std::bsearch(&key, Data(), count,
+ IndirectHelper<T>::element_stride, KeyCompare<K>);
+
+ if (!search_result) {
+ return nullptr; // Key not found.
}
- return nullptr; // Key not found.
+
+ const uint8_t *data = reinterpret_cast<const uint8_t *>(search_result);
+
+ return IndirectHelper<T>::Read(data, 0);
}
protected:
@@ -335,6 +324,17 @@ protected:
Vector();
uoffset_t length_;
+
+private:
+ template<typename K> static int KeyCompare(const void *ap, const void *bp) {
+ const K *key = reinterpret_cast<const K *>(ap);
+ const uint8_t *data = reinterpret_cast<const uint8_t *>(bp);
+ auto table = IndirectHelper<T>::Read(data, 0);
+
+ // std::bsearch compares with the operands transposed, so we negate the
+ // result here.
+ return -table->KeyCompareWithValue(*key);
+ }
};
// Convenient helper function to get the length of any vector, regardless