libkombilo  0.8
/home/ug/devel/kombilo-py-dev/kombilo/libkombilo/pattern.h
Go to the documentation of this file.
1 
26 #ifndef _PATTERN_H_
27 #define _PATTERN_H_
28 
29 #include <vector>
30 #include <stack>
31 #include <fstream>
32 #include "sqlite3.h"
33 #include "pstdint.h"
34 #include <boost/unordered_map.hpp>
35 
36 #include "abstractboard.h"
37 #include "sgfparser.h"
38 
39 
40 typedef char* char_p;
41 typedef int64_t hashtype;
42 #if (defined(__BORLANDC__) || defined(_MSC_VER))
43 const hashtype NOT_HASHABLE = 9223372036854775807i64;
44 #else
45 const hashtype NOT_HASHABLE = 9223372036854775807LL; // == (1 << 63) -1; in fact, (1<<64)-1 shoul be OK, too.
46 #endif
47 
48 const char NO_CONT = 255;
49 
50 const int CORNER_NW_PATTERN = 0;
51 const int CORNER_NE_PATTERN = 1;
52 const int CORNER_SW_PATTERN = 2;
53 const int CORNER_SE_PATTERN = 3;
54 const int SIDE_N_PATTERN = 4;
55 const int SIDE_W_PATTERN = 5;
56 const int SIDE_E_PATTERN = 6;
57 const int SIDE_S_PATTERN = 7;
58 const int CENTER_PATTERN = 8;
59 const int FULLBOARD_PATTERN = 9;
60 
61 const int ALGO_FINALPOS = 1;
62 const int ALGO_MOVELIST = 2;
63 const int ALGO_HASH_FULL = 4;
64 const int ALGO_HASH_CORNER = 8;
65 // const int ALGO_INTERVALS = 16;
66 const int ALGO_HASH_CENTER = 32;
67 const int ALGO_HASH_SIDE = 64;
68 
69 const int algo_finalpos = 1;
70 const int algo_movelist = 2;
71 const int algo_hash_full = 3;
72 const int algo_hash_corner = 4;
73 const int algo_intervals = 5;
74 const int algo_hash_center = 6;
75 const int algo_hash_side = 7;
76 
78 
79 const int DATE_PROFILE_START = 1600;
80 const int DATE_PROFILE_END = 2020;
83 char* flipped_sig(int f, char* sig, int boardsize);
84 char* symmetrize(char* sig, int boardsize);
85 
86 class SnapshotVector : public std::vector<unsigned char> {
87  public:
89  SnapshotVector(char* c, int size);
90 
91  void pb_int(int d);
92  void pb_hashtype(hashtype d);
93  void pb_int64(int64_t d);
94  void pb_charp(const char* c, int size);
95  void pb_char(char c);
96  void pb_string(std::string s);
97  void pb_intp(int* p, int size);
98 
99  int retrieve_int();
100  hashtype retrieve_hashtype();
101  int64_t retrieve_int64();
102  int* retrieve_intp();
103  char retrieve_char();
104  char* retrieve_charp();
105  std::string retrieve_string();
106 
107  char* to_charp();
108 
109  private:
110  SnapshotVector::iterator current;
111 };
112 
113 
115  public:
116  PatternError();
117 };
118 
119 class DBError {
120  public:
121  DBError();
122 };
123 
124 class Symmetries {
125  public:
126  char* dataX;
127  char* dataY;
128  char* dataCS;
129  char sizeX;
130  char sizeY;
131  Symmetries(char sX=0, char sY=0);
132  ~Symmetries();
133  Symmetries(const Symmetries& s);
134  Symmetries& operator=(const Symmetries& s);
135  void set(char i, char j, char k, char l, char cs) throw(PatternError);
136  char getX(char i, char j) throw(PatternError);
137  char getY(char i, char j) throw(PatternError);
138  char getCS(char i, char j) throw(PatternError);
139  char has_key(char i, char j) throw(PatternError);
140 };
141 
189 class Pattern {
190  public:
191  int left; // left, right, top, bottom "==" anchors
192  int right;
193  int bottom;
194  int top;
195  int boardsize;
196 
197  int sizeX;
198  int sizeY;
199 
200  int flip; // used for elements of a patternList
201  int colorSwitch; // dito
202  char* initialPos;
203  char* finalPos;
204  char* contLabels;
205  std::vector<MoveNC> contList;
206 
207  // Pattern constructors
208  //
209  // the char* contLabels, if != 0, should have the same size as the pattern, and should
210  // contain pre-fixed label (which should be re-used when presenting the search results)
211  // Positions without a given label should contain '.'
212  //
213  // Note: the char*'s iPos and CONTLABELS will NOT be free'ed by the Pattern class.
214 
215  Pattern();
216  Pattern(int le, int ri, int to, int bo, int BOARDSIZE, int sX, int sY, const char* iPos, const std::vector<MoveNC>& CONTLIST, const char* CONTLABELS = 0) throw(PatternError);
217  Pattern(int le, int ri, int to, int bo, int BOARDSIZE, int sX, int sY, const char* iPos) throw(PatternError);
218  Pattern(int type, int BOARDSIZE, int sX, int sY, const char* iPos, const std::vector<MoveNC>& CONTLIST, const char* CONTLABELS = 0);
219  Pattern(int type, int BOARDSIZE, int sX, int sY, const char* iPos, const char* CONTLABELS = 0);
220  Pattern(const Pattern& p);
221  Pattern(SnapshotVector& snv);
222  ~Pattern();
223  Pattern& operator=(const Pattern& p);
224  Pattern& copy(const Pattern& p);
225 
226  char getInitial(int i, int j);
227  char getFinal(int i, int j);
228 
229  char BW2XO(char c);
230  int operator==(const Pattern& p);
231  std::string printPattern();
232  void to_snv(SnapshotVector& snv);
233 
234  static int flipsX(int i, int x, int y, int XX, int YY);
235  static int flipsY(int i, int x, int y, int XX, int YY);
236  static int PatternInvFlip(int i);
237  static int compose_flips(int i, int j); // returns index of flip "first j, then i"
238 };
239 
240 class GameList;
241 
243  public:
244  int x;
245  int y;
246  int B ;
247  int W ;
248  int tB;
249  int tW;
250  int wB;
251  int lB;
252  int wW;
253  int lW;
254  vector<int> dates_B;
255  vector<int> dates_W;
256 
257  char label;
258 
259  GameList* gamelist;
260 
261  Continuation(GameList* gl);
262  Continuation(const Continuation& c);
263  Continuation& operator=(const Continuation& c);
264  void add(const Continuation c);
265  int earliest();
266  int earliest_B();
267  int earliest_W();
268  int latest();
269  int latest_B();
270  int latest_W();
271  float average_date();
272  float average_date_B();
273  float average_date_W();
274  int became_popular();
275  int became_popular_B();
276  int became_popular_W();
277  int became_unpopular();
278  int became_unpopular_B();
279  int became_unpopular_W();
280  // TODO weighted average
281 
284  int total();
285 
286  friend class GameList;
287 
288  private:
289  void from_snv(SnapshotVector& snv);
290  void to_snv(SnapshotVector& snv);
291 };
292 
293 class PatternList {
294  public:
295  Pattern pattern;
297  int nextMove;
298  std::vector<Pattern> data;
299  std::vector<Symmetries> symmetries;
300  vector<Continuation* > continuations;
301  int* flipTable;
302  int special;
303 
304  PatternList(Pattern& p, int fColor, int nMove, GameList* gl) throw (PatternError);
305  ~PatternList();
306 
307  void patternList();
308  Pattern get(int i);
309  int size();
310 
311  friend class GameList;
312  friend class Algo_movelist;
313  friend class Algo_hash_full;
314 
315  private:
316  char* updateContinuations(int orientation, int x, int y, char co, bool tenuki, char winner, int date);
317  char* sortContinuations(); // and give them names to be used as labels
318  char invertColor(char co);
319 };
320 
321 class Candidate {
322  public:
323  char x;
324  char y;
325  char orientation; // == index in corresp patternList
326 
327  Candidate(char X, char Y, char ORIENTATION);
328 };
329 
330 class Hit {
331  public:
332  ExtendedMoveNumber* pos;
333  char* label; // this does not really contain the label, but rather the position of the continuation move
334  Hit(ExtendedMoveNumber* POS, char* LABEL);
335  Hit(SnapshotVector& snv); // takes a SnapshotVector and reads information produced by Hit::to_snv()
336  ~Hit();
337  static bool cmp_pts(Hit* a, Hit* b);
338  void to_snv(SnapshotVector& snv);
339 };
340 
341 
342 
343 
344 #endif
345 
Definition: pattern.h:86
Definition: pattern.h:114
Definition: pattern.h:119
int W
number of all white continuations
Definition: pattern.h:247
Definition: algos.h:299
Definition: pattern.h:189
Definition: pattern.h:242
int tB
number of black tenuki plays
Definition: pattern.h:248
Definition: pattern.h:293
int lB
black losses (where next play is B)
Definition: pattern.h:251
Definition: sgfparser.h:42
The GameList class is the main interface to the libkombilo functionality.
Definition: search.h:187
Definition: pattern.h:330
int lW
black losses (where next play is W)
Definition: pattern.h:253
int wW
black wins (where next play is W)
Definition: pattern.h:252
Definition: pattern.h:321
int B
number of all black continuations
Definition: pattern.h:246
int y
y coordinate of corresp. label on board
Definition: pattern.h:245
int special
== -1, unless there exists a symmetry which yields the color-switched pattern
Definition: pattern.h:302
int tW
number of white tenuki plays
Definition: pattern.h:249
Definition: pattern.h:124
int x
x coordinate of corresp. label on board
Definition: pattern.h:244
Hashing for full board patterns.
Definition: algos.h:369
int wB
black wins (where next play is B)
Definition: pattern.h:250
int nextMove
1: next must be black, 2: next must be white, 0: no restriction
Definition: pattern.h:297
int fixedColor
search for pattern with exchanged colors as well?
Definition: pattern.h:296