forked from psanse/BITSCAN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitboards.cpp
859 lines (701 loc) · 20.6 KB
/
bitboards.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
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
// BitBoardS.cpp: implementation of the BitBoardS classd, wrapper for sparse bitstrings
//
//////////////////////////////////////////////////////////////////////
#include "bitboards.h"
#include "bbalg.h"
#include <iostream>
#include <sstream>
#include <cstdio>
#include <iterator>
using namespace std;
int BitBoardS::nElem=EMPTY_ELEM;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
BitBoardS::BitBoardS(int size /*1 based*/, bool is_popsize ){
(is_popsize)? m_MAXBB=INDEX_1TO1(size) : m_MAXBB=size;
m_aBB.reserve(DEFAULT_CAPACITY); //*** check efficiency
}
BitBoardS::BitBoardS (const BitBoardS& bbS){
//////////////
// copies state
m_aBB=bbS.m_aBB;
m_MAXBB=bbS.m_MAXBB;
}
int BitBoardS::init (int size, bool is_popsize){
///////////////
// allocates memory
clear();
(is_popsize)? m_MAXBB=INDEX_1TO1(size) : m_MAXBB=size;
m_aBB.reserve(DEFAULT_CAPACITY);
return 0; //for API compatibility
}
void BitBoardS::clear(){
m_aBB.clear();
m_MAXBB=EMPTY_ELEM;
}
void BitBoardS::sort (){
std::sort(m_aBB.begin(), m_aBB.end(), elem_less());
}
int BitBoardS::set_bit (int low, int high){
///////////////////
// sets bits to one in the corresponding CLOSED range
//
// REMARKS:
// 1.only one binary search is performed for the lower block
// 2.does not check maximum possible size
//
//*** OPTIMIZATION: restrict sorting when required to [low,high] interval
int bbh=WDIV(high);
int bbl=WDIV(low);
bool req_sorting=false;
//m_aBB.reserve(m_aBB.size()+high-low+1); //maximum possible size, to push_back in place without allocation
velem vapp;
//checks consistency (ASSERT)
if(bbh<bbl || bbl<0 || low>high || low<0){
cerr<<"Error in set bit in range"<<endl;
return -1;
}
//A) bbh==bbl
pair<bool,velem_it> p=find_block(bbl);
if(bbh==bbl){
if(p.first){
BITBOARD bb_high=p.second->bb| ~Tables::mask_left[high-WMUL(bbh)];
BITBOARD bb_low=p.second->bb| ~Tables::mask_right[low-WMUL(bbl)];
p.second->bb=bb_low & bb_high;
return 0; //no need for sorting
}else{
BITBOARD bb_high= ~Tables::mask_left[high-WMUL(bbh)];
m_aBB.push_back(elem(bbl,bb_high&~ Tables::mask_right[low-WMUL(bbl)]));
return 0;
}
}
//B) bbl outside range
if(p.second==m_aBB.end()){
//append blocks at the end
m_aBB.push_back(elem(bbl,~Tables::mask_right[low-WMUL(bbl)]));
for(int i=low+1; i<bbh; ++i){
m_aBB.push_back(elem(i,ONE));
}
m_aBB.push_back(elem(bbh,~Tables::mask_left[high-WMUL(bbh)]));
return 0;
}
//C) bbl
int block=bbl;
if(p.first){ //block exists
p.second->bb|=~Tables::mask_right[low-WMUL(bbl)];
++p.second;
}else{ //block does not exist:marked for append
vapp.push_back(elem(bbl,~Tables::mask_right[low-WMUL(bbl)]));
}
++block;
//D) Remaining blocks
while(true){
//exit condition I
if(p.second==m_aBB.end()){
//append blocks at the end
for(int i=block; i<bbh; ++i){
m_aBB.push_back(elem(i,ONE));
}
m_aBB.push_back(elem(bbh,~Tables::mask_left[high-WMUL(bbh)]));
req_sorting=true;
break;
}
//exit condition II
if(block==bbh){
if(p.second->index==block){ //block exists: trim and overwrite
p.second->bb|=~Tables::mask_left[high-WMUL(bbh)];
break;
}else{ //block doesn't exist, trim and append
m_aBB.push_back(elem(bbh,~Tables::mask_left[high-WMUL(bbh)]));
req_sorting=true;
break;
}
}
//update before either of the bitstrings has reached its end
if(p.second->index==block){
p.second->bb=ONE;
++p.second; ++block;
}else if(block<p.second->index){ //m_aBB[pos].index<block cannot occur
vapp.push_back(elem(block,ONE)); //not added in place to avoid loosing indexes
// m_aBB.push_back(elem(block,ONE));
req_sorting=true;
++block;
}
}
//sorts if necessary
m_aBB.insert(m_aBB.end(), vapp.begin(), vapp.end());
if(req_sorting)
sort();
return 0;
}
int BitBoardS::init_bit (int low, int high){
/////////////////////////
// sets bits in the closed range and clears the rest
//*** ASSERT (MAX SIZE)
int bbh=WDIV(high);
int bbl=WDIV(low);
m_aBB.clear();
//same bitblock
if(bbh==bbl){
BITBOARD bb= ~Tables::mask_right[low-WMUL(bbl)];
m_aBB.push_back(elem(bbl, bb & ~Tables::mask_left[high-WMUL(bbh)]));
return 0;
}
//first
m_aBB.push_back(elem(bbl, ~Tables::mask_right[low-WMUL(bbl)]));
//middle
for(int block=bbl+1; block<bbh; ++block){
m_aBB.push_back(elem(block,ONE));
}
//last
m_aBB.push_back(elem(bbh, ~Tables::mask_left[high-WMUL(bbh)]));
return 0;
}
BitBoardS& BitBoardS::set_bit (const BitBoardS& rhs){
/////////////////////////////////
// sets 1-bits in rhs (equivalent to an |=)
//
// REMARKS: experimental, currently only defined for bit strings of same maximum size
if(rhs.is_empty()) return *this;
//m_aBB.reserve(rhs.m_aBB.size()+m_aBB.size()); //maximum possible size, to push_back in place without allocation
velem vapp;
velem_cit i2=rhs.m_aBB.cbegin();
velem_it i1=m_aBB.begin();
bool req_sorting;
while(true){
//exit condition I
if( i2==rhs.m_aBB.end() ){
break;
}
//exit condition II
if(i1==m_aBB.end()){
//append blocks at the end
m_aBB.insert(m_aBB.end(),i2, rhs.m_aBB.end());
req_sorting=true;
break;
}
//update before either of the bitstrings has reached its end
if(i1->index==i2->index){
i1->bb|=i2->bb;
++i1, ++i2;
}else if(i1->index<i2->index){
++i1;
}else if(i2->index<i1->index){
vapp.push_back(*i2);
//m_aBB.push_back(*i2);
req_sorting=true;
++i2;
}
}
//always keep array sorted
m_aBB.insert(m_aBB.end(), vapp.begin(), vapp.end());
if(req_sorting)
sort();
return *this;
}
BitBoardS& BitBoardS::set_block (int first_block, const BitBoardS& rhs){
/////////////////////////////////
//
// REMARKS: experimental, currently only defined for bit strings of same size
//m_aBB.reserve(rhs.m_aBB.size()+m_aBB.size()); //maximum possible size, to push_back in place without allocation
velem vapp;
pair<bool, BitBoardS::velem_it> p1=find_block(first_block);
pair<bool, BitBoardS::velem_cit> p2=rhs.find_block(first_block);
bool req_sorting=false;
if(p2.second==rhs.m_aBB.end()){ //check in this order (captures rhs empty on init)
return *this;
}
if(p1.second==m_aBB.end()){
//append rhs at the end
m_aBB.insert(m_aBB.end(),p2.second, rhs.m_aBB.end());
req_sorting=true;
sort();
return *this;
}
while(true){
if(p2.second==rhs.m_aBB.end()){ //exit condition I
break;
}else if(p1.second==m_aBB.end()){ //exit condition II
m_aBB.insert(m_aBB.end(),p2.second, rhs.m_aBB.end());
req_sorting=true;
break;
}
//update before either of the bitstrings has reached its end
if(p1.second->index==p2.second->index){
p1.second->bb|=p2.second->bb;
++p1.second, ++p2.second;
}else if(p1.second->index<p2.second->index){
++p1.second;
}else if(p2.second->index<p1.second->index){
vapp.push_back(*p2.second);
//m_aBB.push_back(*p2.second);
req_sorting=true;
++p2.second;
}
}
//always keep array sorted
m_aBB.insert(m_aBB.end(), vapp.begin(), vapp.end());
if(req_sorting)
sort();
return *this;
}
BitBoardS& BitBoardS::set_block (int first_block, int last_block, const BitBoardS& rhs){
/////////////////////////////////
//
// REMARKS: experimental, currently only defined for bit strings of same size
//m_aBB.reserve(m_aBB.size()+ last_block-first_block+1); //maximum possible size, to push_back in place without allocation
velem vapp;
pair<bool, BitBoardS::velem_it> p1i=find_block(first_block);
pair<bool, BitBoardS::velem_cit> p2i=rhs.find_block(first_block);
pair<bool, BitBoardS::velem_cit> p2f=rhs.find_block(last_block);
bool req_sorting=false;
if(p2i.second==rhs.m_aBB.end()){ //check in this order (captures rhs empty on init)
return *this;
}
BitBoardS::velem_cit p2it_end=(p2f.first)? p2f.second+1 : p2f.second; //iterator to the last block +1 in the rhs
if(p1i.second==m_aBB.end()){
//append rhs at the end
m_aBB.insert(m_aBB.end(),p2i.second, p2it_end);
sort();
return *this;
}
do{
//update before either of the bitstrings has reached its end
if(p1i.second->index==p2i.second->index){
p1i.second->bb|=p2i.second->bb;
++p1i.second, ++p2i.second;
}else if(p1i.second->index<p2i.second->index){
++p1i.second;
}else if(p2i.second->index<p1i.second->index){
vapp.push_back(*p2i.second);
//m_aBB.push_back(*p2i.second); //*** iterators!!!
req_sorting=true;
++p2i.second;
}
//exit condition
if( p1i.second==m_aBB.end() || p1i.second->index>last_block ){
m_aBB.insert(m_aBB.end(),p2i.second, (p2f.first)? p2f.second+1 : p2f.second);
req_sorting=true;
break;
}else if(p2i.second == p2it_end ){ //exit condition II
break;
}
}while(true);
//always keep array sorted
m_aBB.insert(m_aBB.end(), vapp.begin(), vapp.end());
if(req_sorting)
sort();
return *this;
}
int BitBoardS::clear_bit (int low, int high){
///////////////////
// clears bits in the corresponding CLOSED range (effectively deletes bitblocks)
//
// PARTICULAR CASES: low = EMPTY_ELEM, high= EMPTY_ELEM mark bitstring bounds
//
// EXPERIMENTAL
int bbl=EMPTY_ELEM, bbh=EMPTY_ELEM;
pair<bool, BitBoardS::velem_it> pl;
pair<bool, BitBoardS::velem_it> ph;
////////////////////////
//special cases
if(high==EMPTY_ELEM && low==EMPTY_ELEM){
m_aBB.clear();
return 0;
}
if(high==EMPTY_ELEM){
bbl=WDIV(low);
pl=find_block(bbl);
if(pl.second==m_aBB.end()) return 0;
if(pl.first){ //lower block exists
pl.second->bb&=Tables::mask_right[low-WMUL(bbl)];
++pl.second;
}
//remaining
m_aBB.erase(pl.second, m_aBB.end());
return 0;
}else if(low==EMPTY_ELEM){
bbh=WDIV(high);
ph=find_block(bbh);
if(ph.first){ //upper block exists
ph.second->bb&=Tables::mask_left[high-WMUL(bbh)];
}
//remaining
m_aBB.erase(m_aBB.begin(), ph.second);
return 0;
}
////////////////
// general cases
//check consistency
if(low>high){
cerr<<"Error in set bit in range"<<endl;
return -1;
}
bbl=WDIV(low);
bbh=WDIV(high);
pl=find_block(bbl);
ph=find_block(bbh);
//tratamiento
if(pl.second!=m_aBB.end()){
//updates lower bitblock
if(pl.first){ //lower block exists
if(bbh==bbl){ //case update in the same bitblock
BITBOARD bb_low=pl.second->bb & Tables::mask_left[high-WMUL(bbh)];
BITBOARD bb_high=pl.second->bb &Tables::mask_right[low-WMUL(bbl)];
pl.second->bb=bb_low | bb_high;
return 0;
}
//update lower block
pl.second->bb&=Tables::mask_right[low-WMUL(bbl)];
++pl.second;
}
//updates upper bitblock
if(ph.first){ //lower block exists
if(bbh==bbl){ //case update in the same bitblock
BITBOARD bb_low=pl.second->bb & Tables::mask_left[high-WMUL(bbh)];
BITBOARD bb_high=pl.second->bb &Tables::mask_right[low-WMUL(bbl)];
pl.second->bb=bb_low | bb_high;
return 0;
}
//update lower block
ph.second->bb&=Tables::mask_left[high-WMUL(bbh)];
}
//remaining
m_aBB.erase(pl.second, ph.second);
}
return 0;
}
BitBoardS& BitBoardS::erase_bit (const BitBoardS& rhs ){
////////////////////
// removes 1-bits from current object (equialent to set_difference)
int i1=0, i2=0;
while(true){
//exit condition I
if(i1==m_aBB.size() || i2==rhs.m_aBB.size() ){ //size should be the same
return *this;
}
//update before either of the bitstrings has reached its end
if(m_aBB[i1].index==rhs.m_aBB[i2].index){
m_aBB[i1].bb&=~rhs.m_aBB[i2].bb;
i1++, i2++;
}else if(m_aBB[i1].index<rhs.m_aBB[i2].index){
i1++;
}else if(rhs.m_aBB[i2].index<m_aBB[i1].index){
i2++;
}
}
return *this;
}
BitBoardS& BitBoardS::operator &= (const BitBoardS& rhs){
///////////////////
// AND mask in place
int i1=0, i2=0;
while(true){
//exit conditions
if(i1==m_aBB.size() ){ //size should be the same
return *this;
}else if(i2==rhs.m_aBB.size()){ //fill with zeros from last block in rhs onwards
for(; i1<m_aBB.size(); i1++)
m_aBB[i1].bb=ZERO;
return *this;
}
//update before either of the bitstrings has reached its end
if(m_aBB[i1].index<rhs.m_aBB[i2].index){
m_aBB[i1].bb=0;
i1++;
}else if (rhs.m_aBB[i2].index<m_aBB[i1].index){
i2++;
}else{
m_aBB[i1].bb &= rhs.m_aBB[i2].bb;
i1++, i2++;
}
/*if(m_aBB[i1].index==rhs.m_aBB[i2].index){
m_aBB[i1].bb &= rhs.m_aBB[i2].bb;
i1++, i2++;
}else if(m_aBB[i1].index<rhs.m_aBB[i2].index){
m_aBB[i1].bb=0;
i1++;
}else if(rhs.m_aBB[i2].index<m_aBB[i1].index){
i2++;
}*/
}
return *this;
}
BitBoardS& BitBoardS::operator |= (const BitBoardS& rhs){
///////////////////
// OR mask in place
// date:10/02/2015
// last_update: 10/02/2015
int i1=0, i2=0;
while(true){
//exit conditions
if(i1==m_aBB.size() || i2==rhs.m_aBB.size() ){ //size should be the same
return *this;
}
//update before either of the bitstrings has reached its end
if(m_aBB[i1].index<rhs.m_aBB[i2].index){
i1++;
}else if(rhs.m_aBB[i2].index<m_aBB[i1].index){
i2++;
}else{
m_aBB[i1].bb |= rhs.m_aBB[i2].bb;
i1++, i2++;
}
}
return *this;
}
BITBOARD BitBoardS::find_bitboard (int block_index) const{
///////////////////
// returns the bitblock of the block index or EMPTY_ELEM if it does not exist
velem_cit it=lower_bound(m_aBB.begin(), m_aBB.end(), elem(block_index), elem_less());
if(it!=m_aBB.end()){
if(it->index==block_index){
return it->bb;
}
}
return EMPTY_ELEM;
}
pair<bool, int> BitBoardS::find_pos (int block_index) const{
////////////////
// returns first:true if block exists second:lower bound index in the collection or EMPTY_ELEM if no block exists above the index
pair<bool, int> res(false, EMPTY_ELEM);
velem_cit it=lower_bound(m_aBB.begin(), m_aBB.end(), elem(block_index), elem_less());
if(it!=m_aBB.end()){
res.second=it-m_aBB.begin();
if(it->index==block_index){
res.first=true;
}
}
return res;
}
pair<bool, BitBoardS::velem_it> BitBoardS::find_block (int block_index, bool is_lower_bound) {
pair<bool, BitBoardS::velem_it>res;
if(is_lower_bound)
res.second=lower_bound(m_aBB.begin(), m_aBB.end(), elem(block_index), elem_less());
else
res.second=upper_bound(m_aBB.begin(), m_aBB.end(), elem(block_index), elem_less());
res.first= (res.second!=m_aBB.end()) && (res.second->index==block_index);
return res;
}
pair<bool, BitBoardS::velem_cit> BitBoardS::find_block (int block_index, bool is_lower_bound) const {
pair<bool, BitBoardS::velem_cit>res;
if(is_lower_bound)
res.second=lower_bound(m_aBB.begin(), m_aBB.end(), elem(block_index), elem_less());
else
res.second=upper_bound(m_aBB.begin(), m_aBB.end(), elem(block_index), elem_less());
res.first= res.second!=m_aBB.end() && res.second->index==block_index;
return res;
}
///////////////////////////////
//
// Bit updates
//
//
////////////////////////////////
int BitBoardS::lsbn64() const{
/////////////////
// different implementations of lsbn depending on configuration
#ifdef DE_BRUIJN
for(int i=0; i<m_aBB.size(); i++){
if(m_aBB[i].bb)
#ifdef ISOLANI_LSB
return(Tables::indexDeBruijn64_ISOL[((m_aBB[i].bb & -m_aBB[i].bb) * DEBRUIJN_MN_64_ISOL/*magic num*/) >> DEBRUIJN_MN_64_SHIFT]+ WMUL(m_aBB[i].index));
#else
return(Tables::indexDeBruijn64_SEP[((m_aBB[i].bb^ (m_aBB[i].bb-1)) * DEBRUIJN_MN_64_SEP/*magic num*/) >> DEBRUIJN_MN_64_SHIFT]+ WMUL(m_aBB[i].index));
#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].bb;
if(val.b){
if(val.c[0]) return (Tables::lsba[0][val.c[0]]+WMUL(m_aBB[i].index));
if(val.c[1]) return (Tables::lsba[1][val.c[1]]+WMUL(m_aBB[i].index));
if(val.c[2]) return (Tables::lsba[2][val.c[2]]+WMUL(m_aBB[i].index));
if(val.c[3]) return (Tables::lsba[3][val.c[3]]+WMUL(m_aBB[i].index));
}
}
#endif
return EMPTY_ELEM;
}
int BitBoardS::msbn64() const{
///////////////////////
// Look up table implementation (best found so far)
union u {
U16 c[4];
BITBOARD b;
};
u val;
for(int i=m_aBB.size()-1; i>=0; i--){
val.b=m_aBB[i].bb;
if(val.b){
if(val.c[3]) return (Tables::msba[3][val.c[3]]+WMUL(m_aBB[i].index));
if(val.c[2]) return (Tables::msba[2][val.c[2]]+WMUL(m_aBB[i].index));
if(val.c[1]) return (Tables::msba[1][val.c[1]]+WMUL(m_aBB[i].index));
if(val.c[0]) return (Tables::msba[0][val.c[0]]+WMUL(m_aBB[i].index));
}
}
return EMPTY_ELEM; //should not reach here
}
int BitBoardS::next_bit(int nBit/* 0 based*/) const {
////////////////////////////
// RETURNS next bit from nBit in the bitstring (to be used in a bitscan loop)
// if nBit is FIRST_BITSCAN returns lsb
//
// NOTES:
// 1. A preliminay implementation. It becomes clear the necessity of caching the index of the vector at each iteration (*)
if(nBit==EMPTY_ELEM){
return lsbn64();
}
//look in the remaining bitblocks (needs caching last i visited)
int index=WDIV(nBit);
for(int i=0; i<m_aBB.size(); i++){
if(m_aBB[i].index<index) continue; //(*)
if(m_aBB[i].index==index){
int npos=BitBoard::lsb64_de_Bruijn(Tables::mask_left[WMOD(nBit) /*-WORD_SIZE*index*/] & m_aBB[i].bb);
if(npos!=EMPTY_ELEM) return (WMUL(index) + npos);
continue;
}
//new bitblock
if( m_aBB[i].bb){
return BitBoard::lsb64_de_Bruijn(m_aBB[i].bb) + WMUL(m_aBB[i].index);
}
}
return -1;
}
int BitBoardS::previous_bit(int nBit/* 0 bsed*/) const{
////////////////////////////
// Gets the previous bit to nBit.
// If nBits is FIRST_BITSCAN is a MSB
//
// NOTES:
// 1. A preliminary implementation. It becomes clear the necessity of caching the index of the vector at each iteration (*)
if(nBit==EMPTY_ELEM)
return msbn64();
//look in the remaining bitblocks
int index=WDIV(nBit);
for(int i=m_aBB.size()-1; i>=0; i--){
if(m_aBB[i].index>index) continue; //(*)
if(m_aBB[i].index==index){
int npos=BitBoard::msb64_lup(Tables::mask_right[WMOD(nBit) /*-WORD_SIZE*index*/] & m_aBB[i].bb);
if(npos!=EMPTY_ELEM) return (WMUL(index) + npos);
continue;
}
if(m_aBB[i].bb ){
return BitBoard::msb64_lup(m_aBB[i].bb) + WMUL(m_aBB[i].index);
}
}
return -1;
}
//////////////
//
// Operators
//
///////////////
BitBoardS& BitBoardS::operator = (const BitBoardS& bbs){
///////////////
// Deep copy of collection
if(this!=&bbs){
this->m_aBB=bbs.m_aBB;
this->m_MAXBB=bbs.m_MAXBB;
}
return *this;
}
BitBoardS& OR (const BitBoardS& lhs, const BitBoardS& rhs, BitBoardS& res){
///////////////////////////
// OR between sparse sets out of place (does not requires sorting)
//
// REMARKS: does not check for null information
int i1=0, i2=0;
res.erase_bit(); //experimental (and simplest solution)
while(true){
//exit condition I
if(i1==lhs.m_aBB.size()){
res.m_aBB.insert(res.m_aBB.end(), rhs.m_aBB.begin()+i2, rhs.m_aBB.end());
return res;
}else if(i2==rhs.m_aBB.size()){
res.m_aBB.insert(res.m_aBB.end(), lhs.m_aBB.begin()+i1, lhs.m_aBB.end());
return res;
}
//update before either of the bitstrings has reached its end
if(lhs.m_aBB[i1].index<rhs.m_aBB[i2].index){
BitBoardS::elem e(lhs.m_aBB[i1].index, lhs.m_aBB[i1].bb );
res.m_aBB.push_back(e);
++i1;
}else if(rhs.m_aBB[i2].index<lhs.m_aBB[i1].index){
BitBoardS::elem e(rhs.m_aBB[i2].index, rhs.m_aBB[i2].bb );
res.m_aBB.push_back(e);
++i2;
}else{
BitBoardS::elem e(lhs.m_aBB[i1].index, lhs.m_aBB[i1].bb | rhs.m_aBB[i2].bb);
res.m_aBB.push_back(e);
++i1, ++i2;
}
/* if(lhs.m_aBB[i1].index==rhs.m_aBB[i2].index){
BitBoardS::elem e(lhs.m_aBB[i1].index, lhs.m_aBB[i1].bb | rhs.m_aBB[i2].bb);
res.m_aBB.push_back(e);
++i1, ++i2;
}else if(lhs.m_aBB[i1].index<rhs.m_aBB[i2].index){
BitBoardS::elem e(lhs.m_aBB[i1].index, lhs.m_aBB[i1].bb );
res.m_aBB.push_back(e);
++i1;
}else if(rhs.m_aBB[i2].index<lhs.m_aBB[i1].index){
BitBoardS::elem e(rhs.m_aBB[i2].index, rhs.m_aBB[i2].bb );
res.m_aBB.push_back(e);
++i2;
}*/
}
return res;
}
////////////////////////////
////
//// I/O FILES
////
////////////////////////////
void BitBoardS::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 BitBoardS::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 BitBoardS::to_vector (std::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);
}
}