mirror of
https://gitlab.com/KevinRoebert/ClearUrls
synced 2025-12-17 22:55:36 +07:00
init 2
This commit is contained in:
63
source/utils/CircularBuffer.ts
Normal file
63
source/utils/CircularBuffer.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
export default class CircularBuffer<T> {
|
||||
private buffer: T[]
|
||||
public readonly capacity: number
|
||||
|
||||
/**
|
||||
* Create a new circular buffer, that removes the oldes elements if the capacity is reached.
|
||||
*
|
||||
* @param capacity - the capacity of this queue
|
||||
* @throws error if the capacity is <= 0
|
||||
*/
|
||||
public constructor(capacity: number) {
|
||||
if (capacity <= 0) {
|
||||
throw new Error('The capacity must be > 0')
|
||||
}
|
||||
|
||||
this.capacity = capacity
|
||||
this.buffer = []
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the buffer is empty.
|
||||
*
|
||||
* @returns true if the buffer is empty otherwise false
|
||||
*/
|
||||
public isEmpty() : boolean {
|
||||
return this.buffer.length === 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the buffer is full.
|
||||
*
|
||||
* @returns true if the buffer is full otherwise false
|
||||
*/
|
||||
public isFull() : boolean {
|
||||
return this.buffer.length === this.capacity
|
||||
}
|
||||
|
||||
/**
|
||||
* Enqueues an item.
|
||||
*
|
||||
* @param item - item that should be enqueued
|
||||
*/
|
||||
public enqueue(item: T) : void {
|
||||
if (this.isFull()) {
|
||||
this.buffer.shift()
|
||||
}
|
||||
|
||||
this.buffer.push(item)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the buffer in a fresh array.
|
||||
*
|
||||
* @returns the buffer
|
||||
*/
|
||||
public toArray() : T[] {
|
||||
return [...this.buffer]
|
||||
}
|
||||
|
||||
get size() : number {
|
||||
return this.buffer.length
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user