1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
/* Dictionary.cpp
Copyright (c) 2017 by Michael Zahniser
Endless Sky is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later version.
Endless Sky is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with
this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "Dictionary.h"
#include "StringInterner.h"
#include <cstring>
#include <mutex>
#include <set>
#include <string>
using namespace std;
namespace {
// Perform a binary search on a sorted vector. Return the key's location (or
// proper insertion spot) in the first element of the pair, and "true" in
// the second element if the key is already in the vector.
pair<size_t, bool> Search(const char *key, const vector<pair<const char *, double>> &v)
{
// At each step of the search, we know the key is in [low, high).
size_t low = 0;
size_t high = v.size();
while(low != high)
{
size_t mid = (low + high) / 2;
int cmp = strcmp(key, v[mid].first);
if(!cmp)
return make_pair(mid, true);
if(cmp < 0)
high = mid;
else
low = mid + 1;
}
return make_pair(low, false);
}
}
double &Dictionary::operator[](const char *key)
{
pair<size_t, bool> pos = Search(key, *this);
if(pos.second)
return data()[pos.first].second;
return insert(begin() + pos.first, make_pair(StringInterner::Intern(key), 0.))->second;
}
double &Dictionary::operator[](const string &key)
{
return (*this)[key.c_str()];
}
double Dictionary::Get(const char *key) const
{
pair<size_t, bool> pos = Search(key, *this);
return (pos.second ? data()[pos.first].second : 0.);
}
double Dictionary::Get(const string &key) const
{
return Get(key.c_str());
}
void Dictionary::Erase(const char *key)
{
auto [pos, exists] = Search(key, *this);
if(exists)
erase(next(this->begin(), pos));
}
|