File size: 5,092 Bytes
4cadbaf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

const Config = require("./config.js");
const Node = require("./node.js");



class Navigator {
	constructor (criterion, sample, options = {}) {
		this.criterion = criterion;
		this.sample = sample;

		this.getCursorOffset = options.getCursorOffset || (() => null);
		this.outOfPage = options.outOfPage;

		this.bestNode = null;
		this.fineCursor = null;

		this.breakingSI = sample.notes.length - 1;

		this.zeroNode = Node.zero();
		this.zeroNode.offset = this.getCursorOffset() || 0;

		this.relocationThreshold = options.relocationThreshold || Config.RelocationThreshold;
	}


	step (index) {
		//console.log("step:", this.zeroNode.offset);
		const note = this.sample.notes[index];

		if (note.matches.length > 0) {
			//console.log("zeroNode.offset:", index, this.zeroNode.offset);
			note.matches.forEach(node => {
				node.evaluatePrev(this.zeroNode);
				//console.log("node:", node, node.evaluatePrevCost(this.zeroNode), node.offset, this.zeroNode.offset);

				for (let si = index - 1; si >= Math.max(this.breakingSI + 1, index - Config.SkipDeep); --si) {
					//const skipCost = Config.SkipCost * (index - 1 - si);

					const prevNote = this.sample.notes[si];
					console.assert(prevNote, "prevNote is null:", si, index, this.sample.notes);
					prevNote.matches.forEach(prevNode => {
						const bias = node.offset - prevNode.offset;
						if (/*prevNode.totalCost + skipCost < node.totalCost
							&&*/ (bias < 2 / Config.LagOffsetCost && bias > -2 / Config.LeadOffsetCost))
							node.evaluatePrev(prevNode);
					});
				}

				node.prior = node.totalCost > 1.99 ? -1 : node.priorByOffset(this.zeroNode.offset);

				if (node.prior > 0 && this.outOfPage) {
					const tick = this.criterion.notes[node.ci].startTick;
					if (this.outOfPage(tick))
						node.prior -= 0.7;
				}
			});

			note.matches.sort((c1, c2) => c2.prior - c1.prior);
			this.cursors = note.matches;
			//console.log("navigator cursors:", this.cursors);

			let fineCursor = null;
			const nullLength = this.nullSteps(index);

			const cursor = this.cursors[0];
			if (cursor && cursor.totalCost < 1) {
				//console.log("nullLength:", nullLength, nullLength * Math.log(cursor.value / 4));
				if (cursor.prior > 0 || (cursor.totalCost < 0.4 && Math.log(Math.max(nullLength * cursor.value, 1e-3)) > this.relocationThreshold)) {
					this.zeroNode.offset = cursor.offset;

					fineCursor = cursor;

					if (!this.bestNode || cursor.value > this.bestNode.value)
						this.bestNode = cursor;
				}
			}

			if (fineCursor)
				this.fineCursor = fineCursor;
			else {
				if (!this.resetCursor(index, {breaking: false/*nullLength > Config.SkipDeep*/})) {
					this.zeroNode.offset += note.deltaSi * Math.tanh(nullLength);
					console.assert(!Number.isNaN(this.zeroNode.offset), "zeroNode.offset is NaN.", note.deltaSi, nullLength);
				}
			}
		}
		else
			this.cursors = [];
	}


	path ({fromIndex = 0, toIndex = this.sample.notes.length - 1} = {}) {
		const path = [];

		let offset = null;

		for (let si = toIndex; si >= fromIndex;) {
			const note = this.sample.notes[si];

			if (!note.matches.length || note.matches[0].prior < -0.01 || note.matches[0].totalCost >= 1) {
				//if (note.matches.length)
				//	console.log("path -1:", si, note.matches[0].prior, note.matches[0].totalCost);
				path[si] = -1;
				--si;
				continue;
			}

			// sort nodes by backwards heuristic offset
			if (offset != null) {
				note.matches.forEach(node => node.backPrior = (node.totalCost < 1.99 ? node.priorByOffset(offset) : -1));
				note.matches.sort((n1, n2) => n2.backPrior - n1.backPrior);
			}

			const node = note.matches[0];
			node.path.forEach((ci, si) => path[si] = ci);
			//console.log("node path:", si, node.path);

			offset = node.root.offset;

			si = node.rootSi - 1;
		}

		console.assert(path.length == toIndex + 1, "path length error:", path, fromIndex, toIndex + 1,
			this.sample.notes.length, this.sample.notes.length ? this.sample.notes[this.sample.notes.length - 1].index : null);

		return path;
	}


	nullSteps (index) {
		return index - (this.fineCursor ? this.fineCursor.si : -1) - 1;
	}


	resetCursor (index, {breaking = true} = {}) {
		if (breaking)
			this.breakingSI = index;

		const cursorOffset = this.getCursorOffset();
		if (cursorOffset != null) {
			//console.log("cursorOffset:", cursorOffset);

			this.zeroNode.offset = cursorOffset;
			//this.breaking = this.nullSteps(index) > Config.SkipDeep;
			//if (this.breaking)	// trivial zero node si resets result in focus path interruption
			this.zeroNode.si = index;
			this.fineCursor = null;

			console.assert(!Number.isNaN(this.zeroNode.offset), "zeroNode.offset is NaN.", cursorOffset);
			//console.log("cursor offset reset:", cursorOffset);

			return true;
		}

		return false;
	}


	get relocationTendency () {
		const cursor = this.cursors && this.cursors[0];
		if (!cursor)
			return null;

		const nullLength = this.nullSteps(cursor.si);
		if (nullLength <= 0)
			return 0;

		return Math.log(Math.max(nullLength * cursor.value, 1e-3)) / this.relocationThreshold;
	}
};



module.exports = Navigator;