Cute Chess  0.1
openingbook.h
1 /*
2  This file is part of Cute Chess.
3  Copyright (C) 2008-2018 Cute Chess authors
4 
5  Cute Chess is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  Cute Chess is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with Cute Chess. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef OPENING_BOOK_H
20 #define OPENING_BOOK_H
21 
22 #include <QtGlobal>
23 #include <QMultiMap>
24 #include "board/genericmove.h"
25 
26 class QString;
27 class QDataStream;
28 class PgnGame;
29 class PgnStream;
30 
31 
43 class LIB_EXPORT OpeningBook
44 {
45  public:
48  {
49  Ram,
50  Disk
51  };
52 
60  struct Entry
61  {
69  quint16 weight;
70  };
71 
73  OpeningBook(AccessMode mode = Ram);
75  virtual ~OpeningBook();
76 
86  int import(const PgnGame& pgn, int maxMoves);
96  int import(PgnStream& in, int maxMoves);
97 
109  Chess::GenericMove move(quint64 key) const;
110 
112  QList<Entry> entries(quint64 key) const;
113 
118  bool read(const QString& filename);
119 
124  bool write(const QString& filename) const;
125 
126 
127  protected:
128  friend LIB_EXPORT QDataStream& operator>>(QDataStream& in, OpeningBook* book);
129  friend LIB_EXPORT QDataStream& operator<<(QDataStream& out, const OpeningBook* book);
130 
133 
135  virtual int entrySize() const = 0;
136 
138  void addEntry(const Entry& entry, quint64 key);
139 
146  virtual Entry readEntry(QDataStream& in, quint64* key) const = 0;
147 
149  virtual void writeEntry(const Map::const_iterator& it,
150  QDataStream& out) const = 0;
151 
152  private:
153  QList<Entry> entriesFromDisk(quint64 key) const;
154 
155  AccessMode m_mode;
156  QString m_filename;
157  Map m_map;
158 };
159 
166 extern LIB_EXPORT QDataStream& operator>>(QDataStream& in, OpeningBook* book);
167 
175 extern LIB_EXPORT QDataStream& operator<<(QDataStream& out, const OpeningBook* book);
176 
177 #endif // OPENING_BOOK_H
A class for reading games in PGN format from a text stream.
Definition: pgnstream.h:42
Load the entire book to RAM.
Definition: openingbook.h:49
QMultiMap< quint64, Entry > Map
Definition: openingbook.h:132
A game of chess in PGN format.
Definition: pgngame.h:51
quint16 weight
Definition: openingbook.h:69
A chess move independent of chess variant or opening book format.
Definition: genericmove.h:34
An entry in the opening book.
Definition: openingbook.h:60
AccessMode
Definition: openingbook.h:47
Chess::GenericMove move
Definition: openingbook.h:63
A collection of opening moves for chess.
Definition: openingbook.h:43