-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotatedSiteswap.java
727 lines (666 loc) · 20.5 KB
/
NotatedSiteswap.java
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
package siteswapsuite;
import java.util.ArrayList;
class IncompatibleNotationException extends SiteswapException {
String s1;
String s2;
SiteswapNotation n;
int numHands;
IncompatibleNotationException(String s1, String s2) {
this.s1 = s1;
this.s2 = s2;
}
IncompatibleNotationException(SiteswapNotation n, int numHands) {
this.n = n;
this.numHands = numHands;
this.s1 = null;
this.s2 = null;
}
public String getMessage() {
if(this.n == null)
return "ERROR: notation strings `" + s1 + "' and `" + s2 + "' are incompatible";
else {
if(n == SiteswapNotation.EMPTY) {
return "ERROR: notation type `" + n.name() + "' is incompatible with period > 0";
} else {
return "ERROR: notation type `" + n.name() + "' is incompatible with numHands==" + numHands;
}
}
}
}
class IncompatibleNumberOfHandsException extends SiteswapException {
String inputNotation;
int numHands;
IncompatibleNumberOfHandsException(String inputNotation, int numHands) {
this.inputNotation = inputNotation;
this.numHands = numHands;
}
IncompatibleNumberOfHandsException() {
this.inputNotation = null;
}
public String getMessage() {
if(this.inputNotation != null)
return "ERROR: cannot parse input string '" + this.inputNotation + "' as having " + this.numHands + " hands";
else
return "ERROR: incompatible number of hands";
}
}
class SprungException extends SiteswapException {
String message;
SprungException(String message) {
this.message = message;
}
public String getMessage() {
return this.message;
}
}
public abstract class NotatedSiteswap {
SiteswapNotation notationType;
Siteswap siteswap;
// querying basic info
public SiteswapNotation notationType() { return this.notationType; }
public Siteswap siteswap() { return this.siteswap; }
// printing notation
public abstract String print();
// deep copy
public abstract NotatedSiteswap deepCopy();
// spring
public abstract NotatedSiteswap spring() throws SprungException;
// constructor
private NotatedSiteswap(Siteswap ss, SiteswapNotation notationType) {
this.siteswap = ss;
this.notationType = notationType;
}
/* -------------- */
/* STATIC METHODS */
/* -------------- */
public static NotatedSiteswap assembleAutomatic(Siteswap ss) {
SiteswapNotation notationType = SiteswapNotation.defaultNotationType(ss.numHands());
switch(notationType) {
case EMPTY:
return new EmptyNotatedSiteswap(ss);
case ASYNCHRONOUS:
return new OneHandedNotatedSiteswap(ss);
case SYNCHRONOUS:
return new TwoHandedAsyncNotatedSiteswap(ss);
default: // case PASSING
return new NotatedPassingSiteswap(ss);
}
}
// pair a Siteswap with a pre-computed NotationType
public static NotatedSiteswap assemble(Siteswap ss, SiteswapNotation notationType) throws IncompatibleNotationException {
switch(notationType) {
case EMPTY:
if(ss.period() == 0) {
return new EmptyNotatedSiteswap(ss);
} else {
throw new IncompatibleNotationException(notationType, -1);
}
case ASYNCHRONOUS:
if(ss.numHands() == 1) {
return new OneHandedNotatedSiteswap(ss);
} if(ss.numHands() == 2) {
return new TwoHandedAsyncNotatedSiteswap(ss); // TODO: figure out what to do here regarding startHand
} else {
throw new IncompatibleNotationException(SiteswapNotation.ASYNCHRONOUS, ss.numHands());
}
case SYNCHRONOUS:
if(ss.numHands() == 2) {
return new TwoHandedSyncNotatedSiteswap(ss);
} else {
throw new IncompatibleNotationException(SiteswapNotation.SYNCHRONOUS, ss.numHands());
}
case MIXED:
if(ss.numHands() == 2) {
return new TwoHandedMixedNotatedSiteswap(ss);
} else {
throw new IncompatibleNotationException(SiteswapNotation.MIXED, ss.numHands());
}
default: // case PASSING:
if(ss.numHands() == 4) {
return new NotatedPassingSiteswap(ss);
} else {
throw new IncompatibleNotationException(SiteswapNotation.PASSING, ss.numHands());
}
}
}
// build a Siteswap from a notation string and pair it with the determined NotationType
public static NotatedSiteswap parse(String inputNotation, int numHands, int startHand) throws InvalidSiteswapNotationException, IncompatibleNumberOfHandsException {
SiteswapNotation n;
try {
n = SiteswapNotation.analyze(inputNotation);
} catch(InvalidSiteswapNotationException e) {
throw e;
}
switch(n) {
case EMPTY:
if(numHands == -1) {
numHands = 0;
}
return new EmptyNotatedSiteswap(numHands);
case ASYNCHRONOUS:
if(numHands == -1 || numHands == 1) {
return new OneHandedNotatedSiteswap(inputNotation);
} else if(numHands == 2) {
return new TwoHandedAsyncNotatedSiteswap(inputNotation, startHand);
} else {
break;
}
case SYNCHRONOUS:
if(numHands == -1 || numHands == 2) {
return new TwoHandedSyncNotatedSiteswap(inputNotation);
} else {
break;
}
case MIXED:
if(numHands == -1 || numHands == 2) {
return new TwoHandedMixedNotatedSiteswap(inputNotation);
} else {
break;
}
default: // case PASSING
return new NotatedPassingSiteswap(inputNotation);
}
throw new IncompatibleNumberOfHandsException(inputNotation, numHands);
}
/* -------------- */
/* THE SUBCLASSES */
/* -------------- */
static class EmptyNotatedSiteswap extends NotatedSiteswap {
// assemble
EmptyNotatedSiteswap(Siteswap ss) {
super(ss, SiteswapNotation.EMPTY);
}
// "parse"
EmptyNotatedSiteswap(int numHands) {
this(new Siteswap(numHands));
}
// print
public String print() {
return SiteswapNotation.emptyNotationPrint;
}
// deep copy
public NotatedSiteswap deepCopy() {
return (NotatedSiteswap)(new EmptyNotatedSiteswap(this.siteswap.deepCopy()));
}
public NotatedSiteswap spring() throws SprungException {
throw new SprungException("WARNING: cannot spring a non-async pattern");
}
}
static class OneHandedNotatedSiteswap extends NotatedSiteswap {
// assemble
OneHandedNotatedSiteswap(Siteswap ss) {
super(ss, SiteswapNotation.ASYNCHRONOUS);
}
// parse
OneHandedNotatedSiteswap(String s) {
super(new Siteswap(1), SiteswapNotation.ASYNCHRONOUS);
char[] a = s.toCharArray();
char curToken;
int i=0; //index in input string
int b=0; //index (beat) in output siteswap
boolean multi = false; //whether or not we're currently in a multiplex throw
boolean isNegative = false;
boolean isAntitoss = false;
while(i < a.length) {
curToken = a[i];
switch(curToken) {
//comment
case '[':
multi = true;
this.siteswap.appendEmptyBeat();
break;
case ']':
multi = false;
b++;
break;
case '-':
isNegative = true;
break;
case '_':
isAntitoss = true;
break;
default:
ExtendedInteger height = SiteswapNotation.throwHeight(curToken);
if(isNegative) {
height.negate();
isNegative = false;
}
if(!multi) {
if(b == this.siteswap.period()) {
this.siteswap.appendEmptyBeat();
}
}
if(height.isInfinite()) {
if(isAntitoss) {
this.siteswap.addInfiniteAntitoss(b, 0, height.infiniteValue());
} else {
this.siteswap.addInfiniteToss(b, 0, height.infiniteValue());
}
} else {
if(isAntitoss) {
this.siteswap.addFiniteAntitoss(b, 0, height.finiteValue(), 0);
} else {
this.siteswap.addFiniteToss(b, 0, height.finiteValue(), 0);
}
}
if(!multi) {
b++;
}
isAntitoss = false;
break;
}
i++;
}
}
// print
public String print() {
String out = "";
for(int b=0; b<this.siteswap.period(); b++) {
if(this.siteswap.numTossesAtSite(b, 0) > 1) {
out += "[";
for(int t=0; t<this.siteswap.numTossesAtSite(b,0); t++) {
out += SiteswapNotation.reverseThrowHeight(this.siteswap.getToss(b, 0, t));
}
out += "]";
} else {
out += SiteswapNotation.reverseThrowHeight(this.siteswap.getToss(b, 0, 0));
}
}
return out;
}
// deep copy
public NotatedSiteswap deepCopy() {
return (NotatedSiteswap)(new OneHandedNotatedSiteswap(this.siteswap.deepCopy()));
}
public NotatedSiteswap spring() throws SprungException {
throw new SprungException("WARNING: cannot spring a non-async pattern");
}
}
static class TwoHandedAsyncNotatedSiteswap extends NotatedSiteswap {
private int startHand;
// assemble
TwoHandedAsyncNotatedSiteswap(Siteswap ss) {
super(ss, SiteswapNotation.ASYNCHRONOUS);
// TODO: set startHand by looking at beats
this.startHand = 0;
}
// parse
TwoHandedAsyncNotatedSiteswap(String s, int startHand) {
super(new Siteswap(2), SiteswapNotation.ASYNCHRONOUS);
this.startHand = startHand;
this.notationType = SiteswapNotation.ASYNCHRONOUS;
char[] a = s.toCharArray();
char curToken;
int b = 0; //index (beat) in output siteswap
int curHand = startHand; // which hand's turn it is to throw
ExtendedInteger height;
int destHand;
boolean multi = false; //whether or not we're currently in a multiplex throw
boolean isNegative = false;
boolean isAntitoss = false;
do {
int i = 0; //index in input string
while(i < a.length) {
curToken = a[i];
//update current hand
curHand = (b + this.startHand) % 2;
//System.out.println(curToken);
switch(curToken) {
//if curToken is "[", we're now in a multiplex throw, so add all subsequent tosses to the same hand until "]"
case '[':
multi = true;
this.siteswap.appendEmptyBeat();
break;
case ']':
multi = false;
b++;
break;
//if curToken is "-", the next toss is negative
case '-':
isNegative = true;
break;
//if curToken is anything else, it has to be a throw height (since it matched the regex for async pattern)
default:
height = SiteswapNotation.throwHeight(curToken);
if(isNegative) {
height.negate();
isNegative = false;
}
if(!multi) {
//create new beat
this.siteswap.appendEmptyBeat();
//add toss of correct height and destination to current hand
if(height.isInfinite()) {
if(isAntitoss) {
this.siteswap.addInfiniteAntitoss(b, curHand, height.infiniteValue());
} else {
this.siteswap.addInfiniteToss(b, curHand, height.infiniteValue());
}
} else {
destHand = (curHand + height.finiteValue()) % 2; //0=left, 1=right
if(destHand < 0) {
destHand += 2;
}
if(isAntitoss) {
this.siteswap.addFiniteAntitoss(b, curHand, height.finiteValue(), destHand);
} else {
this.siteswap.addFiniteToss(b, curHand, height.finiteValue(), destHand);
}
//increment beat index
b++;
}
} else {
//add toss of correct height and destination to current hand
if(height.isInfinite()) {
if(isAntitoss) {
this.siteswap.addInfiniteAntitoss(b, curHand, height.infiniteValue());
} else {
this.siteswap.addInfiniteToss(b, curHand, height.infiniteValue());
}
} else {
destHand = (curHand + height.finiteValue()) % 2; //0=left, 1=right
if(destHand < 0) {
destHand += 2;
}
if(isAntitoss) {
this.siteswap.addFiniteAntitoss(b, curHand, height.finiteValue(), destHand);
} else {
this.siteswap.addFiniteToss(b, curHand, height.finiteValue(), destHand);
}
}
}
isAntitoss = false;
break;
}
//increment index in input string
i++;
}
} while(this.siteswap.period() % 2 == 1);
}
// print
public String print() {
String out = "";
int curHandIndex = this.startHand;
//loop through beats of siteswap
for(int b=0; b<this.siteswap.period(); b++) {
//see if we need to use multiplex notation
if(this.siteswap.numTossesAtSite(b, curHandIndex) > 1) {
out += "[";
//loop through tosses of current hand
for(int t=0; t<this.siteswap.numTossesAtSite(b, curHandIndex); t++) {
out += SiteswapNotation.reverseThrowHeight(this.siteswap.getToss(b, curHandIndex, t));
}
out += "]";
} else {
out += SiteswapNotation.reverseThrowHeight(this.siteswap.getToss(b, curHandIndex, 0));
}
//alternate curHandIndex
curHandIndex = (curHandIndex + 1) % 2;
}
return out;
}
// deep copy
public NotatedSiteswap deepCopy() {
return (NotatedSiteswap)(new TwoHandedAsyncNotatedSiteswap(this.siteswap.deepCopy(), this.startHand));
}
// additional constructor for deep copy
TwoHandedAsyncNotatedSiteswap(Siteswap ss, int startHand) {
super(ss, SiteswapNotation.ASYNCHRONOUS);
// TODO: set startHand by looking at beats
this.startHand = startHand;
}
public NotatedSiteswap spring() throws SprungException {
TwoHandedSyncNotatedSiteswap newSiteswap = new TwoHandedSyncNotatedSiteswap(new Siteswap(2));
int sprungHand = (this.startHand + 1) % 2;
for(int b=0; b<this.siteswap.period(); b++) {
newSiteswap.siteswap.appendEmptyBeat();
for(int h=0; h<2; h++) {
if(h == sprungHand) {
Toss newToss = new Toss(2, (h + 1) % 2, false);
newSiteswap.siteswap.addToss(b * 2, h, newToss);
} else {
if(this.siteswap.siteIsEmpty(b, h))
continue;
for(int t=0; t<this.siteswap.numTossesAtSite(b, h); t++) {
Toss curToss = this.siteswap.getToss(b, h, t);
Toss newToss;
if(curToss.height().isInfinite() || curToss.charge() == 0)
newToss = curToss.deepCopy();
else
newToss = new Toss(curToss.height().finiteValue() * 2, curToss.destHand(), curToss.isAntitoss());
newSiteswap.siteswap.addToss(b * 2, h, newToss);
}
}
}
newSiteswap.siteswap.appendEmptyBeat();
sprungHand = (sprungHand + 1) % 2;
}
return newSiteswap;
}
}
static class TwoHandedSyncNotatedSiteswap extends NotatedSiteswap {
// assemble
TwoHandedSyncNotatedSiteswap(Siteswap ss) {
super(ss, SiteswapNotation.SYNCHRONOUS);
}
// parse
TwoHandedSyncNotatedSiteswap(String s) {
this(new Siteswap(2));
this.notationType = SiteswapNotation.SYNCHRONOUS;
char[] a = s.toCharArray();
//create new sync siteswap
int i = 0; //index in index string
int b = 0; //index of beat within output siteswap
int curHand = 0;
int lastStarredBeat = 0;
ExtendedInteger height;
int destHand;
boolean isNegative = false;
boolean isAntitoss = false;
char curToken;
Toss newToss = null;
//
while(i < a.length) {
curToken = a[i];
switch(curToken) {
case '(':
//create new beat
this.siteswap.appendEmptyBeat();
curHand = 0;
break;
case ',':
curHand = 1;
break;
case ')':
//add empty beat, cuz that's how sync works
this.siteswap.appendEmptyBeat();
//increase beat index by 2
b += 2;
break;
case '[':
//doesn't matter whether we're in a multiplex pattern
// b/c curHand is determined by other notation (commas and parens)
break;
case ']':
break;
case 'x':
//toggle destination hand of most recently added toss
newToss.starify();
break;
case '!':
//remove last beat
this.siteswap.removeLastBeat();
b--;
//decrement beat index
break;
case '*':
this.siteswap.starify();
break;
case '-':
isNegative = true;
break;
case '_':
isAntitoss = true;
break;
default: //curToken is a throw height
height = SiteswapNotation.throwHeight(curToken);
if(isNegative) {
height.negate();
isNegative = false;
}
if(height.isInfinite()) {
if(isAntitoss) {
newToss = new Toss(height.infiniteValue(), true);
} else {
newToss = new Toss(height.infiniteValue(), false);
}
} else {
destHand = (curHand + height.finiteValue()) % 2;
if(destHand < 0) {
destHand += 2;
} if(isAntitoss) {
newToss = new Toss(height.finiteValue(), destHand, true);
} else {
newToss = new Toss(height.finiteValue(), destHand, false);
}
isAntitoss = false;
}
this.siteswap.addToss(b, curHand, newToss);
}
i++;
}
}
// print
public String print() {
String out = "";
String nextBeat;
boolean skippedLastBeat = false;
boolean allZeroes = true;
//loop through beats of siteswap
for(int b=0; b<this.siteswap.period(); b++) {
nextBeat = "(";
allZeroes = true;
//loop through hands within each beat (we know numHands = 2 since we screened for that in parse())
for(int h=0; h<2; h++) {
Util.printf("nextBeat: " + nextBeat, Util.DebugLevel.DEBUG);
//see if we need to add multiplex notation
if(this.siteswap.numTossesAtSite(b, h) > 1) {
nextBeat += "[";
//loop through tosses within hand
for(int t=0; t<this.siteswap.numTossesAtSite(b, h); t++) {
Toss curToss = this.siteswap.getToss(b, h, t);
Util.printf(curToss, Util.DebugLevel.DEBUG);
nextBeat += SiteswapNotation.reverseThrowHeight(curToss);
if(curToss.charge() != 0) {
allZeroes = false;
if(!curToss.height().isInfinite() && curToss.destHand() != (h + Math.abs(curToss.height().finiteValue())) % 2) {
nextBeat += "x";
}
}
}
nextBeat += "]";
} else if(this.siteswap.numTossesAtSite(b, h) == 1) {
//account for only toss in hand
Toss curToss = this.siteswap.getToss(b, h, 0);
if(!curToss.isZero(h)) {
Util.printf("encountered non-zero toss", Util.DebugLevel.DEBUG);
Util.printf(curToss, Util.DebugLevel.DEBUG);
allZeroes = false;
}
nextBeat += SiteswapNotation.reverseThrowHeight(curToss);
if(!curToss.height().isInfinite() && curToss.destHand() != (h + Math.abs(curToss.height().finiteValue())) % 2) {
nextBeat += "x";
}
} else {
// notate empty site
nextBeat += "0";
}
//put a comma if we've just finished doing the left hand
if(h == 0) {
nextBeat += ",";
}
}
nextBeat += ")";
if(b == 0) {
Util.printf("not skipping beat 0", Util.DebugLevel.DEBUG);
out += nextBeat;
skippedLastBeat = false;
} else if(!skippedLastBeat && allZeroes) {
// skip this beat
Util.printf("skipping beat " + b, Util.DebugLevel.DEBUG);
skippedLastBeat = true;
} else {
// don't skip this beat
Util.printf("not skipping beat " + b, Util.DebugLevel.DEBUG);
if(!skippedLastBeat) {
out += "!";
}
out += nextBeat;
skippedLastBeat = false;
}
}
if(!skippedLastBeat) {
Util.printf("adding final '!'", Util.DebugLevel.DEBUG);
out += "!";
}
return out;
}
// deep copy
public NotatedSiteswap deepCopy() {
return (NotatedSiteswap)(new TwoHandedSyncNotatedSiteswap(this.siteswap.deepCopy()));
}
public NotatedSiteswap spring() throws SprungException {
throw new SprungException("WARNING: cannot spring a non-async pattern");
}
}
static class TwoHandedMixedNotatedSiteswap extends NotatedSiteswap {
// assemble
TwoHandedMixedNotatedSiteswap(Siteswap ss) {
super(ss, SiteswapNotation.MIXED);
}
// parse
TwoHandedMixedNotatedSiteswap(String s) {
this(new Siteswap(2));
Util.printf("WARNINR: Parsing of mixed notation not yet implemented...", Util.DebugLevel.ERROR);
System.exit(1);
}
// print
public String print() {
Util.printf("WARNINR: Parsing of mixed notation not yet implemented...", Util.DebugLevel.ERROR);
System.exit(1);
return null;
}
// deep copy
public NotatedSiteswap deepCopy() {
return (NotatedSiteswap)(new TwoHandedMixedNotatedSiteswap(this.siteswap));
}
public NotatedSiteswap spring() throws SprungException {
throw new SprungException("WARNING: cannot spring a non-async pattern");
}
}
static class NotatedPassingSiteswap extends NotatedSiteswap {
// assemble
NotatedPassingSiteswap(Siteswap ss) {
super(ss, SiteswapNotation.PASSING);
}
// parse
NotatedPassingSiteswap(String s) {
this(new Siteswap(4));
Util.printf("Parsing of passing notation not yet implemented...", Util.DebugLevel.ERROR);
System.exit(1);
}
// print
public String print() {
Util.printf("Parsing of passing notation not yet implemented...", Util.DebugLevel.ERROR);
System.exit(1);
return null;
}
// deep copy
public NotatedSiteswap deepCopy() {
return (NotatedSiteswap)(new NotatedPassingSiteswap(this.siteswap));
}
public NotatedSiteswap spring() throws SprungException {
throw new SprungException("WARNING: cannot spring a non-async pattern");
}
}
}