Scid  4.6.5
pbook.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////
2 //
3 // FILE: pbook.h
4 // PBook (Position Book) class
5 //
6 // Part of: Scid (Shane's Chess Information Database)
7 // Version: 2.3
8 //
9 // Notice: Copyright (c) 1999-2000 Shane Hudson. All rights reserved.
10 //
11 // Author: Shane Hudson (sgh@users.sourceforge.net)
12 //
13 //////////////////////////////////////////////////////////////////////
14 
15 #ifndef SCID_PBOOK_H
16 #define SCID_PBOOK_H
17 
18 #include "common.h"
19 #include "position.h"
20 #include "strtree.h"
21 #include <stdio.h>
22 
23 class DString;
24 
25 #define PBOOK_SUFFIX ".epd"
26 #define PBOOK_MAX_MATERIAL 32
27 
28 struct bookDataT {
30  char * comment;
31  squareT enpassant; // Original en passant square.
32 };
33 
35 
37 
38 class PBook
39 {
40  private:
41 
42  // We store 33 Trees, one for each possible total material count.
43  // This way, the average number of position comparisons is greatly
44  // reduced.
46 
47  bool Altered; // True if book is altered since loading or saving.
48  bool ReadOnly;
49  char * FileName;
50  uint LineCount;
51  bookNodeT ** NodeList;
52  uint NodeListCapacity;
53  uint NodeListCount;
54  uint NextIndex; // For jumping to next pbook position.
55  uint SkipCount; // Number of searches saved by LeastMaterial
56  // comparison.
57  uint LeastMaterial; // The smallest amount of material in any
58  // position in the book. In the range 0..32.
59  // It is a lower bound, and may be lower than
60  // the actual value if nodes are deleted.
61 
62  byte * HashFlags;
63 
64  uint Stats_PositionBytes; // Bytes in .epd file used for positions.
65  uint Stats_CommentBytes; // Bytes in .epd file used for comments.
66 
67  uint Stats_Lookups [PBOOK_MAX_MATERIAL + 1];
68  uint Stats_TotalLookups;
69  uint Stats_Inserts [PBOOK_MAX_MATERIAL + 1];
70  uint Stats_TotalInserts;
71 
72  void SetHashFlag (Position * pos);
73  bool GetHashFlag (Position * pos);
74 
75  void AddNodeToList (bookNodeT * node);
76 
77  public:
78  void Init();
79 
80  PBook() { Init(); }
81  ~PBook();
82 
83  const char * GetFileName () { return (FileName == NULL ? "" : FileName); }
84  void SetFileName (const char * filename);
85  bool IsAltered() { return Altered; }
86  bool IsReadOnly() { return ReadOnly; }
87 
88  uint GetLineNumber (void) { return LineCount; }
89 
90  uint Size () {
91  uint total = 0;
92  for (uint i=0; i <= PBOOK_MAX_MATERIAL; i++) {
93  total += Tree[i]->Size();
94  }
95  return total;
96  }
97  uint FewestPieces () { return LeastMaterial; }
98  uint NumSkippedSearches() { return SkipCount; }
99 
100  errorT ReadFile();
101  errorT WriteFile();
102  errorT ReadEcoFile ();
103 
104  errorT Find (Position * pos, const char ** ptrComment);
105  errorT FindNext (Position * pos, bool forwards);
106  errorT Insert (Position * pos, const char * comment);
107  errorT Delete (Position * pos);
108  errorT FindOpcode (Position * pos, const char * opcode, DString * target);
109  errorT FindSummary (Position * pos, DString * target);
110  uint StripOpcode (const char * opcode);
111  void EcoSummary (const char * ecoPrefix, DString * dstr);
112  void DumpStats (FILE * fp);
113  uint NumPositionBytes () { return Stats_PositionBytes; }
114  uint NumCommentBytes () { return Stats_CommentBytes; }
115 
116 };
117 
118 #endif // SCID_PBOOK_H
119 
120 //////////////////////////////////////////////////////////////////////
121 // EOF: pbook.h
122 //////////////////////////////////////////////////////////////////////
123 
unsigned char byte
Definition: common.h:97
#define PBOOK_MAX_MATERIAL
Definition: pbook.h:26
uint id
Definition: pbook.h:29
uint GetLineNumber(void)
Definition: pbook.h:88
uint FewestPieces()
Definition: pbook.h:97
uint Size()
Definition: pbook.h:90
uint NumSkippedSearches()
Definition: pbook.h:98
PBook()
Definition: pbook.h:80
squareT enpassant
Definition: pbook.h:31
uint32_t uint
Definition: common.h:99
char * comment
Definition: pbook.h:30
bool IsAltered()
Definition: pbook.h:85
unsigned short errorT
Definition: error.h:20
const char * GetFileName()
Definition: pbook.h:83
bool IsReadOnly()
Definition: pbook.h:86
bookNodeT * bookNodePtrT
Definition: pbook.h:36
uint NumPositionBytes()
Definition: pbook.h:113
nodeT< bookDataT > bookNodeT
Definition: pbook.h:34
uint NumCommentBytes()
Definition: pbook.h:114
Definition: pbook.h:38
byte squareT
Definition: common.h:113
uint Size()
Definition: strtree.h:146