-
Notifications
You must be signed in to change notification settings - Fork 14
/
bitboardn.cpp
344 lines (269 loc) · 6.45 KB
/
bitboardn.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// BitBoardN.cpp: implementation of the BitBoardN class.
//
//////////////////////////////////////////////////////////////////////
#include "bitboardn.h"
#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
using namespace std;
BitBoardN& AND (const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res){
for(int i=0; i<lhs.m_nBB; i++){
res.m_aBB[i]=lhs.m_aBB[i] & rhs.m_aBB[i];
}
return res;
}
BitBoardN& OR (const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res){
for(int i=0; i<lhs.m_nBB; i++){
res.m_aBB[i]=lhs.m_aBB[i] | rhs.m_aBB[i];
}
return res;
}
BitBoardN& AND (int first_block, const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res){
for(int i=first_block; i<lhs.m_nBB; i++){
res.m_aBB[i]=rhs.m_aBB[i] & lhs.m_aBB[i];
}
return res;
}
BitBoardN& AND (int first_block, int last_block, const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res){
for(int i=first_block; i<=last_block; i++){
res.m_aBB[i]=rhs.m_aBB[i] & lhs.m_aBB[i];
}
return res;
}
BitBoardN& ERASE(const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res){
/////////////
// removes rhs FROM lhs
for(int i=0; i<lhs.m_nBB; i++){
res.m_aBB[i]=lhs.m_aBB[i] &~ rhs.m_aBB[i];
}
return res;
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BitBoardN::BitBoardN(int popsize /*1 based*/ , bool reset){
m_nBB=INDEX_1TO1(popsize);
#ifndef _MEM_ALIGNMENT
if(!(m_aBB= new BITBOARD[m_nBB])){
#else
if(!(m_aBB = (BITBOARD*)_aligned_malloc(sizeof(BITBOARD)*m_nBB,_MEM_ALIGNMENT))){
#endif
printf("Error al reservar memoria");
m_nBB=-1;
}
//Sets to 0 all bits
if(reset) erase_bit();
}
BitBoardN::BitBoardN(const BitBoardN& bbN){
///////////////////////////
// copy constructor
//allcoates memory
if(bbN.m_aBB==NULL || bbN.m_nBB<0 ){
m_nBB=-1;
m_aBB=NULL;
return;
}
m_nBB=bbN.m_nBB;
#ifndef _MEM_ALIGNMENT
m_aBB=new BITBOARD[m_nBB];
#else
m_aBB = (BITBOARD*)_aligned_malloc(sizeof(BITBOARD)*m_nBB, _MEM_ALIGNMENT );
#endif
//copies bitblocks
for(int i=0; i<m_nBB; i++)
m_aBB[i]=bbN.m_aBB[i];
}
BitBoardN::BitBoardN(const vector<int>& v){
///////////////////
// vector numbers should be zero based (i.e v[0]=3, bit-index 3=1)
//Getting BB Size
m_nBB=INDEX_0TO1(*(max_element(v.begin(), v.end())) ) ;
#ifndef _MEM_ALIGNMENT
m_aBB=new BITBOARD[m_nBB];
#else
m_aBB = (BITBOARD*)_aligned_malloc(sizeof(BITBOARD)*m_nBB, _MEM_ALIGNMENT );
#endif
erase_bit();
for(int i=0; i<v.size(); i++){
if(v[i]>=0)
set_bit(v[i]);
}
}
BitBoardN::~BitBoardN(){
if(m_aBB!=NULL){
delete [] m_aBB;
}
m_aBB=NULL;
}
void BitBoardN::init(int popsize, const vector<int> &v){
///////////////////////////
// values in vector are 1-bits in the bitboard (0 based)
if(m_aBB!=NULL){
#ifndef _MEM_ALIGNMENT
delete [] m_aBB;
#else
_aligned_free(m_aBB);
#endif
m_aBB=NULL;
}
m_nBB=INDEX_1TO1(popsize); //((popsize-1)/WORD_SIZE)+1;
#ifndef _MEM_ALIGNMENT
m_aBB= new BITBOARD[m_nBB];
#else
m_aBB = (BITBOARD*)_aligned_malloc(sizeof(BITBOARD)*m_nBB,_MEM_ALIGNMENT);
#endif
//sets bit conveniently
erase_bit();
for(int i=0; i<v.size(); i++){
if(v[i]>=0 && v[i]<popsize)
set_bit(v[i]);
else{
erase_bit(); //Errr. exit
cout<<"Error en valor.BitBoardN.New(...)"<<endl;
break;
}
}
}
BITBOARD* BitBoardN::get_bitstring (){
return m_aBB;
}
const BITBOARD* BitBoardN::get_bitstring () const{
return m_aBB;
}
void BitBoardN::init(int popsize, bool reset){
//////////////////////
// only way to change storage space once constructed
if(m_aBB!=NULL){
#ifndef _MEM_ALIGNMENT
delete [] m_aBB;
#else
_aligned_free(m_aBB);
#endif
m_aBB=NULL;
}
//nBBs
m_nBB=INDEX_1TO1(popsize);
#ifndef _MEM_ALIGNMENT
m_aBB= new BITBOARD[m_nBB];
#else
m_aBB = (BITBOARD*)_aligned_malloc(sizeof(BITBOARD)*m_nBB,_MEM_ALIGNMENT);
#endif
//Sets to 0
if(reset)
erase_bit();
return ;
}
//void BitBoardN::add_bitstring_left(){
/////////////////////
////
//// Adds 1 bit to MSB
////
//// Date of creation: 26/4/2010
//// Date last modified: 26/4/2010
//// Autor:PSS
//
// int nvertex=this->msbn64(); //is -1 if empty
// if(nvertex!=(WMUL(nvertex)-1)){
// set_bit(nvertex+1); //add bit 0 if BB empty
// }
//}
//////////////////////////
//
// BITSET OPERATORS
// (size is determined by *this)
/////////////////////////
BitBoardN& BitBoardN::operator &= (const BitBoardN& bbn){
for(int i=0; i<m_nBB; i++)
m_aBB[i]&=bbn.m_aBB[i];
return *this;
}
BitBoardN& BitBoardN::operator |= (const BitBoardN& bbn){
for(int i=0; i<m_nBB; i++)
m_aBB[i]|=bbn.m_aBB[i];
return *this;
}
BitBoardN& BitBoardN::AND_EQ (int first_block, const BitBoardN& rhs ){
//////////////////////
// mask in range [first_block , END[
for(int i=first_block; i<m_nBB; i++)
m_aBB[i]&=rhs.m_aBB[i];
return *this;
}
BitBoardN& BitBoardN::OR_EQ (int first_block, const BitBoardN& rhs ){
//////////////////////
// mask in range [first_block , END[
for(int i=first_block; i<m_nBB; i++)
m_aBB[i]|=rhs.m_aBB[i];
return *this;
}
BitBoardN& BitBoardN::operator ^= (const BitBoardN& bbn){
for(int i=0; i<m_nBB; i++)
m_aBB[i]^=bbn.m_aBB[i];
return *this;
}
BitBoardN& BitBoardN::flip (){
for(int i=0; i<m_nBB; i++)
m_aBB[i]=~m_aBB[i];
return *this;
}
//////////////////////////
//
// BOOLEAN FUNCTIONS
//
//////////////////////////
//////////////////////////
//
// I/O FILES
//
//////////////////////////
void BitBoardN::print(std::ostream& o, bool show_pc ) const {
/////////////////////////
// shows bit string as [bit1 bit2 bit3 ... <(pc)>] (if empty: [ ]) (<pc> optional)
o<<"[";
int nBit=EMPTY_ELEM;
while(1){
nBit=next_bit(nBit);
if(nBit==EMPTY_ELEM) break;
o<<nBit<<" ";
}
if(show_pc){
int pc=popcn64();
if(pc) o<<"("<<popcn64()<<")";
}
o<<"]";
}
string BitBoardN::to_string (){
ostringstream sstr;
sstr<<"[";
this->print();
int nBit=EMPTY_ELEM;
while(true){
nBit=next_bit(nBit);
if(nBit==EMPTY_ELEM) break;
sstr<<nBit<<" ";
}
sstr<<"("<<popcn64()<<")";
sstr<<"]";
return sstr.str();
}
void BitBoardN::to_vector(vector<int>& vl ) const {
//////////////////////
// copies bit string to vector
vl.clear();
int v=EMPTY_ELEM;
while(true){
v=next_bit(v);
if(v==EMPTY_ELEM) break;
vl.push_back(v);
}
}
BitBoardN& BitBoardN::operator = (const BitBoardN& bbN){
if(m_nBB!=bbN.m_nBB){
//allocates memory
init(bbN.m_nBB,false);
}
for(int i=0; i<m_nBB; i++)
m_aBB[i]=bbN.m_aBB[i];
return *this;
}