Path: blob/main/src/vs/editor/common/model/fixedArray.ts
3294 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { arrayInsert } from '../../../base/common/arrays.js';67/**8* An array that avoids being sparse by always9* filling up unused indices with a default value.10*/11export class FixedArray<T> {12private _store: T[] = [];1314constructor(15private readonly _default: T16) { }1718public get(index: number): T {19if (index < this._store.length) {20return this._store[index];21}22return this._default;23}2425public set(index: number, value: T): void {26while (index >= this._store.length) {27this._store[this._store.length] = this._default;28}29this._store[index] = value;30}3132public replace(index: number, oldLength: number, newLength: number): void {33if (index >= this._store.length) {34return;35}3637if (oldLength === 0) {38this.insert(index, newLength);39return;40} else if (newLength === 0) {41this.delete(index, oldLength);42return;43}4445const before = this._store.slice(0, index);46const after = this._store.slice(index + oldLength);47const insertArr = arrayFill(newLength, this._default);48this._store = before.concat(insertArr, after);49}5051public delete(deleteIndex: number, deleteCount: number): void {52if (deleteCount === 0 || deleteIndex >= this._store.length) {53return;54}55this._store.splice(deleteIndex, deleteCount);56}5758public insert(insertIndex: number, insertCount: number): void {59if (insertCount === 0 || insertIndex >= this._store.length) {60return;61}62const arr: T[] = [];63for (let i = 0; i < insertCount; i++) {64arr[i] = this._default;65}66this._store = arrayInsert(this._store, insertIndex, arr);67}68}6970function arrayFill<T>(length: number, value: T): T[] {71const arr: T[] = [];72for (let i = 0; i < length; i++) {73arr[i] = value;74}75return arr;76}777879