My Project
numSimpleEffect.h
Go to the documentation of this file.
1 #ifndef OSL_NUM_SIMPLE_EFFECT_H
2 #define OSL_NUM_SIMPLE_EFFECT_H
3 
4 #include "osl/simpleState.h"
6 #include "osl/bits/boardMask.h"
7 #include "osl/bits/bitXmask.h"
10 
11 namespace osl
12 {
13  namespace checkmate
14  {
15  class King8Info;
16  }
17  namespace effect
18  {
19  class NumSimpleEffectTable;
20  bool operator==(const NumSimpleEffectTable&,const NumSimpleEffectTable&);
21  std::ostream& operator<<(std::ostream&, const NumSimpleEffectTable&);
22 
27  {
28  protected:
30 #ifdef __GNUC__
31  __attribute__((aligned(16)))
32 #endif
33  ;
37  public:
53  template<Player P,Ptype T,Direction Dir,NumBitmapEffect::Op OP,bool UC>
54  void doEffectShort(const SimpleState& state,Square pos,int num)
55  {
57  {
58  const Square target = pos+DirectionPlayerTraits<Dir,P>::offset();
59  effects[target.index()].template opEqual<OP>(NumBitmapEffect::makeEffect<P>(num));
60  if(UC){
61  int posIndex=BoardMask::index(pos);
62  changed_effects[P].set(posIndex+BoardMask::getIndexOffset<Dir,P>());
63  int num1;
64  if(Piece::isPieceNum(num1=state.pieceAt(target).number())){
65  if(OP==NumBitmapEffect::Add){
66  effected_mask[P].set(num1);
67  }
68  else{ // OP==Sub
69  if((effects[target.index()].getMask(1)&NumBitmapEffect::playerEffectMask(P)).none()){
70  effected_mask[P].reset(num1);
71  }
72  }
73  effected_changed_mask[P].set(num1);
74  }
75  }
76  }
77  }
88  template<Player P,Ptype T,Direction Dir,NumBitmapEffect::Op OP,bool UC>
89  void doEffectLong(const SimpleState& state,Square pos,int num)
90  {
92  {
93  int posIndex;
94  if(UC){
95  posIndex=BoardMask::index(pos);
96  }
98  assert(!offset.zero());
99  NumBitmapEffect effect=NumBitmapEffect::makeLongEffect<P>(num);
100 
101  const Direction SD=longToShort(Dir);
102  if(OP==NumBitmapEffect::Sub){
103  Square ePos=mobilityTable.get(longToShort(Dir),num);
104  int count=((SD==D || SD==DL || SD==DR) ? ePos.y()-pos.y() :
105  ( (SD==U || SD==UL || SD==UR) ? pos.y()-ePos.y() :
106  ( SD==L ? ePos.x()-pos.x() : pos.x()-ePos.x())));
107  assert(0<=count && count<=9);
108  if(UC){
109  for(int i=1;i<count;i++){
110  pos+=offset;
111  posIndex+=BoardMask::getIndexOffset<Dir,BLACK>();
112  effects[pos.index()].template opEqual<OP>(effect);
113  changed_effects[P].set(posIndex);
114  }
115  Piece p;
117  int num1=state.pieceAt(ePos).number();
118  if (!Piece::isEdgeNum(num1)){
119  effectedNumTable[num1][SD]=EMPTY_NUM;
120  effects[ePos.index()].template opEqual<OP>(effect);
121  effected_changed_mask[P].set(num1);
122  posIndex+=BoardMask::getIndexOffset<Dir,BLACK>();
123  changed_effects[P].set(posIndex);
124  if((effects[ePos.index()].getMask(1)&NumBitmapEffect::playerEffectMask(P)).none()){
125  effected_mask[P].reset(num1);
126  }
127  }
128  }
129  else{
130  for(int i=0;i<count;i++){
131  pos+=offset;
132  effects[pos.index()].template opEqual<OP>(effect);
133  }
134  int num1=state.pieceAt(ePos).number();
135  if (!Piece::isEdgeNum(num1))
136  effectedNumTable[num1][SD]=EMPTY_NUM;
137  }
138  }
139  else{ // OP==Add
140  for (;;)
141  {
142  pos=pos+offset;
143  if(UC){
144  posIndex+=BoardMask::getIndexOffset<Dir,BLACK>();
145  changed_effects[P].set(posIndex);
146  }
147  effects[pos.index()].template opEqual<OP>(effect);
148  // effect内にemptyを含むようにしたら短くなる
149  int num1=state.pieceAt(pos).number();
150  if (!Piece::isEmptyNum(num1)){
151  if(UC){
152  mobilityTable.set(longToShort(Dir),num,pos);
153  if(!Piece::isEdgeNum(num1)){
154  effectedNumTable[num1][SD]=num;
155  changed_effects[P].set(posIndex);
156  effected_mask[P].set(num1);
157  effected_changed_mask[P].set(num1);
158  }
159  }
160  else if(!Piece::isEdgeNum(num1)){
161  effectedNumTable[num1][SD]=num;
162  }
163  break;
164  }
165  }
166  }
167  }
168  }
178  template<Player P,Ptype T,NumBitmapEffect::Op OP,bool UC>
179  void doEffectBy(const SimpleState& state,Square pos,int num);
188  template<NumBitmapEffect::Op OP,bool UC>
189  void doEffect(const SimpleState& state,PtypeO ptypeo,Square pos,int num);
190 
197  template<NumBitmapEffect::Op OP,bool UC>
198  void doEffect(const SimpleState& state,Piece p)
199  {
200  doEffect<OP,UC>(state,p.ptypeO(),p.square(),p.number());
201  }
206  void init(const SimpleState& state);
211  {
212  assert(reinterpret_cast<size_t>(this) % 16 == 0);
213  init(state);
214  }
220  {
221  return effects[pos.index()];
222  }
229  template<NumBitmapEffect::Op OP,bool UC>
230  void doBlockAt(const SimpleState& state,Square pos,int piece_num);
231  friend bool operator==(const NumSimpleEffectTable& et1,const NumSimpleEffectTable& et2);
232  /*
233  *
234  */
235  const BoardMask changedEffects(Player pl) const{
236  return changed_effects[pl];
237  }
239  return changed_effect_pieces;
240  }
241  const PieceMask effectedMask(Player pl) const {
242  return effected_mask[playerToIndex(pl)];
243  }
244  const PieceMask effectedChanged(Player pl) const {
246  }
247  void setChangedPieces(NumBitmapEffect const& effect) {
248  changed_effect_pieces |= effect;
249  }
251  changed_effects[0].clear();
252  changed_effects[1].clear();
254  }
256  changed_effects[0].invalidate();
257  changed_effects[1].invalidate();
259  }
261  effected_changed_mask[0].resetAll();
262  effected_changed_mask[1].resetAll();
263  }
265  void copyFrom(const NumSimpleEffectTable& src);
266  };
267 
268  inline bool operator!=(const NumSimpleEffectTable& et1,const NumSimpleEffectTable& et2)
269  {
270  return !(et1==et2);
271  }
272 
273  } // namespace effect
274  using effect::NumBitmapEffect;
275 
276 } // namespace osl
277 
284 template<osl::effect::NumBitmapEffect::Op OP,bool UC>
286 NumSimpleEffectTable::doBlockAt(const SimpleState& state,Square pos,int piece_num)
287 {
288  if(UC){
289  setChangedPieces(effects[pos.index()]);
290  }
291  mask_t mask1 =((effects[pos.index()].getMask(1))
292  & NumBitmapEffect::longEffectMask());
293  while (mask1.any()){
294  int num=mask1.takeOneBit()+NumBitmapEffect::longToNumOffset;
295  assert(32<=num && num<=39);
296  Piece p1=state.pieceOf(num);
297  Player pl1=p1.owner();
298  assert(p1.ptype()!=PPAWN);
299  Square pos1=p1.square();
300  Offset offset0;
301  Direction d=Board_Table.getShort8<BLACK>(pos1,pos,offset0);
302  if(OP==NumBitmapEffect::Sub){
303  Square endSquare=mobilityTable.get(d,num);
304  NumBitmapEffect effect=NumBitmapEffect::makeLongEffect(pl1,num);
305  Piece p;
306  Square pos2=pos+offset0;
307  int pos2Index, offset81;
308  if(UC){
309  int posIndex=BoardMask::index(pos);
310  pos2Index=BoardMask::index(pos2);
311  offset81=pos2Index-posIndex;
312  }
313  for(;pos2!=endSquare;pos2+=offset0){
314  if(UC){
315  changed_effects[pl1].set(pos2Index);
316  pos2Index+=offset81;
317  }
318  effects[pos2.index()].template opEqual<OP>(effect);
319  }
320  effects[pos2.index()].template opEqual<OP>(effect);
321  int num1=state.pieceAt(endSquare).number();
322  if (!Piece::isEdgeNum(num1)){
323  effectedNumTable[num1][d]=EMPTY_NUM;
324  if(UC){
325  changed_effects[pl1].set(pos2Index);
326  if((effects[endSquare.index()].getMask(1)&NumBitmapEffect::playerEffectMask(pl1)).none()){
327  effected_mask[pl1].reset(num1);
328  }
329  effected_changed_mask[pl1].set(num1);
330  mobilityTable.set(d,num,pos);
331  }
332  }
333  else
334  mobilityTable.set(d,num,pos);
335  effectedNumTable[piece_num][d]=num;
336  }
337  else{
338  NumBitmapEffect effect=NumBitmapEffect::makeLongEffect(pl1,num);
339  Square pos2=pos+offset0;
340  int pos2Index, offset81;
341  if(UC){
342  int posIndex=BoardMask::index(pos);
343  pos2Index=BoardMask::index(pos2);
344  offset81=pos2Index-posIndex;
345  }
346  for(;;){
347  int num1=state.pieceAt(pos2).number();
348  if(!Piece::isEmptyNum(num1)){
349  if(UC){
350  mobilityTable.set(d,num,pos2);
351  if(!Piece::isEdgeNum(num1)){
352  effectedNumTable[num1][d]=num;
353  effects[pos2.index()].template opEqual<OP>(effect);
354  changed_effects[pl1].set(pos2Index);
355  effected_mask[pl1].set(num1);
356  effected_changed_mask[pl1].set(num1);
357  }
358  }
359  else if(!Piece::isEdgeNum(num1)){
360  effectedNumTable[num1][d]=num;
361  effects[pos2.index()].template opEqual<OP>(effect);
362  }
363  break;
364  }
365  if(UC){
366  changed_effects[pl1].set(pos2Index);
367  pos2Index+=offset81;
368  }
369  effects[pos2.index()].template opEqual<OP>(effect);
370  pos2+=offset0;
371  }
372  }
373  }
374 }
375 
376 #endif // OSL_NUM_SIMPLE_EFFECT_H
377 // ;;; Local Variables:
378 // ;;; mode:c++
379 // ;;; c-basic-offset:2
380 // ;;; End:
Direction getShort8(Square from, Square to) const
Definition: boardTable.h:147
座標の差分
Definition: basic_type.h:430
bool zero() const
Definition: basic_type.h:502
駒番号のビットセット.
Definition: pieceMask.h:21
PtypeO ptypeO() const
Definition: basic_type.h:824
Ptype ptype() const
Definition: basic_type.h:821
const Square square() const
Definition: basic_type.h:832
Player owner() const
Definition: basic_type.h:963
static bool isPieceNum(int num)
Definition: basic_type.h:926
static bool isEmptyNum(int num)
Definition: basic_type.h:916
int number() const
Definition: basic_type.h:828
static bool isEdgeNum(int num)
Definition: basic_type.h:922
const Piece pieceOf(int num) const
Definition: simpleState.h:76
const Piece pieceAt(Square sq) const
Definition: simpleState.h:167
unsigned int index() const
Definition: basic_type.h:572
int y() const
将棋としてのY座標を返す.
Definition: basic_type.h:567
static const Square STAND()
Definition: basic_type.h:548
int x() const
将棋としてのX座標を返す.
Definition: basic_type.h:563
static int index(int x, int y)
Definition: boardMask.h:80
現在の定義 (2005/3/4以降)
static mask_t playerEffectMask()
局面全体の利きデータ.
void copyFrom(const NumSimpleEffectTable &src)
主要部分を高速にコピーする.
void doEffect(const SimpleState &state, PtypeO ptypeo, Square pos, int num)
ある種類の駒が持つ利きを更新する.
EffectedNumTable effectedNumTable
effected num
const BoardMask changedEffects(Player pl) const
void doEffectLong(const SimpleState &state, Square pos, int num)
ある位置からある方向に長い利きがある時に,その方向の利きを更新する.
void doEffectShort(const SimpleState &state, Square pos, int num)
ある位置からある方向に短い利きがある時に,その方向の利きを更新する.
const NumBitmapEffect effectSetAt(Square pos) const
ある位置の利きデータを取り出す.
void doEffect(const SimpleState &state, Piece p)
ある駒が持つ利きを更新する.
const NumBitmapEffect changedPieces() const
const PieceMask effectedMask(Player pl) const
friend bool operator==(const NumSimpleEffectTable &et1, const NumSimpleEffectTable &et2)
NumSimpleEffectTable(const SimpleState &state)
コンストラクタ.
void init(const SimpleState &state)
盤面のデータを元に初期化する.
void doBlockAt(const SimpleState &state, Square pos, int piece_num)
posに駒を設置/削除して長い利きをブロック/延長する際の利きデータの更新.
void doEffectBy(const SimpleState &state, Square pos, int num)
ある種類の駒が持つ利きを更新する.
CArray< NumBitmapEffect, Square::SIZE > effects
const PieceMask effectedChanged(Player pl) const
mobility::MobilityTable mobilityTable
mobility
CArray< PieceMask, 2 > effected_mask
void setChangedPieces(NumBitmapEffect const &effect)
CArray< BoardMask, 2 > changed_effects
CArray< PieceMask, 2 > effected_changed_mask
NumBitmapEffect changed_effect_pieces
set of pieces whose effect changed by previous move
駒番号からMobilityContentを得る
Definition: mobilityTable.h:52
const Square get(Direction d, int num) const
Definition: mobilityTable.h:65
void set(Direction d, int num, Square pos)
Definition: mobilityTable.h:61
std::ostream & operator<<(std::ostream &, const EffectedNumTable &)
bool operator==(const EffectedNumTable &, const EffectedNumTable &)
bool operator!=(const NumSimpleEffectTable &et1, const NumSimpleEffectTable &et2)
GeneralMask< mask_int_t > mask_t
Definition: mask.h:351
@ PPAWN
Definition: basic_type.h:87
const int EMPTY_NUM
Definition: basic_type.h:778
constexpr Direction longToShort(Direction d)
Definition: basic_type.h:380
constexpr int playerToIndex(Player player)
Definition: basic_type.h:16
const BoardTable Board_Table
Definition: tables.cc:95
Direction
Definition: basic_type.h:310
@ D
Definition: basic_type.h:319
@ UL
Definition: basic_type.h:313
@ DR
Definition: basic_type.h:320
@ U
Definition: basic_type.h:314
@ L
Definition: basic_type.h:316
@ UR
Definition: basic_type.h:315
@ DL
Definition: basic_type.h:318
Player
Definition: basic_type.h:8
@ BLACK
Definition: basic_type.h:9
const PtypeO PTYPEO_EDGE __attribute__((unused))
PtypeO
Player + Ptype [-15, 15] PtypeO の O は Owner の O.
Definition: basic_type.h:199