File size: 5,191 Bytes
d605f27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import {SimpleClass} from "./jsonRecovery";



enum LayoutType {
	Ordinary = "ordinary",
	Full = "full",
	Conservative = "conservative",
	Once = "once",
};


interface MeasureLayout {
	serialize (type: LayoutType): number[];

	seq: MeasureSeq;
	code: string;
};


export type MeasureSeq = MeasureLayout[];


const spreadMeasureSeq = (seq: MeasureSeq, type: LayoutType = LayoutType.Ordinary): number[] => [].concat(...seq.map(layout => layout.serialize(type)));


const seqToCode = (seq: MeasureSeq, {withBrackets = false}: {withBrackets?: boolean} = {}): string => {
	//const code = seq.map(layout => layout.code).join(", ");
	let code = "";
	let inRange = false;

	for (let i = 0; i < seq.length; ++i) {
		const middle = seq[i - 1] instanceof SingleMLayout && seq[i] instanceof SingleMLayout && seq[i + 1] instanceof SingleMLayout;
		if (middle) {
			if (!inRange) {
				code += "..";
				inRange = true;
			}
		}
		else {
			if (i > 0 && !inRange)
				code += ", ";

			inRange = false;

			code += seq[i].code;
		}
	}

	return withBrackets ? `[${code}]` : code;
};


class SingleMLayout extends SimpleClass implements MeasureLayout {
	static className = "SingleMLayout";


	measure: number;


	static from (measure: number) {
		const layout = new SingleMLayout();
		layout.measure = measure;

		return layout;
	}


	serialize (): number[] {
		return [this.measure];
	}


	get seq (): MeasureSeq {
		return [this];
	}


	get code (): string {
		return this.measure.toString();
	}
};


class BlockMLayout extends SimpleClass implements MeasureLayout {
	static className = "BlockMLayout";


	seq: MeasureSeq;


	static trimSeq (seq: MeasureSeq): MeasureSeq {
		const seq2 = [];
		for (const layout of seq) {
			if (layout instanceof BlockMLayout) {
				for (const sub of layout.seq)
					seq2.push(sub);
			}
			else
				seq2.push(layout);
		}

		// reduce duplicated or backwards single measures
		const seq3 = [];
		let measure = null;
		for (const layout of seq2) {
			if (layout instanceof SingleMLayout) {
				if (layout.measure > measure) {
					seq3.push(layout);
					measure = layout.measure;
				}
			}
			else
				seq3.push(layout);
		}

		return seq3;
	}


	static fromSeq (seq: MeasureSeq): BlockMLayout {
		const layout = new BlockMLayout();
		layout.seq = BlockMLayout.trimSeq(seq);

		return layout;
	}


	serialize (type: LayoutType): number[] {
		return spreadMeasureSeq(this.seq, type);
	}


	get code (): string {
		return seqToCode(this.seq, {withBrackets: true});
	}
};


class VoltaMLayout extends SimpleClass implements MeasureLayout {
	static className = "VoltaMLayout";


	times: number;
	body: MeasureSeq;
	alternates: MeasureSeq[];


	serialize (type: LayoutType): number[] {
		const bodySeq = spreadMeasureSeq(this.body);

		if (this.alternates) {
			const alternateSeqs = this.alternates.map(seq => spreadMeasureSeq(seq));
			const lastAlternateSeq = alternateSeqs[alternateSeqs.length - 1];

			switch (type) {
			case LayoutType.Ordinary:
				return bodySeq.concat(...alternateSeqs);

			case LayoutType.Conservative:
			case LayoutType.Full: {
				const priorSeq = [].concat(...Array(this.times - 1).fill(null).map((_, i) => [
					...bodySeq,
					...alternateSeqs[i % (this.times - 1)],
				]));

				return [
					...priorSeq,
					...bodySeq,
					...lastAlternateSeq,
				];
			}

			case LayoutType.Once:
				return [
					...bodySeq,
					...lastAlternateSeq,
				];
			}
		}
		else {
			switch (type) {
			case LayoutType.Ordinary:
			case LayoutType.Conservative:
			case LayoutType.Once:
				return bodySeq;

			case LayoutType.Full:
				return [].concat(...Array(this.times).fill(null).map(() => bodySeq));
			}
		}

		console.warn("the current case not handled:", type, this);
	}


	get seq (): MeasureSeq {
		const alternates = this.alternates ? this.alternates[this.alternates.length - 1] : [];

		return [
			...this.body,
			...alternates,
		];
	}


	get code (): string {
		const body = seqToCode(this.body, {withBrackets: true});

		let code = `${this.times}*${body}`;
		if (this.alternates)
			code += "{" + this.alternates.map(seq => seqToCode(seq, {withBrackets: seq.length > 1})).join(", ") +"}";

		return code;
	}
};


class ABAMLayout extends SimpleClass implements MeasureLayout {
	static className = "ABAMLayout";


	main: MeasureLayout;
	rest: MeasureSeq;


	serialize (type: LayoutType): number[] {
		const seqA = this.main.serialize(type);
		const seqA_ = spreadMeasureSeq(this.main.seq, LayoutType.Once);
		const seqB = spreadMeasureSeq(this.rest, type);

		switch (type) {
		case LayoutType.Ordinary:	// A B
			return [
				...seqA,
				...seqB,
			];

		case LayoutType.Once:	// B A'
			return [
				...seqB,
				...seqA_,
			];

		case LayoutType.Conservative: // A B A'
		case LayoutType.Full:	// A B A'
			return [
				...seqA,
				...seqB,
				...seqA_,
			];

		default:
			console.warn("the current case not handled:", type, this);
		}
	}


	get seq (): MeasureSeq {
		return [
			this.main,
			...this.rest,
		];
	}


	get code (): string {
		return "<" + this.main.code + ", " + seqToCode(this.rest) + ">";
	}
};



export {
	LayoutType,
	MeasureLayout,
	SingleMLayout,
	BlockMLayout,
	VoltaMLayout,
	ABAMLayout,
};