dds-bridge-sys 2.1.1

Generated bindings to DDS, the double dummy solver for bridge
Documentation
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
/*
   DDS, a bridge double dummy solver.

   Copyright (C) 2006-2014 by Bo Haglund /
   2014-2018 by Bo Haglund & Soren Hein.

   See LICENSE and README.
*/


#include <iostream>
#include <iomanip>
#include <sstream>
#include <math.h>

#include "Scheduler.h"


Scheduler::Scheduler()
{
  numThreads = 0;
  numHands = 0;

  Scheduler::InitHighCards();

#ifdef DDS_SCHEDULER
  Scheduler::InitTimes();
  for (int i = 0; i < 10000; i++)
  {
    timeHist[i] = 0;
    timeHistNT[i] = 0;
    timeHistSuit[i] = 0;
  }
#endif

  Scheduler::RegisterThreads(1);
}


void Scheduler::InitHighCards()
{
  // highCards[i] is a point value of a given suit holding i.
  // This can be HCP, for instance. Currently it is close to
  // 6 - 4 - 2 - 1 - 0.5 for A-K-Q-J-T, but with 6.5 for the ace
  // in order to make the sum come out to 28, an even number, so
  // that the average number is an integer.

  highCards.resize(1 << 13);
  const unsigned pA = 1 << 12;
  const unsigned pK = 1 << 11;
  const unsigned pQ = 1 << 10;
  const unsigned pJ = 1 << 9;
  const unsigned pT = 1 << 8;

  for (unsigned suit = 0; suit < (1 << 13); suit++)
  {
    int j = 0;
    if (suit & pA) j += 13;
    if (suit & pK) j += 8;
    if (suit & pQ) j += 4;
    if (suit & pJ) j += 2;
    if (suit & pT) j += 1;
    highCards[suit] = j;
  }
}


#ifdef DDS_SCHEDULER
void Scheduler::InitTimes()
{
  timeStrain.Init("Suit/NT", 2);
  timeRepeat.Init("Repeat number", 16);
  timeDepth.Init("Trace depth", 60);
  timeStrength.Init("Evenness", 60);
  timeFanout.Init("Fanout", 100);
  timeThread.Init("Threads", numThreads);

  timeGroupActualStrain.Init("Group actual suit/NT", 2);
  timeGroupPredStrain.Init("Group predicted suit/NT", 2);
  timeGroupDiffStrain.Init("Group diff suit/NT", 2);

  blockMax = 0;
  timeBlock = 0;
}
#endif


Scheduler::~Scheduler()
{
}


void Scheduler::Reset()
{
  for (int b = 0; b < MAXNOOFBOARDS; b++)
    hands[b].next = -1;

  numGroups = 0;
  extraGroups = 0;

  // One extra for NT, one extra for splitting collisions.
  for (int strain = 0; strain < DDS_SUITS + 2; strain++)
    for (int key = 0; key < HASH_MAX; key++)
      list[strain][key].first = -1;

  for (unsigned t = 0; t < static_cast<unsigned>(numThreads); t++)
  {
    threadGroup[t] = -1;
    threadCurrGroup[t] = -1;
  }

  currGroup = -1;
}


void Scheduler::RegisterThreads(
  const int n)
{
  if (n == numThreads)
    return;
  numThreads = n;

  const unsigned nu = static_cast<unsigned>(n);
  threadGroup.resize(nu);
  threadCurrGroup.resize(nu);
  threadToHand.resize(nu);

#ifdef DDS_SCHEDULER
  timeThread.Init("Threads", numThreads);
  timersThread.resize(numThreads);
#endif
}


void Scheduler::RegisterRun(
  const enum RunMode mode,
  const boards& bds,
  const playTracesBin& pl)
{
  for (int b = 0; b < bds.noOfBoards; b++)
    hands[b].depth = pl.plays[b].number;
  
  Scheduler::RegisterRun(mode, bds);
}


void Scheduler::RegisterRun(
  const enum RunMode mode,
  const boards& bds)
{
  Scheduler::Reset();

  numHands = bds.noOfBoards;

  // First split the hands according to strain and hash key.
  // This will lead to a few random collisions as well.

  Scheduler::MakeGroups(bds);

  // Then check whether groups with at least two elements are
  // homogeneous or whether they need to be split.

  Scheduler::FinetuneGroups();

  Scheduler::SortHands(mode);
}


void Scheduler::SortHands(const enum RunMode mode)
{
  // Make predictions per group.

  if (mode == DDS_RUN_SOLVE)
    Scheduler::SortSolve();
  else if (mode == DDS_RUN_CALC)
    Scheduler::SortCalc();
  else if (mode == DDS_RUN_TRACE)
    Scheduler::SortTrace();
}


void Scheduler::MakeGroups(const boards& bds)
{
  deal const * dl;
  listType * lp;

  for (int b = 0; b < numHands; b++)
  {
    dl = &bds.deals[b];

    int strain = dl->trump;

    unsigned dlXor =
      dl->remainCards[0][0] ^
      dl->remainCards[1][1] ^
      dl->remainCards[2][2] ^
      dl->remainCards[3][3];

    int key = static_cast<int>(((dlXor >> 2) ^ (dlXor >> 6)) & 0x7f);

    hands[b].spareKey = static_cast<int>(
                          (dl->remainCards[1][0] << 17) ^
                          (dl->remainCards[2][1] << 11) ^
                          (dl->remainCards[3][2] << 5) ^
                          (dl->remainCards[0][3] >> 2));

    for (int h = 0; h < DDS_HANDS; h++)
      for (int s = 0; s < DDS_SUITS; s++)
        hands[b].remainCards[h][s] = dl->remainCards[h][s];

    hands[b].NTflag = (strain == 4 ? 1 : 0);
    hands[b].first = dl->first;
    hands[b].strain = strain;
    hands[b].fanout = Scheduler::Fanout(* dl);
    // hands[b].strength = Scheduler::Strength(* dl);

    lp = &list[strain][key];

    if (lp->first == -1)
    {
      lp->first = b;
      lp->last = b;
      lp->length = 1;

      group[numGroups].strain = strain;
      group[numGroups].hash = key;
      numGroups++;
    }
    else
    {
      int l = lp->last;
      hands[l].next = b;

      lp->last = b;
      lp->length++;
    }
  }
}


void Scheduler::FinetuneGroups()
{
  listType * lp;
  int strain, key, b1, b2;
  int numGroupsOrig = numGroups;

  for (int g = 0; g < numGroupsOrig; g++)
  {
    strain = group[g].strain;
    key = group[g].hash;

    lp = &list[strain][key];

    if (lp->length == 1)
      continue;

    else if (lp->length == 2)
    {
      // This happens quite often, so worth optimizing.

      b1 = lp->first;
      b2 = hands[lp->first].next;

      bool match = false;
      if (hands[b1].spareKey == hands[b2].spareKey)
      {
        // It is now extremely likely that it is a repeat hand,
        // but we have to be sure.
        match = true;
        for (int h = 0; h < DDS_HANDS && match; h++)
          for (int s = 0; s < DDS_SUITS && match; s++)
            if (hands[b1].remainCards[h][s] != hands[b2].remainCards[h][s])
              match = false;
      }

      if (match)
        continue;

      // Leave the first hand in place.
      hands[lp->first].next = -1;
      lp->last = lp->first;
      lp->length = 1;

      // Move the second hand to the special list.
      lp = &list[5][extraGroups];

      lp->first = b2;
      lp->last = b2;
      lp->length = 1;

      group[numGroups].strain = 5;
      group[numGroups].hash = extraGroups;

      numGroups++;
      extraGroups++;
    }

    else
    {
      // This is the general case. The comparison is not quite
      // as thorough here, but it's better than above and it uses
      // a different hand.

      sortType st;
      sortLen = lp->length;
      int index = lp->first;

      for (int i = 0; i < sortLen; i++)
      {
        sortList[i].number = index;
        sortList[i].value = hands[index].spareKey;

        index = hands[index].next;
      }

      // Sort the list heuristically by spareKey value.

      for (int i = 1; i < sortLen; i++)
      {
        st = sortList[i];
        int j = i;
        for (; j && st.value > sortList[j - 1].value; --j)
          sortList[j] = sortList[j - 1];
        sortList[j] = st;
      }

      // First group stays where it is, but shorter and rejigged.
      // From here on, hand comparisons are completely rigorous.
      // We might miss duplicates, but we won't let different
      // hands through as belonging to the same group.

      int l = 0;
      while (l < sortLen-1 && 
        Scheduler::SameHand(sortList[l].number, sortList[l+1].number))
        l++;

      if (l == sortLen-1)
        continue;

      lp->first = sortList[0].number;
      lp->last = sortList[l].number;
      lp->length = l + 1;

      index = lp->first;

      for (int i = 0; i < l; i++)
      {
        hands[index].next = sortList[i + 1].number;
        index = hands[index].next;
      }

      hands[index].next = -1;

      // The rest is moved to special groups.
      l++;

      while (l < sortLen)
      {
        if (Scheduler::SameHand(sortList[l].number, sortList[l-1].number))
        {
          // Same group
          int nOld = sortList[l - 1].number;
          int nNew = sortList[l].number;
          hands[nOld].next = nNew;
          hands[nNew].next = -1;

          lp->last = nNew;
          lp->length++;
        }
        else
        {
          // New group
          int n = sortList[l].number;
          hands[n].next = -1;

          lp = &list[5][extraGroups];
          lp->first = n;
          lp->last = n;
          lp->length = 1;

          group[numGroups].strain = 5;
          group[numGroups].hash = extraGroups;

          numGroups++;
          extraGroups++;
        }
        l++;
      }
    }
  }
}


bool Scheduler::SameHand(
  const int hno1,
  const int hno2) const
{
  for (int h = 0; h < DDS_HANDS; h++)
    for (int s = 0; s < DDS_SUITS; s++)
      if (hands[hno1].remainCards[h][s] != hands[hno2].remainCards[h][s])
        return false;

  return true;
}


// These are specific times from a 12-core PC. The hope is
// that they scale somewhat proportionally to other cases.
// The strength parameter is currently not used.

int SORT_SOLVE_TIMES[2][8] =
{
  { 284000,  91000, 37000, 23000, 17000, 15000, 13000, 4000 },
  { 388000, 140000, 60000, 40000, 30000, 23000, 18000, 6000 },
};

#define SORT_SOLVE_STRENGTH_CUTOFF 0

double SORT_SOLVE_STRENGTH[2][3] =
{
  { 1.525, 1.810, 0.0285 },
  { 1.585, 1.940, 0.0354 }
};

// Lower end of linear, upper end of linear, slope of linear,
// exponential start, coefficient.

double SORT_SOLVE_FANOUT[2][5] =
{
  { 30., 50., 0.07577, 1.515, 12. },
  { 30., 50., 0.08144, 1.629, 12. }
};

void Scheduler::SortSolve()
{
  listType * lp;
  handType * hp;
  int strain, key, index;

  for (int g = 0; g < numGroups; g++)
  {
    strain = group[g].strain;
    key = group[g].hash;
    lp = &list[strain][key];
    index = lp->first;
    hp = &hands[index];

    // Taking into account repeat times saves 1-2%.

    int repeatNo = 0;
    int firstPrev = -1;
    group[g].pred = 0;
    do
    {
      // Skip complete duplicates, as we won't solve them again.
      if (hands[index].first != firstPrev)
      {
        group[g].pred += SORT_SOLVE_TIMES[hp->NTflag][repeatNo];
        if (repeatNo < 7)
          repeatNo++;
        firstPrev = hands[index].first;
      }

      index = hands[index].next;
    }
    while (index != -1);

    // Taking into account fanout saves 4-6%.

    int fanout = hp->fanout;
    double * slist = SORT_SOLVE_FANOUT[hp->NTflag];
    double fanoutFactor;

    if (fanout < slist[0])
      fanoutFactor = 0.; // A bit extreme...
    else if (fanout < slist[1])
      fanoutFactor = slist[2] * (fanout - slist[0]);
    else
      fanoutFactor = slist[3] * exp( (fanout - slist[1]) / slist[4] );

    group[g].pred = static_cast<int>(
      (fanoutFactor * static_cast<double>(group[g].pred)));
  }

  // Sort groups using merge sort.
  groupType gp;
  for (int g = 0; g < numGroups; g++)
  {
    gp = group[g];
    int j = g;
    for (; j && gp.pred > group[j - 1].pred; --j)
      group[j] = group[j - 1];
    group[j] = gp;
  }
}


// For calc there is no repeat overhead ever, as this is always
// a direct copy.

// Lower end of linear, upper end of linear, slope of linear,
// exponential start, coefficient.

double SORT_CALC_FANOUT[2][5] =
{
  { 30., 50., 0.07812, 1.563, 13. },
  { 30., 50., 0.07739, 1.548, 12. }
};


void Scheduler::SortCalc()
{
  listType * lp;
  handType * hp;
  int strain, key, index;

  for (int g = 0; g < numGroups; g++)
  {
    strain = group[g].strain;
    key = group[g].hash;
    lp = &list[strain][key];
    index = lp->first;
    hp = &hands[index];

    // Taking into account repeat times saves 1-2%.

    group[g].pred = 272000;

    int fanout = hp->fanout;
    double * slist = SORT_CALC_FANOUT[hp->NTflag];
    double fanoutFactor;

    if (fanout < slist[0])
      fanoutFactor = 0.; // A bit extreme...
    else if (fanout < slist[1])
      fanoutFactor = slist[2] * (fanout - slist[0]);
    else
      fanoutFactor = slist[3] * exp( (fanout - slist[1]) / slist[4] );

    group[g].pred = static_cast<int>(
      (fanoutFactor * static_cast<double>(group[g].pred)));
  }

  // Sort groups using merge sort.
  groupType gp;
  for (int g = 0; g < numGroups; g++)
  {
    gp = group[g];
    int j = g;
    for (; j && gp.pred > group[j - 1].pred; --j)
      group[j] = group[j - 1];
    group[j] = gp;
  }
}


// These are specific times from a 12-core PC. The hope is
// that they scale somewhat proportionally to other cases.

int SORT_TRACE_TIMES[2][8] =
{
  { 157000, 47000, 26000, 18000, 16000, 14000, 10000,  6000 },
  { 205000, 87000, 45000, 36000, 32000, 28000, 24000, 20000 },
};

// Initial value for 0 and 1 cards
// Value up to 15 cards incl
// Slope between 16 and 48 incl
// Average for 49-52

double SORT_TRACE_DEPTH[2][4] =
{
  { 0.742, 0.411, 0.0414, 1.820 },
  { 0.669, 0.428, 0.0346, 1.606 }
};

// Lower end of linear, upper end of linear, slope of linear,
// exponential start, coefficient.

double SORT_TRACE_FANOUT[2][5] =
{
  { 30., 50., 0.07577, 1.515, 12. },
  { 30., 50., 0.08166, 1.633, 13. }
};

void Scheduler::SortTrace()
{
  listType * lp;
  handType * hp;
  int strain, key, index;

  for (int g = 0; g < numGroups; g++)
  {
    strain = group[g].strain;
    key = group[g].hash;
    lp = &list[strain][key];
    index = lp->first;
    hp = &hands[index];

    // Taking into account repeat times.

    int repeatNo = 0;
    int firstPrev = -1;
    group[g].pred = 0;
    do
    {
      // Skip complete duplicates, as we won't solve them again.
      if (hands[index].first != firstPrev)
      {
        group[g].pred += SORT_TRACE_TIMES[hp->NTflag][repeatNo];
        if (repeatNo < 7)
          repeatNo++;
        firstPrev = hands[index].first;
      }

      index = hands[index].next;
    }
    while (index != -1);

    double depthFactor;
    int depth = hp->depth;
    double * slist = SORT_TRACE_DEPTH[hp->NTflag];

    if (depth <= 1)
      depthFactor = slist[0];
    else if (depth <= 15)
      depthFactor = slist[1];
    else if (depth >= 49)
      depthFactor = slist[3];
    else
      depthFactor = slist[1] + (depth - 15) * slist[2];

    group[g].pred = static_cast<int>(
      (depthFactor * static_cast<double>(group[g].pred)));

    // Taking into account fanout.

    int fanout = hp->fanout;
    slist = SORT_TRACE_FANOUT[hp->NTflag];
    double fanoutFactor;

    if (fanout < slist[0])
      fanoutFactor = 0.; // A bit extreme...
    else if (fanout < slist[1])
      fanoutFactor = slist[2] * (fanout - slist[0]);
    else
      fanoutFactor = slist[3] * exp( (fanout - slist[1]) / slist[4] );

    group[g].pred = static_cast<int>(
      (fanoutFactor * static_cast<double>(group[g].pred)));
  }

  // Sort groups using merge sort.
  groupType gp;
  for (int g = 0; g < numGroups; g++)
  {
    gp = group[g];
    int j = g;
    for (; j && gp.pred > group[j - 1].pred; --j)
      group[j] = group[j - 1];
    group[j] = gp;
  }
}


int Scheduler::Strength(const deal& dl) const
{
  // If the strength in all suits is evenly split, then the
  // "strength" returned is close to 0. Maximum is 49.

  const unsigned sp = (dl.remainCards[0][0] | dl.remainCards[2][0]) >> 2;
  const unsigned he = (dl.remainCards[0][1] | dl.remainCards[2][1]) >> 2;
  const unsigned di = (dl.remainCards[0][2] | dl.remainCards[2][2]) >> 2;
  const unsigned cl = (dl.remainCards[0][3] | dl.remainCards[2][3]) >> 2;

  const int hsp = highCards[sp];
  const int hhe = highCards[he];
  const int hdi = highCards[di];
  const int hcl = highCards[cl];

  int dev = (hsp >= 14 ? hsp - 14 : 14 - hsp) +
    (hhe >= 14 ? hhe - 14 : 14 - hhe) +
    (hdi >= 14 ? hdi - 14 : 14 - hdi) +
    (hcl >= 14 ? hcl - 14 : 14 - hcl);

  if (dev >= 50) dev = 49;

  return dev;
}


int Scheduler::Fanout(const deal& dl) const
{
  // The fanout for a given suit and a given player is the number
  // of bit groups, so KT982 has 3 groups. In a given suit the
  // maximum number over all four players is 13.
  // A void counts as the sum of the other players' groups.

  int fanout = 0;
  int fanoutSuit, numVoids, c;

  for (int h = 0; h < DDS_HANDS; h++)
  {
    fanoutSuit = 0;
    numVoids = 0;
    for (int s = 0; s < DDS_SUITS; s++)
    {
      c = static_cast<int>(dl.remainCards[h][s] >> 2);
      fanoutSuit += groupData[c].lastGroup + 1;
      if (c == 0)
        numVoids++;
    }
    fanoutSuit += numVoids * fanoutSuit;
    fanout += fanoutSuit;
  }

  return fanout;
}


schedType Scheduler::GetNumber(const int thrId)
{
  const unsigned tu = static_cast<unsigned>(thrId);
  int g = threadGroup[tu];
  listType * lp;
  schedType st;

  if (g == -1)
  {
    // Find a new group

    if (currGroup >= numGroups - 1)
    {
      // Out of groups. Just an optimization not to touch the
      // shared variable unnecessarily.
      st.number = -1;
      return st;
    }

    // Atomic.
    g = ++currGroup;

    if (g >= numGroups)
    {
      // Out of groups. currGroup could have changed in the
      // meantime in another thread, so test again.

      st.number = -1;
      return st;
    }

    // A bit inelegant to duplicate this, but seems better than
    // the alternative, as threadGroup must get set to -1 in some
    // cases.
    threadGroup[tu] = g;
    threadCurrGroup[tu] = g;
    group[g].repeatNo = 0;
    group[g].actual = 0;
  }

  // Continue with existing or new group

  int strain = group[g].strain;
  int key = group[g].hash;

  lp = &list[strain][key];
  st.number = lp->first;
  lp->first = hands[lp->first].next;

  if (group[g].repeatNo == 0)
  {
    group[g].head = st.number;
    st.repeatOf = -1;

    // Only first-solve suited hands for statistics right now.
    hands[st.number].selectFlag =
      (hands[st.number].strain == 4 ? 1 : 0);
  }
  else
  {
    st.repeatOf = group[g].head;
    //hands[st.number].selectFlag = 0;

    if (hands[st.number].first == hands[st.repeatOf].first)
      hands[st.number].selectFlag = 0;
    else if (hands[st.number].strain == 4)
      hands[st.number].selectFlag = 1;
    else
      hands[st.number].selectFlag = 0;
  }

  hands[st.number].repeatNo = group[g].repeatNo++;

  threadToHand[tu] = st.number;

  if (lp->first == -1)
    threadGroup[tu] = -1;

  return st;
}


int Scheduler::NumGroups() const
{
  return numGroups;
}


#ifdef DDS_SCHEDULER
void Scheduler::StartThreadTimer(const int thrId)
{
  timersThread[thrId].Reset();
  timersThread[thrId].Start();
}


void Scheduler::EndThreadTimer(const int thrId)
{
  timersThread[thrId].End();
  int timeUser = timersThread[thrId].UserTime();

  hands[ threadToHand[thrId] ].time = timeUser;
  hands[ threadToHand[thrId] ].thread = thrId;

  group[ threadCurrGroup[thrId] ].actual += timeUser;
}


void Scheduler::StartBlockTimer()
{
  timerBlock.Reset();
  timerBlock.Start();
}


void Scheduler::EndBlockTimer()
{
  timerBlock.End();
  const int timeUserBlock = timerBlock.UserTime();

  handType * hp;
  for (int b = 0; b < numHands; b++)
  {
    hp = &hands[b];
    int timeUser = hp->time;
    double timesq = (double) timeUser * (double) timeUser;

    if (hp->selectFlag)
    {
      TimeStat ts;
      ts.Set(timeUser, timesq);

      timeStrain.Add(hp->NTflag, ts);
      timeRepeat.Add(hp->repeatNo, ts);
      timeDepth.Add(hp->depth, ts);
      timeStrength.Add(hp->strength, ts);
      timeFanout.Add(hp->fanout, ts);
      timeThread.Add(hp->thread, ts);
    }

    if (timeUser > blockMax)
      blockMax = timeUser;

    if (hp->repeatNo == 0)
    {
      int bin = timeUser / 1000;
      timeHist[bin]++;
      if (hp->NTflag)
        timeHistNT[bin]++;
      else
        timeHistSuit[bin]++;
    }
  }

  for (int g = 0; g < numGroups; g++)
  {
    int head = group[g].head;
    int NTflag = (hands[head].strain == 4 ? 1 : 0);

    TimeStat ts;

    ts.Set(group[g].actual);
    timeGroupActualStrain.Add(NTflag, ts);

    ts.Set(group[g].pred);
    timeGroupPredStrain.Add(NTflag, ts);

    int diff = group[g].actual - group[g].pred;
    ts.Set(diff);
    timeGroupDiffStrain.Add(NTflag, ts);
  }


  timeBlock += timeUserBlock;
  timeMax += blockMax;
  blockMax = 0;
}


void Scheduler::PrintTiming() const
{
  const string fname = string(DDS_SCHEDULER_PREFIX) + DDS_DEBUG_SUFFIX;
  ofstream fout;
  fout.open(fname);

  fout << timeStrain.List();
  fout << timeRepeat.List();
  fout << timeDepth.List();
  fout << timeStrength.List();
  fout << timeFanout.List();
  fout << timeThread.List();
  fout << timeGroupActualStrain.List();
  fout << timeGroupPredStrain.List();
  fout << timeGroupDiffStrain.List();

#if 0
  fout << setw(13) << "Hist" <<
    setw(10) << "Hist suit" <<
    setw(10) << "Hist NT" << "\n";
  for (int i = 0; i < 10000; i++)
  {
    if (timeHist[i] || timeHistSuit[i] || timeHistNT[i])
    {
      fout << setw(4) << i <<
        setw(9) << timeHist[i] <<
        setw(10) << timeHistSuit[i] <<
        setw(10) << timeHistNT[i] << "\n";
    }
  }
  fout << endl;
#endif

  if (timeBlock == 0)
    return;

  const double avg = 100. * (double) timeMax / (double) timeBlock;
  fout << "Largest hand" <<
    setw(13) << timeMax << 
    setw(13) << timeBlock <<
    setw(6) << setprecision(2) << fixed << avg << "%\n\n";

  fout.close();
}

#endif // DDS_SCHEDULER


int Scheduler::PredictedTime(
  deal& dl,
  int number) const
{
  int trump = dl.trump;
  int NT = (trump == 4 ? 100 : 0);

  int dev1 = Scheduler::Strength(dl);

  int pred;
  if (NT)
  {
    if (dev1 >= 25)
      pred = 125000 - 2500 * dev1;
    else
      // This branch is not very accurate.
      pred = 200000 - 5500 * dev1;

    if (number >= 1)
      pred = static_cast<int>(1.25 * pred);

    if (number >= 2)
      pred = static_cast<int> (pred *
        (1.185 - 0.185 * exp( -(number - 1) / 6.0)));
  }
  else
  {
    pred = 125000 - 2500 * dev1;
    if (number >= 1)
      pred = static_cast<int>(1.2 * pred);

    if (number >= 2)
      pred = static_cast<int>(pred *
        (1.185 - 0.185 * exp( -(number - 1) / 5.5)));
  }

  return pred;
}