| 8 | 8 | * @param {number} capacity |
| 9 | 9 | */ |
| 10 | 10 | constructor(capacity) { |
| 11 | | throw new Error('Remove this line and implement the function'); |
| 11 | // Edge Case: Buffer initialization with zero or negative size |
| 12 | if (capacity <= 0) { |
| 13 | throw new Error('Capacity must be a positive number'); |
| 14 | } |
| 15 | |
| 16 | this.buffer = new Array(capacity); |
| 17 | this.capacity = capacity; |
| 18 | this.size = 0; |
| 19 | this.readIndex = 0; |
| 20 | this.writeIndex = 0; |
| 12 | 21 | } |
| 13 | 22 | |
| 14 | 23 | /** |
| 15 | 24 | * @param {*} value |
| 16 | 25 | */ |
| 17 | 26 | write(value) { |
| 18 | | throw new Error('Remove this line and implement the function'); |
| 27 | // Edge Case: Writing to a full buffer |
| 28 | if (this.size === this.capacity) { |
| 29 | throw new BufferFullError(); |
| 30 | } |
| 31 | |
| 32 | this.buffer[this.writeIndex] = value; |
| 33 | this.writeIndex = (this.writeIndex + 1) % this.capacity; |
| 34 | this.size++; |
| 19 | 35 | } |
| 20 | 36 | |
| 21 | 37 | /** |
| 22 | 38 | * @returns {*} |
| 23 | 39 | */ |
| 24 | 40 | read() { |
| 25 | | throw new Error('Remove this line and implement the function'); |
| 41 | // Edge Case: Reading from an empty buffer |
| 42 | if (this.size === 0) { |
| 43 | throw new BufferEmptyError(); |
| 44 | } |
| 45 | |
| 46 | const value = this.buffer[this.readIndex]; |
| 47 | this.readIndex = (this.readIndex + 1) % this.capacity; |
| 48 | this.size--; |
| 49 | return value; |
| 26 | 50 | } |
| 27 | 51 | |
| 28 | 52 | /** |
| 29 | 53 | * @param {*} value |
| 30 | 54 | */ |
| 31 | 55 | forceWrite(value) { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 56 | // Edge Case: Force writing to a full buffer (overwrites oldest element) |
| 57 | if (this.size === this.capacity) { |
| 58 | // Overwrite the oldest element (at readIndex) |
| 59 | this.buffer[this.readIndex] = value; |
| 60 | this.readIndex = (this.readIndex + 1) % this.capacity; |
| 61 | this.writeIndex = (this.writeIndex + 1) % this.capacity; |
| 62 | } else { |
| 63 | // If buffer is not full, just write normally |
| 64 | this.write(value); |
| 65 | } |
| 33 | 66 | } |
| 34 | 67 | |
| 35 | 68 | clear() { |
| 36 | | throw new Error('Remove this line and implement the function'); |
| 69 | // Edge Case: Clearing an already empty buffer (no effect needed) |
| 70 | this.size = 0; |
| 71 | this.readIndex = 0; |
| 72 | this.writeIndex = 0; |
| 37 | 73 | } |
| 38 | 74 | } |
| 39 | 75 | |
| 41 | 77 | |
| 42 | 78 | export class BufferFullError extends Error { |
| 43 | 79 | constructor() { |
| 44 | | throw new Error('Remove this line and implement the function'); |
| 80 | super('Buffer full'); |
| 81 | this.name = 'BufferFullError'; |
| 45 | 82 | } |
| 83 | |
| 84 | // Handled Edge Cases: None (this is an error class) |
| 46 | 85 | } |
| 47 | 86 | |
| 48 | 87 | export class BufferEmptyError extends Error { |
| 49 | 88 | constructor() { |
| 50 | | throw new Error('Remove this line and implement the function'); |
| 89 | super('Buffer empty'); |
| 90 | this.name = 'BufferEmptyError'; |
| 51 | 91 | } |
| 92 | |
| 93 | // Handled Edge Cases: None (this is an error class) |
| 52 | 94 | } |
| 95 | |
| 96 | // Handled Edge Cases: Buffer initialization with zero or negative size, Writing to a full buffer, Reading from an empty buffer, Force writing to a full buffer (overwrites oldest element), Clearing an already empty buffer |