Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | 45x 45x 45x 45x 45x 3041x 3041x 3041x 3041x 3041x 4x 415x 415x 322x 337x 415x 117x 13499x 12x 12x 13x 12x 962x 962x 71x 71x 80x 71x 11002x 11002x 11002x 4117x 6885x 6885x 1790x 1790x 1788x 2x 2x 2x 1x 1x 1x 1x 1x 1x 1x 2x 1x 1045x 1045x 5905x 5905x 122x 122x 5905x 5905x 5905x 302x 302x 302x 93x 872x 872x 12792x 12792x 12792x 12792x 12792x 12792x 12790x 58x 12790x 12790x 12790x | import { Range } from 'vscode-languageserver-types';
/**
* The slots of one chunk: start line, start character, end line, end character
* and the index of the next chunk of the same key.
*/
const SLOTS_PER_CHUNK = 5;
const NEXT_SLOT = 4;
const END_OF_CHAIN = 0xFFFFFFFF;
const INITIAL_CHUNK_CAPACITY = 64;
const INITIAL_KEY_CAPACITY = 16;
/**
* Maps keys, such as IRIs or blank node identifiers, to the ranges where they
* occur in a document. A document index holds one entry per token occurrence,
* so the storage is kept dense: the ranges of all keys live in one typed array
* of fixed-size chunks that are chained per key, and each key costs one map
* entry. Compared with a `Range` object per occurrence this is about an order
* of magnitude less memory across a workspace. `Range` objects are created on
* demand when ranges are read, so lookups by key stay O(1).
*/
export class RangeIndex {
/**
* The number of each key, which indexes the first and the last chunk of its chain.
* Appending a range to a known key thus takes a single map lookup.
*/
private readonly _keyNumbers = new Map<string, number>();
private _heads = new Uint32Array(INITIAL_KEY_CAPACITY);
private _tails = new Uint32Array(INITIAL_KEY_CAPACITY);
private _chunks = new Uint32Array(INITIAL_CHUNK_CAPACITY * SLOTS_PER_CHUNK);
private _chunkCount = 0;
/**
* The number of keys in the index.
*/
get size(): number {
return this._keyNumbers.size;
}
/**
* Creates an index from a plain record of ranges, as produced by parsers and used in tests.
* @param record The ranges per key.
* @returns A new index holding the same ranges.
*/
static fromRecord(record: Record<string, Range[]>): RangeIndex {
const index = new RangeIndex();
for (const [key, ranges] of Object.entries(record)) {
for (const range of ranges) {
index.add(key, range);
}
}
return index;
}
/**
* The keys in the index, in insertion order.
*/
keys(): IterableIterator<string> {
return this._keyNumbers.keys();
}
/**
* Indicates whether at least one range is recorded for a key.
* @param key The key.
*/
has(key: string): boolean {
return this._keyNumbers.has(key);
}
/**
* The number of ranges recorded for a key; `0` for an unknown key.
* @param key The key.
*/
count(key: string): number {
let count = 0;
for (let chunk = this._headOf(key); chunk !== END_OF_CHAIN; chunk = this._nextOf(chunk)) {
count++;
}
return count;
}
/**
* The first range recorded for a key, or `undefined` for an unknown key.
* @param key The key.
*/
first(key: string): Range | undefined {
const head = this._headOf(key);
return head === END_OF_CHAIN ? undefined : this._rangeOf(head);
}
/**
* All ranges recorded for a key, in insertion order; empty for an unknown key.
* @param key The key.
*/
getRanges(key: string): Range[] {
const ranges: Range[] = [];
for (let chunk = this._headOf(key); chunk !== END_OF_CHAIN; chunk = this._nextOf(chunk)) {
ranges.push(this._rangeOf(chunk));
}
return ranges;
}
/**
* Appends a range to the ranges of a key.
* @param key The key.
* @param range The range to record.
*/
add(key: string, range: Range): void {
const keyNumber = this._keyNumbers.get(key);
const chunk = this._appendChunk(range);
if (keyNumber === undefined) {
this._addKey(key, chunk);
} else {
this._chunks[this._tails[keyNumber] * SLOTS_PER_CHUNK + NEXT_SLOT] = chunk;
this._tails[keyNumber] = chunk;
}
}
/**
* Replaces the ranges of a key with a single range.
* @param key The key.
* @param range The range to record.
*/
set(key: string, range: Range): void {
const keyNumber = this._keyNumbers.get(key);
if (keyNumber === undefined) {
this._addKey(key, this._appendChunk(range));
} else {
// Reuse the first chunk and cut the chain; chunks further down are only
// reclaimed by clear(), which is fine for the rare repeated set.
const head = this._heads[keyNumber];
this._writeChunk(head, range);
this._tails[keyNumber] = head;
}
}
/**
* Removes all keys and ranges.
*/
clear(): void {
this._keyNumbers.clear();
this._heads = new Uint32Array(INITIAL_KEY_CAPACITY);
this._tails = new Uint32Array(INITIAL_KEY_CAPACITY);
this._chunks = new Uint32Array(INITIAL_CHUNK_CAPACITY * SLOTS_PER_CHUNK);
this._chunkCount = 0;
}
/**
* Returns the index as a plain record of range arrays.
*/
toRecord(): Record<string, Range[]> {
const record: Record<string, Range[]> = {};
for (const key of this._keyNumbers.keys()) {
record[key] = this.getRanges(key);
}
return record;
}
private _headOf(key: string): number {
const keyNumber = this._keyNumbers.get(key);
return keyNumber === undefined ? END_OF_CHAIN : this._heads[keyNumber];
}
/**
* Adds a key whose chain consists of a single chunk, growing the storage by doubling.
* @param key The key.
* @param chunk The index of the chunk.
*/
private _addKey(key: string, chunk: number): void {
const keyNumber = this._keyNumbers.size;
if (keyNumber >= this._heads.length) {
this._heads = this._grow(this._heads);
this._tails = this._grow(this._tails);
}
this._keyNumbers.set(key, keyNumber);
this._heads[keyNumber] = chunk;
this._tails[keyNumber] = chunk;
}
/**
* Returns a copy of an array with twice its length.
* @param array The array to grow.
*/
private _grow(array: Uint32Array<ArrayBuffer>): Uint32Array<ArrayBuffer> {
const grown = new Uint32Array(array.length * 2);
grown.set(array);
return grown;
}
private _nextOf(chunk: number): number {
return this._chunks[chunk * SLOTS_PER_CHUNK + NEXT_SLOT];
}
private _rangeOf(chunk: number): Range {
const offset = chunk * SLOTS_PER_CHUNK;
return Range.create(this._chunks[offset], this._chunks[offset + 1], this._chunks[offset + 2], this._chunks[offset + 3]);
}
/**
* Writes a range into a chunk and terminates the chain there.
*/
private _writeChunk(chunk: number, range: Range): void {
const offset = chunk * SLOTS_PER_CHUNK;
this._chunks[offset] = range.start.line;
this._chunks[offset + 1] = range.start.character;
this._chunks[offset + 2] = range.end.line;
this._chunks[offset + 3] = range.end.character;
this._chunks[offset + NEXT_SLOT] = END_OF_CHAIN;
}
/**
* Appends a new chunk holding the range, growing the storage by doubling.
* @returns The index of the new chunk.
*/
private _appendChunk(range: Range): number {
if ((this._chunkCount + 1) * SLOTS_PER_CHUNK > this._chunks.length) {
this._chunks = this._grow(this._chunks);
}
const chunk = this._chunkCount++;
this._writeChunk(chunk, range);
return chunk;
}
}
|