Repository navigation
Expand file tree
/
Copy pathTransTableL.cpp
More file actions
1841 lines (1485 loc) · 45.4 KB
/
Copy pathTransTableL.cpp
File metadata and controls
1841 lines (1485 loc) · 45.4 KB
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
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
DDS, a bridge double dummy solver.
Copyright (C) 2006-2014 by Bo Haglund /
2014-2018 by Bo Haglund & Soren Hein.
See LICENSE and README.
*/
/*
Explanation of full memory version:
There are some constants that only need to be calculated
once. In fact they are the same for all instances of the
object. In order to save time and memory, they share a
single memory.
Each 13-bit number, aggr, represents a possible set of cards
remaining in a suit. For example, 0x15a2 represents
A(1) QT(5) 97(a) 3(2).
TTlowestRank[aggr] gives the lowest relative rank that is in
play in aggr. The ace is 14, the deuce is 2. A void counts as
rank 15 ("not even the ace"). It would go horribly wrong
if this rank were chosen to be 0, as might seem intuitive.
This is not the same as lowestRank, the lowest absolute rank.
maskBytes[aggr][suit] is a set of 4 32-bit integers,
where suit is 0 ..3 (spades .. clubs). Each integer only
has 8 of its 32 bits set, but these 8 bits could be either
in the top byte (byte 0) or any of the others (bytes 1 ..3).
The bytes are abbreviated as B0 .. B3 below.
int 0 int 1 int 2 int 3
suit 0, spades B0 R0 B0 R1 B0 R2 B0 R3
suit 1, hearts B1 R0 B1 R1 B1 R2 B1 R3
suit 2, diamonds B2 R0 B2 R1 B2 R2 B2
suit 3, clubs B3 R0 B3 R1 B3 R2 B3
R0 .. R3 are explained now. The purpose of maskBytes is
to generate 32-bit masks for later use with actual suits.
As a card can be with either of 4 players, 2 bits are needed
to encode the position of a card. Therefore the masks also
need 2 bits per card, even though the 2 bits are identical.
In the table, R0 means the top byte (8 bits = 4 cards) of
a holding.
R0 AKQJ
R1 T987
R2 6543
R3 2
For example, if the ace is held by North, the king by South, the
queen and jack by West, then the top byte for that SUIT would be
00(North) 10(South) 11(West) 11(West)
The MASK for that holding would be 11 11 11 11, as all four
cards are in play.
If the jack were missing, because it had already been played,
then the suit would be 00 10 11 00 (a missing card is also
encoded as 00), and the mask would be 11 11 11 00.
Later on, when we have a specific set of spades .. clubs,
we want to check whether those cards are already in the
transposition table. As far as the necessary masks is
concerned, this is generated by an OR (|) of the four
32-bit integers in a column of the table above.
So the first column yields four bytes which are already
shifted in place, all corresponding to R0. The mask
corresponds to the AKQJ of the four suits in order.
It's not really AKQJ, but the four highest cards still in
play in that suit. So missing cards are always at the end
of the list.
*/
#include <iomanip>
#include <sstream>
#include <math.h>
#include "TransTableL.h"
#include "debug.h"
extern unsigned char cardRank[16];
extern char relRank[8192][15];
static bool _constantsSet = false;
static int TTlowestRank[8192];
static unsigned maskBytes[8192][DDS_SUITS][TT_BYTES];
static vector<string> players =
{
"North", "East", "South", "West"
};
TransTableL::TransTableL()
{
if (! _constantsSet)
{
_constantsSet = true;
TransTableL::SetConstants();
}
poolp = nullptr;
pagesDefault = NUM_PAGES_DEFAULT;
pagesMaximum = NUM_PAGES_MAXIMUM;
pagesCurrent = 0;
memState = FROM_POOL;
harvestTrick = FIRST_HARVEST_TRICK;
harvestHand = 0;
harvested.nextBlockNo = 0;
timestamp = 0;
pageStats.numResets = 0;
pageStats.numCallocs = 0;
pageStats.numFrees = 0;
pageStats.numHarvests = 0;
pageStats.lastCurrent = 0;
TTInUse = 0;
}
TransTableL::~TransTableL()
{
TransTableL::ReturnAllMemory();
}
void TransTableL::SetConstants()
{
unsigned int topBitRank = 1;
TTlowestRank[0] = 15; // Void
unsigned winMask[8192];
winMask[0] = 0;
for (unsigned ind = 1; ind < 8192; ind++)
{
if (ind >= (topBitRank + topBitRank)) /* Next top bit */
topBitRank <<= 1;
// winMask is a growing list of 11's. In the end it will
// have 26 bits, so 13 groups of two bits. It always
// consists of all 11's, then all 00's.
winMask[ind] = (winMask[ind ^ topBitRank] >> 2) | (3 << 24);
maskBytes[ind][0][0] = (winMask[ind] << 6) & 0xff000000;
maskBytes[ind][0][1] = (winMask[ind] << 14) & 0xff000000;
maskBytes[ind][0][2] = (winMask[ind] << 22) & 0xff000000;
maskBytes[ind][0][3] = (winMask[ind] << 30) & 0xff000000;
maskBytes[ind][1][0] = (winMask[ind] >> 2) & 0x00ff0000;
maskBytes[ind][1][1] = (winMask[ind] << 6) & 0x00ff0000;
maskBytes[ind][1][2] = (winMask[ind] << 14) & 0x00ff0000;
maskBytes[ind][1][3] = (winMask[ind] << 22) & 0x00ff0000;
maskBytes[ind][2][0] = (winMask[ind] >> 10) & 0x0000ff00;
maskBytes[ind][2][1] = (winMask[ind] >> 2) & 0x0000ff00;
maskBytes[ind][2][2] = (winMask[ind] << 6) & 0x0000ff00;
maskBytes[ind][2][3] = (winMask[ind] << 14) & 0x0000ff00;
maskBytes[ind][3][0] = (winMask[ind] >> 18) & 0x000000ff;
maskBytes[ind][3][1] = (winMask[ind] >> 10) & 0x000000ff;
maskBytes[ind][3][2] = (winMask[ind] >> 2) & 0x000000ff;
maskBytes[ind][3][3] = (winMask[ind] << 6) & 0x000000ff;
TTlowestRank[ind] = TTlowestRank[ind ^ topBitRank] - 1;
}
}
void TransTableL::Init(const int handLookup[][15])
{
// This is very similar to SetConstants, except that it
// happens with actual cards. It also makes sense to
// keep a record of aggrRanks for each suit. These are
// only used later for xorSet.
unsigned int topBitRank = 1;
unsigned int topBitNo = 2;
aggrType * ap;
for (int s = 0; s < DDS_SUITS; s++)
{
aggr[0].aggrRanks[s] = 0;
aggr[0].aggrBytes[s][0] = 0;
aggr[0].aggrBytes[s][1] = 0;
aggr[0].aggrBytes[s][2] = 0;
aggr[0].aggrBytes[s][3] = 0;
}
for (unsigned ind = 1; ind < 8192; ind++)
{
if (ind >= (topBitRank << 1))
{
/* Next top bit */
topBitRank <<= 1;
topBitNo++;
}
aggr[ind] = aggr[ind ^ topBitRank];
ap = &aggr[ind];
for (int s = 0; s < DDS_SUITS; s++)
{
ap->aggrRanks[s] = ap->aggrRanks[s] >> 2 |
static_cast<unsigned>(handLookup[s][topBitNo] << 24);
}
ap->aggrBytes[0][0] = (ap->aggrRanks[0] << 6) & 0xff000000;
ap->aggrBytes[0][1] = (ap->aggrRanks[0] << 14) & 0xff000000;
ap->aggrBytes[0][2] = (ap->aggrRanks[0] << 22) & 0xff000000;
ap->aggrBytes[0][3] = (ap->aggrRanks[0] << 30) & 0xff000000;
ap->aggrBytes[1][0] = (ap->aggrRanks[1] >> 2) & 0x00ff0000;
ap->aggrBytes[1][1] = (ap->aggrRanks[1] << 6) & 0x00ff0000;
ap->aggrBytes[1][2] = (ap->aggrRanks[1] << 14) & 0x00ff0000;
ap->aggrBytes[1][3] = (ap->aggrRanks[1] << 22) & 0x00ff0000;
ap->aggrBytes[2][0] = (ap->aggrRanks[2] >> 10) & 0x0000ff00;
ap->aggrBytes[2][1] = (ap->aggrRanks[2] >> 2) & 0x0000ff00;
ap->aggrBytes[2][2] = (ap->aggrRanks[2] << 6) & 0x0000ff00;
ap->aggrBytes[2][3] = (ap->aggrRanks[2] << 14) & 0x0000ff00;
ap->aggrBytes[3][0] = (ap->aggrRanks[3] >> 18) & 0x000000ff;
ap->aggrBytes[3][1] = (ap->aggrRanks[3] >> 10) & 0x000000ff;
ap->aggrBytes[3][2] = (ap->aggrRanks[3] >> 2) & 0x000000ff;
ap->aggrBytes[3][3] = (ap->aggrRanks[3] << 6) & 0x000000ff;
}
}
void TransTableL::SetMemoryDefault(int megabytes)
{
double blockMem = BLOCKS_PER_PAGE * sizeof(winBlockType) /
static_cast<double>(1024.);
pagesDefault = static_cast<int>((1024 * megabytes) / blockMem);
}
void TransTableL::SetMemoryMaximum(int megabytes)
{
double blockMem = BLOCKS_PER_PAGE * sizeof(winBlockType) /
static_cast<double>(1024.);
pagesMaximum = static_cast<int>((1024 * megabytes) / blockMem);
}
/////////////////////////////////////////////////////////////
// //
// Full memory TT functions. //
// //
/////////////////////////////////////////////////////////////
void TransTableL::MakeTT()
{
if (! TTInUse)
{
TTInUse = 1;
for (int t = 0; t < TT_TRICKS; t++)
{
for (int h = 0; h < DDS_HANDS; h++)
{
TTroot[t][h] = static_cast<distHashType *>
(malloc(256 * sizeof(distHashType)));
if (TTroot[t][h] == nullptr)
exit(1);
}
}
}
TransTableL::InitTT();
}
void TransTableL::InitTT()
{
for (int c = 0; c < TT_TRICKS; c++)
{
for (int h = 0; h < DDS_HANDS; h++)
{
for (int i = 0; i < 256; i++)
{
TTroot[c][h][i].nextNo = 0;
TTroot[c][h][i].nextWriteNo = 0;
}
lastBlockSeen[c][h] = nullptr;
}
}
}
void TransTableL::ReleaseTT()
{
if (! TTInUse)
return;
TTInUse = 0;
for (int t = 0; t < TT_TRICKS; t++)
{
for (int h = 0; h < DDS_HANDS; h++)
{
if (TTroot[t][h] == nullptr)
continue;
free(TTroot[t][h]);
}
}
}
void TransTableL::ResetMemory(const TTresetReason reason)
{
UNUSED(reason);
if (poolp == nullptr)
return;
pageStats.numResets++;
pageStats.numCallocs += pagesCurrent - pageStats.lastCurrent;
pageStats.lastCurrent = pagesCurrent;
while (pagesCurrent > pagesDefault)
{
free(poolp->list);
poolp = poolp->prev;
free(poolp->next);
poolp->next = nullptr;
pagesCurrent--;
}
pageStats.numFrees += pageStats.lastCurrent - pagesCurrent;
pageStats.lastCurrent = pagesCurrent;
while (poolp->prev)
poolp = poolp->prev;
poolp->nextBlockNo = 0;
nextBlockp = poolp->list;
TransTableL::InitTT();
timestamp = 0;
memState = FROM_POOL;
return;
}
void TransTableL::ReturnAllMemory()
{
poolType * tmp;
if (poolp)
{
while (poolp->next)
poolp = poolp->next;
while (poolp)
{
free(poolp->list);
tmp = poolp;
poolp = poolp->prev;
free(tmp);
}
}
pagesCurrent = 0;
pageStats.numResets = 0;
pageStats.numCallocs = 0;
pageStats.numFrees = 0;
pageStats.numHarvests = 0;
pageStats.lastCurrent = 0;
TransTableL::ReleaseTT();
return;
}
int TransTableL::BlocksInUse() const
{
poolType * pp = poolp;
int count = 0;
do
{
count += pp->nextBlockNo;
pp = pp->prev;
}
while (pp);
return count;
}
double TransTableL::MemoryInUse() const
{
int blockMem = BLOCKS_PER_PAGE * pagesCurrent *
static_cast<int>(sizeof(winBlockType));
int aggrMem = 8192 * static_cast<int>(sizeof(aggrType));
int rootMem = TT_TRICKS * DDS_HANDS * 256 *
static_cast<int>(sizeof(distHashType));
return (blockMem + aggrMem + rootMem) / static_cast<double>(1024.);
}
TransTableL::winBlockType * TransTableL::GetNextCardBlock()
{
/*
Spaghetti code. The basic idea is that there is a pool of
pages. When a page runs out, we get a next pool. But we're
only allowed a certain maximum number, and calloc might also
fail before then. We have a default number of pages that
we don't give back voluntarily once we have acquired them,
but we give back anything more than that at the end of each
hand. If this overall mechanism fails, then we try to harvest
old entries scattered throughout the TT memory. If we get
enough for a "page", then we use that single page, and if
that runs out later, we try to harvest some more, starting
where we left off harvesting last time. If the harvesting also
fails, then we reset whatever TT memory we do have, and we
continue with that.
*/
if (poolp == nullptr)
{
// Have to be able to get at least one pool.
poolp = static_cast<poolType *>(calloc(1, sizeof(poolType)));
if (poolp == nullptr)
exit(1);
poolp->list = static_cast<winBlockType *>
(malloc(BLOCKS_PER_PAGE * sizeof(winBlockType)));
if (! poolp->list)
exit(1);
poolp->next = nullptr;
poolp->prev = nullptr;
poolp->nextBlockNo = 1;
nextBlockp = poolp->list;
pagesCurrent++;
return nextBlockp++;
}
else if (memState == FROM_HARVEST)
{
// Not allowed to get more memory, so reuse old one.
int n = harvested.nextBlockNo;
if (n == BLOCKS_PER_PAGE)
{
if (! TransTableL::Harvest())
{
TransTableL::ResetMemory(TT_RESET_UNKNOWN);
poolp->nextBlockNo++;
return nextBlockp++;
}
n = 0;
}
harvested.nextBlockNo++;
return harvested.list[n];
}
else if (poolp->nextBlockNo == BLOCKS_PER_PAGE)
{
if (poolp->next)
{
// Reuse a dormant block that has not been freed.
poolp = poolp->next;
poolp->nextBlockNo = 1;
nextBlockp = poolp->list;
return nextBlockp++;
}
else if (pagesCurrent == pagesMaximum)
{
// Have to try to reclaim memory.
if (! TransTableL::Harvest())
{
TransTableL::ResetMemory(TT_RESET_UNKNOWN);
poolp->nextBlockNo++;
return nextBlockp++;
}
memState = FROM_HARVEST;
harvested.nextBlockNo++;
return harvested.list[0];
}
else
{
// Make a new pool.
poolType * newpoolp = static_cast<poolType *>
(calloc(1, sizeof(poolType)));
if (newpoolp == nullptr)
{
// Unexpected, but try harvesting before we give up
// and start over.
if (! TransTableL::Harvest())
{
TransTableL::ResetMemory(TT_RESET_UNKNOWN);
poolp->nextBlockNo++;
return nextBlockp++;
}
memState = FROM_HARVEST;
harvested.nextBlockNo++;
return harvested.list[0];
}
newpoolp->list = static_cast<winBlockType *>
(malloc(BLOCKS_PER_PAGE * sizeof(winBlockType)));
if (! newpoolp->list)
{
if (! TransTableL::Harvest())
{
TransTableL::ResetMemory(TT_RESET_UNKNOWN);
poolp->nextBlockNo++;
return nextBlockp++;
}
memState = FROM_HARVEST;
harvested.nextBlockNo++;
return harvested.list[0];
}
newpoolp->nextBlockNo = 1;
newpoolp->next = nullptr;
newpoolp->prev = poolp;
poolp->next = newpoolp;
poolp = newpoolp;
nextBlockp = newpoolp->list;
pagesCurrent++;
return nextBlockp++;
}
}
poolp->nextBlockNo++;
return nextBlockp++;
}
bool TransTableL::Harvest()
{
distHashType * rootptr = TTroot[harvestTrick][harvestHand];
distHashType * ptr;
winBlockType * bp;
int trick = harvestTrick;
int hand = harvestHand;
int hash, suit, hno = 0;
while (1)
{
for (hash = 0; hash < 256; hash++)
{
ptr = &rootptr[hash];
for (suit = ptr->nextNo - 1; suit >= 0; suit--)
{
bp = ptr->list[suit].posBlock;
if (timestamp - bp->timestampRead > HARVEST_AGE)
{
bp->nextMatchNo = 0;
bp->nextWriteNo = 0;
bp->timestampRead = timestamp;
harvested.list[hno] = bp;
// Swap the last element down.
if (suit != ptr->nextNo - 1)
ptr->list[suit] = ptr->list[ ptr->nextNo - 1 ];
ptr->nextNo--;
ptr->nextWriteNo = ptr->nextNo;
if (++hno == BLOCKS_PER_PAGE)
{
if (++harvestHand >= DDS_HANDS)
{
// Skip rest of this [trick][hand] for simplicity.
harvestHand = 0;
if (--harvestTrick < 0)
harvestTrick = FIRST_HARVEST_TRICK;
}
harvested.nextBlockNo = 0;
pageStats.numHarvests++;
return true;
}
}
}
}
if (++harvestHand >= DDS_HANDS)
{
harvestHand = 0;
if (--harvestTrick < 0)
harvestTrick = FIRST_HARVEST_TRICK;
}
if (harvestTrick == trick && harvestHand == hand)
return false;
rootptr = TTroot[harvestTrick][harvestHand];
}
}
int TransTableL::hash8(const int handDist[]) const
{
/*
handDist is an array of hand distributions, North .. West.
Each entry is a 12-bit number with 3 groups of 4 bits.
Each group is the binary representation of the number of
cards held in that suit. The suits are in order spades,
hearts, diamonds. Clubs can be neglected, as the total
number of cards in a hand is given by the trick number.
For example, if handDist[1] equals 0x0433, then East holds
4 spades, 3 hearts, 3 diamonds and the rest in clubs.
If this is after the second trick, there are 11 cards, so
East must hold 1 club.
The hash function turns all 4 hand distributions into a
single 8-bit number. The numbers should be spread as
evenly as possible across the 256 possibilities. I've not
done extensive research into finding the best hash function,
but this one seems OK. It uses a small prime, 5, and its
powers. The shift at the end is in order to get some use
out of the bits above the first 8 ones.
*/
int h =
(handDist[0] ^
((handDist[1] * 5) ) ^
((handDist[2] * 25) ) ^
((handDist[3] * 125) ) );
return (h ^ (h >> 5)) & 0xff;
}
nodeCardsType * TransTableL::Lookup(
const int tricks,
const int hand,
const unsigned short aggrTarget[],
const int handDist[],
const int limit,
bool& lowerFlag)
{
// First look up distribution.
long long suitLengths =
(static_cast<long long>(handDist[0]) << 36) |
(static_cast<long long>(handDist[1]) << 24) |
(static_cast<long long>(handDist[2]) << 12) |
(static_cast<long long>(handDist[3]) );
int hashkey = hash8(handDist);
bool empty;
lastBlockSeen[tricks][hand] =
LookupSuit(&TTroot[tricks][hand][hashkey], suitLengths, empty);
if (empty)
return nullptr;
// If that worked, look up cards.
unsigned * ab0 = aggr[ aggrTarget[0] ].aggrBytes[0];
unsigned * ab1 = aggr[ aggrTarget[1] ].aggrBytes[1];
unsigned * ab2 = aggr[ aggrTarget[2] ].aggrBytes[2];
unsigned * ab3 = aggr[ aggrTarget[3] ].aggrBytes[3];
winMatchType TTentry;
TTentry.topSet1 = ab0[0] | ab1[0] | ab2[0] | ab3[0];
TTentry.topSet2 = ab0[1] | ab1[1] | ab2[1] | ab3[1];
TTentry.topSet3 = ab0[2] | ab1[2] | ab2[2] | ab3[2];
TTentry.topSet4 = ab0[3] | ab1[3] | ab2[3] | ab3[3];
return TransTableL::LookupCards(TTentry,
lastBlockSeen[tricks][hand], limit, lowerFlag);
}
TransTableL::winBlockType * TransTableL::LookupSuit(
distHashType * dp,
const long long key,
bool& empty)
{
/*
Always returns a valid winBlockType.
If empty == true, there was no match, so there is
no point in looking for a card match.
If empty == false, there were entries already.
*/
int n = dp->nextNo;
for (int i = 0; i < n; i++)
{
if (dp->list[i].key == key)
{
empty = false;
return dp->list[i].posBlock;
}
}
empty = true;
int m;
if (n == DISTS_PER_ENTRY)
{
// No room for new exact suits at this hash position.
// Have to reuse an existing posBlock.
if (dp->nextWriteNo == DISTS_PER_ENTRY)
{
m = 0;
dp->nextWriteNo = 1;
}
else
m = dp->nextWriteNo++;
}
else
{
// Didn't find an exact match, but there is still room.
// The following looks a bit odd because it is possible that
// GetNextCardBlock wipes out the whole memory, so we
// have to use the up-to-date location, not m from above.
winBlockType * bp = GetNextCardBlock();
m = dp->nextWriteNo++;
dp->list[m].posBlock = bp;
dp->list[m].posBlock->timestampRead = timestamp;
dp->nextNo++;
}
// As long as the secondary Lookup loop in ABsearch exists,
// it will cause spurious extra blocks to be created here
// which are not useful, because nothing is ever Add'ed.
// This is not a memory leak, as the memory is properly freed,
// but it is also a small waste of about 0.5%. I don't mind.
dp->list[m].key = key;
dp->list[m].posBlock->nextMatchNo = 0;
dp->list[m].posBlock->nextWriteNo = 0;
return dp->list[m].posBlock;
}
nodeCardsType * TransTableL::LookupCards(
const winMatchType& search,
winBlockType * bp,
const int limit,
bool& lowerFlag)
{
const int n = bp->nextWriteNo - 1;
winMatchType * wp = &bp->list[n];
// It may be a bit silly to duplicate the code like this.
// It could be combined to one loop with a slight overhead.
for (int i = n; i >= 0; i--, wp--)
{
if ((wp->topSet1 ^ search.topSet1) & wp->topMask1)
continue;
if (wp->lastMaskNo != 1)
{
if ((wp->topSet2 ^ search.topSet2) & wp->topMask2)
continue;
if (wp->lastMaskNo != 2)
{
if ((wp->topSet3 ^ search.topSet3) & wp->topMask3)
continue;
}
}
// Check bounds.
nodeCardsType * nodep = &wp->first;
if (nodep->lbound > limit)
{
bp->timestampRead = ++timestamp;
lowerFlag = true;
return nodep;
}
else if (nodep->ubound <= limit)
{
bp->timestampRead = ++timestamp;
lowerFlag = false;
return nodep;
}
}
const int n2 = bp->nextMatchNo - 1;
wp = &bp->list[n2];
for (int i = n2; i > n; i--, wp--)
{
if ((wp->topSet1 ^ search.topSet1) & wp->topMask1)
continue;
if (wp->lastMaskNo != 1)
{
if ((wp->topSet2 ^ search.topSet2) & wp->topMask2)
continue;
if (wp->lastMaskNo != 2)
{
if ((wp->topSet3 ^ search.topSet3) & wp->topMask3)
continue;
}
}
nodeCardsType * nodep = &wp->first;
if (nodep->lbound > limit)
{
lowerFlag = true;
bp->timestampRead = ++timestamp;
return nodep;
}
else if (nodep->ubound <= limit)
{
lowerFlag = false;
bp->timestampRead = ++timestamp;
return nodep;
}
}
return nullptr;
}
void TransTableL::CreateOrUpdate(
winBlockType * bp,
const winMatchType& search,
const bool flag)
{
// Either updates an existing SOP or creates a new one.
// A new one is created at the end of the bp list if this
// is not already full, or the oldest one in the list is
// overwritten.
winMatchType * wp = bp->list;
int n = bp->nextMatchNo;
for (int i = 0; i < n; i++, wp++)
{
if (wp->xorSet != search.xorSet ) continue;
if (wp->maskIndex != search.maskIndex) continue;
if (wp->topSet1 != search.topSet1 ) continue;
if (wp->topSet2 != search.topSet2 ) continue;
if (wp->topSet3 != search.topSet3 ) continue;
nodeCardsType& node = wp->first;
if (search.first.lbound > node.lbound)
node.lbound = search.first.lbound;
if (search.first.ubound < node.ubound)
node.ubound = search.first.ubound;
node.bestMoveSuit = search.first.bestMoveSuit;
node.bestMoveRank = search.first.bestMoveRank;
return;
}
if (n == BLOCKS_PER_ENTRY)
{
if (bp->nextWriteNo >= BLOCKS_PER_ENTRY)
bp->nextWriteNo = 0;
}
else
bp->nextMatchNo++;
wp = &bp->list[ bp->nextWriteNo++ ];
*wp = search;
if (!flag)
{
wp->first.bestMoveSuit = 0;
wp->first.bestMoveRank = 0;
}
}
void TransTableL::Add(
const int tricks,
const int hand,
const unsigned short aggrTarget[],
const unsigned short ourWinRanks[],
const nodeCardsType& first,
const bool flag)
{
if (lastBlockSeen[tricks][hand] == nullptr)
{
// We have recently reset the entire memory, and we were
// in the middle of a recursion. So we'll just have to
// drop this entry that we were supposed to be adding.
return;
}
unsigned * ab[DDS_SUITS];
unsigned * mb[DDS_SUITS];
char low[DDS_SUITS];
unsigned short int ag;
int w;
winMatchType TTentry;
// Inefficient, as it also copies leastWin.
// In fact I'm not quite happy with the treatment of
// leastWin in general.
TTentry.first = first;
TTentry.xorSet = 0;
for (int ss = 0; ss < DDS_SUITS; ss++)
{
w = static_cast<int>(ourWinRanks[ss]);
if (w == 0)
{
ab[ss] = aggr[0].aggrBytes[ss];
mb[ss] = maskBytes[0][ss];
low[ss] = 15;
TTentry.first.leastWin[ss] = 0;
}
else
{
w = w & (-w); /* Only lowest win */
ag = static_cast<unsigned short>(aggrTarget[ss] & (-w));
ab[ss] = aggr[ag].aggrBytes[ss];
mb[ss] = maskBytes[ag][ss];
low[ss] = static_cast<char>(TTlowestRank[ag]);
TTentry.first.leastWin[ss] = 15 - low[ss];
TTentry.xorSet ^= aggr[ag].aggrRanks[ss];
}
}
// It's a bit annoying that we may be regenerating these.
// But winRanks can cause them to change after lookup().
TTentry.topSet1 = ab[0][0] | ab[1][0] | ab[2][0] | ab[3][0];
TTentry.topSet2 = ab[0][1] | ab[1][1] | ab[2][1] | ab[3][1];
TTentry.topSet3 = ab[0][2] | ab[1][2] | ab[2][2] | ab[3][2];
TTentry.topSet4 = ab[0][3] | ab[1][3] | ab[2][3] | ab[3][3];
TTentry.topMask1 = mb[0][0] | mb[1][0] | mb[2][0] | mb[3][0];
TTentry.topMask2 = mb[0][1] | mb[1][1] | mb[2][1] | mb[3][1];
TTentry.topMask3 = mb[0][2] | mb[1][2] | mb[2][2] | mb[3][2];
TTentry.topMask4 = mb[0][3] | mb[1][3] | mb[2][3] | mb[3][3];
TTentry.maskIndex =
(low[0] << 12) | (low[1] << 8) | (low[2] << 4) | low[3];
if (TTentry.topMask2 == 0)
TTentry.lastMaskNo = 1;
else if (TTentry.topMask3 == 0)
TTentry.lastMaskNo = 2;
else if (TTentry.topMask4 == 0)
TTentry.lastMaskNo = 3;
else
TTentry.lastMaskNo = 4;
TransTableL::CreateOrUpdate(lastBlockSeen[tricks][hand],
TTentry, flag);
}
void TransTableL::PrintMatch(
ofstream& fout,
const winMatchType& wp,
const unsigned char lengths[DDS_HANDS][DDS_SUITS]) const
{