Scid  4.6.5
namebase.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2001 Shane Hudson.
3 * Copyright (C) 2014-2017 Fulvio Benini
4 
5 * This file is part of Scid (Shane's Chess Information Database).
6 *
7 * Scid is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation.
10 *
11 * Scid is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Scid. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #ifndef SCID_NAMEBASE_H
21 #define SCID_NAMEBASE_H
22 
23 #include "common.h"
24 #include "misc.h"
25 #include <vector>
26 #include <map>
27 #include <algorithm>
28 
29 class Index;
30 
31 // There are four NameBases, one each for PLAYER, EVENT , SITE and ROUND tags.
32 typedef uint nameT;
33 enum {
37 };
38 
39 typedef uint idNumberT;
40 
41 const char NAME_TYPE_STRING [NUM_NAME_TYPES][8] = {
42  "player",
43  "event",
44  "site",
45  "round"
46 };
47 
48 
49 class NameBase
50 {
51  static const char* NAMEBASE_MAGIC;
52  static const char* NAMEBASE_SUFFIX;
53 
54  std::string filename_;
55  std::vector<const char*> names_[NUM_NAME_TYPES];
56  std::vector<eloT> eloV_;
57  struct idxCmp {
58  bool operator() (const char* str1, const char* str2) const {
59  // *** Compatibility ***
60  // Older code used a custom StrTree class with a peculiar sorting:
61  // - the first char was interpreted as an unsigned char;
62  // - the remaining part was compared with the function strComapare(),
63  // which converts the chars to ints, and is not consistent with
64  // the standard function strcmp().
65  // The old StrTree class did also have unpredictable behaviors when
66  // fed with names not sorted according to that criteria, for example
67  // it could create Namebase objects with duplicate entries.
68  // ***
69  if (*str1 == *str2)
70  return strCompare(str1, str2) < 0;
71 
72  return static_cast<uint>(*str1) < static_cast<uint>(*str2);
73  }
74  };
75  std::map<const char*, idNumberT, idxCmp> idx_[NUM_NAME_TYPES];
76  bool modified_;
77 
78 public:
79  static bool IsValidNameType (nameT nt) { return (nt < NUM_NAME_TYPES); }
80  static nameT NameTypeFromString (const char * str);
81  static const char* Suffix() { return NAMEBASE_SUFFIX; }
82 
83  NameBase() : modified_(false) {}
84  ~NameBase() { Clear(); }
85  void Clear();
86 
87  errorT Create (const char* filename);
88  errorT ReadEntireFile (const char* filename);
89  errorT flush(const Index* idx) {
90  errorT err = OK;
91  if (modified_ && !filename_.empty()) err = WriteNameFile(idx);
92  if (err == OK) modified_ = false;
93  return err;
94  }
95  void hackedNameFreq() { modified_ = true; }
96 
97  const char* GetName (nameT nt, idNumberT id) const { return names_[nt][id]; }
98  eloT GetElo (idNumberT id) const { return eloV_[id]; }
99 
100  errorT AddName (nameT nt, const char * str, idNumberT * idPtr);
101  void AddElo (idNumberT id, eloT elo) { if (elo > eloV_[id]) eloV_[id] = elo; }
102 
103  errorT FindExactName (nameT nt, const char * str, idNumberT * idPtr) const;
104  uint GetFirstMatches (nameT nt, const char * str, uint maxMatches,
105  idNumberT * array) const;
106 
107  idNumberT GetNumNames (nameT n) const { return names_[n].size(); }
108 
109  std::vector<uint32_t> generateHashMap(nameT nt) const {
110  std::vector<uint32_t> res(names_[nt].size());
111  std::transform(names_[nt].begin(), names_[nt].end(), res.begin(),
112  strStartHash);
113  return res;
114  }
115 
116 private:
117  typedef std::map<const char*, idNumberT, idxCmp>::const_iterator iterator;
118 
119  NameBase(const NameBase&);
120  NameBase& operator=(const NameBase&);
121  bool setFileName(const char* filename);
122  errorT WriteNameFile(const Index* idx);
123 };
124 
125 #endif // #ifdef SCID_NAMEBASE_H
126 
127 //////////////////////////////////////////////////////////////////////
128 // EOF: namebase.h
129 //////////////////////////////////////////////////////////////////////
130 
errorT flush(const Index *idx)
Definition: namebase.h:89
NameBase()
Definition: namebase.h:83
void Clear()
NameBase::clear() - clears file associations and frees memory.
Definition: namebase.cpp:38
uint idNumberT
Definition: namebase.h:39
uint32_t strStartHash(const char *str)
Definition: misc.h:200
const errorT OK
Definition: error.h:23
errorT FindExactName(nameT nt, const char *str, idNumberT *idPtr) const
Definition: namebase.cpp:328
static nameT NameTypeFromString(const char *str)
Definition: namebase.cpp:370
void AddElo(idNumberT id, eloT elo)
Definition: namebase.h:101
void hackedNameFreq()
Definition: namebase.h:95
errorT ReadEntireFile(const char *filename)
NameBase::ReadNameFile() - Reads a NameBase file into memory.
Definition: namebase.cpp:110
errorT AddName(nameT nt, const char *str, idNumberT *idPtr)
NameBase::AddName() - Returns the idNumberT corresponding to : a valid name type : the name to looku...
Definition: namebase.cpp:291
uint nameT
Definition: namebase.h:29
sizew
Definition: board.tcl:619
static bool IsValidNameType(nameT nt)
Definition: namebase.h:79
static const char * Suffix()
Definition: namebase.h:81
uint32_t uint
Definition: common.h:99
uint GetFirstMatches(nameT nt, const char *str, uint maxMatches, idNumberT *array) const
Definition: namebase.cpp:347
const char * GetName(nameT nt, idNumberT id) const
Definition: namebase.h:97
errorT Create(const char *filename)
NameBase::Create() - Create an empty NameBase file : the filename (without extension) ...
Definition: namebase.cpp:83
Definition: index.h:61
ushort eloT
Definition: common.h:160
const char NAME_TYPE_STRING[NUM_NAME_TYPES][8]
Definition: namebase.h:41
unsigned short errorT
Definition: error.h:20
idNumberT GetNumNames(nameT n) const
Definition: namebase.h:107
~NameBase()
Definition: namebase.h:84
std::vector< uint32_t > generateHashMap(nameT nt) const
Definition: namebase.h:109
eloT GetElo(idNumberT id) const
Definition: namebase.h:98
int strCompare(const char *s1, const char *s2)
Definition: misc.h:361