-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into release-config
- Loading branch information
Showing
31 changed files
with
180 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export class RingBuffer<T> { | ||
private _array: T[] | ||
private _effectiveLength: number | ||
|
||
constructor(n: number) { | ||
this._array = new Array(n) | ||
this._effectiveLength = 0 | ||
} | ||
|
||
public toString() { | ||
return '[object RingBuffer(' + this._array.length + ') effectiveLength ' + this._effectiveLength + ']' | ||
} | ||
|
||
public length() { | ||
return Math.min(this._array.length, this._effectiveLength) | ||
} | ||
|
||
public effectiveLength() { | ||
return this._effectiveLength | ||
} | ||
|
||
public get(i: number) { | ||
if (i < 0 || i >= this.length()) return undefined | ||
const index = this.computeActualIndex(i) | ||
return this._array[index] | ||
} | ||
|
||
public set(i: number, v: T) { | ||
if (i < 0 || i >= this.length()) throw new Error('set() Index out of range') | ||
const index = this.computeActualIndex(i) | ||
this._array[index] = v | ||
} | ||
|
||
public push(v: T) { | ||
const index = this.computeActualIndex(this._effectiveLength) | ||
this._array[index] = v | ||
this._effectiveLength++ | ||
} | ||
|
||
public clear() { | ||
this._effectiveLength = 0 | ||
} | ||
|
||
public *[Symbol.iterator]() { | ||
for (let i = 0; i < this.length(); i++) { | ||
yield this.get(i) | ||
} | ||
} | ||
|
||
private computeActualIndex(offset: number) { | ||
return Math.max((this._effectiveLength - this._array.length, 0) + offset) % this._array.length | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.