forked from michiguel/Ordo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
groups.c
1737 lines (1421 loc) · 34 KB
/
groups.c
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
/*
Ordo is program for calculating ratings of engine or chess players
Copyright 2013 Miguel A. Ballicora
This file is part of Ordo.
Ordo is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ordo is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ordo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "groups.h"
#include "mytypes.h"
#include "mymem.h"
#include "bitarray.h"
#include "mytimer.h"
#define NO_ID -1
static bool_t group_buffer_init (struct GROUP_BUFFER *g, player_t n);
static void group_buffer_done (struct GROUP_BUFFER *g);
static bool_t participant_buffer_init (struct PARTICIPANT_BUFFER *x, player_t n);
static void participant_buffer_done (struct PARTICIPANT_BUFFER *x);
static bool_t connection_buffer_init (struct CONNECT_BUFFER *x, gamesnum_t n);
static void connection_buffer_done (struct CONNECT_BUFFER *x);
//---------------------------------------------------------------------
// supporting memory
// SE2: list of "Super" encounters that do not belong to the same group
struct SUPER {
struct ENC * SE2;
gamesnum_t N_se2;
};
typedef struct SUPER super_t;
//----------------------------------------------------------------------
static void groupvar_simplify (group_var_t *gv);
static void groupvar_finish (group_var_t *gv);
static void connect_init (group_var_t *gv) {gv->connectionbuffer.n = 0;}
static connection_t * connection_new (group_var_t *gv)
{
assert (gv->connectionbuffer.n < gv->connectionbuffer.max);
return &gv->connectionbuffer.list[gv->connectionbuffer.n++];
}
static void participant_init (group_var_t *gv) {gv->participantbuffer.n = 0;}
static participant_t * participant_new (group_var_t *gv)
{
assert (gv->participantbuffer.n < gv->participantbuffer.max);
return &gv->participantbuffer.list[gv->participantbuffer.n++];
}
// prototypes
static group_t * group_new (group_var_t *gv);
static group_t * group_reset (group_t *g);
static group_t * group_combined (group_t *g);
static group_t * group_pointed_by_conn (connection_t *c);
static group_t * group_pointed_by_node (node_t *nd);
static void groupvar_assign_newid (group_var_t *gv);
static void groupvar_list_sort (group_var_t *gv);
static void groupvar_list_output (group_var_t *gv, FILE *f);
static void group_output (group_t *s, group_var_t *gv, FILE *f, bool_t showlinks);
// groupset functions
#ifndef NDEBUG
static bool_t
groupset_sanity_check(group_var_t *gv)
{
// verify g is properly double-linked
group_t *g = gv->groupbuffer.prehead;
if (g == NULL) return FALSE;
for (g = g->next; g != NULL; g = g->next) {
if (g->prev == NULL) return FALSE;
}
return TRUE;
}
static bool_t
groupset_sanity_check_nocombines(group_var_t *gv)
{
// verify g is properly double-linked
group_t *g = gv->groupbuffer.prehead;
if (g == NULL) return FALSE;
for (g = g->next; g != NULL; g = g->next) {
if (g->combined != NULL) return FALSE;
}
return TRUE;
}
#endif
static void
groupset_init (group_var_t *gv)
{
gv->groupbuffer.tail = &gv->groupbuffer.list[0];
gv->groupbuffer.prehead = &gv->groupbuffer.list[0];
group_reset(gv->groupbuffer.prehead);
gv->groupbuffer.n = 1;
}
static group_t * groupset_tail (group_var_t *gv) {return gv->groupbuffer.tail;}
static group_t * groupset_head (group_var_t *gv) {return gv->groupbuffer.prehead->next;}
#if 0
// for debuggin purposes
static void groupset_print(group_var_t *gv)
{
group_t * s;
printf("ID added {\n");
for (s = groupset_head(gv); s != NULL; s = s->next) {
printf(" id=%ld\n", (long)s->id);
}
printf("}\n");
return;
}
#endif
static void
groupset_add (group_var_t *gv, group_t *a)
{
group_t *t = groupset_tail(gv);
t->next = a;
a->prev = t;
gv->groupbuffer.tail = a;
}
static void
groupset_reset_finding (group_var_t *gv)
{
group_t * s;
player_t i;
for (i = 0; i < gv->nplayers; i++) {
gv->tofindgroup[i] = NULL;
}
for (s = groupset_head(gv); s != NULL; s = s->next) {
gv->tofindgroup[s->id] = s;
}
}
static group_t *
groupset_find (group_var_t *gv, player_t id)
{
#if 1
return gv->tofindgroup[id];
#else
group_t * s;
for (s = groupset_head(gv); s != NULL; s = s->next) {
if (id == s->id) return s;
}
return NULL;
#endif
}
//---------------------------------- INITIALIZATION --------------------------
typedef struct SELINK selink_t;
struct SELINK {
player_t iwin;
player_t ilos;
};
static bool_t
supporting_encmem_init (gamesnum_t nenc, super_t *sp, selink_t **pse)
{
struct ENC *a;
selink_t *b;
if (NULL == (a = memnew (sizeof(struct ENC) * (size_t)nenc))) {
return FALSE;
} else
if (NULL == (b = memnew (sizeof(selink_t) * (size_t)nenc))) {
memrel(a);
return FALSE;
}
sp->SE2 = a;
*pse = b;
return TRUE;
}
static void
supporting_encmem_done (super_t *sp, selink_t *se)
{
if (sp->SE2) memrel (sp->SE2);
sp->N_se2 = 0;
if (se) memrel (se);
return;
}
bool_t
groupvar_init (group_var_t *gv, player_t nplayers, gamesnum_t nenc)
{
player_t *a;
player_t *b;
groupcell_t *c;
node_t *d;
player_t *e;
group_t * *f;
size_t sa = sizeof(player_t);
size_t sb = sizeof(player_t);
size_t sc = sizeof(groupcell_t);
size_t sd = sizeof(node_t);
size_t se = sizeof(player_t);
size_t sf = sizeof(group_t *);
if (NULL == (a = memnew (sa * (size_t)nplayers))) {
return FALSE;
} else
if (NULL == (b = memnew (sb * (size_t)nplayers))) {
memrel(a);
return FALSE;
} else
if (NULL == (c = memnew (sc * (size_t)nplayers))) {
memrel(a);
memrel(b);
return FALSE;
} else
if (NULL == (d = memnew (sd * (size_t)nplayers))) {
memrel(a);
memrel(b);
memrel(c);
return FALSE;
} else
if (NULL == (e = memnew (se * (size_t)nplayers))) {
memrel(a);
memrel(b);
memrel(c);
memrel(d);
return FALSE;
} else
if (NULL == (f = memnew (sf * (size_t)nplayers))) {
memrel(a);
memrel(b);
memrel(c);
memrel(d);
memrel(e);
return FALSE;
}
gv->groupbelong = a;
gv->getnewid = b;
gv->groupfinallist = c;
gv->node = d;
gv->gchain = e;
gv->tofindgroup = f;
gv->nplayers = nplayers;
gv->groupfinallist_n = 0;
if (!group_buffer_init (&gv->groupbuffer, nplayers)) {
return FALSE;
}
if (!participant_buffer_init (&gv->participantbuffer, nplayers)) {
group_buffer_done (&gv->groupbuffer);
return FALSE;
}
if (!connection_buffer_init (&gv->connectionbuffer, nenc*2)) {
group_buffer_done (&gv->groupbuffer);
participant_buffer_done (&gv->participantbuffer);
return FALSE;
}
return TRUE;
}
void
groupvar_done (group_var_t *gv)
{
assert(gv);
if (gv->groupbelong) memrel (gv->groupbelong );
if (gv->getnewid) memrel (gv->getnewid);
if (gv->groupfinallist) memrel (gv->groupfinallist);
if (gv->node) memrel (gv->node);
if (gv->gchain) memrel (gv->gchain);
if (gv->tofindgroup) memrel (gv->tofindgroup);
gv->groupbelong = NULL;
gv->getnewid = NULL;
gv->groupfinallist = NULL;
gv->node = NULL;
gv->gchain = NULL;
gv->tofindgroup = NULL;
gv->groupfinallist_n = 0;
gv->nplayers = 0;
group_buffer_done (&gv->groupbuffer);
participant_buffer_done (&gv->participantbuffer);
connection_buffer_done (&gv->connectionbuffer);
return;
}
//==
static bool_t
group_buffer_init (struct GROUP_BUFFER *g, player_t n)
{
group_t *p;
size_t elements = (size_t)n + 1; //one extra added for the head
if (NULL != (p = memnew (sizeof(group_t) * elements))) {
g->list = p;
g->tail = NULL;
g->prehead = NULL;
g->n = 0;
g->max = n;
}
return p != NULL;
}
static void
group_buffer_done (struct GROUP_BUFFER *g)
{
if (g->list != NULL) {
memrel(g->list);
g->list = NULL;
}
g->tail = NULL;
g->prehead = NULL;
g->n = 0;
}
static bool_t
participant_buffer_init (struct PARTICIPANT_BUFFER *x, player_t n)
{
participant_t *p;
if (NULL != (p = memnew (sizeof(participant_t) * (size_t)n))) {
x->list = p;
x->n = 0;
x->max = n;
}
return p != NULL;
}
static void
participant_buffer_done (struct PARTICIPANT_BUFFER *x)
{
if (x->list != NULL) {
memrel(x->list);
x->list = NULL;
}
x->n = 0;
}
static bool_t
connection_buffer_init (struct CONNECT_BUFFER *x, gamesnum_t n)
{
connection_t *p;
if (NULL != (p = memnew (sizeof(connection_t) * (size_t)n))) {
x->list = p;
x->n = 0;
x->max = n;
}
return p != NULL;
}
static void
connection_buffer_done (struct CONNECT_BUFFER *x)
{
if (x->list != NULL) {
memrel(x->list);
x->list = NULL;
}
x->n = 0;
}
//----------------------------------------------------------------------------
static group_t * group_new (group_var_t *gv)
{
return &gv->groupbuffer.list[gv->groupbuffer.n++];
}
static group_t * group_reset (group_t *g)
{
if (g == NULL) return NULL;
g->next = NULL;
g->prev = NULL;
g->combined = NULL;
g->pstart = NULL; g->plast = NULL;
g->cstart = NULL; g->clast = NULL;
g->lstart = NULL; g->llast = NULL;
g->id = NO_ID;
g->isolated = FALSE;
return g;
}
static group_t * group_combined (group_t *g)
{
while (g->combined != NULL)
g = g->combined;
return g;
}
//no globals
static void
add_participant (group_var_t *gv, group_t *g, player_t i, const char *name)
{
participant_t *nw;
nw = participant_new(gv);
nw->next = NULL;
nw->name = name;
nw->id = i;
if (g->pstart == NULL) {
g->pstart = nw;
g->plast = nw;
} else {
g->plast->next = nw;
g->plast = nw;
}
}
//no globals
static void
add_beat_connection (group_var_t *gv, group_t *g, struct NODE *pnode)
{
// Assume scan counters properly sorted and eliminated duplicates
// otherwise, duplicates could be added
connection_t *nw;
assert(g && pnode && pnode->group);
nw = connection_new(gv);
nw->next = NULL;
nw->node = pnode;
if (g->cstart == NULL) {
g->cstart = nw;
} else {
g->clast->next = nw;
}
g->clast = nw;
}
// no globals
static void
add_lost_connection (group_var_t *gv, group_t *g, struct NODE *pnode)
{
// Assume scan counters properly sorted and eliminated duplicates
// otherwise, duplicates could be added
connection_t *nw;
assert(g && pnode && pnode->group);
nw = connection_new(gv);
nw->next = NULL;
nw->node = pnode;
if (g->lstart == NULL) {
g->lstart = nw;
} else {
g->llast->next = nw;
}
g->llast = nw;
}
//
typedef struct PNODE pnode_t;
struct PNODE {
player_t indx;
pnode_t * next;
pnode_t * last;
};
static void
pnode_clear (pnode_t *pn, player_t n)
{
player_t i;
for (i = 0; i < n; i++) {
pn[i].indx = i;
pn[i].next = NULL;
pn[i].last = NULL;
}
}
static void
pnode_connect (pnode_t *pn, player_t i, player_t j)
{
player_t z;
pnode_t *a, *b, *c, *l;
a = pn + i;
b = pn + j;
z = a->indx;
for (c = b; c != NULL; c = c->next) c->indx = z; // fill
l = b->last? b->last: b;
l->next = a->next;
a->next = b;
if (a->last == NULL) a->last = l;
b->last = NULL;
}
static int compare_selink (const void * a, const void * b)
{
const selink_t *sa = a;
const selink_t *sb = b;
if (sa->iwin < sb->iwin) return -1;
if (sa->iwin > sb->iwin) return 1;
if (sa->ilos < sb->ilos) return -1;
if (sa->ilos > sb->ilos) return 1;
return 0;
}
static player_t get_iwin (struct ENC *pe);
static player_t get_ilos (struct ENC *pe);
static bool_t encounter_is_SW (const struct ENC *e) {return e->W > 0 && e->D == 0 && e->L == 0;}
static bool_t encounter_is_SL (const struct ENC *e) {return e->W == 0 && e->D == 0 && e->L > 0;}
static bool_t
same_SE_link (selink_t *a, selink_t *b)
{
return a->iwin == b->iwin &&
a->ilos == b->ilos;
}
// no globals
static void
scan_encounters ( const struct ENC *enc, gamesnum_t n_enc
, player_t *belongto, player_t n_plyrs
, struct ENC *sup_enc, gamesnum_t *pn_enc
, selink_t *sel
, gamesnum_t *psel_n
)
{
// sup_enc: list of "Super" encounters that do not belong to the same group
pnode_t *xx;
player_t i;
gamesnum_t e;
const struct ENC *pe;
gamesnum_t n_a, n_b;
player_t gw, gb, lowerg, higherg;
assert (sup_enc != NULL);
assert (pn_enc != NULL);
assert (belongto != NULL);
for (i = 0; i < n_plyrs; i++) {
belongto[i] = i;
}
//
if (NULL != (xx = memnew( (size_t)n_plyrs * sizeof(pnode_t) ))) {
pnode_clear (xx, n_plyrs);
for (e = 0, n_a = 0; e < n_enc; e++) {
pe = &enc[e];
if (encounter_is_SL(pe) || encounter_is_SW(pe)) {
sup_enc[n_a++] = *pe;
} else {
gw = xx[pe->wh].indx;
gb = xx[pe->bl].indx;
if (gw != gb) {
lowerg = gw < gb? gw : gb;
higherg = gw > gb? gw : gb;
// join
pnode_connect(xx, lowerg, higherg);
}
}
}
for (i = 0; i < n_plyrs; i++) {
belongto[i] = xx[i].indx;
}
memrel(xx);
} else {
fprintf(stderr,"Not enough memory available, FILE=%s, LINE=%d\n",__FILE__, __LINE__);
exit(EXIT_FAILURE);
}
//
for (e = 0, n_b = 0 ; e < n_a; e++) {
player_t x,y;
x = sup_enc[e].wh;
y = sup_enc[e].bl;
if (belongto[x] != belongto[y]) {
if (e != n_b)
sup_enc[n_b] = sup_enc[e];
n_b++;
}
}
*pn_enc = n_b; // number of "Super" encounters that do not belong to the same group
{
gamesnum_t r;
for (e = 0; e < n_b; e++) {
sel[e].iwin = belongto [ get_iwin(&sup_enc[e]) ];
sel[e].ilos = belongto [ get_ilos(&sup_enc[e]) ];
}
qsort (sel, (size_t)n_b, sizeof(selink_t), compare_selink);
*psel_n = n_b;
if (n_b > 0) {
for (e = 1, r = 0 ; e < n_b; e++) {
if (!same_SE_link (&sel[r], &sel[e]) ) {
r++;
if (r != e) sel[r] = sel[e];
}
}
*psel_n = r + 1;
} else {
*psel_n = 0;
}
}
return;
}
static bool_t node_is_occupied (group_var_t *gv, player_t x)
{
assert(gv);
assert(gv->node);
return gv->node[x].group != NULL;
}
static void
node_add_group (group_var_t *gv, player_t x)
{
group_t *g;
assert (!node_is_occupied(gv,x));
if (NULL == (g = groupset_find (gv, gv->groupbelong[x]))) {
g = group_reset(group_new(gv));
g->id = gv->groupbelong[x];
groupset_add(gv,g);
gv->tofindgroup[gv->groupbelong[x]] = g;
}
assert (g->id == gv->groupbelong[x]);
gv->node[x].group = g;
}
static player_t
group_belonging (group_var_t *gv, player_t x)
{
group_t *g = group_pointed_by_node (&gv->node[x]);
return NULL == g? NO_ID: g->id;
}
static group_t *
group_pointed_by_node (node_t *nd)
{
if (nd && nd->group) {
return group_combined(nd->group);
} else {
return NULL;
}
}
static group_t *
group_pointed_by_conn (connection_t *c)
{
return c == NULL? NULL: group_pointed_by_node (c->node);
}
// no globals
static void
selink2group (player_t iwin, player_t ilos, group_var_t *gv)
{
if (!node_is_occupied (gv,iwin)) node_add_group (gv,iwin);
if (!node_is_occupied (gv,ilos)) node_add_group (gv,ilos);
add_beat_connection (gv, gv->node[iwin].group, &gv->node[ilos]);
add_lost_connection (gv, gv->node[ilos].group, &gv->node[iwin]);
}
static player_t get_iwin (struct ENC *pe) {return pe->W > 0? pe->wh: pe->bl;}
static player_t get_ilos (struct ENC *pe) {return pe->W > 0? pe->bl: pe->wh;}
#if 0
// no globals
static void
sup_enc2group (struct ENC *pe, group_var_t *gv)
{
// sort super encounter
// into their respective groups
player_t iwin, ilos;
assert(pe);
assert(encounter_is_SL(pe) || encounter_is_SW(pe));
iwin = get_iwin(pe);
ilos = get_ilos(pe);
selink2group (iwin, ilos, gv);
}
#endif
static void
group_gocombine (group_t *g, group_t *h);
static void
convert_general_init (group_var_t *gv, player_t n_plyrs)
{
player_t i;
connect_init(gv);
participant_init(gv);
groupset_init(gv);
for (i = 0; i < n_plyrs; i++) {
gv->node[i].group = NULL;
}
return;
}
static player_t
groupvar_counter (group_var_t *gv)
{
return gv->groupfinallist_n;
}
static void
groupvar_finallist_sort(group_var_t *gv);
player_t
groupvar_build (group_var_t *gv, player_t n_plyrs, const char **name, const struct PLAYERS *players, const struct ENCOUNTERS *encounters)
{
selink_t *SElnk;
gamesnum_t SElnk_n;
player_t ret;
player_t i;
gamesnum_t e;
super_t SP;
super_t *sp = &SP;
timelog("memory initialization...");
if (supporting_encmem_init (encounters->n, sp, &SElnk)) {
timelog("scan games...");
scan_encounters (encounters->enc, encounters->n, gv->groupbelong, players->n, sp->SE2, &sp->N_se2, SElnk , &SElnk_n);
timelog("general initialization...");
convert_general_init (gv, n_plyrs);
// Initiate groups from critical "super" encounters
timelog_ld("incorporate links into groups... N=", (long)sp->N_se2);
timelog_ld("incorporate links into groups... Unique=", (long)SElnk_n);
groupset_reset_finding (gv);
for (e = 0 ; e < SElnk_n; e++) {
selink2group (SElnk[e].iwin, SElnk[e].ilos, gv);
}
supporting_encmem_done (sp, SElnk);
} else {
fprintf (stderr, "No memory available\n");
exit(EXIT_FAILURE);
}
timelog("start groups for each player with no links...");
// Initiate groups for each player present in the database that did not have
// critical super encounters
for (i = 0; i < n_plyrs; i++) {
if (players->present_in_games[i] && !node_is_occupied (gv,i))
node_add_group (gv,i);
}
timelog("add list of participants...");
// for all the previous added groups, add respective list of participants
for (i = 0; i < n_plyrs; i++) {
if (node_is_occupied(gv,i)) {
group_t *g = groupset_find(gv, gv->groupbelong[i]);
if (g) add_participant(gv, g, i, name[i]);
}
}
assert(groupset_sanity_check_nocombines(gv));
timelog("group joining and simplification...");
groupvar_simplify(gv);
timelog("finish complex circuits...");
groupvar_finish(gv);
timelog("sort...");
groupvar_list_sort (gv);
groupvar_finallist_sort(gv);
timelog("assign new indexes...");
groupvar_assign_newid (gv);
ret = groupvar_counter(gv) ;
return ret;
}
static void
groupvar_sieveenc ( group_var_t *gv
, const struct ENC *enc
, gamesnum_t n_enc
, gamesnum_t *N_enca
, gamesnum_t *N_encb
)
{
gamesnum_t e;
player_t w,b;
gamesnum_t na = 0, nb = 0;
for (e = 0; e < n_enc; e++) {
w = enc[e].wh;
b = enc[e].bl;
if (group_belonging(gv,w) == group_belonging(gv,b)) {
na += 1;
} else {
nb += 1;
}
}
*N_enca = na;
*N_encb = nb;
return;
}
static void
group_gocombine (group_t *g, group_t *h)
{
// unlink h
group_t *pr = h->prev;
group_t *ne = h->next;
assert(pr);
if (h->combined == g) {
return;
}
// unlink h
h->prev = NULL;
h->next = NULL;
pr->next = ne;
if (ne) ne->prev = pr;
// if h is called, go to h
h->combined = g;
// inherit information from h to g
g->plast->next = h->pstart;
g->plast = h->plast;
h->plast = NULL;
h->pstart = NULL;
g->clast->next = h->cstart;
g->clast = h->clast;
h->clast = NULL;
h->cstart = NULL;
g->llast->next = h->lstart;
g->llast = h->llast;
h->llast = NULL;
h->lstart = NULL;
}
//-----------------------------------------------
static void simplify (group_t *g, group_t **cmbbuf);
static group_t *group_next (group_t *g)
{
assert(g);
return g->next;
}
static void
groupvar_simplify (group_var_t *gv)
{
group_t *g;
group_t * *cmbbuf;
size_t mem = (size_t)gv->nplayers * sizeof (group_t *);
if (NULL != (cmbbuf = memnew (mem))) {
g = groupset_head(gv);
assert(g);
while(g) {
simplify(g,cmbbuf);
g = group_next(g);
}
assert(groupset_sanity_check(gv));
memrel(cmbbuf);
} else {
fprintf (stderr, "No memory available for group detection\n");
exit (EXIT_FAILURE);
}
return;
}
#if 0
static void
beat_lost_output (group_t *g)
{
group_t *beat_to, *lost_to;
connection_t *c;
printf (" G=%ld, beat_to: ",g->id);
// loop connections, examine id if repeated or self point (delete them)
beat_to = NULL;
c = g->cstart;
do {
if (c && NULL != (beat_to = group_pointed_by_conn(c))) {
printf ("%ld, ",beat_to->id);
c = c->next;
}
} while (c && beat_to);
printf ("\n");
printf (" G=%ld, lost_to: ",g->id);
lost_to = NULL;
c = g->lstart;
do {
if (c && NULL != (lost_to = group_pointed_by_conn(c))) {
printf ("%ld, ",lost_to->id);
c = c->next;
}
} while (c && lost_to);
printf ("\n");
}
#endif
static player_t
id_pointed_by_conn(connection_t *c)
{
group_t *gg;
return NULL != c && NULL != (gg = group_pointed_by_conn(c))? gg->id: NO_ID;
}
static player_t
find_top_id (group_t *g)
{
connection_t *c;
player_t id;
player_t topid = g->id;
for (c = g->cstart; NULL != c; c = c->next) {
id = id_pointed_by_conn(c);
if (id > topid) topid = id;
}
for (c = g->lstart; NULL != c; c = c->next) {
id = id_pointed_by_conn(c);
if (id > topid) topid = id;
}
return topid;