xoreos  0.0.5
binsearch.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef COMMON_BINSEARCH_H
26 #define COMMON_BINSEARCH_H
27 
28 #include <cstddef>
29 
30 namespace Common {
31 
33 template<typename TK, typename TV>
35  TK key;
36  TV value;
37 };
38 
40 template<typename TK, typename TV>
41 const BinSearchValue<TK, TV> *binarySearch(const BinSearchValue<TK, TV> *map, size_t size, const TK &value) {
42  size_t low = 0, high = size - 1, midpoint = 0;
43 
44  while ((low <= high) && (high < size) && (low < size)) {
45  midpoint = low + ((high - low) / 2);
46 
47  if (value == map[midpoint].key)
48  return &map[midpoint];
49  else if (value < map[midpoint].key)
50  high = midpoint - 1;
51  else
52  low = midpoint + 1;
53  }
54 
55  return 0;
56 }
57 
58 } // End of namespace Common
59 
60 #endif // COMMON_BINSEARCH_H
Definition: 2dafile.h:39
const BinSearchValue< TK, TV > * binarySearch(const BinSearchValue< TK, TV > *map, size_t size, const TK &value)
Search through this sorted list of key/value pairs.
Definition: binsearch.h:41
Struct template for a generic searchable key/value pair.
Definition: binsearch.h:34