libscid 0.1.0
Chess applications made easy.
Loading...
Searching...
No Matches
book.h
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_ECO_BOOK_H
22#define SCID_ECO_BOOK_H
23
24#include "scid/core/error.h"
25#include "scid/eco/code.h"
26#include <filesystem>
27#include <memory>
28#include <string>
29#include <string_view>
30#include <unordered_map>
31#include <vector>
32
33namespace scid::core {
34class Position;
35}
36
37namespace scid::eco {
38
39using Error = scid::core::errorT;
40using Position = scid::core::Position;
41
42inline constexpr Error OK = scid::core::OK;
43inline constexpr Error ERROR_FileOpen = scid::core::ERROR_FileOpen;
44inline constexpr Error ERROR_Corrupt = scid::core::ERROR_Corrupt;
45
50class Book {
51public:
52 struct Line {
53 std::string_view code;
54 std::string_view name;
55 std::string_view moves;
56 };
57
58private:
59 struct BookData {
60 std::unique_ptr<char[]> compactStr;
61 std::unique_ptr<char[]> comment;
62
63 BookData(char* compact, char* comm)
64 : compactStr(compact), comment(comm) {}
65 };
66
67 std::unordered_multimap<unsigned, BookData> pos_;
68 std::vector<const char*> comments_;
69 unsigned lineCount_ = 0;
70 unsigned leastMaterial_ = 32; // The smallest amount of material in any
71 // position in the book. In the range 0..32.
72
73public:
83 static std::pair<Error, Book> load(const std::filesystem::path& path);
84
90 std::string_view findEcoString(const Position& position) const;
91
97 Code findEco(const Position& position) const;
98
99 std::vector<Line> linesWithPrefix(std::string_view ecoPrefix) const;
100
101 unsigned lineCount() const { return lineCount_; }
102 unsigned fewestPieces() const { return leastMaterial_; }
103 size_t size() const { return pos_.size(); }
104};
105
106} // namespace scid::eco
107
108#endif // SCID_ECO_BOOK_H
Definition position.h:44
A Book is a collection of chess positions, each with the corresponding ECO code, a mnemonic name,...
Definition book.h:50
Code findEco(const Position &position) const
Retrieve the ECO code of a position.
static std::pair< Error, Book > load(const std::filesystem::path &path)
Read a file with a list of ECO codes and creates a Book object.
std::string_view findEcoString(const Position &position) const
Retrieve an ECO string containing the ECO code and the mnemonic name.
Shared status and error codes.
Definition book.h:52