Scid  4.7.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
pbook.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1999-2000 Shane Hudson
3  * Copyright (C) 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 
21 #ifndef SCID_PBOOK_H
22 #define SCID_PBOOK_H
23 
24 #include "common.h"
25 #include "misc.h"
26 #include <algorithm>
27 #include <memory>
28 #include <string>
29 #include <unordered_map>
30 #include <vector>
31 class Position;
32 
33 /**
34  * A PBook is a collection of chess positions, each with the corresponding ECO
35  * code, a mnemonic name, and the list of moves to reach the position.
36  */
37 class PBook {
38  struct bookDataT {
39  std::unique_ptr<char[]> compactStr;
40  std::unique_ptr<char[]> comment;
41 
42  bookDataT(char* compact, char* comm)
43  : compactStr(compact), comment(comm) {}
44  };
45  std::unordered_multimap<unsigned, bookDataT> pos_;
46  std::vector<const char*> comments_;
47  unsigned LineCount = 0;
48  unsigned LeastMaterial = 32; // The smallest amount of material in any
49  // position in the book. In the range 0..32.
50 public:
51  /**
52  * Read a file with a list of ECO codes and creates a PBook object.
53  * The file is composed of lines like this:
54  * C50a "Italian Game" 1.e4 e5 2.Nf3 Nc6 3.Bc4 *
55  * @param FileName: the name of the file to be read.
56  * @returns
57  * - on success, a @e std::pair containing OK and the pointer to the newly
58  * created object.
59  * - on failure, a @e std::pair containing an error code and nullptr.
60  */
61  static std::pair<errorT, std::unique_ptr<PBook> >
62  ReadEcoFile(const char* FileName);
63 
64  /**
65  * Retrieve an ECO string containing the ECO code and the mnemonic name.
66  * @param pos: the position to search for.
67  * @returns
68  * - if the position is found, a @e std::pair with the range iterators for
69  * the string (string_view).
70  * - a @e std::pair containing nullptr otherwise.
71  */
72  std::pair<const char*, const char*> findECOstr(Position* pos) const;
73 
74  /**
75  * Retrieve the ECO code of a position.
76  * @param pos: the position to search for.
77  * @returns the corresponding ECO code or ECO_None if not found.
78  */
79  ecoT findECO(Position* pos) const {
80  auto it = findECOstr(pos);
81  if (!it.first)
82  return ECO_None;
83 
84  char buf[7] = {0};
85  std::copy_n(it.first,
86  std::min(ptrdiff_t(6), std::distance(it.first, it.second)),
87  buf);
88  return eco_FromString(buf);
89  }
90 
91  std::string EcoSummary(const char* ecoPrefix) const;
92 
93  unsigned GetLineNumber() const { return LineCount; }
94  unsigned FewestPieces() const { return LeastMaterial; }
95  size_t Size() const { return pos_.size(); }
96 };
97 
98 #endif // SCID_PBOOK_H
static std::pair< errorT, std::unique_ptr< PBook > > ReadEcoFile(const char *FileName)
Read a file with a list of ECO codes and creates a PBook object.
Definition: pbook.cpp:135
const ecoT ECO_None
Definition: common.h:168
ecoT findECO(Position *pos) const
Retrieve the ECO code of a position.
Definition: pbook.h:79
unsigned FewestPieces() const
Definition: pbook.h:94
std::string EcoSummary(const char *ecoPrefix) const
Definition: pbook.cpp:107
ushort ecoT
Definition: common.h:165
ecoT eco_FromString(const char *ecoStr)
Definition: misc.cpp:36
unsigned GetLineNumber() const
Definition: pbook.h:93
std::pair< const char *, const char * > findECOstr(Position *pos) const
Retrieve an ECO string containing the ECO code and the mnemonic name.
Definition: pbook.cpp:78
size_t Size() const
Definition: pbook.h:95
A PBook is a collection of chess positions, each with the corresponding ECO code, a mnemonic name...
Definition: pbook.h:37