-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCircularBuffer.ts
77 lines (62 loc) · 2.16 KB
/
CircularBuffer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class CircularBuffer<T>{
private buffer: T[] = [];
private size: number = 0;
private capacity: number = 0;
private writeIndex: number = 0;
private readIndex: number = 0;
constructor(capacity: number) {
this.buffer = new Array(capacity);
this.capacity = capacity;
}
public isEmpty = (): boolean => this.size === 0;
public getSize = (): number => this.size;
public clear = (): void => {
this.buffer = new Array(this.capacity);
this.size = 0;
}
public peekHead = (): T | null => {
if (this.isEmpty())
return null;
return this.buffer[this.readIndex];
}
public peekTail = (): T | null => {
if (this.isEmpty())
return null;
if(this.writeIndex === 0){
return this.buffer[this.size - 1];
}
return this.buffer[this.writeIndex - 1];
}
public enqueue = (data: T): void => {
this.buffer[this.writeIndex] = data;
const isOverwritten = this.size !== 0 && this.readIndex === this.writeIndex;
if(isOverwritten)
this.readIndex = (this.readIndex + 1) % this.capacity;
this.writeIndex = (this.writeIndex + 1) % this.capacity;
this.size++;
}
public dequeue = (): T | null => {
const read = this.buffer[this.readIndex];
this.readIndex = (this.readIndex + 1) % this.capacity;
this.size--;
return read;
}
public contains = (comparator: (data: T) => boolean) => {
return this.buffer.some(item => comparator(item));
}
}
/********************************************************************************************************************/
const circularBuffer = new CircularBuffer<number>(5);
circularBuffer.enqueue(1);
circularBuffer.enqueue(2);
circularBuffer.enqueue(3);
circularBuffer.enqueue(4);
circularBuffer.enqueue(5);
circularBuffer.enqueue(6);
circularBuffer.enqueue(7);
circularBuffer.dequeue();
circularBuffer.dequeue();
circularBuffer.dequeue();
// console.log(circularBuffer.contains((data) => data === 4)); false
// console.log(circularBuffer.contains((data) => data === 1)); true
console.log(circularBuffer)