Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef COUNTBOARD_H
00025 #define COUNTBOARD_H
00026
00027 #ifndef USE_PCH
00028 #include <cstring>
00029 #ifndef DOXYGEN
00030 #include "debug.h"
00031 #endif
00032 #endif
00033
00034 #include "BitBoard.h"
00035
00036 namespace cwchess {
00037
00038 class CountBoard {
00039 private:
00040 BitBoard M_bits[4];
00041 BitBoard M_any;
00042
00043 public:
00044 CountBoard(void) { }
00045 CountBoard(CountBoard const& count_board) { std::memcpy(this,& count_board, sizeof(CountBoard)); }
00046 CountBoard& operator=(CountBoard const& count_board) { std::memcpy(this,& count_board, sizeof(CountBoard)); return* this; }
00047
00048 void add(BitBoard const& bit_board)
00049 {
00050 #ifdef CWDEBUG
00051 if (debug::channels::dc::countboard.is_on())
00052 {
00053
00054 libcwd::location_ct location((char*)__builtin_return_address(0) + libcwd::builtin_return_address_offset);
00055
00056 std::string demangled_function_name;
00057 libcwd::demangle_symbol(location.mangled_function_name(), demangled_function_name);
00058
00059 Dout(dc::countboard, "CountBoard::add() with this = " << (void*)this << " [called from " << demangled_function_name << '(' << location << ")], adds:");
00060 for (int row = 7; row >= 0; --row)
00061 {
00062 for (int col = 0; col <= 7; ++col)
00063 {
00064 if (bit_board.test(col, row))
00065 std::cout << "1 ";
00066 else
00067 std::cout << "0 ";
00068 }
00069 std::cout << '\n';
00070 }
00071 }
00072 #endif
00073
00074 BitBoard input(bit_board);
00075 M_any |= input;
00076 BitBoard bits_and1(M_bits[0] & input);
00077 M_bits[0] ^= input;
00078 BitBoard bits_and2(M_bits[1] & bits_and1);
00079 M_bits[1] ^= bits_and1;
00080 bits_and1 = M_bits[2] & bits_and2;
00081 M_bits[2] ^= bits_and2;
00082 M_bits[3] ^= bits_and1;
00083 }
00084
00085 void sub(BitBoard const& bit_board)
00086 {
00087 #ifdef CWDEBUG
00088 if (debug::channels::dc::countboard.is_on())
00089 {
00090
00091 libcwd::location_ct location((char*)__builtin_return_address(0) + libcwd::builtin_return_address_offset);
00092
00093 std::string demangled_function_name;
00094 libcwd::demangle_symbol(location.mangled_function_name(), demangled_function_name);
00095
00096 Dout(dc::countboard, "CountBoard::sub() with this = " << (void*)this << " [called from " << demangled_function_name << '(' << location << ")], subtracts:");
00097 for (int row = 7; row >= 0; --row)
00098 {
00099 for (int col = 0; col <= 7; ++col)
00100 {
00101 if (bit_board.test(col, row))
00102 std::cout << "1 ";
00103 else
00104 std::cout << "0 ";
00105 }
00106 std::cout << '\n';
00107 }
00108 }
00109 #endif
00110
00111 BitBoard input(bit_board);
00112 BitBoard bits_and1(~M_bits[0] & input);
00113 BitBoard collect(M_bits[0] ^= input);
00114 BitBoard bits_and2(~M_bits[1] & bits_and1);
00115 collect |= (M_bits[1] ^= bits_and1);
00116 bits_and1 = ~M_bits[2] & bits_and2;
00117 collect |= (M_bits[2] ^= bits_and2);
00118 collect |= (M_bits[3] ^= bits_and1);
00119 M_any = collect;
00120 }
00121
00122 void reset(void)
00123 {
00124 std::memset(this, 0, sizeof(CountBoard));
00125 #if DEBUG_BITBOARD_INITIALIZATION
00126 M_bits[0].reset();
00127 M_bits[1].reset();
00128 M_bits[2].reset();
00129 M_bits[3].reset();
00130 M_any.reset();
00131 #endif
00132 }
00133
00134 BitBoard any(void) const { return M_any; }
00135
00136 int count(BitBoard const& pos) const
00137 {
00138 return ((M_bits[0] & pos) ? 1 : 0) | ((M_bits[1] & pos) ? 2 : 0) | ((M_bits[2] & pos) ? 4 : 0) | ((M_bits[3] & pos) ? 8 : 0);
00139 }
00140
00141 int count(Index const& index) const
00142 {
00143 BitBoard pos(index);
00144 return count(pos);
00145 }
00146 };
00147
00148 }
00149
00150 #endif // COUNTBOARD_H