forked from psanse/BITSCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitboardn.h
582 lines (476 loc) · 15.4 KB
/
bitboardn.h
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/*
* bitboardn.h file from the BITSCAN library, a C++ library for bit set
* optimization. BITSCAN has been used to implement BBMC, a very
* succesful bit-parallel algorithm for exact maximum clique.
* (see license file for references)
*
* Copyright (C)
* Author: Pablo San Segundo
* Intelligent Control Research Group (CSIC-UPM)
*
* Permission to use, modify and distribute this software is
* granted provided that this copyright notice appears in all
* copies, in source code or in binaries. For precise terms
* see the accompanying LICENSE file.
*
* This software is provided "AS IS" with no warranty of any
* kind, express or implied, and with no claim as to its
* suitability for any purpose.
*
*/
#ifndef __BITBOARDN__H_
#define __BITBOARDN__H_
#include "bbobject.h"
#include "bitboard.h"
#include <vector>
using namespace std;
/////////////////////////////////
//
// class BitBoardN
//
// Manages bit strings greater than WORD_SIZE
// Does not use intrinsics nor does it cache information for very fast bitscanning
//
///////////////////////////////////
class BitBoardN:public BBObject{
public:
/*friend bool similar (const BitBoardN& , const BitBoardN& , int nBitDiff);
friend bool disjoint (const BitBoardN& bb1, const BitBoardN& bb2);
friend bool subsumes (const BitBoardN& bb1, const BitBoardN& bb2);*/
friend bool operator== (const BitBoardN& lhs, const BitBoardN& rhs);
friend BitBoardN& AND (const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res);
friend BitBoardN& AND (int first_block, const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res);
friend BitBoardN& AND (int first_block, int last_block, const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res);
friend BitBoardN& OR (const BitBoardN& lhs, const BitBoardN& rhs, BitBoardN& res);
BitBoardN (): m_nBB(EMPTY_ELEM),m_aBB(NULL){};
BitBoardN (int popsize /*1 based*/, bool reset=true);
BitBoardN (const BitBoardN& bbN);
BitBoardN (const std::vector<int>& v);
virtual ~BitBoardN ();
void init (int popsize, bool reset=true);
void init (int popsize, const vector<int> & );
BitBoardN& operator = (const BitBoardN& );
/////////////////////
//setters and getters (will not allocate memory)
BITBOARD* get_bitstring ();
const BITBOARD* get_bitstring () const;
int number_of_bitblocks () const {return m_nBB;}
const BITBOARD get_bitboard (int block) const {return m_aBB[block];}
BITBOARD& get_bitboard (int block) {return m_aBB[block];}
//////////////////////////////
// Bitscanning
//find least/most signinficant bit
inline int msbn64 () const; //lookup
inline int lsbn64 () const; //de Bruijn / lookup
//for looping (does not use state info)
inline int next_bit (int nBit) const; //de Bruijn
inline int next_bit_if_del (int nBit) const; //de Bruijn
inline int previous_bit (int nbit) const; //lookup
/////////////////
// Popcount
virtual inline int popcn64 () const; //lookup
inline int popcn64 (int nBit/* 0 based*/) const;
/////////////////////
//Set/Delete Bits
inline void init_bit (int nbit);
inline int init_bit (int lbit, int rbit);
inline void copy_from_block (int first_block, const BitBoardN& bb_add); //copies from first_block (included) onwards
inline void copy_up_to_block (int last_block, const BitBoardN& bb_add); //copies up to last_block (included)
inline void set_bit (int nbit);
inline int set_bit (int low, int high); //closed range
inline void set_bit ();
void set_bit (const BitBoardN& bb_add); //OR
void set_block (int first_block, const BitBoardN& bb_add); //OR:closed range
void set_block (int first_block, int last_block, const BitBoardN& bb_add); //OR:closed range
inline void erase_bit (int nbit);
inline int erase_bit (int low, int high);
inline void erase_bit ();
BitBoardN& erase_bit (const BitBoardN& bb_del);
BitBoardN& erase_block (int first_block, const BitBoardN& bb_del);
BitBoardN& erase_block (int first_block, int last_block, const BitBoardN& bb_del);
//slice
/*inline void copy_bitstring_right (int nbit,const BitBoardN& bba_copy);
inline void copy_bitstring_left (int nbit,const BitBoardN& bba_copy);
void add_bitstring_left (int nbit,const BitBoardN& bba_copy);
void add_bitstring_left (); */
////////////////////////
//Masking Operators (only for same block size)
BitBoardN& operator &= (const BitBoardN& ); //Equivalent to set_intersection
BitBoardN& operator |= (const BitBoardN& ); //Equivalente to set_union
BitBoardN& operator ^= (const BitBoardN& ); //Equivalente to set_ difference
//set operations (will not resize)
BitBoardN& flip ();
BitBoardN& bitset_union (const BitBoardN& );
BitBoardN& bitset_difference (const BitBoardN& );
BitBoardN& bitset_symmetric_difference (const BitBoardN& );
BitBoardN& bitset_intersection (const BitBoardN& );
/////////////////////////////
//Boolean functions
inline bool is_bit (int nbit) const;
inline bool is_empty () const;
inline bool is_empty (int nBBL, int nBBH) const;
inline bool is_singleton() const;
inline bool is_disjoint (const BitBoardN& rhs) const;
/////////////////////
// I/O
void print (std::ostream& = std::cout, bool show_pc = true) const;
string to_string ();
void to_vector (std::vector<int>& ) const;
////////////////////////
//Member data
protected:
BITBOARD* m_aBB;
int m_nBB; //Number of BITBOARDS (1 based)
};
inline int BitBoardN::msbn64() const{
///////////////////////
// Look up table implementation (best found so far)
union u {
U16 c[4];
BITBOARD b;
};
u val;
for(int i=m_nBB-1; i>=0; i--){
val.b=m_aBB[i];
if(val.b){
if(val.c[3]) return (Tables::msba[3][val.c[3]]+WMUL(i));
if(val.c[2]) return (Tables::msba[2][val.c[2]]+WMUL(i));
if(val.c[1]) return (Tables::msba[1][val.c[1]]+WMUL(i));
if(val.c[0]) return (Tables::msba[0][val.c[0]]+WMUL(i));
}
}
return EMPTY_ELEM; //should not reach here
}
//inline void BitBoardN::copy_bitstring_left (int nbit/*0 based*/, const BitBoardN& bbn){
////////////////////////////////
//// copies upper bits from and excluding nBit
//
// int index=WDIV(nbit);
// for(int i=index; i<m_nBB; i++)
// m_aBB[i] = bbn.m_aBB[i];
//
// //trim Left
// m_aBB[index] &= Tables::mask_left[nbit-WMUL(index)];
//}
//
//inline void BitBoardN::copy_bitstring_right (int nbit/* 0 based*/, const BitBoardN& bbn){
////////////////////////////////
//// copies lower bits from and excluding nBit
//
// int index=WDIV(nbit);
// for(int i=0; i<=index; i++)
// m_aBB[i] = bbn.m_aBB[i];
//
// //trim Right
// m_aBB[index] &= Tables::mask_right[nbit-WMUL(index)];
//}
//
//inline void BitBoardN::add_bitstring_left (int nbit, const BitBoardN& bbn){
////////////////////////////////
//// adds lower bits from and excluding nBit
////
// int index=WDIV(nbit);
// BITBOARD bb_min=bbn.m_aBB[index];
//
// for(int i=index+1; i<m_nBB; i++)
// m_aBB[i] |= bbn.m_aBB[i];
//
// //trim Left
// bb_min &= Tables::mask_left[nbit-WMUL(index)];
// m_aBB[index] |= bb_min;
//}
inline int BitBoardN::next_bit(int nBit/* 0 based*/) const{
////////////////////////////
//
// Returns next bit from nBit in the bitstring (to be used in a bitscan loop)
//
// NOTES: if nBit is FIRST_BITSCAN returns lsb
int index, npos;
if(nBit==EMPTY_ELEM)
return lsbn64();
else{
index=WDIV(nBit);
//looks in same BB as nBit
npos=BitBoard::lsb64_de_Bruijn(Tables::mask_left[WMOD(nBit) /*-WORD_SIZE*index*/] & m_aBB[index] );
if(npos>=0)
return (WMUL(index)/*WORD_SIZE*index*/ + npos);
//looks in remaining BBs
for(int i=index+1; i<m_nBB; i++){
if(m_aBB[i])
return(BitBoard::lsb64_de_Bruijn(m_aBB[i])+WMUL(i)/*WORD_SIZE*i*/ );
}
}
return -1;
}
inline int BitBoardN::next_bit_if_del(int nBit/* 0 based*/) const{
////////////////////////////
// Returns next bit assuming, when used in a loop, that the last bit
// scanned is deleted prior to the call
if(nBit==EMPTY_ELEM)
return lsbn64();
else{
for(int i=WDIV(nBit); i<m_nBB; i++){
if(m_aBB[i]){
return(BitBoard::lsb64_de_Bruijn(m_aBB[i])+WMUL(i) );
}
}
}
return -1;
}
inline int BitBoardN::previous_bit(int nBit/* 0 bsed*/) const{
////////////////////////////
// Gets the previous bit to nBit. If nBits is -10 is a MSB
//
// COMMENTS
// 1-There is no control of EMPTY_ELEM
if(nBit==EMPTY_ELEM)
return msbn64();
int index=WDIV(nBit);
int npos;
union u {
U16 c[4];
BITBOARD b;
};
u val;
//BitBoard pos
npos=BitBoard::msb64_lup( Tables::mask_right[WMOD(nBit) /*nBit-WMUL(index)*/] & m_aBB[index] );
if(npos!=EMPTY_ELEM)
return (WMUL(index) + npos);
for(int i=index-1; i>=0; i--){
val.b=m_aBB[i];
if(val.b){
if(val.c[3]) return (Tables::msba[3][val.c[3]]+ WMUL(i));
if(val.c[2]) return (Tables::msba[2][val.c[2]]+ WMUL(i));
if(val.c[1]) return (Tables::msba[1][val.c[1]]+ WMUL(i));
if(val.c[0]) return (Tables::msba[0][val.c[0]]+ WMUL(i));
}
}
return -1; //should not reach here
}
inline bool BitBoardN::is_bit (int nbit/*0 based*/) const{
//////////////////////////////
// RETURNS: TRUE if the bit is 1 in the position nbit, FALSE if opposite case or ERROR
return (m_aBB[WDIV(nbit)] & Tables::mask[WMOD(nbit)]);
}
inline bool BitBoardN::is_empty() const
{
for(int i=0; i<m_nBB; i++)
if(m_aBB[i]) return false;
return true;
}
inline bool BitBoardN::is_empty (int nBBL, int nBBH) const{
for(int i=nBBL; i<=nBBH; ++i)
if(m_aBB[i]) return false;
return true;
}
inline bool BitBoardN::is_disjoint (const BitBoardN& rhs) const{
for(int i=0; i<m_nBB; ++i)
if(m_aBB[i]& rhs.m_aBB[i]) return false;
return true;
}
inline void BitBoardN::erase_bit (int nbit /*0 based*/){
m_aBB[WDIV(nbit)] &= ~Tables::mask[WMOD(nbit)];
}
inline void BitBoardN::init_bit(int nbit){
///////////////////
// sets nbit and clears the rest
erase_bit();
set_bit(nbit);
}
inline int BitBoardN::init_bit(int low, int high){
///////////////////
// sets bits to 1 from lbit to rbit included and clears the rest
int bbl= WDIV(low);
int bbh= WDIV(high);
//checks consistency (***use ASSERT)
if(bbh<bbl || bbl<0 || low>high || low<0){
cerr<<"Error in set bit in range"<<endl;
return -1;
}
if(bbl==bbh){
m_aBB[bbh]=BitBoard::MASK_1(low-WMUL(bbl), high-WMUL(bbh));
}else{
for(int i=bbl+1; i<=bbh-1; i++)
m_aBB[i]=ONE;
m_aBB[bbl]=~Tables::mask_right[low-WMUL(bbl)];
m_aBB[bbh]=~Tables::mask_left[high-WMUL(bbh)];
}
//clears the rest
for(int i=0; i<bbl; i++)
m_aBB[i]=ZERO;
for(int i=bbh+1; i<m_nBB; i++)
m_aBB[i]=ZERO;
return 0;
}
inline void BitBoardN::copy_from_block (int first_block, const BitBoardN& bb_add){
//////////////
// copies from first_bit included onwards
for(int i=first_block; i<m_nBB; i++){
m_aBB[i]=bb_add.m_aBB[i];
}
}
inline void BitBoardN::copy_up_to_block (int last_block, const BitBoardN& bb_add){
//////////////
// copies up to last_bit included and clears t
// OBSERVATIONS: No out-of-bounds check for last_bit (will throw exception)
for(int i=0; i<=last_block; i++){
m_aBB[i]=bb_add.m_aBB[i];
}
}
inline void BitBoardN::set_bit (int nbit /*0 based*/){
m_aBB[WDIV(nbit)] |= Tables::mask[WMOD(nbit)];
}
inline int BitBoardN::set_bit (int low, int high){
/////////////////////
// Set all bits (0 based) to 1 in the closed range (including both ends)
// date: 22/9/14
int bbl= WDIV(low);
int bbh= WDIV(high);
//checks consistency (ASSERT)
if(bbh<bbl || bbl<0 || low>high || low<0){
cerr<<"Error in set bit in range"<<endl;
return -1;
}
if(bbl==bbh){
BITBOARD bb1=m_aBB[bbh]| ~Tables::mask_left[high-WMUL(bbh)];
BITBOARD bb2=m_aBB[bbl]| ~Tables::mask_right[low-WMUL(bbl)];
m_aBB[bbh]=bb1 & bb2;
}
else{
for(int i=bbl+1; i<=bbh-1; i++)
m_aBB[i]=ONE;
//lower
m_aBB[bbh]|=~Tables::mask_left[high-WMUL(bbh)];
m_aBB[bbl]|=~Tables::mask_right[low-WMUL(bbl)];
}
return 0;
}
inline void BitBoardN::set_bit (){
///////////////
// sets all bit blocks to ONE
for(int i=0; i<m_nBB; i++)
m_aBB[i]=ONE;
}
inline
void BitBoardN::set_bit (const BitBoardN& bb_add){
//////////////
// copies 1-bits (equivalent to OR, set_union etc)
for(int i=0; i<m_nBB; i++)
m_aBB[i]|=bb_add.m_aBB[i];
}
inline
void BitBoardN::set_block (int first_block, const BitBoardN& bb_add){
//////////////
// copies bb_add 1-bits (equibvalent to OR, set_union etc) from the first block onwards
for(int i=first_block; i<m_nBB; i++)
m_aBB[i]|=bb_add.m_aBB[i];
}
inline
void BitBoardN::set_block (int first_block, int last_block, const BitBoardN& bb_add){
//////////////
// copies bb_add 1-bits (equibvalent to OR, set_union etc) in CLOSED RANGE
for(int i=first_block; i<=last_block; i++)
m_aBB[i]|=bb_add.m_aBB[i];
}
inline void BitBoardN::erase_bit (){
///////////////
// sets all bit blocks to ZERO
for(int i=0; i<m_nBB; i++)
m_aBB[i]=ZERO;
}
inline int BitBoardN::erase_bit (int low, int high){
/////////////////////
// Set all bits (0 based) to 0 in the closed range (including both ends)
// date: 22/9/14
int bbl= WDIV(low);
int bbh= WDIV(high);
//checks consistency (ASSERT)
if(bbh<bbl || bbl<0 || low>high || low<0){
cerr<<"Error in set bit in range"<<endl;
return -1;
}
if(bbl==bbh){
BITBOARD bb1=m_aBB[bbh] & Tables::mask_left[high-WMUL(bbh)];
BITBOARD bb2=m_aBB[bbl] & Tables::mask_right[low-WMUL(bbl)];
m_aBB[bbh]=bb1 | bb2;
}
else{
for(int i=bbl+1; i<=bbh-1; i++)
m_aBB[i]=ZERO;
//lower
m_aBB[bbh] &= Tables::mask_left[high-WMUL(bbh)]; //r
m_aBB[bbl] &= Tables::mask_right[low-WMUL(bbl)];
}
return 0;
}
inline int BitBoardN::lsbn64() const{
/////////////////
// different implementations of lsbn depending on configuration
#ifdef DE_BRUIJN
for(int i=0; i<m_nBB; i++){
if(m_aBB[i])
#ifdef ISOLANI_LSB
return(Tables::indexDeBruijn64_ISOL[((m_aBB[i] & -m_aBB[i]) * DEBRUIJN_MN_64_ISOL/*magic num*/) >> DEBRUIJN_MN_64_SHIFT]+ WMUL(i));
#else
return(Tables::indexDeBruijn64_SEP[((m_aBB[i]^ (m_aBB[i]-1)) * DEBRUIJN_MN_64_SEP/*magic num*/) >> DEBRUIJN_MN_64_SHIFT]+ WMUL(i));
#endif
}
#elif LOOKUP
union u {
U16 c[4];
BITBOARD b;
};
u val;
for(int i=0; i<m_nBB; i++){
val.b=m_aBB[i];
if(val.b){
if(val.c[0]) return (Tables::lsba[0][val.c[0]]+WMUL(i));
if(val.c[1]) return (Tables::lsba[1][val.c[1]]+WMUL(i));
if(val.c[2]) return (Tables::lsba[2][val.c[2]]+WMUL(i));
if(val.c[3]) return (Tables::lsba[3][val.c[3]]+WMUL(i));
}
}
#endif
return EMPTY_ELEM;
}
inline bool BitBoardN::is_singleton()const{
/////////////////////////////
// optimized for dense graphs
int pc=0;
for(int i=0; i<m_nBB; i++){
if((pc+= BitBoard::popc64(m_aBB[i]))>1) return false;
}
if(pc) return true;
return false;
}
int BitBoardN::popcn64() const{
int npc=0;
union u {
U16 c[4];
BITBOARD b;
}val;
for(int i=0; i<m_nBB; i++){
val.b = m_aBB[i]; //Loads union
npc+= Tables::pc[val.c[0]] + Tables::pc[val.c[1]] + Tables::pc[val.c[2]] + Tables::pc[val.c[3]];
}
return npc;
}
int BitBoardN::popcn64(int nBit) const{
/////////////////////////
// Population size from nBit(included) onwards
int npc=0;
union u {
U16 c[4];
BITBOARD b;
}val;
int nBB=WDIV(nBit);
for(int i=nBB+1; i<m_nBB; i++){
val.b = m_aBB[i]; //Loads union
npc+= Tables::pc[val.c[0]] + Tables::pc[val.c[1]] + Tables::pc[val.c[2]] + Tables::pc[val.c[3]];
}
//special case of nBit bit block
val.b = m_aBB[nBB]&~Tables::mask_right[WMOD(nBit)]; //Loads union
npc+= Tables::pc[val.c[0]] + Tables::pc[val.c[1]] + Tables::pc[val.c[2]] + Tables::pc[val.c[3]];
return npc;
}
#endif