19 #ifndef SCID_NAMEBASE_H 20 #define SCID_NAMEBASE_H 34 std::vector<std::unique_ptr<const char[]> > names_[
NUM_NAME_TYPES];
35 std::vector<eloT> eloV_;
37 bool operator()(
const char* str1,
const char* str2)
const {
52 return static_cast<uint>(*str1) <
static_cast<uint>(*str2);
85 auto exists = idx_[nt].lower_bound(name);
86 if (exists != idx_[nt].end() &&
87 !idx_[nt].key_comp()(name, exists->first))
88 return std::make_pair(
OK, exists->second);
90 const size_t nameLen = strlen(name);
91 if (nameLen > MAX_LEN)
94 if (names_[nt].
size() >= MAX_ID)
97 char* buf =
new char[nameLen + 1];
98 std::copy_n(name, nameLen + 1, buf);
100 names_[nt].emplace_back(buf);
101 idx_[nt].emplace_hint(exists, buf, newID);
106 return std::make_pair(
OK, newID);
118 std::tuple<decltype(idx_) &, decltype(names_) &, decltype(eloV_) &>
120 return std::tuple<decltype(idx_)&, decltype(names_)&, decltype(eloV_)&>(
121 idx_, names_, eloV_);
141 size_t maxMatches)
const {
144 std::vector<idNumberT> res;
145 size_t len = strlen(str);
146 for (
auto it = idx_[nt].lower_bound(str);
147 it != idx_[nt].end() && res.size() < maxMatches; ++it) {
148 const char* s = it->first;
149 if (strlen(s) < len || !std::equal(str, str + len, s))
151 res.emplace_back(it->second);
164 return names_[nt][id].get();
171 const decltype(idx_) &
getNames()
const {
return idx_; }
180 return static_cast<idNumberT>(names_[nt].size());
193 auto it = idx_[nt].find(str);
194 if (it != idx_[nt].end()) {
195 *idPtr = (*it).second;
207 std::vector<uint32_t> res(names_[nt].
size());
208 std::transform(names_[nt].begin(), names_[nt].end(), res.begin(),
209 [](
const std::unique_ptr<const char[]>&
name) {
252 #endif // SCID_NAMEBASE_H void Clear()
Frees memory, leaving the object empty.
std::pair< errorT, idNumberT > addName(nameT nt, const char *name, size_t MAX_LEN, idNumberT MAX_ID)
Add a name (string) to the NameBase.
uint32_t strStartHash(const char *str)
errorT FindExactName(nameT nt, const char *str, idNumberT *idPtr) const
Finds an exact full, case-sensitive name.
void AddElo(idNumberT id, eloT elo)
A NameBase stores the max ELO for each player.
const errorT ERROR_NameTooLong
const errorT ERROR_NameNotFound
std::vector< uint32_t > generateHashMap(nameT nt) const
For every name generates a 32bit hash with the first 4 chars.
const char * GetName(nameT nt, idNumberT id) const
Retrieve a name.
This class stores the database's names (players, events, sites and rounds).
static bool IsValidNameType(nameT nt)
Validate a nameT type.
std::vector< idNumberT > getFirstMatches(nameT nt, const char *str, size_t maxMatches) const
Get the first few matches of a name prefix.
std::tuple< decltype(idx_) &, decltype(names_) &, decltype(eloV_) & > getData()
eloT GetElo(idNumberT id) const
idNumberT GetNumNames(nameT nt) const
decltype(idx_) const & getNames() const
const errorT ERROR_NameLimit
int strCompare(const char *s1, const char *s2)
static nameT NameTypeFromString(const char *str)
Match a string to a nameT.
bool strIsAlphaPrefix(const char *prefix, const char *longStr)