UNPKG

@sussudio/base

Version:

Internal APIs for VS Code's utilities and user interface building blocks.

55 lines (54 loc) 1.35 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ export function clamp(value, min, max) { return Math.min(Math.max(value, min), max); } export function rot(index, modulo) { return (modulo + (index % modulo)) % modulo; } export class Counter { _next = 0; getNext() { return this._next++; } } export class MovingAverage { _n = 1; _val = 0; update(value) { this._val = this._val + (value - this._val) / this._n; this._n += 1; return this._val; } get value() { return this._val; } } export class SlidingWindowAverage { _n = 0; _val = 0; _values = []; _index = 0; _sum = 0; constructor(size) { this._values = new Array(size); this._values.fill(0, 0, size); } update(value) { const oldValue = this._values[this._index]; this._values[this._index] = value; this._index = (this._index + 1) % this._values.length; this._sum -= oldValue; this._sum += value; if (this._n < this._values.length) { this._n += 1; } this._val = this._sum / this._n; return this._val; } get value() { return this._val; } }