-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathnausparse.c
1746 lines (1525 loc) · 54 KB
/
nausparse.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
/*****************************************************************************
* *
* Sparse-graph-specific auxiliary source file for version 2.7 of nauty. *
* *
* Copyright (2004-2016) Brendan McKay. All rights reserved. *
* Subject to waivers and disclaimers in nauty.h. *
* *
* CHANGE HISTORY *
* 26-Oct-04 : initial creation *
* 23-Nov-06 : dispatch uses targetcell_sg, not bestcell_sg *
* 8-Dec-06 : add adjacencies_sg() *
* 10-Nov-09 : remove types shortish and permutation *
* 14-Nov-09 : added copy_sg() *
* 11-May-10 : use sorttemplates.c for sorting procedures *
* 19-May-10 : add two *_tr procedures for traces. *
* 21-May-10 : fixes for nde,v fields becoming size_t *
* 23-May-10 : add sparsenauty() *
* 15-Jan-12 : add TLS_ATTR attributes *
* 17-Dec-15 : extend sortlists_sg() to sort weights *
* : add weights to copy_sg() and updatecan_sg() *
* 11-Mar-16 : add cleanup_sg(). This can be used in the cleanup *
* field of the dispatch vector to sort the lists of the *
* canonical graph, but isn't there by default. *
* 15-Oct-19 : fix static declaration of snwork[] *
* *
*****************************************************************************/
#define TMP
/* #define ONE_WORD_SETS not sure about this! See notes.txt. */
#include "nausparse.h"
/* macros for hash-codes: */
#define MASH(l,i) ((((l) ^ 065435) + (i)) & 077777)
/* : expression whose long value depends only on long l and int/long i.
Anything goes, preferably non-commutative. */
#define CLEANUP(l) ((int)((l) % 077777))
/* : expression whose value depends on long l and is less than 077777
when converted to int then short. Anything goes. */
#if MAXM==1
#define M 1
#else
#define M m
#endif
#define ACCUM(x,y) x = (((x) + (y)) & 077777)
static const int fuzz1[] = {037541,061532,005257,026416};
static const int fuzz2[] = {006532,070236,035523,062437};
#define FUZZ1(x) ((x) ^ fuzz1[(x)&3])
#define FUZZ2(x) ((x) ^ fuzz2[(x)&3])
/* aproto: header new_nauty_protos.h */
dispatchvec dispatch_sparse =
{isautom_sg,testcanlab_sg,updatecan_sg,refine_sg,refine_sg,cheapautom_sg,
targetcell_sg,nausparse_freedyn,nausparse_check,init_sg,NULL};
#if !MAXN
DYNALLSTAT(short,vmark1,vmark1_sz);
DYNALLSTAT(short,vmark2,vmark2_sz);
DYNALLSTAT(int,work1,work1_sz);
DYNALLSTAT(int,work2,work2_sz);
DYNALLSTAT(int,work3,work3_sz);
DYNALLSTAT(int,work4,work4_sz);
DYNALLSTAT(set,snwork,snwork_sz);
#else
static TLS_ATTR short vmark1[MAXN];
static TLS_ATTR short vmark2[MAXN];
static TLS_ATTR int work1[MAXN];
static TLS_ATTR int work2[MAXN];
static TLS_ATTR int work3[MAXN];
static TLS_ATTR int work4[MAXN];
static TLS_ATTR set snwork[2*500*MAXM];
#endif
static TLS_ATTR short vmark1_val = 32000;
#define MARK1(i) vmark1[i] = vmark1_val
#define UNMARK1(i) vmark1[i] = 0
#define ISMARKED1(i) (vmark1[i] == vmark1_val)
#define ISNOTMARKED1(i) (vmark1[i] != vmark1_val)
static TLS_ATTR short vmark2_val = 32000;
#define MARK2(i) vmark2[i] = vmark2_val
#define UNMARK2(i) vmark2[i] = 0
#define ISMARKED2(i) (vmark2[i] == vmark2_val)
#define ISNOTMARKED2(i) (vmark2[i] != vmark2_val)
#if !MAXN
#define RESETMARKS1 {if (vmark1_val++ >= 32000) \
{size_t ij; for (ij=0;ij<vmark1_sz;++ij) vmark1[ij]=0; vmark1_val=1;}}
#define PREPAREMARKS1(nn) preparemarks1(nn)
#define RESETMARKS2 {if (vmark2_val++ >= 32000) \
{size_t ij; for (ij=0;ij<vmark2_sz;++ij) vmark2[ij]=0; vmark2_val=1;}}
#define PREPAREMARKS2(nn) preparemarks2(nn)
#else
#define RESETMARKS1 {if (vmark1_val++ >= 32000) \
{size_t ij; for (ij=0;ij<MAXN;++ij) vmark1[ij]=0; vmark1_val=1;}}
#define PREPAREMARKS1(nn)
#define RESETMARKS2 {if (vmark2_val++ >= 32000) \
{size_t ij; for (ij=0;ij<MAXN;++ij) vmark2[ij]=0; vmark2_val=1;}}
#define PREPAREMARKS2(nn)
#endif
/*****************************************************************************
* *
* preparemarks1(N) and preparemarks2(N) *
* make vmarks array large enough to mark 0..N-1 and such that *
* the next RESETMARKS command will work correctly *
* *
*****************************************************************************/
#if !MAXN
static void
preparemarks1(size_t nn)
{
size_t oldsize;
short *oldpos;
oldsize = vmark1_sz;
oldpos = vmark1;
DYNALLOC1(short,vmark1,vmark1_sz,nn,"preparemarks");
if (vmark1_sz != oldsize || vmark1 != oldpos) vmark1_val = 32000;
}
#endif
#if !MAXN
static void
preparemarks2(size_t nn)
{
size_t oldsize;
short *oldpos;
oldsize = vmark2_sz;
oldpos = vmark2;
DYNALLOC1(short,vmark2,vmark2_sz,nn,"preparemarks");
if (vmark2_sz != oldsize || vmark2 != oldpos) vmark2_val = 32000;
}
#endif
/*****************************************************************************
* *
* isautom_sg(g,perm,digraph,m,n) = TRUE iff perm is an automorphism of g *
* (i.e., g^perm = g). Symmetry is assumed unless digraph = TRUE. *
* *
*****************************************************************************/
boolean
isautom_sg(graph *g, int *p, boolean digraph, int m, int n)
{
int *d,*e;
size_t *v;
int i,pi,di;
size_t vi,vpi,j;
SG_VDE(g,v,d,e);
PREPAREMARKS1(n);
for (i = 0; i < n; ++i)
if (p[i] != i || digraph)
{
pi = p[i];
di = d[i];
if (d[pi] != di) return FALSE;
vi = v[i];
vpi = v[pi];
RESETMARKS1;
for (j = 0; j < di; ++j) MARK1(p[e[vi+j]]);
for (j = 0; j < di; ++j) if (ISNOTMARKED1(e[vpi+j])) return FALSE;
}
return TRUE;
}
/*****************************************************************************
* *
* aresame_sg(g1,g2) = TRUE iff g1 and g2 are identical as labelled digraphs *
* *
*****************************************************************************/
boolean
aresame_sg(sparsegraph *g1, sparsegraph *g2)
{
int *d1,*e1;
int *d2,*e2;
int n,i,di;
size_t vi,*v1,*v2,j;
n = g1->nv;
if (g2->nv != n || g2->nde != g1->nde) return FALSE;
SG_VDE(g1,v1,d1,e1);
SG_VDE(g2,v2,d2,e2);
PREPAREMARKS1(n);
for (i = 0; i < n; ++i)
{
di = d1[i];
if (d2[i] != di) return FALSE;
vi = v1[i];
RESETMARKS1;
for (j = 0; j < di; ++j) MARK1(e1[vi+j]);
vi = v2[i];
for (j = 0; j < di; ++j) if (ISNOTMARKED1(e2[vi+j])) return FALSE;
}
return TRUE;
}
/*****************************************************************************
* *
* testcanlab_sg(g,canong,lab,samerows,m,n) compares g^lab to canong, *
* using an ordering which is immaterial since it's only used here. The *
* value returned is -1,0,1 if g^lab <,=,> canong. *samerows is set to *
* the number of rows (0..n) of canong which are the same as those of g^lab. *
* *
*****************************************************************************/
int
testcanlab_sg(graph *g, graph *canong, int *lab, int *samerows, int m, int n)
{
int *d,*e;
int *cd,*ce;
int i,k,di,dli;
size_t j,vi,vli,*v,*cv;
int mina;
SG_VDE(g,v,d,e);
SG_VDE(canong,cv,cd,ce);
#if !MAXN
DYNALLOC1(int,work1,work1_sz,n,"testcanlab_sg");
#endif
#define INVLAB work1
PREPAREMARKS1(n);
for (i = 0; i < n; ++i) INVLAB[lab[i]] = i;
for (i = 0; i < n; ++i)
{
/* compare g[lab[i]]^INVLAB to canong[i] */
vi = cv[i];
di = cd[i];
vli = v[lab[i]];
dli = d[lab[i]];
if (di != dli)
{
*samerows = i;
if (di < dli) return -1;
return 1;
}
RESETMARKS1;
mina = n;
for (j = 0; j < di; ++j) MARK1(ce[vi+j]);
for (j = 0; j < di; ++j)
{
k = INVLAB[e[vli+j]];
if (ISMARKED1(k)) UNMARK1(k);
else if (k < mina) mina = k;
}
if (mina != n)
{
*samerows = i;
for (j = 0; j < di; ++j)
{
k = ce[vi+j];
if (ISMARKED1(k) && k < mina) return -1;
}
return 1;
}
}
*samerows = n;
return 0;
}
/*****************************************************************************
* *
* updatecan_sg(g,canong,lab,samerows,m,n) sets canong = g^lab, assuming *
* the first samerows vertices of canong are ok already. Also assumes *
* contiguity and ample space in canong. *
* *
*****************************************************************************/
void
updatecan_sg(graph *g, graph *canong, int *lab, int samerows, int m, int n)
{
int *d,*e;
int *cd,*ce;
int i,dli;
size_t *v,*cv,vli,j,k;
sg_weight *wt,*cwt;
SWG_VDE(g,v,d,e,wt);
SWG_VDE(canong,cv,cd,ce,cwt);
#if !MAXN
DYNALLOC1(int,work1,work1_sz,n,"testcanlab_sg");
#endif
#define INVLAB work1
((sparsegraph*)canong)->nv = n;
((sparsegraph*)canong)->nde = ((sparsegraph*)g)->nde;
for (i = 0; i < n; ++i) INVLAB[lab[i]] = i;
if (samerows == 0) k = 0;
else k = cv[samerows-1]+cd[samerows-1];
for (i = samerows; i < n; ++i)
{
cv[i] = k;
cd[i] = dli = d[lab[i]];
vli = v[lab[i]];
if (wt)
{
for (j = 0; j < dli; ++j)
{
ce[k] = INVLAB[e[vli+j]];
cwt[k] = wt[vli+j];
++k;
}
}
else
for (j = 0; j < dli; ++j) ce[k++] = INVLAB[e[vli+j]];
}
}
/*****************************************************************************
* *
* comparelab_tr(g,lab1,invlab1,lab2,invlab2,cls,col) compares *
* g^lab1 to g^lab2 and returns -1,0,1 according to the comparison. *
* invlab1[] and invlab2[] are assumed to hold inverses of lab1,lab2. *
* *
*****************************************************************************/
int
comparelab_tr(sparsegraph *g,
int *lab1, int *invlab1, int *lab2, int *invlab2, int *cls, int *col)
{
int d1,*e1,d2,*e2;
int i,j,k,n,c,end;
int mina;
n = g->nv;
PREPAREMARKS1(n);
for (c=0; c<n; c+=cls[c])
{
if (cls[c] == 1)
{
end = c+cls[c];
for (i = c; i < end; ++i)
{
e1 = g->e + g->v[lab1[i]];
d1 = g->d[lab1[i]];
e2 = g->e + g->v[lab2[i]];
d2 = g->d[lab2[i]];
if (d1 < d2) return -1;
else if (d1 > d2) return 1;
RESETMARKS1;
mina = n;
for (j = 0; j < d1; ++j) MARK1(col[invlab1[e1[j]]]);
for (j = 0; j < d1; ++j)
{
k = col[invlab2[e2[j]]];
if (ISMARKED1(k)) UNMARK1(k);
else if (k < mina) mina = k;
}
if (mina != n)
{
for (j = 0; j < d1; ++j)
{
k = col[invlab1[e1[j]]];
if (ISMARKED1(k) && k < mina) return -1;
}
return 1;
}
}
}
}
return 0;
}
/*****************************************************************************
* *
* testcanlab_tr(g,canong,lab,invlab,samerows) compares g^lab to canong, *
* using an ordering which is immaterial since it's only used here. The *
* value returned is -1,0,1 if g^lab <,=,> canong. *samerows is set to *
* the number of rows (0..n) of canong which are the same as those of g^lab. *
* invlab[] is assumed to hold the inverse of lab[] *
* *
*****************************************************************************/
int
testcanlab_tr(sparsegraph *g, sparsegraph *canong,
int *lab, int *invlab, int *samerows)
{
int *d,*e;
int *cd,*ce;
int i,di,dli;
int k,n;
size_t *v,*cv,vi,vli,j;
int mina;
SG_VDE(g,v,d,e);
SG_VDE(canong,cv,cd,ce);
n = g->nv;
PREPAREMARKS1(n);
for (i = 0; i < n; ++i)
{
/* compare g[lab[i]]^invlab to canong[i] */
vi = cv[i];
di = cd[i];
vli = v[lab[i]];
dli = d[lab[i]];
if (di != dli)
{
*samerows = i;
if (di < dli) return -1;
return 1;
}
RESETMARKS1;
mina = n;
for (j = 0; j < di; ++j) MARK1(ce[vi+j]);
for (j = 0; j < di; ++j)
{
k = invlab[e[vli+j]];
if (ISMARKED1(k)) UNMARK1(k);
else if (k < mina) mina = k;
}
if (mina != n)
{
*samerows = i;
for (j = 0; j < di; ++j)
{
k = ce[vi+j];
if (ISMARKED1(k) && k < mina) return -1;
}
return 1;
}
}
*samerows = n;
return 0;
}
/*****************************************************************************
* *
* updatecan_tr(g,canong,lab,invlab,samerows) sets canong = g^lab, *
* assuming the first samerows vertices of canong are ok already. *
* Also assumes contiguity and ample space in canong. *
* Assumes invlab[] holds the inverse of lab[] *
* *
*****************************************************************************/
void
updatecan_tr(sparsegraph *g, sparsegraph *canong,
int *lab, int *invlab, int samerows)
{
int *d,*e;
int *cd,*ce;
int i,dli,n;
size_t *v,*cv,vli,j,k;
SG_VDE(g,v,d,e);
SG_VDE(canong,cv,cd,ce);
n = g->nv;
PREPAREMARKS1(n);
canong->nv = n;
canong->nde = g->nde;
if (samerows == 0) k = 0;
else k = cv[samerows-1]+cd[samerows-1];
for (i = samerows; i < n; ++i)
{
cv[i] = k;
cd[i] = dli = d[lab[i]];
vli = v[lab[i]];
for (j = 0; j < dli; ++j) ce[k++] = invlab[e[vli+j]];
}
}
#define SORT_OF_SORT 3
#define SORT_NAME sortindirect
#define SORT_TYPE1 int
#define SORT_TYPE2 int
#include "sorttemplates.c"
#define SORT_OF_SORT 1
#define SORT_NAME sortints
#define SORT_TYPE1 int
#include "sorttemplates.c"
#define SORT_OF_SORT 2
#define SORT_NAME sortweights
#define SORT_TYPE1 int
#define SORT_TYPE2 sg_weight
#include "sorttemplates.c"
/*****************************************************************************
* *
* init_sg(graph *gin, graph **gout, graph *hin, graph **hout, *
* int *lab, int *ptn, set *active, optionblk *options, *
* int *status, int m, int n) *
* Initialise routine for dispatch vector. This one just makes sure *
* that *hin has enough space and sets fields for n=0. *
* *
*****************************************************************************/
void
init_sg(graph *gin, graph **gout, graph *hin, graph **hout, int *lab,
int *ptn, set *active, struct optionstruct *options, int *status,
int m, int n)
{
sparsegraph *sg,*sh;
if (options->getcanon)
{
sg = (sparsegraph*)gin;
sh = (sparsegraph*)hin;
SG_ALLOC(*sh,sg->nv,sg->nde,"init_sg");
sh->nv = sg->nv;
sh->nde = sg->nde;
}
*status = 0;
}
/*****************************************************************************
* *
* cleanup_sg(graph *gin, graph **gout, graph *hin, graph **hout, *
* int *lab, int *ptn, optionblk *options, *
* statsblk *stats, int m, int n) *
* Cleanup routine for dispatch vector. This one sorts the adjacency *
* lists for the canonical labelling. *
* *
*****************************************************************************/
void
cleanup_sg(graph *gin, graph **gout, graph *hin, graph **hout, int *lab,
int *ptn, optionblk *options, statsblk *stats, int m, int n)
{
sparsegraph *sh;
if (options->getcanon
&& (stats->errstatus == 0 || stats->errstatus == NAUABORTED))
{
sh = (sparsegraph*)hin;
sortlists_sg(sh);
}
}
/*****************************************************************************
* *
* distvals(sparsegraph *sg, int v0, int *dist, int n) sets dist[i] *
* to the distance from v0 to i, for each i, or to n if there is no such *
* distance. work4[] is used as a queue. *
* *
*****************************************************************************/
void
distvals(sparsegraph *g, int v0, int *dist, int n)
{
int *d,*e;
int i,head,tail;
int di,k;
size_t *v,vi,j;
SG_VDE(g,v,d,e);
#if !MAXN
DYNALLOC1(int,work4,work4_sz,n,"distvals");
#endif
#define QUEUE work4
for (i = 0; i < n; ++i) dist[i] = n;
QUEUE[0] = v0;
dist[v0] = 0;
head = 0;
tail = 1;
while (tail < n && head < tail)
{
i = QUEUE[head++];
vi = v[i];
di = d[i];
for (j = 0; j < di; ++j)
{
k = e[vi+j];
if (dist[k] == n)
{
dist[k] = dist[i] + 1;
QUEUE[tail++] = k;
}
}
}
}
/*****************************************************************************
* *
* refine_sg(g,lab,ptn,level,numcells,count,active,code,m,n) performs a *
* refinement operation on the partition at the specified level of the *
* partition nest (lab,ptn). *numcells is assumed to contain the number of *
* cells on input, and is updated. The initial set of active cells (alpha *
* in the paper) is specified in the set active. Precisely, x is in active *
* iff the cell starting at index x in lab is active. *
* The resulting partition is equitable if active is correct (see the paper *
* and the Guide). *
* *code is set to a value which depends on the fine detail of the *
* algorithm, but which is independent of the labelling of the graph. *
* count is used for work space. *
* *
*****************************************************************************/
void
refine_sg(graph *g, int *lab, int *ptn, int level, int *numcells,
int *count, set *active, int *code, int m, int n)
{
int i,j,k,l,v1,v2,v3,isplit;
int w1,w2,w3;
long longcode;
int *d,*e;
int size,bigsize,bigpos;
int nactive,hitcells;
int lj,di,splitv;
boolean trivsplit;
size_t *v,vi,ii;
SG_VDE(g,v,d,e);
#if !MAXN
DYNALLOC1(int,work1,work1_sz,n,"refine_sg");
DYNALLOC1(int,work2,work2_sz,n,"refine_sg");
DYNALLOC1(int,work3,work3_sz,n,"refine_sg");
DYNALLOC1(int,work4,work4_sz,n,"refine_sg");
#endif
#define CELLSTART work1
#define ACTIVE work2
#define HITS work3
#define HITCELL work4
PREPAREMARKS1(n);
PREPAREMARKS2(n);
longcode = *numcells;
/* Set ACTIVE[0..nactive-1] = queue of active cell starts */
nactive = 0;
for (i = -1; (i = nextelement(active,m,i)) >= 0;)
ACTIVE[nactive++] = i;
if (nactive == 0)
{
*code = CLEANUP(longcode);
return;
}
/* Set CELLSTART[i] = starting point in lab[] of nontrivial cell
containing i, or n if i is a singleton */
for (i = 0; i < n; )
{
/* Just here, i is a cell starting position */
if (ptn[i] <= level)
{
CELLSTART[lab[i]] = n;
++i;
}
else
{
j = i;
do
{
CELLSTART[lab[i]] = j;
} while (ptn[i++] > level);
}
}
if (level <= 2 && nactive == 1 && ptn[ACTIVE[0]] <= level
&& *numcells <= n/8)
{
isplit = ACTIVE[--nactive];
DELELEMENT(active,isplit);
distvals((sparsegraph*)g,lab[isplit],HITS,n);
for (v1 = 0; v1 < n; )
{
if (ptn[v1] <= level)
{
++v1;
continue;
}
longcode = MASH(longcode,v1);
w1 = HITS[lab[v1]];
v2 = v1+1;
while (ptn[v2-1] > level && HITS[lab[v2]] == w1) ++v2;
if (ptn[v2-1] <= level)
{
v1 = v2;
continue;
}
w2 = NAUTY_INFINITY;
v3 = j = v2;
do
{
lj = lab[j];
w3 = HITS[lj];
if (w3 == w1)
{
lab[j] = lab[v3];
lab[v3] = lab[v2];
lab[v2] = lj;
++v2;
++v3;
}
else if (w3 == w2)
{
lab[j] = lab[v3];
lab[v3] = lj;
++v3;
}
else if (w3 < w1)
{
lab[j] = lab[v2];
lab[v2] = lab[v1];
lab[v1] = lj;
v3 = v2 + 1;
v2 = v1 + 1;
w2 = w1;
w1 = w3;
}
else if (w3 < w2)
{
lab[j] = lab[v2];
lab[v2] = lj;
v3 = v2 + 1;
w2 = w3;
}
} while (ptn[j++] > level);
longcode = MASH(longcode,w2);
longcode = MASH(longcode,v2);
if (j != v2) /* At least two fragments
* v1..v2-1 = w1; v2..v3-1 = w2 */
{
if (v2 == v1+1)
CELLSTART[lab[v1]] = n;
if (v3 == v2+1)
CELLSTART[lab[v2]] = n;
else
for (k = v2; k < v3; ++k)
CELLSTART[lab[k]] = v2;
++*numcells;
ptn[v2-1] = level;
if (j == v3)
{
/* Two fragments only */
if (v2-v1 <= v3-v2 && !ISELEMENT(active,v1))
{
ADDELEMENT(active,v1);
ACTIVE[nactive++] = v1;
}
else
{
ADDELEMENT(active,v2);
ACTIVE[nactive++] = v2;
}
}
else
{
/* Extra fragments: v3..j-1 > w2 */
sortindirect(lab+v3,HITS,j-v3);
ACTIVE[nactive++] = v2;
ADDELEMENT(active,v2);
if (v2-v1 >= v3-v2)
{
bigpos = -1;
bigsize = v2-v1;
}
else
{
bigpos = nactive-1;
bigsize = v3-v2;
}
for (k = v3-1; k < j-1;)
{
ptn[k] = level;
longcode = MASH(longcode,k);
++*numcells;
l = k+1;
ADDELEMENT(active,l);
ACTIVE[nactive++] = l;
w3 = HITS[lab[l]];
for (k = l; k < j-1
&& HITS[lab[k+1]] == w3; ++k)
CELLSTART[lab[k+1]] = l;
size = k-l+1;
if (size == 1)
CELLSTART[lab[l]] = n;
else
{
CELLSTART[lab[l]] = l;
if (size > bigsize)
{
bigsize = size;
bigpos = nactive-1;
}
}
}
if (bigpos >= 0 && !ISELEMENT(active,v1))
{
longcode = MASH(longcode,bigpos);
DELELEMENT(active,ACTIVE[bigpos]);
ADDELEMENT(active,v1);
ACTIVE[bigpos] = v1;
}
}
}
v1 = j;
}
}
/* Iterate until complete */
while (nactive > 0 && *numcells < n)
{
for (i = 0; i < nactive && i < 10; ++i)
if (ptn[ACTIVE[i]] <= level) break;
if (i < nactive && i < 10)
{
trivsplit = TRUE;
isplit = ACTIVE[i];
ACTIVE[i] = ACTIVE[--nactive];
}
else
{
isplit = ACTIVE[--nactive];
trivsplit = ptn[isplit] <= level;
}
DELELEMENT(active,isplit);
longcode = MASH(longcode,isplit);
if (trivsplit)
{
RESETMARKS1;
RESETMARKS2;
hitcells = 0;
splitv = lab[isplit];
vi = v[splitv];
di = d[splitv];
for (ii = 0; ii < di; ++ii)
{
j = e[vi+ii];
MARK2(j);
k = CELLSTART[j];
if (k != n && ISNOTMARKED1(k))
{
MARK1(k);
HITCELL[hitcells++] = k;
}
}
if (hitcells > 1) sortints(HITCELL,hitcells);
longcode = MASH(longcode,hitcells);
/* divide cells according to which vertices are hit */
for (i = 0; i < hitcells; ++i)
{
j = v1 = v2 = HITCELL[i];
longcode = MASH(longcode,v2);
k = 0;
do
{
lj = lab[j];
if (ISMARKED2(lj))
HITS[k++] = lj;
else
lab[v2++] = lj;
} while (ptn[j++] > level);
longcode = MASH(longcode,k);
v3 = v2;
while (--k >= 0)
{
j = HITS[k];
CELLSTART[j] = v2;
lab[v3++] = j;
}
if (v2 != v3 && v2 != v1)
{
++*numcells;
if (v2 == v1+1) CELLSTART[lab[v1]] = n;
if (v3 == v2+1) CELLSTART[lab[v2]] = n;
ptn[v2-1] = level;
longcode = MASH(longcode,v2);
if (v2-v1 <= v3-v2 && !ISELEMENT(active,v1))
{
ADDELEMENT(active,v1);
ACTIVE[nactive++] = v1;
}
else
{
ADDELEMENT(active,v2);
ACTIVE[nactive++] = v2;
}
}
}
}
else /* non-trivial splitting */
{
/* isplit is the start of the splitting cell.
Set HITS[i] = hits of i for i in non-trivial cells,
HITCELL[0..hitcells-1] = starts of hit non-trivial cells */
RESETMARKS1;
hitcells = 0;
do
{
vi = v[lab[isplit]];
di = d[lab[isplit]];
for (ii = 0; ii < di; ++ii)
{
j = e[vi+ii];
k = CELLSTART[j];
if (k != n)
{
if (ISNOTMARKED1(k))
{
MARK1(k);
HITCELL[hitcells++] = k;
do
{
HITS[lab[k]] = 0;
} while (ptn[k++] > level);
}
++HITS[j];
}
}
} while (ptn[isplit++] > level);
if (hitcells > 1) sortints(HITCELL,hitcells);
/* divide cells according to hit counts */
longcode = MASH(longcode,hitcells);
for (i = 0; i < hitcells; ++i)
{
v1 = HITCELL[i];
w1 = HITS[lab[v1]];
longcode = MASH(longcode,v1);
v2 = v1+1;
while (ptn[v2-1] > level && HITS[lab[v2]] == w1) ++v2;
if (ptn[v2-1] <= level) continue;
w2 = NAUTY_INFINITY;
v3 = j = v2;
do
{
lj = lab[j];
w3 = HITS[lj];
if (w3 == w1)
{
lab[j] = lab[v3];
lab[v3] = lab[v2];