-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.ts
More file actions
34 lines (27 loc) · 941 Bytes
/
timeline.ts
File metadata and controls
34 lines (27 loc) · 941 Bytes
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
import { swap } from '../../../common/support/iteration.ts'
import { type uint32 } from '../../../common/types/numbers.ts'
import { type ISortingComparison, type ISortingTimelineSnapshot } from '../types.ts'
export class SortingTimeline {
readonly initialArray: uint32[]
readonly comparisons: ISortingComparison[]
constructor(initialArray: uint32[], comparisons: ISortingComparison[]) {
this.initialArray = initialArray
this.comparisons = comparisons
}
isComplete(moment: uint32) {
return this.comparisons.length < moment
}
getSnapshot(moment: uint32): ISortingTimelineSnapshot {
const array = this.initialArray.slice()
for (let t = 1; t <= Math.min(moment, this.comparisons.length); t++) {
const action = this.comparisons[t - 1]
if (action.swapped) {
swap(array, action.i, action.j)
}
}
return {
array,
action: this.comparisons[moment - 1],
}
}
}