smallImmutableSet.ts ×14

Frontier kind: Code frontier

unlabeled · c_332de8cfafc3

870 tests · 3447 LOC · 19 files · introduces 0 tests · 69 LOC · 1 file

Introduces — evidence that enters the hierarchy at this concept

Code
14 ranges69 lines · 1 files
Tests
0 tests

Contains — complete concept membership

All code (extent)
494 ranges3447 lines · 19 files · Browse complete extent
All tests (intent)
870 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: 69 introduced LOC across 14 ranges. Expand a file to inspect source; the > gutter marks introduced lines.

src/vs/editor/common/model/bracketPairsTextModelPart/bracketPairsTree/smallImmutableSet.ts 69 introduced LOC · 14 ranges

Open complete file

1 > /*--------------------------------------------------------------------------------------------- smallImmutableSet.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 > const emptyArr: number[] = [];
7 >
8 > /**
9 > * Represents an immutable set that works best for a small number of elements (less than 32).
10 > * It uses bits to encode element membership efficiently.
11 > */
12 > export class SmallImmutableSet<T> {
13 > // eslint-disable-next-line @typescript-eslint/no-explicit-any
14 > private static cache = new Array<SmallImmutableSet<any>>(129);
15 >
16 > private static create<T>(items: number, additionalItems: readonly number[]): SmallImmutableSet<T> {
17 > if (items <= 128 && additionalItems.length === 0) {
18 > // We create a cache of 128=2^7 elements to cover all sets with up to 7 (dense) elements.
19 > let cached = SmallImmutableSet.cache[items];
20 > if (!cached) {
21 > cached = new SmallImmutableSet(items, additionalItems);
22 > SmallImmutableSet.cache[items] = cached;
23 > }
24 > return cached;
25 > }
26
27 return new SmallImmutableSet(items, additionalItems);
29 >
30 > // eslint-disable-next-line @typescript-eslint/no-explicit-any
31 > private static empty = SmallImmutableSet.create<any>(0, emptyArr);
32 > public static getEmpty<T>(): SmallImmutableSet<T> {
33 return this.empty;
34 }
36 > private constructor(
37 > private readonly items: number,
38 > private readonly additionalItems: readonly number[]
39 > ) {
40 > }
41 >
42 > public add(value: T, keyProvider: IDenseKeyProvider<T>): SmallImmutableSet<T> {
43 const key = keyProvider.getKey(value);
44 let idx = key >> 5; // divided by 32
61 return SmallImmutableSet.create(this.items, newItems);
62 }
64 > public has(value: T, keyProvider: IDenseKeyProvider<T>): boolean {
65 const key = keyProvider.getKey(value);
66 let idx = key >> 5; // divided by 32
73 return ((this.additionalItems[idx] || 0) & (1 << (key & 31))) !== 0;
74 }
76 > public merge(other: SmallImmutableSet<T>): SmallImmutableSet<T> {
77 const merged = this.items | other.items;
78
98 return SmallImmutableSet.create(merged, newItems);
99 }
101 > public intersects(other: SmallImmutableSet<T>): boolean {
102 if ((this.items & other.items) !== 0) {
103 return true;
112 return false;
113 }
115 > public equals(other: SmallImmutableSet<T>): boolean {
116 if (this.items !== other.items) {
117 return false;
130 return true;
131 }
133 >
134 > export interface IDenseKeyProvider<T> {
135 > getKey(value: T): number;
136 > }
137 >
138 > export const identityKeyProvider: IDenseKeyProvider<number> = {
139 > getKey(value: number) {
140 return value;
141 }
143 >
144 > /**
145 > * Assigns values a unique incrementing key.
146 > */
147 > export class DenseKeyProvider<T> {
148 private readonly items = new Map<T, number>();
150 > getKey(value: T): number {
151 let existing = this.items.get(value);
152 if (existing === undefined) {
156 return existing;
157 }
159 > reverseLookup(value: number): T | undefined {
160 return [...this.items].find(([_key, v]) => v === value)?.[0];
161 }
163 > reverseLookupSet(set: SmallImmutableSet<T>): T[] {
164 const result: T[] = [];
165 for (const [key] of this.items) {