-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
1293 lines (1217 loc) · 32.1 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"sort"
"strconv"
"strings"
"time"
)
// degs is a slice of vertex degrees
type degs []int
// copy copies the slice, so that the copy can be modified safely without
// affecting the initial slice.
func (d degs) copy() degs {
r := make(degs, len(d))
copy(r, d)
return r
}
// sum counts the sum of vertex degrees.
func (d degs) sum() (s int) {
for _, d := range d {
s += d
}
return s
}
// bVer denotes a boundary vertex by the presence of boundary darts incident to it.
type bVer struct{ l, r bool }
// hole defines a boundary component of a surface.
// If the root lies not on this boundary component, this slice is considered up to cyclic shifts.
type hole []bVer
// copy deep-copies this hole.
func (h hole) copy() hole {
r := make(hole, len(h))
copy(r, h)
return r
}
// insert inserts a new bVer into this hole into every position, with every combination
// of boundary vertices; hole is rotated so that the inserted vertex is always at index 0
// in the result.
func (h hole) insert() (res holes) {
for mask := 0; mask < 4; mask++ {
n := hole{{mask&1 > 0, mask&2 > 0}}
rot := h.rotate()
if len(rot) == 0 {
rot = holes{{}}
}
for _, h := range rot {
res = append(res, append(n, h...))
}
}
return res
}
// split splits this hole into two holes, along an edge from vertex 0 to every
// other bVer and to any interval between two bVers.
func (h hole) split() (res [][2]hole) {
for i := 1; i < len(h); i++ {
res = append(res, [2]hole{
append(hole{{h[i].l, h[0].r}}, h[i+1:]...),
append(hole{{h[0].l, h[i].r}}, h[1:i]...),
})
}
for i := 1; i <= len(h); i++ {
for mask := 0; mask < 4; mask++ {
res = append(res, [2]hole{
append(hole{{mask&1 > 0, h[0].r}}, h[i:]...),
append(hole{{h[0].l, mask&2 > 0}}, h[1:i]...),
})
}
}
return res
}
// flip returns the result of fliping this hole, that is,
// changing the cyclic ordering of vertices to opposite.
func (h hole) flip() hole {
res := make(hole, len(h))
for i := range h {
res[i] = h[(len(h)-i)%len(h)]
res[i].l, res[i].r = res[i].r, res[i].l
}
return res
}
// rotate returns the result of cyclically permuting this hole for every possible index.
func (h hole) rotate() (res holes) {
for i := range h {
var n hole
n = append(n, h[i:]...)
n = append(n, h[:i]...)
res = append(res, n)
}
return res
}
// normalize replaces this hole with its canonical representation,
// which is equal to some cyclic shift of it. All holes that
// differ only by cyclic shifts will have the same canonical representation.
// This function uses Booth's Algorithm for finding lexicographically minimal string rotation.
func (h hole) normalize() {
s := make([]int, len(h))
for i, b := range h {
if b.l {
s[i] |= 0x1
}
if b.r {
s[i] |= 0x2
}
}
s = append(s, s...)
f := make([]int, len(s))
for i := range s {
f[i] = -1
}
k := 0
for j := 1; j < len(s); j++ {
sj := s[j]
i := f[j-k-1]
for i != -1 && sj != s[k+i+1] {
if sj < s[k+i+1] {
k = j - i - 1
}
i = f[i]
}
if sj != s[k+i+1] {
if sj < s[k] {
k = j
}
f[j-k] = -1
} else {
f[j-k] = i + 1
}
}
if k == 0 {
return
}
n := append(h[k:], h[:k]...)
copy(h, n)
}
// String converts this hole to an immutable representation, which can be used in map keys.
// The result won't be a human-readable string.
func (h hole) String() string {
bs := make([]byte, len(h))
for i, b := range h {
if b.l {
bs[i] |= 0x1
}
if b.r {
bs[i] |= 0x2
}
}
return string(bs)
}
// holes is a slice of hole-s.
type holes []hole
// Len is a utility function required for sorting holes.
func (h holes) Len() int {
return len(h)
}
// Swap is a utility function required for sorting holes.
func (h holes) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
// Less is a utility function required for sorting holes.
func (h holes) Less(i, j int) bool {
if len(h[i]) != len(h[j]) {
return len(h[i]) < len(h[j])
}
for k := range h[i] {
if h[i][k].l != h[j][k].l {
return h[i][k].l
}
if h[i][k].r != h[j][k].r {
return h[i][k].r
}
}
return false
}
// normalize normalizes this slice of holes:
// normalizes every hole individually and then sorts the result.
func (hs holes) normalize() {
for _, h := range hs {
h.normalize()
}
sort.Sort(hs)
}
// copy deep-copies a slice of holes.
func (hs holes) copy() holes {
r := make(holes, len(hs))
for i, h := range hs {
r[i] = h.copy()
}
return r
}
// filp flips all holes in the slice.
func (hs holes) flip() (res holes) {
for _, h := range hs {
res = append(res, h.flip())
}
return res
}
// selAll returns the result of "selecting" each hole in this slice, that is, moving
// it to the 0th position.
func (hs holes) selAll() (res []holes) {
for i, h := range hs {
nhs := make(holes, len(hs))
nhs[0] = h
copy(nhs[1:], hs[:i])
copy(nhs[i+1:], hs[i+1:])
res = append(res, nhs)
}
return res
}
// pwSet splits the given set into two subsets in all possible 2^n ways.
// It is assumed that although all elements are distinct, the only thing that
// really matters for the caller is their string representations, so some
// ways to split may yield practically the same result. To optimize the
// return format in this case, each "practically distinct" way of splitting is
// mapped to the number of ways it occurred.
// Example: {x[String() == "a"], y[String() == "a"], z[String() == "b"]} will
// yield six ways of splitting, with multiplicities:
// ({x, y}, {z}, 1), ({x}, {y, z}, 2), ({}, {x, y, z}, 1).
// ({x, y, z}, {}, 1), ({x, z}, {y}, 2), ({z}, {x, y}, 1).
func pwSet(xs []fmt.Stringer) map[*[2][]fmt.Stringer]int64 {
var m [][]fmt.Stringer
idx := map[string]int{}
for _, x := range xs {
s := x.String()
id := idx[s]
if _, ok := idx[s]; !ok {
id = len(idx)
idx[s] = id
m = append(m, nil)
}
m[id] = append(m[id], x)
}
res := map[*[2][]fmt.Stringer]int64{}
var rec func(int, int64, [2][]fmt.Stringer)
rec = func(i int, cnt int64, cur [2][]fmt.Stringer) {
if i == len(m) {
cur[0] = append(([]fmt.Stringer)(nil), cur[0]...)
cur[1] = append(([]fmt.Stringer)(nil), cur[1]...)
res[&cur] = cnt
return
}
for k := 0; k <= len(m[i]); k++ {
n := len(m[i])
rec(i+1, cnt*fact(n)/fact(k)/fact(n-k), [2][]fmt.Stringer{
append(cur[0], m[i][:k]...),
append(cur[1], m[i][k:]...),
})
}
}
rec(0, 1, [2][]fmt.Stringer{})
return res
}
// IRRELEVANT is a special value denoting that we should ignore the value of innervf
// and count all maps regardless of the number of inner vertices and faces.
const IRRELEVANT = 128
// signature is the core object of the computation this program performs:
// it defines a class of maps on a surface that can be counted recursively.
type signature struct {
// If !maybeNonorientable, the number of handles.
// If maybeNonorientable, this number is defined by the Euler's characteristics in such a way
// that if the map is indeed non-orientable, this is the number of crosscaps.
genus int
// Boundary components of the surface that the root vertex does not lie on.
holes holes
// Degrees of non-root distinguished vertices lying in the surface interior.
degs degs
// Total darts on the surface (strictly speaking, 2 * #flags).
darts int
// If nil or empty, the root vertex is internal;
// otherwise it's the boundary component containing the root vertex.
hole0 hole
// Degree of the root vertex.
deg0 int
// If false, the surface is orientable;
// if true, we count maps regardless of surface orientability.
maybeNonorientable bool
// The sum of numbers of vertices and faces in the interior of the surface.
innervf int
}
// copy deep-copies this signature.
func (s signature) copy() *signature {
ns := s
ns.degs = s.degs.copy()
ns.holes = s.holes.copy()
return &ns
}
// incInnerVF increases the value of innervf respecting special value "IRRELEVANT".
func (s *signature) incInnerVF(val int) {
if s.innervf == IRRELEVANT {
return
}
s.innervf += val
}
// splitInnerVF splits interior vertices and faces among two new maps in all possible ways.
func (s *signature) splitInnerVF() (res [][2]int) {
if s.innervf == IRRELEVANT {
return [][2]int{{IRRELEVANT, IRRELEVANT}}
}
for i := 0; i <= s.innervf; i++ {
res = append(res, [2]int{i, s.innervf - i})
}
return res
}
// isClosedSurface returns true iff the surface has no boundary.
func (s *signature) isClosedSurface() bool {
return len(s.hole0) == 0 && len(s.holes) == 0
}
// intStringer makes an integer satisfy fmt.Stringer interface.
type intStringer int
// String makes intStringer satisfy fmt.Stringer.
func (i intStringer) String() string {
return strconv.Itoa(int(i))
}
// split split this signature into two, in all possible ways:
// chosen vertices and holes are split among two maps in 2^n ways,
// genus and darts are split as "unlabelled" objects, in n+1 ways.
func (s *signature) split() map[*[2]signature]int64 {
degSlice := make([]fmt.Stringer, len(s.degs))
for i, d := range s.degs {
degSlice[i] = intStringer(d)
}
degSet := make(map[*[2]degs]int64)
for degss, cnt := range pwSet(degSlice) {
var d [2]degs
for _, deg := range (*degss)[0] {
d[0] = append(d[0], int(deg.(intStringer)))
}
for _, deg := range (*degss)[1] {
d[1] = append(d[1], int(deg.(intStringer)))
}
degSet[&d] = cnt
}
holeSlice := make([]fmt.Stringer, len(s.holes))
for i, d := range s.holes {
holeSlice[i] = d
}
holeSet := make(map[*[2]holes]int64)
for holess, cnt := range pwSet(holeSlice) {
var h [2]holes
for _, hl := range (*holess)[0] {
h[0] = append(h[0], hl.(hole))
}
for _, hl := range (*holess)[1] {
h[1] = append(h[1], hl.(hole))
}
holeSet[&h] = cnt
}
res := map[*[2]signature]int64{}
for degss, degCnt := range degSet {
lb2 := (*degss)[0].sum()
hb2 := s.darts - (*degss)[1].sum()
if lb2 > hb2 {
continue
}
for holess, holeCnt := range holeSet {
for g := 0; g <= s.genus; g++ {
lb, hb := 0, s.darts
/* optimization */
lb, hb = 2*g+len(holess[0])+2*len(degss[0]), s.darts-2*(s.genus-g)-len(holess[1])-2*len(degss[1])
if !s.maybeNonorientable {
lb += 2 * g
hb -= 2 * (s.genus - g)
}
if lb2 > lb {
lb = lb2
}
if hb2 < hb {
hb = hb2
}
/* end optimization */
for d := lb; d <= hb; d++ {
for _, ivf := range s.splitInnerVF() {
res[&[2]signature{{
genus: g,
holes: (*holess)[0],
degs: (*degss)[0],
darts: d,
hole0: s.hole0,
deg0: s.deg0,
maybeNonorientable: s.maybeNonorientable,
innervf: ivf[0],
}, {
genus: s.genus - g,
holes: (*holess)[1],
degs: (*degss)[1],
darts: s.darts - d,
maybeNonorientable: s.maybeNonorientable,
innervf: ivf[1],
}}] = degCnt * holeCnt
}
}
}
}
}
return res
}
// String returns a string representation of this signature which
// defines it uniquely; it is needed in order to use signatures as map keys.
func (s *signature) String() string {
// We encode each parameter with 1 byte, assuming it never goes above
// 255. Realistic values of each parameter are < 30.
cnt := 1 + len(s.hole0)
for _, h := range s.holes {
cnt += 1 + len(h)
}
b := make([]byte, 6+cnt+len(s.degs))
b[0] = byte(s.innervf)
b[1] = byte(s.darts)
b[2] = byte(s.genus)
b[3] = byte(s.deg0)
if s.maybeNonorientable {
b[4] = 1
}
j := 5
b[j] = byte(len(s.holes))
j++
for i := 0; i <= len(s.holes); i++ {
h := s.hole0
if i != len(s.holes) {
h = s.holes[i]
}
b[j] = byte(len(h))
j++
for _, v := range h {
if v.l {
b[j] |= 2
}
if v.r {
b[j] |= 4
}
j++
}
}
for _, d := range s.degs {
b[j] = byte(d)
j++
}
return string(b)
}
// m is a cache storing a mapping from map signature (as string)
// to the number of maps with this signature.
var m = map[string]int64{
(&signature{0, nil, nil, 0, nil, 0, true, 2}).String(): 1,
(&signature{0, nil, nil, 0, nil, 0, false, 2}).String(): 1,
(&signature{0, nil, nil, 0, nil, 0, true, IRRELEVANT}).String(): 1,
(&signature{0, nil, nil, 0, nil, 0, false, IRRELEVANT}).String(): 1,
(&signature{0, nil, nil, 0, hole{{}}, 0, true, 0}).String(): 1,
(&signature{0, nil, nil, 0, hole{{}}, 0, false, 0}).String(): 1,
(&signature{0, nil, nil, 0, hole{{}}, 0, true, IRRELEVANT}).String(): 1,
(&signature{0, nil, nil, 0, hole{{}}, 0, false, IRRELEVANT}).String(): 1,
}
// Chi is the Euler characteristic of the surface.
func (s *signature) Chi() int {
var res int
if len(s.hole0) > 0 {
res--
}
if s.maybeNonorientable {
res += 2 - s.genus - len(s.holes)
} else {
res += 2 - 2*s.genus - len(s.holes)
}
return res
}
// reduce_JoinInternal contracts an edge that leads into a non-distinguished vertex in the surface interior.
func (s signature) reduce_JoinInternal() (res []signature) {
if s.deg0 <= 0 {
return nil
}
s.darts -= 2
s.incInnerVF(-1)
for d := s.deg0 - 1; d <= s.darts-s.degs.sum(); d++ {
s := s
s.deg0 = d
res = append(res, s)
}
return res
}
// reduce_JoinInternalChosen contracts an edge that leads into a distinguished vertex in the surface interior.
func (s signature) reduce_JoinInternalChosen() (res []signature) {
if s.deg0 <= 0 {
return nil
}
s.incInnerVF(-1)
for i := 0; i < len(s.degs); i++ {
s := s
s.deg0 += s.degs[i] - 2
s.degs = append(append((degs)(nil), s.degs[:i]...), s.degs[i+1:]...)
s.darts -= 2
res = append(res, s)
}
return res
}
// zeroCount is a debug counter that counts how many times signature.count()
// returned zero or non-zero by computing the value instead of taking it from cache.
var zeroCount = map[bool]int{}
// minDarts returns the lower bound of darts necessary to make this map connected.
func (s *signature) minDarts() int {
cnt := s.deg0 + s.degs.sum()
return cnt
}
// isMaybeRealizable returns false if a map with this signature definitely cannot exist.
// It returns true if it can't tell. This is for optimization purposes, to prune
// "useless" recursion branches.
func (s *signature) isMaybeRealizable() bool {
if s.deg0 < 0 || (s.deg0 == 0 && len(s.hole0) == 0) || s.genus < 0 {
return false
}
if s.darts < s.minDarts() {
return false
}
closed := s.isClosedSurface()
if closed && s.darts%2 == 1 {
return false
}
if s.innervf == IRRELEVANT {
k := 0
if !closed {
k = -2
}
if len(s.hole0) > 0 {
k = -3
if len(s.holes) == 0 {
k = -2
}
}
if s.darts < (4-2*s.Chi())+len(s.holes)+k+2*len(s.degs) {
return false
}
return true
}
if s.innervf < len(s.degs) {
return false
}
if 2*s.innervf > s.darts+2*s.Chi()+len(s.holes) {
return false
}
if !s.isClosedSurface() {
return true
}
if 2*s.innervf-s.darts != 2*s.Chi() {
return false
}
return true
}
// count enumerates maps with a given signature.
func (s *signature) count() int64 {
sort.Ints(s.degs)
s.holes.normalize()
k := s.String()
if c, ok := m[k]; ok {
return c
}
if !s.isMaybeRealizable() {
return 0
}
cnt := s.countInterior() + s.countExterior()
m[k] = cnt
zeroCount[cnt == 0]++
return cnt
}
// reduce_Internal_EdgeIntoHole returns all signatures that can arise
// after contracting a complete root edge that starts in surface interior and ends on a boundary.
func (s signature) reduce_Internal_EdgeIntoHole() (sgns []signature) {
if len(s.hole0) != 0 || s.deg0 <= 0 {
return nil
}
s.incInnerVF(-1)
for _, hs := range s.holes.selAll() {
hole0s := append(hs[0].insert(), hs[0].rotate()...)
if s.maybeNonorientable {
n := len(hole0s)
for i := 0; i < n; i++ {
hole0s = append(hole0s, hole0s[i].flip())
}
}
for _, nh := range hole0s {
for d := s.deg0 - 1; d <= s.darts-s.degs.sum()-2; d++ {
s := s
s.deg0 = d
s.darts -= 2
s.hole0 = nh
s.holes = hs[1:]
if len(s.holes) == 0 {
s.holes = nil
}
sgns = append(sgns, s)
}
}
}
return sgns
}
// reduce_Internal_EdgeIntoHole returns all signatures that can arise
// after contracting a root halfedge that starts in surface interior and ends on a boundary.
func (s signature) reduce_Internal_SemiedgeIntoHole() (sgns []signature) {
if len(s.hole0) != 0 || s.deg0 <= 0 {
return nil
}
s.incInnerVF(-1)
for _, hs := range s.holes.selAll() {
hole0s := append(hs[0].insert())
if s.maybeNonorientable {
n := len(hole0s)
for i := 0; i < n; i++ {
hole0s = append(hole0s, hole0s[i].flip())
}
}
for _, nh := range hole0s {
if nh[0] != (bVer{}) {
continue
}
s := s
s.deg0--
s.darts--
s.hole0 = nh
s.holes = hs[1:]
sgns = append(sgns, s)
}
}
return sgns
}
// reduce_Internal_DecreaseGenus returns all signatures that can arise
// after contracting a root edge that is a loop lying in surface interior, such that
// this contraction cuts either a handle or two crosscaps.
func (s signature) reduce_Internal_DecreaseGenus() (sgns []signature) {
if s.genus <= 0 || len(s.hole0) != 0 || (s.maybeNonorientable && s.genus == 1) {
return nil
}
s.incInnerVF(+1)
for d := 1; d <= s.deg0-3; d++ {
s := s
s.genus--
if s.maybeNonorientable {
s.genus-- // number of crosscaps
}
s.darts -= 2
s.degs = append(s.degs.copy(), d)
s.deg0 = s.deg0 - d - 2
sgns = append(sgns, s)
}
return sgns
}
// reduce_Internal_CutCrosscap returns all signatures that can arise
// after contracting a root edge that is a loop lying in surface interior, such that
// this contraction cuts a crosscap.
func (s signature) reduce_Internal_CutCrosscap() []signature {
if s.genus <= 0 || len(s.hole0) != 0 || !s.maybeNonorientable || s.deg0 < 2 {
return nil
}
s.genus--
s.deg0 -= 2
s.darts -= 2
return []signature{s}
}
// reduce_Internal_Split returns all signatures that can arise
// after contracting a root edge that is a loop lying in surface interior, such that
// this contraction splits a map into two maps.
func (s signature) reduce_Internal_Split() map[*[2]signature]int64 {
if s.genus < 0 || len(s.hole0) != 0 {
return nil
}
s.darts -= 2
s.incInnerVF(+1)
res := map[*[2]signature]int64{}
for sgns, cnt := range s.split() {
for deg := 0; deg <= s.deg0-2; deg++ {
var newSgns [2]signature
newSgns = *sgns
newSgns[0].deg0 = deg
newSgns[1].deg0 = s.deg0 - 2 - deg
res[&newSgns] = cnt
}
}
return res
}
// countInterior counts maps with the root in the interior of the surface.
func (s signature) countInterior() (cnt int64) {
if len(s.hole0) != 0 {
return 0
}
for _, t := range s.reduce_Internal_EdgeIntoHole() {
cnt += int64(t.deg0-s.deg0+2) * t.count()
}
for _, t := range s.reduce_Internal_SemiedgeIntoHole() {
cnt += t.count()
}
for _, t := range s.reduce_JoinInternal() {
cnt += t.count()
}
for _, t := range s.reduce_JoinInternalChosen() {
k := int64(1)
if s.maybeNonorientable {
k = 2
}
cnt += k * int64(t.deg0-s.deg0+2) * t.count()
}
for _, t := range s.reduce_Internal_DecreaseGenus() {
cnt += t.count()
}
for sgns, c := range s.reduce_Internal_Split() {
cnt += c * (*sgns)[0].count() * (*sgns)[1].count()
}
for _, t := range s.reduce_Internal_CutCrosscap() {
cnt += int64(s.deg0-1) * t.count()
}
return cnt
}
// reduce_External_AlongHole returns all signatures that can arise
// after contracting a root edge that runs along a boundary component.
func (s signature) reduce_External_AlongHole() (res []signature) {
if len(s.hole0) == 0 || !s.hole0[0].l {
return nil
}
if len(s.hole0) == 1 && s.hole0[0].r {
s := s
s.hole0 = nil
s.darts--
s.incInnerVF(+1)
res = append(res, s)
}
if len(s.hole0) > 1 && s.hole0[1].r {
for d := 0; d < s.darts-s.deg0-s.degs.sum(); d++ {
s := s
s.deg0 += d
s.hole0 = append(hole{{s.hole0[1].l, s.hole0[0].r}}, s.hole0[2:]...)
s.darts--
res = append(res, s)
}
}
for d := 0; d < s.darts-s.deg0-s.degs.sum(); d++ {
for _, l := range []bool{true, false} {
s := s
s.deg0 += d
s.hole0 = append(hole{{l, s.hole0[0].r}}, s.hole0[1:]...)
s.darts--
res = append(res, s)
}
}
return res
}
// reduce_External_AnotherHole returns all signatures that can arise
// after contracting a root edge that joins two boundary components.
func (s signature) reduce_External_AnotherHole() (res []signature) {
h := s.hole0
if len(s.holes) == 0 || len(h) == 0 || h[0].l || s.deg0 <= 0 {
return
}
s.hole0 = nil
s.incInnerVF(+1)
for _, s := range s.reduce_Internal_EdgeIntoHole() {
nh := hole{{s.hole0[0].l, h[0].r}}
nh = append(nh, s.hole0[1:]...)
nh = append(nh, bVer{false, s.hole0[0].r})
nh = append(nh, h[1:]...)
s.hole0 = nh
res = append(res, s)
}
return res
}
// reduce_External_HoleDecreaseGenus returns all signatures that can arise
// after contracting a root edge that joins two boundary components.
func (s signature) reduce_External_HoleDecreaseGenus() (res []signature) {
if len(s.hole0) == 0 || s.hole0[0].l || s.genus <= 0 || s.deg0 <= 0 || (s.maybeNonorientable && s.genus == 1) {
return
}
for _, hs := range s.hole0.split() {
for d := s.deg0 - 1; d <= s.darts-2-s.degs.sum(); d++ {
s := s
s.hole0 = hs[0]
s.holes = append(holes{hs[1]}, s.holes...)
s.darts -= 2
s.deg0 = d
s.genus--
if s.maybeNonorientable {
s.genus--
}
res = append(res, s)
}
}
return res
}
// reduce_External_LoopDecreaseGenus returns all signatures that can arise
// after contracting a root edge that is a loop incident to a vertex on the boundary,
// such that this contraction cuts either a handle or two crosscaps.
func (s signature) reduce_External_LoopDecreaseGenus() (res []signature) {
if len(s.hole0) == 0 || s.hole0[0].l || s.genus <= 0 || s.deg0 <= 1 {
return
}
h := s.hole0
s.hole0 = nil
s.incInnerVF(+1)
for _, s := range s.reduce_Internal_DecreaseGenus() {
s.hole0 = h
s.incInnerVF(-1)
res = append(res, s)
}
return res
}
// reduce_External_LoopCutCrosscap returns all signatures that can arise
// after contracting a root edge that is a loop incident to a vertex on the boundary,
// such that this contraction cuts a crosscap.
func (s signature) reduce_External_LoopCutCrosscap() (res []signature) {
if len(s.hole0) == 0 || s.hole0[0].l || !s.maybeNonorientable {
return
}
h := s.hole0
s.hole0 = nil
s.incInnerVF(+1)
for _, t := range s.reduce_Internal_CutCrosscap() {
t.hole0 = h
t.incInnerVF(-1)
res = append(res, t)
}
return res
}
// reduce_External_HoleCutCrosscap returns all signatures that can arise
// after contracting a root edge that joins two vertices on the same boundary component
// and goes through a crosscap.
func (s signature) reduce_External_HoleCutCrosscap() (res []signature) {
if len(s.hole0) == 0 || s.hole0[0].l || s.deg0 <= 0 || !s.maybeNonorientable {
return
}
for _, hs := range s.hole0.split() {
hs[1] = hs[1].flip()
h := hole{{hs[1][0].l, hs[0][0].r}}
h = append(h, hs[1][1:]...)
h = append(h, bVer{hs[0][0].l, hs[1][0].r})
h = append(h, hs[0][1:]...)
s := s
s.hole0 = h
s.darts -= 2
s.genus--
for d := s.deg0 - 1; d <= s.darts-s.degs.sum(); d++ {
s.deg0 = d
res = append(res, s)
}
}
return res
}
// reduce_External_LoopSplit returns all signatures that can arise
// after contracting a root edge that is a loop incident to a vertex on the boundary,
// such that this contraction splits the map into two.
func (s signature) reduce_External_LoopSplit() map[*[2]signature]int64 {
if len(s.hole0) == 0 || s.hole0[0].l {
return nil
}
h := s.hole0
s.hole0 = nil
s.incInnerVF(+1)
res := map[*[2]signature]int64{}
for sgns, cnt := range s.reduce_Internal_Split() {
var newSgns [2]signature
newSgns = *sgns
newSgns[0].hole0 = h
newSgns[0].incInnerVF(-1)
res[&newSgns] = cnt
}
return res
}
// reduce_External_HoleSplit returns all signatures that can arise
// after contracting a root edge that joins two vertices on the same boundary component
// such that this contraction splits the map into two.
func (s signature) reduce_External_HoleSplit() map[*[2]signature]int64 {
if len(s.hole0) == 0 || s.hole0[0].l || s.deg0 <= 0 {
return nil
}
s.darts -= 2
res := map[*[2]signature]int64{}
for sgns, cnt := range s.split() {
for _, hs := range s.hole0.split() {
for d0 := s.deg0 - 1; d0 <= (*sgns)[0].darts; d0++ {
for d1 := 0; d1 <= (*sgns)[1].darts; d1++ {
var newSgns [2]signature
newSgns = *sgns
newSgns[0].hole0 = hs[0]
newSgns[1].hole0 = hs[1]
newSgns[0].deg0 = d0
newSgns[1].deg0 = d1
res[&newSgns] = cnt
}
}
}
}
return res
}
// countExterior counts maps that have a root vertex that lies on the boundary.
func (s signature) countExterior() (cnt int64) {
if len(s.hole0) == 0 {
return 0
}
if s.hole0[0].r && !s.hole0[0].l {
s.hole0 = s.hole0.flip()
s.holes = s.holes.flip()
}
for _, t := range s.reduce_External_AlongHole() {
cnt += t.count()
}
for _, s := range s.reduce_External_AnotherHole() {
cnt += s.count()
}
for _, s := range s.reduce_External_HoleDecreaseGenus() {
cnt += s.count()
}
for _, s := range s.reduce_External_LoopDecreaseGenus() {
cnt += s.count()
}
for _, s := range s.reduce_External_LoopCutCrosscap() {
cnt += int64(s.deg0+1) * s.count()
}
for sgns, c := range s.reduce_External_LoopSplit() {
cnt += c * (*sgns)[0].count() * (*sgns)[1].count()
}
for sgns, c := range s.reduce_External_HoleSplit() {
cnt += c * (*sgns)[0].count() * (*sgns)[1].count()
}
for _, t := range s.reduce_External_HoleCutCrosscap() {
cnt += t.count()
}
if !s.hole0[0].l {
for _, t := range s.reduce_JoinInternal() {
cnt += t.count()
}
for _, t := range s.reduce_JoinInternalChosen() {
k := int64(1)
if s.maybeNonorientable {
k = 2
}
cnt += k * int64(t.deg0-s.deg0+2) * t.count()
}
{
// dangling edge to a hole
s := s
s.darts--
s.deg0--
cnt += s.count()
}
}
return cnt
}