| 1 |
/** |
| 2 |
* Copyright (c) 2016 The xterm.js authors. All rights reserved. |
| 3 |
* @license MIT |
| 4 |
*/ |
| 5 |
|
| 6 |
import { ICircularList } from 'common/Types'; |
| 7 |
import { Disposable } from 'vs/base/common/lifecycle'; |
| 8 |
import { Emitter } from 'vs/base/common/event'; |
| 9 |
|
| 10 |
export interface IInsertEvent { |
| 11 |
index: number; |
| 12 |
amount: number; |
| 13 |
} |
| 14 |
|
| 15 |
export interface IDeleteEvent { |
| 16 |
index: number; |
| 17 |
amount: number; |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Represents a circular list; a list with a maximum size that wraps around when push is called, |
| 22 |
* overriding values at the start of the list. |
| 23 |
*/ |
| 24 |
export class CircularList<T> extends Disposable implements ICircularList<T> { |
| 25 |
protected _array: (T | undefined)[]; |
| 26 |
private _startIndex: number; |
| 27 |
private _length: number; |
| 28 |
|
| 29 |
public readonly onDeleteEmitter = this._register(new Emitter<IDeleteEvent>()); |
| 30 |
public readonly onDelete = this.onDeleteEmitter.event; |
| 31 |
public readonly onInsertEmitter = this._register(new Emitter<IInsertEvent>()); |
| 32 |
public readonly onInsert = this.onInsertEmitter.event; |
| 33 |
public readonly onTrimEmitter = this._register(new Emitter<number>()); |
| 34 |
public readonly onTrim = this.onTrimEmitter.event; |
| 35 |
|
| 36 |
constructor( |
| 37 |
private _maxLength: number |
| 38 |
) { |
| 39 |
super(); |
| 40 |
this._array = new Array<T>(this._maxLength); |
| 41 |
this._startIndex = 0; |
| 42 |
this._length = 0; |
| 43 |
} |
| 44 |
|
| 45 |
public get maxLength(): number { |
| 46 |
return this._maxLength; |
| 47 |
} |
| 48 |
|
| 49 |
public set maxLength(newMaxLength: number) { |
| 50 |
// There was no change in maxLength, return early. |
| 51 |
if (this._maxLength === newMaxLength) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
// Reconstruct array, starting at index 0. Only transfer values from the |
| 56 |
// indexes 0 to length. |
| 57 |
const newArray = new Array<T | undefined>(newMaxLength); |
| 58 |
for (let i = 0; i < Math.min(newMaxLength, this.length); i++) { |
| 59 |
newArray[i] = this._array[this._getCyclicIndex(i)]; |
| 60 |
} |
| 61 |
this._array = newArray; |
| 62 |
this._maxLength = newMaxLength; |
| 63 |
this._startIndex = 0; |
| 64 |
} |
| 65 |
|
| 66 |
public get length(): number { |
| 67 |
return this._length; |
| 68 |
} |
| 69 |
|
| 70 |
public set length(newLength: number) { |
| 71 |
if (newLength > this._length) { |
| 72 |
for (let i = this._length; i < newLength; i++) { |
| 73 |
this._array[i] = undefined; |
| 74 |
} |
| 75 |
} |
| 76 |
this._length = newLength; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Gets the value at an index. |
| 81 |
* |
| 82 |
* Note that for performance reasons there is no bounds checking here, the index reference is |
| 83 |
* circular so this should always return a value and never throw. |
| 84 |
* @param index The index of the value to get. |
| 85 |
* @returns The value corresponding to the index. |
| 86 |
*/ |
| 87 |
public get(index: number): T | undefined { |
| 88 |
return this._array[this._getCyclicIndex(index)]; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Sets the value at an index. |
| 93 |
* |
| 94 |
* Note that for performance reasons there is no bounds checking here, the index reference is |
| 95 |
* circular so this should always return a value and never throw. |
| 96 |
* @param index The index to set. |
| 97 |
* @param value The value to set. |
| 98 |
*/ |
| 99 |
public set(index: number, value: T | undefined): void { |
| 100 |
this._array[this._getCyclicIndex(index)] = value; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Pushes a new value onto the list, wrapping around to the start of the array, overriding index 0 |
| 105 |
* if the maximum length is reached. |
| 106 |
* @param value The value to push onto the list. |
| 107 |
*/ |
| 108 |
public push(value: T): void { |
| 109 |
this._array[this._getCyclicIndex(this._length)] = value; |
| 110 |
if (this._length === this._maxLength) { |
| 111 |
this._startIndex = ++this._startIndex % this._maxLength; |
| 112 |
this.onTrimEmitter.fire(1); |
| 113 |
} else { |
| 114 |
this._length++; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Advance ringbuffer index and return current element for recycling. |
| 120 |
* Note: The buffer must be full for this method to work. |
| 121 |
* @throws When the buffer is not full. |
| 122 |
*/ |
| 123 |
public recycle(): T { |
| 124 |
if (this._length !== this._maxLength) { |
| 125 |
throw new Error('Can only recycle when the buffer is full'); |
| 126 |
} |
| 127 |
this._startIndex = ++this._startIndex % this._maxLength; |
| 128 |
this.onTrimEmitter.fire(1); |
| 129 |
return this._array[this._getCyclicIndex(this._length - 1)]!; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Ringbuffer is at max length. |
| 134 |
*/ |
| 135 |
public get isFull(): boolean { |
| 136 |
return this._length === this._maxLength; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Removes and returns the last value on the list. |
| 141 |
* @returns The popped value. |
| 142 |
*/ |
| 143 |
public pop(): T | undefined { |
| 144 |
return this._array[this._getCyclicIndex(this._length-- - 1)]; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Deletes and/or inserts items at a particular index (in that order). Unlike |
| 149 |
* Array.prototype.splice, this operation does not return the deleted items as a new array in |
| 150 |
* order to save creating a new array. Note that this operation may shift all values in the list |
| 151 |
* in the worst case. |
| 152 |
* @param start The index to delete and/or insert. |
| 153 |
* @param deleteCount The number of elements to delete. |
| 154 |
* @param items The items to insert. |
| 155 |
*/ |
| 156 |
public splice(start: number, deleteCount: number, ...items: T[]): void { |
| 157 |
// Delete items |
| 158 |
if (deleteCount) { |
| 159 |
for (let i = start; i < this._length - deleteCount; i++) { |
| 160 |
this._array[this._getCyclicIndex(i)] = this._array[this._getCyclicIndex(i + deleteCount)]; |
| 161 |
} |
| 162 |
this._length -= deleteCount; |
| 163 |
this.onDeleteEmitter.fire({ index: start, amount: deleteCount }); |
| 164 |
} |
| 165 |
|
| 166 |
// Add items |
| 167 |
for (let i = this._length - 1; i >= start; i--) { |
| 168 |
this._array[this._getCyclicIndex(i + items.length)] = this._array[this._getCyclicIndex(i)]; |
| 169 |
} |
| 170 |
for (let i = 0; i < items.length; i++) { |
| 171 |
this._array[this._getCyclicIndex(start + i)] = items[i]; |
| 172 |
} |
| 173 |
if (items.length) { |
| 174 |
this.onInsertEmitter.fire({ index: start, amount: items.length }); |
| 175 |
} |
| 176 |
|
| 177 |
// Adjust length as needed |
| 178 |
if (this._length + items.length > this._maxLength) { |
| 179 |
const countToTrim = (this._length + items.length) - this._maxLength; |
| 180 |
this._startIndex += countToTrim; |
| 181 |
this._length = this._maxLength; |
| 182 |
this.onTrimEmitter.fire(countToTrim); |
| 183 |
} else { |
| 184 |
this._length += items.length; |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Trims a number of items from the start of the list. |
| 190 |
* @param count The number of items to remove. |
| 191 |
*/ |
| 192 |
public trimStart(count: number): void { |
| 193 |
if (count > this._length) { |
| 194 |
count = this._length; |
| 195 |
} |
| 196 |
this._startIndex += count; |
| 197 |
this._length -= count; |
| 198 |
this.onTrimEmitter.fire(count); |
| 199 |
} |
| 200 |
|
| 201 |
public shiftElements(start: number, count: number, offset: number): void { |
| 202 |
if (count <= 0) { |
| 203 |
return; |
| 204 |
} |
| 205 |
if (start < 0 || start >= this._length) { |
| 206 |
throw new Error('start argument out of range'); |
| 207 |
} |
| 208 |
if (start + offset < 0) { |
| 209 |
throw new Error('Cannot shift elements in list beyond index 0'); |
| 210 |
} |
| 211 |
|
| 212 |
if (offset > 0) { |
| 213 |
for (let i = count - 1; i >= 0; i--) { |
| 214 |
this.set(start + i + offset, this.get(start + i)); |
| 215 |
} |
| 216 |
const expandListBy = (start + count + offset) - this._length; |
| 217 |
if (expandListBy > 0) { |
| 218 |
this._length += expandListBy; |
| 219 |
while (this._length > this._maxLength) { |
| 220 |
this._length--; |
| 221 |
this._startIndex++; |
| 222 |
this.onTrimEmitter.fire(1); |
| 223 |
} |
| 224 |
} |
| 225 |
} else { |
| 226 |
for (let i = 0; i < count; i++) { |
| 227 |
this.set(start + i + offset, this.get(start + i)); |
| 228 |
} |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Gets the cyclic index for the specified regular index. The cyclic index can then be used on the |
| 234 |
* backing array to get the element associated with the regular index. |
| 235 |
* @param index The regular index. |
| 236 |
* @returns The cyclic index. |
| 237 |
*/ |
| 238 |
private _getCyclicIndex(index: number): number { |
| 239 |
return (this._startIndex + index) % this._maxLength; |
| 240 |
} |
| 241 |
} |