boardMask.h Source File

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
boardMask.h
Go to the documentation of this file.
1 /* boardMask.h
2 */
3 #ifndef OSL_BOARDMASK_H
4 #define OSL_BOARDMASK_H
5
6 #include "osl/square.h"
7 #include "osl/directionTraits.h"
8 #include "osl/misc/carray.h"
9 #include "osl/misc/cstdint.h"
10 #include "osl/misc/bitOp.h"
11 #include <iosfwd>
12
13 namespace osl
14 {
15 namespace container
16 {
17 class BoardMask;
18 bool operator==(const BoardMask&, const BoardMask&);
19 std::ostream& operator<<(std::ostream&, const BoardMask&);
21 class BoardMask
22 {
24 CArray<unsigned long long,3> contents;
25 public:
26 BoardMask() { invalidate(); }
27 BoardMask(const BoardMask& src) {
28 contents[0] = src.contents[0];
29 contents[1] = src.contents[1];
30 }
31 BoardMask& operator=(const BoardMask& src) {
32 if (this != &src) {
33 contents[0] = src.contents[0];
34 contents[1] = src.contents[1];
35 }
36 return *this;
37 }
38 void clear() { contents[0]=contents[1]=0; }
39 void invalidate() { contents[0] = static_cast<uint64_t>(-1); }
40 bool isInvalid() const { return contents[0] == static_cast<uint64_t>(-1); }
41 void set(unsigned int i) {
42 int j=(i>>6);
43 contents[j]|=(1ull<<(i&63));
44 }
45 void set(Square pos) {
46 set(index(pos));
47 }
48 void reset(unsigned int i) {
49 int j=(i>>6);
50 contents[j] &= ~(1ull<<(i&63));
51 }
52 void reset(Square pos) { reset(index(pos)); }
53 bool test(unsigned int i) const {
54 int j=(i>>6);
55 return (contents[j]&(1ull<<(i&63)))!=0;
56 }
57 bool test(Square pos) const { return test(index(pos)); }
58 bool anyInRange(const BoardMask& mask) const
59 {
60 return (contents[0] & mask.contents[0])
61 || (contents[1] & mask.contents[1]);
62 }
63 BoardMask& operator|=(const BoardMask& mask)
64 {
65 contents[0] |= mask.contents[0];
66 contents[1] |= mask.contents[1];
67 return *this;
68 }
69 bool any() const
70 {
71 assert(! isInvalid());
72 return contents[0] || contents[1];
73 }
74 Square takeOneBit()
75 {
76 assert(! isInvalid() && any());
77 if (contents[0])
78 return toSquare(misc::BitOp::takeOneBit(contents[0]));
79 return toSquare(misc::BitOp::takeOneBit(contents[1])+64);
80 }
81 static int index(int x,int y){ return x*12+y+1; }
82 static int index(Square pos) {
83 int v=pos.index();
84 return v-((v>>2)&0x3c);
85 }
86 template<Direction Dir,Player P>
87 static int getIndexOffset() {
88 int blackDx=DirectionTraitsGen<Dir>::blackDx;
89 int blackDy=DirectionTraitsGen<Dir>::blackDy;
90 int val=blackDx*12+blackDy;
91 if(P==BLACK) return val;
92 else return -val;
93 }
94 static Square toSquare(int n) { return Square::makeDirect(n+(((n*21)>>8)<<2)); }
95 friend bool operator==(const BoardMask&, const BoardMask&);
96 };
97 inline const BoardMask operator|(const BoardMask& l, const BoardMask& r)
98 {
99 BoardMask result = l;
100 result |= r;
101 return result;
102 }
103 inline bool operator==(const BoardMask& l, const BoardMask& r)
104 {
105 return l.contents[0] == r.contents[0]
106 && l.contents[1] == r.contents[1];
107 }
108 class BoardMaskTable5x5
109 {
110 CArray<BoardMask, Square::SIZE> data;
111 public:
112 BoardMaskTable5x5();
114 const BoardMask& mask(Square p) const { return data[p.index()]; }
115 };
116 extern const BoardMaskTable5x5 Board_Mask_Table5x5;
117
118 class BoardMaskTable3x3
119 {
120 CArray<BoardMask, Square::SIZE> data;
121 public:
122 BoardMaskTable3x3();
124 const BoardMask& mask(Square p) const { return data[p.index()]; }
125 };
126 extern const BoardMaskTable3x3 Board_Mask_Table3x3;
127
128 class BoardMaskTable5x3Center
129 {
130 CArray<BoardMask, Square::SIZE> data;
131 public:
132 BoardMaskTable5x3Center();
134 const BoardMask& mask(Square p) const { return data[p.index()]; }
135 };
136 extern const BoardMaskTable5x3Center Board_Mask_Table5x3_Center;
137 } // namespace container
138 using container::BoardMask;
139 using container::Board_Mask_Table5x5;
140 using container::Board_Mask_Table5x3_Center;
141 using container::Board_Mask_Table3x3;
142 } // namespace osl
143
144
145 #endif /* OSL_BOARDMASK_H */
146 // ;;; Local Variables:
147 // ;;; mode:c++
148 // ;;; c-basic-offset:2
149 // ;;; End: