-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddComb.js
2089 lines (1798 loc) · 53.7 KB
/
AddComb.js
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
// _ _ _ ____ _ _
// / \ __| | __| | / ___| ___ _ __ ___ | |__ (_) ___
// / _ \ / _` | / _` || | / _ \ | '_ ` _ \ | '_ \ | |/ __|
// / ___ \| (_| || (_| || |___| (_) || | | | | || |_) |_ | |\__ \
// /_/ \_\\__,_| \__,_| \____|\___/ |_| |_| |_||_.__/(_)_/ ||___/
// |__/
"use strict";
// constants
const BIT_SIZE = 31;
const B = 2 ** BIT_SIZE - 1;
// errors
function unimplemented() {
throw new Error('This has not been implemented yet.')
}
function unreachable() {
throw new Error('This should not have been reachable.')
}
function stop() {
throw new Error('This was intentional.')
}
function bad_bounds() {
throw new Error("supplied bounds are not possible");
}
// bit helpers
function bin(int) {
// just for testing -- never used
if (int > 2**31 - 1) {
throw new Error('int too large');
}
return (int >>> 0).toString(2).padStart(32,0).substring(1);
}
function num_ones(n) {
// count number of ones in bit representation
let i = 0;
do {
if (n & 1) {
++i;
}
} while (n >>= 1);
return i;
}
function trailing_zeros(n) {
// count number of leading zeros in bit representation.
// ex: 8 = ...001000 -> 3
for (let i = 0; i < BIT_SIZE; i++) {
if (n & (1 << i)) {
return i;
}
}
return BIT_SIZE;
}
function leading_zeros(n) {
// count number of leading zeros in bit representation.
// ex: 2**27 = 00001000000000000000000000000000 -> 4
let i = BIT_SIZE - 1;
while (!(2**i & n) && i >= 0) {
i--;
}
return BIT_SIZE - 1 - i;
}
function cycle(num, i, m) {
// cycle the first m rightmost bits of num by i to the left
let ret = num;
// console.log("1. ret: " + bin(ret));
let wrapped = B << (m - i); // mask bits that will get wrapped
// console.log("2. wrapped: " + bin(wrapped));
wrapped &= ret;
// console.log("3. wrapped: " + bin(wrapped));
wrapped >>= m - i;
// console.log("4. wrapped: " + bin(wrapped));
ret <<= i;
// console.log("5. ret: " + bin(ret));
ret |= wrapped;
// console.log("6. ret: " + bin(ret));
ret &= ~(B << m);
// console.log("7. ~(B << m): " + bin(~(B << m)))
// console.log("8. ret: " + bin(ret));
return ret;
}
function cycle_rev(num, i, m) {
// cycle the first m rightmost bits of num by i to the right
return cycle(num, m - i, m);
}
// other helpers
function range(m,n) {
let r = [];
for (let i = m; i < n; i++) {
r.push(i);
}
return r;
}
function zeros(m) {
let z = [];
for (let i = 0; i < m; i++) {
z.push(0);
}
return z;
}
function fill(fill, l) {
let v = [];
for (let i = 0; i < l; i++) {
v.push(fill);
}
return v;
}
function clone(obj) {
return JSON.parse(JSON.stringify(obj));
}
function sum(arr) {
return arr.reduce((a,b)=>a+b);
}
function in_interval(val, interval) {
// check if val is in the closed interval
return val >= interval[0] && val <= interval[1];
}
function arraysEqual(a1,a2) {
/* WARNING: arrays must not contain {objects} or behavior may be undefined */
return JSON.stringify(a1)==JSON.stringify(a2);
}
function fold(reducer, init, xs) {
let acc = init;
for (let x of xs) {
acc = reducer(acc, x);
}
return acc;
}
// iter tools
function *iter_range(m,n) {
for (let i = m; i < n; i++) {
yield i;
}
return null;
}
function *combinations_with_replacement(n, r) {
let indices = zeros(r);
yield zeros(r);
while (true) {
let found = false;
for (var i = r - 1; i >= 0; i--) {
if (indices[i] != n - 1) {
found = true;
break;
}
}
if (!found) {
return null;
}
indices = [...indices.slice(0,i), ...fill(indices[i] + 1, r - i)];
yield indices;
}
}
function *combinations(iter, r, process_func) {
let process = process_func || function(x) {return x;};
let pool = [...iter];
let n = pool.length;
if (r > n) {
return null;
}
let indices = [...range(0,r)];
yield process(indices.map(x => pool[x]));
while (true) {
let found = false;
for (var i = r - 1; i >= 0; i--) {
if (indices[i] != i + n - r) {
found = true;
break;
}
}
if (!found) {
return null;
}
indices[i] += 1;
for (let j = i + 1; j < r; j++) {
indices[j] = indices[j-1] + 1;
}
yield process(indices.map(x => pool[x]));
}
}
function *each_element(curr, G, zero) {
if (zero) {
yield zeros(G.length);
}
while (true) {
let index = 0;
while (curr[index] == G[index] - 1) {
curr[index] = 0;
index++;
if (index == curr.length) {
return null;
}
}
curr[index] += 1;
yield clone(curr);
}
}
function *sign_combinations(pattern) {
// pattern is a list of integers or other == comparable objects that are not "undefined"
let repeated = []; // list of how many times the elements are repeated (ex: pattern=[1, 2, 2, 4, 4, 5] => repeated=[1, 2, 2, 1])
let num = 1;
for (let i = 1; i < pattern.length + 1; i++) {
if (pattern[i] != pattern[i - 1]) {
repeated.push(num);
num = 1;
} else {
num++;
}
}
let sign_comb = fill(1, repeated.length);
for (let i = 0; i < Math.pow(2, repeated.length); i++) {
// build repeated combination using sign_comb and repeated
let ret = [];
for (let i = 0; i < repeated.length; i++) {
ret.push(...fill(sign_comb[i], repeated[i]));
}
yield ret;
// move sign comb down one in grey code order
for (let j = 0; j < sign_comb.length; j++) {
if (sign_comb[j] == 1) {
sign_comb[j] = -1;
break;
} else {
sign_comb[j] = 1;
}
}
}
return null;
}
function *subsets(A, m, zero_free) {
let vec = A.as_vec();
for (let indices of combinations(range(zero_free && !A.zero_free() ? 1 : 0, A.size()), m)) {
let ret = new A.constructor();
ret.add_all(indices.map(x => vec[x]));
yield ret;
}
return null;
}
function *partitions(num, length, max_val) {
if (num <= max_val) {
yield [num, ...zeros(length - 1)];
}
for (
let first = Math.min(num - 1, max_val);
first > Math.max(0,Math.floor((num - 1)/length));
first--
) {
for (let p of partitions(num - first, length - 1, first)) {
let res = [first, ...p];
yield [...res, ...zeros(length - res.length)];
}
}
return null;
}
class EachSetExact {
constructor(state, setmask, doneflag) {
this.state = state;
this.setmask = setmask;
this.doneflag = doneflag;
}
next() {
// Find the greatest number which can be moved to the left
let can_be_moved_left = this.state & ~(this.state >> 1) & ~(this.setmask >> 1);
let first_moveable = BIT_SIZE - leading_zeros(can_be_moved_left);
if (first_moveable == 0) {
this.doneflag = true;
return new FastSet(this.state);
}
let update_region = ~((1 << (first_moveable - 1)) - 1) & ~this.setmask;
let to_fill_left = num_ones(this.state & update_region) - 1;
let old = this.state;
// Clear the updated region
this.state &= ~update_region;
let newregion = ((1 << (to_fill_left + 1)) - 1) << first_moveable;
this.state |= newregion;
return new FastSet(old);
}
next_zero() {
let ret = this.next();
ret.contents <<= 1;
ret.contents |= 1;
return ret;
}
next_no_zero() {
let ret = this.next();
ret.contents <<= 1;
return ret;
}
*iterable(type) {
while (!this.doneflag) {
yield this[type]();
}
return null;
}
}
// math functions
function D(n) {
return range(1,n+1).filter(i => n % i == 0);
}
function d(n) {
return D(n).length;
}
function choose(n, m) {
if (m == 0 || n == 0) {
return 1;
} if (m > n) {
return 0;
} else {
return n * choose(n - 1, m - 1) / m;
}
}
function a(j,k) {
if (j == 0 || k == 0) {
return 1;
}
return sum(range(0,Math.min(j,k) + 1).map(i => choose(j,i) * choose(k,i) * 2**i));
}
function c(j, k) {
if (j == 0 && k >= 0) {
return 1;
}
if (j >= 1 && k == 0) {
return 0;
}
return sum(range(1,Math.min(j,k) + 1).map(i => choose(j-1,i-1) * choose(k,i) * 2**i));
// starting at i = 1, since if i==0, choose(j-1,i-1)==0
}
function factorial(x) {
var f = 1;
for (let i = 2; i <= x; i++) {
f *= i;
}
return f;
}
function gcd(a,b) {
// thanks Euclid ;)
let x = a;
let y = b;
while (y != 0) {
let t = y;
y = x % y;
x = t;
}
return x;
}
function gcd_arr(arr) {
let g = arr[0];
let i = 1;
while (i < arr.length && g > 1) {
g = gcd(g, arr[i++]);
}
return g;
}
function rel_prime(arr) {
if (arr.length == 1) {
return true;
} else if (arr.length == 2) {
return gcd(...arr) == 1;
} else {
if (rel_prime(arr.slice(1))) {
for (let i = 1; i < arr.length; i++) {
if (gcd(arr[0], arr[i]) != 1) {
return false;
}
}
return true;
} else {
return false;
}
}
}
function mod_add(a, b, G) {
if (G.length == 1) {
return (a + b + G[0]) % G[0];
}
return a.map((x, i) => (x + b[i] + G[i]) % G[i]);
}
function neg_elem(a, G) {
if (G.length == 1) {
return G[0] - a;
}
return a.map((x,i) => G[i] - x);
}
// sides
function v(g, n, h) {
return Math.max(...D(n).map(d => (Math.floor((d - 1 - gcd(d,g)) / h) + 1) * (n / d)));
}
function v_signed(n, h) {
return Math.max(...D(n).map(d => (2 * Math.floor((d - 2) / (2 * h)) + 1) * (n / d)));
}
function u(n, m, h) {
return Math.min(...D(n).map(d => (h * Math.ceil(m / d) - h + 1) * d));
}
function f(d, m, h) {
// page 84
return (h * Math.ceil(m / d) - h + 1) * d;
}
function delta(d, n, m, h) {
let r = h % d || d;
let k = m % d || d;
if (r < k) {
return (k - r) * r - (d - 1);
} else if (k < r && r < d) {
return (d - r) * (r - k) - (d - 1);
} else if (k == r && r == d) {
return d - 1;
} else {
return 0;
}
}
function f_restricted(d, n, m, h) {
let r = h % d || d;
let k = m % d || d;
if (h <= Math.min(k, d - 1)) {
return Math.min(n, f(d,m,h), h * m - h**2 + 1);
} else {
return Math.min(n, h * m - h**2 + 1 - delta(d,n,m,h));
}
}
function u_restricted(n, m, h) {
return Math.min(...D(n).map(d => f_restricted(d,n,m,h)));
}
// classes and associated func
class VerboseWriter {
constructor(DOMElement) {
this.element = DOMElement;
}
c_write(s) {
this.write(">>> " + s);
}
r_write(s) {
this.write(" " + s);
}
ae_write(s) {
this.write("... = " + s);
}
al_write(s) {
this.write("... ≤ " + s);
}
ag_write(s) {
this.write("... ≥ " + s);
}
write(s) {
try {
this.element.innerHTML += s + "\n";
} catch {}
try {
console.log(s);
} catch {}
}
static disp_opt_string(restricted, signed) {
return (restricted ? '^' : '') + (signed ? '\u00b1' : '');
}
}
class FastSet {
// for use in cyclcic groups of size at most 31
constructor(contents) {
this.contents = contents || 0;
}
// STATIC
static singleton(i) {
// create fastset with one element i
var fs = new FastSet();
fs.add(i);
return fs;
}
static empty_set() {
// create and return an empty fastSet
return new FastSet();
}
// STRUCTURAL METHODS
has(i) {
// check containment
return (this.contents & (1 << i)) > 0;
}
add(i) {
// add i to the set
this.contents |= 1 << i;
}
delete(i) {
// delete i from the set (if it is there)
this.contents &= ~(1 << i);
}
add_all(iter) {
for (let el of iter) {
this.add(el);
}
}
is_full(n) {
// Tests if the set is full up to (and including) n
// return (~(this.contents & ((1 << (n + 1)) - 1)) << (BIT_SIZE - n)) == 0;
return this.size() == n;
}
size() {
return num_ones(this.contents);
}
is_empty() {
return this.contents == 0;
}
intersect(other) {
this.contents &= other.contents;
}
clone() {
return new FastSet(this.contents);
}
zero_free() {
return !this.has(0);
}
is_dissociated(n) {
return this.hfold_interval_restricted_signed_sumset([1, this.size()], n).zero_free();
}
dim(n) {
for (let m = 1; m <= this.size(); m++) {
let found = false;
for (let subset of subsets(this, m, true)) {
if (subset.is_dissociated(n)) {
found = true;
break;
}
}
if (!found) {
return m - 1;
}
}
return this.size();
}
// displays
as_vec() {
var ret = [];
let c1 = this.contents;
var p = 0;
while (c1 != 0) {
if (c1 & 1) {
ret.push(p);
}
c1 >>= 1;
p += 1;
}
return ret;
}
to_string() {
return '{' + this.as_vec().toString() + '}';
}
// SUMSETS
hfold_sumset(h, n) {
if (h == 0) {
return FastSet.singleton(0);
}
let res = 0;
let prev = 1;
for (let i = 0; i < h; i++) {
let c1 = this.contents;
while (c1 != 0) {
let shift = trailing_zeros(c1);
let cycled = cycle(prev, shift, n);
res |= cycled;
c1 &= c1 - 1;
}
prev = res;
res = 0;
}
return new FastSet(prev);
}
hfold_restricted_sumset(h, n) {
if (h > this.size()) {
return FastSet.empty_set();
}
if (h == 0) {
return FastSet.singleton(0);
}
return new FastSet(FastSet.hfrs(this.contents, 1, h, n, FastSet.empty_set(), n + 1));
}
static hfrs(stat, curr, h, n, restrictions, ceiling) {
// A "1" in restrictions[i] means i has already been added
if (h == 0) {
return curr;
}
let total = 0;
let toadd = stat;
while (toadd != 0) {
let shift = trailing_zeros(toadd);
if (shift > ceiling) {
break;
}
if (!restrictions.has(shift)) {
let cycled = cycle(curr, shift, n);
let newrestr = restrictions.clone();
newrestr.add(shift);
let rec_call = FastSet.hfrs(stat, cycled, h - 1, n, newrestr, shift);
total |= rec_call;
}
toadd &= toadd - 1;
}
return total;
}
hfold_signed_sumset(h, n) {
if (h == 0) {
return FastSet.singleton(0);
}
return new FastSet(FastSet.hfss(this.contents, 1, h, n, FastSet.empty_set(), FastSet.empty_set(), n + 1))
}
static hfss(stat, curr, h, n, prestrictions, nrestrictions, ceiling) {
if (h == 0) {
return curr;
}
let total = 0;
let toadd = stat;
while (toadd != 0) {
let shift = trailing_zeros(toadd);
if (shift > ceiling) {
break;
}
if (!prestrictions.has(shift)) {
let cycled = cycle(curr, shift, n);
let newnrestr = nrestrictions.clone();
newnrestr.add(shift);
let rec_call = FastSet.hfss(stat, cycled, h - 1, n, prestrictions.clone(), newnrestr, shift);
total |= rec_call;
}
if (!nrestrictions.has(shift)) {
let cycled = cycle_rev(curr, shift, n);
let newprestr = prestrictions.clone();
newprestr.add(shift);
let rec_call = FastSet.hfss(stat, cycled, h - 1, n, newprestr, nrestrictions.clone(), shift);
total |= rec_call;
}
toadd &= toadd - 1;
}
return total;
}
hfold_restricted_signed_sumset(h, n) {
if (h > this.size()) {
return FastSet.empty_set();
}
if (h == 0) {
return FastSet.singleton(0);
}
return new FastSet(FastSet.hfrss(this.contents, 1, h, n, FastSet.empty_set(), n + 1));
}
static hfrss(stat, curr, h, n, restrictions, ceiling) {
// A "1" in restrictions[i] means i has already been added
if (h == 0) {
return curr;
}
let total = 0;
let toadd = stat;
while (toadd != 0) {
let shift = trailing_zeros(toadd);
if (shift > ceiling ){
break;
}
if (!restrictions.has(shift)) {
let cycled = cycle(curr, shift, n);
let newrestr = restrictions.clone();
newrestr.add(shift);
let rec_call = FastSet.hfrss(stat, cycled, h - 1, n, newrestr, shift);
total |= rec_call;
// Also choose -cycled
cycled = cycle_rev(curr, shift, n);
newrestr = restrictions.clone();
newrestr.add(shift);
rec_call = FastSet.hfrss(stat, cycled, h - 1, n, newrestr, shift);
total |= rec_call;
}
toadd &= toadd - 1;
}
return total;
}
hfold_interval_sumset(hs, n) {
let [h1, h2] = hs;
let final_res = 0;
let res = 0;
let prev = 1;
for (let i = 0; i <= h2; i++) {
if (in_interval(i, [h1, h2])) {
final_res |= prev;
}
let c1 = this.contents;
while (c1 != 0) {
let shift = trailing_zeros(c1);
let cycled = cycle(prev, shift, n);
res |= cycled;
c1 &= c1 - 1;
}
prev = res;
res = 0;
}
return new FastSet(final_res);
}
hfold_interval_restricted_sumset(hs, n) {
return new FastSet(FastSet.hfirs(this.contents, 1, hs[1], hs, n, FastSet.empty_set(), n + 1));
}
static hfirs(stat, curr, h, hs, n, restrictions, ceiling) {
// A "1" in restrictions[i] means i has already been added
if (h == 0) {
return curr;
}
let total = 0;
if (in_interval(hs[1] - h, hs)) {
total = curr;
}
let toadd = stat;
while (toadd != 0) {
let shift = trailing_zeros(toadd);
if (shift > ceiling) {
break;
}
if (!restrictions.has(shift)) {
let cycled = cycle(curr, shift, n);
let newrestr = restrictions.clone();
newrestr.add(shift);
let rec_call = FastSet.hfirs(stat, cycled, h - 1, hs, n, newrestr, shift);
total |= rec_call;
// Check if total is full
if ((~(total & ((1 << (n + 1)) - 1)) << (BIT_SIZE - n)) == 0) {
return total;
}
}
toadd &= toadd - 1;
}
return total;
}
hfold_interval_signed_sumset(hs, n) {
return new FastSet(FastSet.hfiss(this.contents, 1, hs[1], hs, n, FastSet.empty_set(), FastSet.empty_set(), n + 1));
}
static hfiss(stat, curr, h, hs, n, prestrictions, nrestrictions, ceiling) {
if (h == 0) {
return curr;
}
let total = 0;
if (in_interval(hs[1] - h, hs)) {
total = curr;
}
let toadd = stat;
while (toadd != 0) {
let shift = trailing_zeros(toadd);
if (shift > ceiling) {
break;
}
if (!prestrictions.has(shift)) {
let cycled = cycle(curr, shift, n);
let newnrestr = nrestrictions.clone();
newnrestr.add(shift);
let rec_call = FastSet.hfiss(stat, cycled, h - 1, hs, n, prestrictions.clone(), newnrestr, shift);
total |= rec_call;
}
if (!nrestrictions.has(shift)) {
let cycled = cycle_rev(curr, shift, n);
let newprestr = prestrictions.clone();
newprestr.add(shift);
let rec_call = FastSet.hfiss(stat, cycled, h - 1, hs, n, newprestr, nrestrictions.clone(), shift);
total |= rec_call;
}
toadd &= toadd - 1;
}
return total;
}
hfold_interval_restricted_signed_sumset(hs, n) {
return new FastSet(FastSet.hfirss(this.contents, 1, hs[1], hs, n, FastSet.empty_set(), n + 1));
}
static hfirss(stat, curr, h, hs, n, restrictions, ceiling) {
// A "1" in restrictions[i] means i has already been added
if (h == 0) {
return curr;
}
let total = 0;
if (in_interval(hs[1] - h, hs)) {
total = curr;
}
let toadd = stat;
while (toadd != 0) {
let shift = trailing_zeros(toadd);
if (shift > ceiling) {
break;
}
if (!restrictions.has(shift)) {
let cycled = cycle(curr, shift, n);
let newrestr = restrictions.clone();
newrestr.add(shift);
let rec_call = FastSet.hfirss(stat, cycled, h - 1, hs, n, newrestr, shift);
total |= rec_call;
// Also choose -cycled
cycled = cycle_rev(curr, shift, n);
newrestr = restrictions.clone();
newrestr.add(shift);
rec_call = FastSet.hfirss(stat, cycled, h - 1, hs, n, newrestr, shift);
total |= rec_call;
}
toadd &= toadd - 1;
}
return total;
}
}
class GeneralSet {
constructor(contents) {
// contents is some iteratable
this.contents = []
this.add_all(contents || []);
}
// STATIC
static singleton(i) {
// create fastset with one element i
var gs = new GeneralSet();
gs.add(i);
return gs;
}
static empty_set() {
// create and return an empty GeneralSet
return new GeneralSet();
}
// STRUCTURAL METHODS
has(el) {
return this.contents.some(x => arraysEqual(x,el));
}
add(el) {
if (!this.has(el)) {
this.contents.push(el);
}
}
delete(el) {
this.contents = this.contents.filter(x => !arraysEqual(x,el));
}
add_all(iter) {
for (let el of iter) {
this.contents.push(el);
}
this.collect();
}
is_full(n) {
return this.size() == n;
}
size() {
return this.contents.length;
}
is_empty() {
return this.size() == 0;
}
intersect(other) {
let to_delete = [];
for (let el of this.contents) {
if (!other.has(el)) {
to_delete.push(el);
}
}
for (let el of to_delete) {
this.delete(el);
}