Repository navigation
Expand file tree
/
Copy pathprefixSumComputer.ts
More file actions
198 lines (160 loc) · 5.61 KB
/
Copy pathprefixSumComputer.ts
File metadata and controls
198 lines (160 loc) · 5.61 KB
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
// Adapted from
// https://github.com/Microsoft/vscode/blob/7a07992127a6ff176f0ed073d0698d81d09fc4bb/src/vs/editor/common/viewModel/prefixSumComputer.ts.
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.
export class PrefixSumIndexOfResult {
public _prefixSumIndexOfResultBrand: void
public index: number
public remainder: number
constructor(index: number, remainder: number) {
this.index = index
this.remainder = remainder
}
}
export class PrefixSumComputer {
/**
* values[i] is the value at index i
*/
private values: Uint32Array
/**
* prefixSum[i] = SUM(heights[j]), 0 <= j <= i
*/
private prefixSum: Uint32Array
/**
* prefixSum[i], 0 <= i <= prefixSumValidIndex can be trusted
*/
private readonly prefixSumValidIndex: Int32Array
constructor(values: Uint32Array) {
this.values = values
this.prefixSum = new Uint32Array(values.length)
this.prefixSumValidIndex = new Int32Array(1)
this.prefixSumValidIndex[0] = -1
}
public getCount(): number {
return this.values.length
}
public insertValues(insertIndex: number, insertValues: Uint32Array): boolean {
insertIndex = toUint32(insertIndex)
const oldValues = this.values
const oldPrefixSum = this.prefixSum
const insertValuesLen = insertValues.length
if (insertValuesLen === 0) {
return false
}
this.values = new Uint32Array(oldValues.length + insertValuesLen)
this.values.set(oldValues.subarray(0, insertIndex), 0)
this.values.set(oldValues.subarray(insertIndex), insertIndex + insertValuesLen)
this.values.set(insertValues, insertIndex)
if (insertIndex - 1 < this.prefixSumValidIndex[0]) {
this.prefixSumValidIndex[0] = insertIndex - 1
}
this.prefixSum = new Uint32Array(this.values.length)
if (this.prefixSumValidIndex[0] >= 0) {
this.prefixSum.set(oldPrefixSum.subarray(0, this.prefixSumValidIndex[0] + 1))
}
return true
}
public changeValue(index: number, value: number): boolean {
index = toUint32(index)
value = toUint32(value)
if (this.values[index] === value) {
return false
}
this.values[index] = value
if (index - 1 < this.prefixSumValidIndex[0]) {
this.prefixSumValidIndex[0] = index - 1
}
return true
}
public removeValues(startIndex: number, cnt: number): boolean {
startIndex = toUint32(startIndex)
cnt = toUint32(cnt)
const oldValues = this.values
const oldPrefixSum = this.prefixSum
if (startIndex >= oldValues.length) {
return false
}
const maxCnt = oldValues.length - startIndex
if (cnt >= maxCnt) {
cnt = maxCnt
}
if (cnt === 0) {
return false
}
this.values = new Uint32Array(oldValues.length - cnt)
this.values.set(oldValues.subarray(0, startIndex), 0)
this.values.set(oldValues.subarray(startIndex + cnt), startIndex)
this.prefixSum = new Uint32Array(this.values.length)
if (startIndex - 1 < this.prefixSumValidIndex[0]) {
this.prefixSumValidIndex[0] = startIndex - 1
}
if (this.prefixSumValidIndex[0] >= 0) {
this.prefixSum.set(oldPrefixSum.subarray(0, this.prefixSumValidIndex[0] + 1))
}
return true
}
public getTotalValue(): number {
if (this.values.length === 0) {
return 0
}
return this._getAccumulatedValue(this.values.length - 1)
}
public getAccumulatedValue(index: number): number {
if (index < 0) {
return 0
}
index = toUint32(index)
return this._getAccumulatedValue(index)
}
private _getAccumulatedValue(index: number): number {
if (index <= this.prefixSumValidIndex[0]) {
return this.prefixSum[index]
}
let startIndex = this.prefixSumValidIndex[0] + 1
if (startIndex === 0) {
this.prefixSum[0] = this.values[0]
startIndex++
}
if (index >= this.values.length) {
index = this.values.length - 1
}
for (let i = startIndex; i <= index; i++) {
this.prefixSum[i] = this.prefixSum[i - 1] + this.values[i]
}
this.prefixSumValidIndex[0] = Math.max(this.prefixSumValidIndex[0], index)
return this.prefixSum[index]
}
public getIndexOf(accumulatedValue: number): PrefixSumIndexOfResult {
accumulatedValue = Math.floor(accumulatedValue) // @perf
// Compute all sums (to get a fully valid prefixSum)
this.getTotalValue()
let low = 0
let high = this.values.length - 1
let mid = 0
let midStop = 0
let midStart = 0
while (low <= high) {
mid = (low + (high - low) / 2) | 0
midStop = this.prefixSum[mid]
midStart = midStop - this.values[mid]
if (accumulatedValue < midStart) {
high = mid - 1
} else if (accumulatedValue >= midStop) {
low = mid + 1
} else {
break
}
}
return new PrefixSumIndexOfResult(mid, accumulatedValue - midStart)
}
}
/** Max unsigned 32-bit integer. */
const MAX_UINT_32 = 4294967295 // 2^32 - 1
export function toUint32(v: number): number {
if (v < 0) {
return 0
}
if (v > MAX_UINT_32) {
return MAX_UINT_32
}
return v | 0
}