Scid  4.6.5
searchpos.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2013-2014 Fulvio Benini
3 
4 * This file is part of Scid (Shane's Chess Information Database).
5 *
6 * Scid is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation.
9 *
10 * Scid 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 Scid. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #ifndef SEARCHPOS_H
20 #define SEARCHPOS_H
21 
22 #include "common.h"
23 #include "position.h"
24 #include "stored.h"
25 #include "fastgame.h"
26 
27 
28 class SearchPos {
29 public:
31  for (int i=0; i<8; ++i) {
32  nPieces_[WHITE][i] = 0;
33  nPieces_[BLACK][i] = 0;
34  }
35  const pieceT* board = pos->GetBoard();
36  squareT wkSq = 0;
37  squareT bkSq = 0;
38  for (squareT i = 0; i < 64; ++i) {
39  board_[i] = board[i];
40  if (board[i] != EMPTY) {
41  ++nPieces_[piece_Color(board[i])][0];
42  ++nPieces_[piece_Color(board[i])][piece_Type(board[i])];
43  if (board[i] == WK) wkSq = i;
44  else if (board[i] == BK) bkSq = i;
45  }
46  }
47  toMove_ = pos->GetToMove();
48  isStdStard_ = pos->IsStdStart();
49 
50  if (wkSq != E1 && wkSq != G1 && bkSq != E8 && bkSq != G8) {
51  unusualKingPos_ = true;
52  storedLine_ = 0;
53  } else {
54  unusualKingPos_ = false;
55  storedLine_ = new StoredLine(board, toMove_);
56  }
57 
58  //Home Pawn Signature
59  uint16_t hps = ~static_cast<uint16_t>(pos->GetHPSig());
60  hpSig_ = hps;
61  for (ply_count_ = 0; hps != 0; ply_count_++) hps &= hps -1; //popcnt
62 
63  //MatSig
64  msig_ = matsig_Make (pos->GetMaterial());
65  }
66 
68  if (storedLine_) delete storedLine_;
69  }
70 
71  bool setFilter(scidBaseT* base, HFilter& filter, const Progress& progress) {
72  if (! isStdStard_) {
73  int i=0;
74  if (unusualKingPos_) i += 1;
75  if (toMove_ == BLACK) i += 2;
76  switch (i) {
77  case 0: return SetFilter<WHITE, true> (base, filter, progress);
78  case 1: return SetFilter<WHITE, false>(base, filter, progress);
79  case 2: return SetFilter<BLACK, true> (base, filter, progress);
80  case 3: return SetFilter<BLACK, false>(base, filter, progress);
81  }
82  }
83  for (uint i = 0, n = base->numGames(); i < n; i++) {
84  const IndexEntry* ie = base->getIndexEntry (i);
85  if (! ie->GetStartFlag()) filter.set (i, 1);
86  else {
87  FastGame game = base->getGame(ie);
88  int ply = game.search<WHITE>(board_, nPieces_);
89  filter.set (i, (ply > 255) ? 255 : ply);
90  }
91  }
92  return true;
93  }
94 
95 private:
96  uint8_t nPieces_[2][8];
97  byte board_[64];
98  StoredLine* storedLine_;
99  uint hpSig_;
100  uint ply_count_;
101  bool isStdStard_;
102  bool unusualKingPos_;
103  matSigT msig_;
104  colorT toMove_;
105 
106  SearchPos(const SearchPos&);
107  SearchPos& operator=(const SearchPos&);
108  template <colorT TOMOVE, bool STOREDLINE>
109  bool SetFilter (scidBaseT* base, HFilter& filter, const Progress& prg) {
110  filter->clear();
111  long progress = 0;
112  for (uint i = 0, n = base->numGames(); i < n; i++) {
113  const IndexEntry* ie = base->getIndexEntry(i);
114  if (! ie->GetStartFlag()) {
115  if (STOREDLINE) {
116  int ply = storedLine_->match(ie->GetStoredLineCode());
117  if (ply >= 0) {
118  filter.set (i, static_cast<byte> (ply +1));
119  continue;
120  }
121  if (ply < -1) continue;
122  }
123  if (! HPSigCanMatch(ie->GetHomePawnData())) continue;
124  }
125  if (!matsig_isReachable (msig_, ie->GetFinalMatSig(), ie->GetPromotionsFlag(), ie->GetUnderPromoFlag())) continue;
126 
127  int ply = base->getGame(ie).search<TOMOVE>(board_, nPieces_);
128  if (ply != 0) filter.set(i, (ply > 255) ? 255 : ply);
129 
130  if ((progress++ % 200) == 0) {
131  if (!prg.report(i, n)) return false;
132  }
133  }
134  return true;
135  }
136 
137  bool HPSigCanMatch(const byte* v) {
138  if (ply_count_ == 16) return *v == ply_count_;
139 // if (ply_count_ == 0 || *v == 0) return true; //*v == 0 if !ie->GetStartFlag()
140  if (*v++ < ply_count_) return false;
141  uint res = 0;
142  for (uint i = 0; i < ply_count_/2; ++i) {
143  res |= 1 << (*v >> 4);
144  res |= 1 << (*v++ & 0x0F);
145  }
146  if (ply_count_ & 1) res |= 1 << (*v >> 4);
147  return res == hpSig_;
148  }
149 };
150 
151 
152 #endif
const pieceT WK
Definition: common.h:236
unsigned char byte
Definition: common.h:97
void clear()
Definition: hfilter.h:132
bool report(size_t done, size_t total) const
Definition: misc.h:136
const colorT WHITE
Definition: common.h:203
pieceT piece_Type(pieceT p)
Definition: common.h:287
const pieceT BK
Definition: common.h:237
uint32_t matSigT
Definition: matsig.h:25
gamenumT numGames() const
Definition: scidbase.h:97
const IndexEntry * getIndexEntry(gamenumT g) const
Definition: scidbase.h:101
Definition: misc.h:124
const colorT BLACK
Definition: common.h:204
byte * GetMaterial()
Definition: position.h:151
bool GetUnderPromoFlag() const
Definition: indexentry.h:314
matSigT GetFinalMatSig() const
Definition: indexentry.h:329
bool IsStdStart()
Definition: position.cpp:652
const pieceT EMPTY
Definition: common.h:234
const squareT G1
Definition: common.h:343
uint32_t uint
Definition: common.h:99
Definition: board.tcl:276
bool GetStartFlag() const
Definition: indexentry.h:312
const squareT G8
Definition: common.h:350
const byte * GetHomePawnData() const
Definition: indexentry.h:331
bool GetPromotionsFlag() const
Definition: indexentry.h:313
SearchPos(Position *pos)
Definition: searchpos.h:30
colorT piece_Color(pieceT p)
Definition: common.h:280
const pieceT * GetBoard() const
Definition: position.h:189
const squareT E8
Definition: common.h:350
~SearchPos()
Definition: searchpos.h:67
matSigT matsig_Make(byte *materialCounts)
Definition: matsig.h:226
byte colorT
Definition: common.h:112
int search(const byte *board, const uint8_t(&nPieces)[2][8])
Definition: fastgame.h:316
colorT GetToMove()
Definition: position.h:155
const squareT E1
Definition: common.h:343
bool setFilter(scidBaseT *base, HFilter &filter, const Progress &progress)
Definition: searchpos.h:71
bool matsig_isReachable(matSigT mStart, matSigT mTarget, bool promos, bool upromo)
Definition: matsig.cpp:56
uint GetHPSig()
Definition: position.cpp:523
void set(gamenumT gnum, byte value)
Definition: hfilter.h:158
byte GetStoredLineCode() const
Definition: indexentry.h:330
Definition: indexentry.h:54
int match(uint code)
Definition: stored.h:32
FastGame getGame(const IndexEntry *ie) const
Definition: scidbase.h:111
byte squareT
Definition: common.h:113
byte pieceT
Definition: common.h:111