prefixSumComputer.ts ×20

Frontier kind: Code frontier

unlabeled · c_b39c3ac3a985

156 tests · 3526 LOC · 20 files · introduces 0 tests · 103 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
20 ranges103 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
503 ranges3526 lines · 20 files · Browse complete extent
All tests (intent)
156 testsBrowse complete intent

Neighbourhood graph

The orange circle is the focus. Violet and green circles are every ancestor and descendant, broader and narrower, at any distance; blue squares and pink diamonds are the introduced files and exact introduced tests of every visible concept, not only the focus's. Arrows point from broader to narrower concepts and bridge only concepts omitted from this view. Undirected links show source or test introduction. Concept and file size follows LOC; exact test nodes use test-count units.

Introduced files, introduced tests, and structurally relevant concept specialization

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the native relationship evidence on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the native relationship evidence on this page while the interactive map is unavailable.

Native relationship evidence

Every exact file and test below is linked only from the concept that introduces it.

Introduced tests

Every collected test enters the hierarchy at exactly one concept.

No tests are introduced at this concept. Its intent tests are introduced by other concepts.

Introduced code

Every collected source range enters the hierarchy at exactly one concept.

1 file ranked by introduced lines: 103 introduced LOC across 20 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/editor/common/model/prefixSumComputer.ts 103 introduced LOC · 20 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- prefixSumComputer.ts
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { arrayInsert } from '../../../base/common/arrays.js';
7 > import { toUint32 } from '../../../base/common/uint.js';
8 >
9 > export class PrefixSumComputer {
10 >
11 > /**
12 > * values[i] is the value at index i
13 > */
14 > private values: Uint32Array;
15 >
16 > /**
17 > * prefixSum[i] = SUM(heights[j]), 0 <= j <= i
18 > */
19 > private prefixSum: Uint32Array;
20 >
21 > /**
22 > * prefixSum[i], 0 <= i <= prefixSumValidIndex can be trusted
23 > */
24 > private readonly prefixSumValidIndex: Int32Array;
25 >
26 > constructor(values: Uint32Array) {
27 this.values = values;
28 this.prefixSum = new Uint32Array(values.length);
30 this.prefixSumValidIndex[0] = -1;
31 }
33 > public getCount(): number {
34 return this.values.length;
35 }
37 > public insertValues(insertIndex: number, insertValues: Uint32Array): boolean {
38 insertIndex = toUint32(insertIndex);
39 const oldValues = this.values;
60 return true;
61 }
63 > public setValue(index: number, value: number): boolean {
64 index = toUint32(index);
65 value = toUint32(value);
74 return true;
75 }
77 > public removeValues(startIndex: number, count: number): boolean {
78 startIndex = toUint32(startIndex);
79 count = toUint32(count);
108 return true;
109 }
111 > public getTotalSum(): number {
112 if (this.values.length === 0) {
113 return 0;
115 return this._getPrefixSum(this.values.length - 1);
116 }
118 > /**
119 > * Returns the sum of the first `index + 1` many items.
120 > * @returns `SUM(0 <= j <= index, values[j])`.
121 > */
122 > public getPrefixSum(index: number): number {
123 if (index < 0) {
124 return 0;
128 return this._getPrefixSum(index);
129 }
131 > private _getPrefixSum(index: number): number {
132 if (index <= this.prefixSumValidIndex[0]) {
133 return this.prefixSum[index];
150 return this.prefixSum[index];
151 }
153 > public getIndexOf(sum: number): PrefixSumIndexOfResult {
154 sum = Math.floor(sum);
155
180 return new PrefixSumIndexOfResult(mid, sum - midStart);
181 }
183 >
184 > /**
185 > * {@link getIndexOf} has an amortized runtime complexity of O(1).
186 > *
187 > * ({@link PrefixSumComputer.getIndexOf} is just O(log n))
188 > */
189 > export class ConstantTimePrefixSumComputer {
190 > private _values: number[];
191 > private _isValid: boolean;
192 > private _validEndIndex: number;
193 >
194 > /**
195 > * _prefixSum[i] = SUM(values[j]), 0 <= j <= i
196 > */
197 > private _prefixSum: number[];
198 >
199 > /**
200 > * _indexBySum[sum] = idx => _prefixSum[idx - 1] <= sum < _prefixSum[idx]
201 > */
202 > private _indexBySum: number[];
203 >
204 > constructor(values: number[]) {
205 this._values = values;
206 this._isValid = false;
209 this._indexBySum = [];
210 }
212 > /**
213 > * @returns SUM(0 <= j < values.length, values[j])
214 > */
215 > public getTotalSum(): number {
216 this._ensureValid();
217 return this._indexBySum.length;
218 }
220 > /**
221 > * Returns the sum of the first `count` many items.
222 > * @returns `SUM(0 <= j < count, values[j])`.
223 > */
224 > public getPrefixSum(count: number): number {
225 this._ensureValid();
226 if (count === 0) {
229 return this._prefixSum[count - 1];
230 }
232 > /**
233 > * @returns `result`, such that `getPrefixSum(result.index) + result.remainder = sum`
234 > */
235 > public getIndexOf(sum: number): PrefixSumIndexOfResult {
236 this._ensureValid();
237 const idx = this._indexBySum[sum];
245 return new PrefixSumIndexOfResult(idx, sum - viewLinesAbove);
246 }
248 > public removeValues(start: number, deleteCount: number): void {
249 this._values.splice(start, deleteCount);
250 this._invalidate(start);
251 }
253 > public insertValues(insertIndex: number, insertArr: number[]): void {
254 this._values = arrayInsert(this._values, insertIndex, insertArr);
255 this._invalidate(insertIndex);
256 }
258 > private _invalidate(index: number): void {
259 this._isValid = false;
260 this._validEndIndex = Math.min(this._validEndIndex, index - 1);
261 }
263 > private _ensureValid(): void {
264 if (this._isValid) {
265 return;
284 this._validEndIndex = this._values.length - 1;
285 }
287 > public setValue(index: number, value: number): void {
288 if (this._values[index] === value) {
289 // no change
293 this._invalidate(index);
294 }
296 >
297 >
298 > export class PrefixSumIndexOfResult {
299 > _prefixSumIndexOfResultBrand: void = undefined;
300 >
301 > constructor(
302 public readonly index: number,
303 public readonly remainder: number