| 4 | 4 | // |
| 5 | 5 | |
| 6 | 6 | class CircularBuffer { |
| 7 | | constructor() { |
| 8 | | throw new Error('Remove this line and implement the function'); |
| 7 | constructor(size) { |
| 8 | // Edge Case: Invalid buffer size (non-positive or non-integer) |
| 9 | if (size <= 0 || !Number.isInteger(size)) { |
| 10 | throw new Error('Invalid buffer size'); |
| 11 | } |
| 12 | |
| 13 | this.buffer = new Array(size); |
| 14 | this.size = size; |
| 15 | this.readIndex = 0; |
| 16 | this.writeIndex = 0; |
| 17 | this.isFull = false; |
| 9 | 18 | } |
| 10 | 19 | |
| 11 | | write() { |
| 12 | | throw new Error('Remove this line and implement the function'); |
| 20 | write(value) { |
| 21 | // Edge Case: Writing to a full buffer |
| 22 | if (this.isFull) { |
| 23 | throw new BufferFullError(); |
| 24 | } |
| 25 | |
| 26 | this.buffer[this.writeIndex] = value; |
| 27 | this.writeIndex = (this.writeIndex + 1) % this.size; |
| 28 | |
| 29 | if (this.writeIndex === this.readIndex) { |
| 30 | this.isFull = true; |
| 31 | } |
| 32 | |
| 33 | return value; |
| 13 | 34 | } |
| 14 | 35 | |
| 15 | 36 | read() { |
| 16 | | throw new Error('Remove this line and implement the function'); |
| 37 | // Edge Case: Reading from an empty buffer |
| 38 | if (this.readIndex === this.writeIndex && !this.isFull) { |
| 39 | throw new BufferEmptyError(); |
| 40 | } |
| 41 | |
| 42 | const value = this.buffer[this.readIndex]; |
| 43 | this.buffer[this.readIndex] = undefined; |
| 44 | this.readIndex = (this.readIndex + 1) % this.size; |
| 45 | this.isFull = false; |
| 46 | |
| 47 | return value; |
| 17 | 48 | } |
| 18 | 49 | |
| 19 | | forceWrite() { |
| 20 | | throw new Error('Remove this line and implement the function'); |
| 50 | forceWrite(value) { |
| 51 | // Edge Case: Force writing to a full buffer (overwrites oldest element) |
| 52 | if (this.isFull) { |
| 53 | this.buffer[this.writeIndex] = value; |
| 54 | this.writeIndex = (this.writeIndex + 1) % this.size; |
| 55 | this.readIndex = (this.readIndex + 1) % this.size; |
| 56 | } else { |
| 57 | this.write(value); |
| 58 | } |
| 59 | |
| 60 | return value; |
| 21 | 61 | } |
| 22 | 62 | |
| 23 | 63 | clear() { |
| 24 | | throw new Error('Remove this line and implement the function'); |
| 64 | // Edge Case: Clearing an already empty buffer |
| 65 | this.buffer = new Array(this.size); |
| 66 | this.readIndex = 0; |
| 67 | this.writeIndex = 0; |
| 68 | this.isFull = false; |
| 69 | } |
| 70 | |
| 71 | isEmpty() { |
| 72 | return this.readIndex === this.writeIndex && !this.isFull; |
| 25 | 73 | } |
| 26 | 74 | } |
| 27 | 75 | |
| 29 | 77 | |
| 30 | 78 | export class BufferFullError extends Error { |
| 31 | 79 | constructor() { |
| 32 | | throw new Error('Remove this line and implement the function'); |
| 80 | super('Buffer full'); |
| 81 | this.name = 'BufferFullError'; |
| 33 | 82 | } |
| 83 | |
| 84 | // Handled Edge Cases: Buffer overflow when full |
| 34 | 85 | } |
| 35 | 86 | |
| 36 | 87 | export class BufferEmptyError extends Error { |
| 37 | 88 | constructor() { |
| 38 | | throw new Error('Remove this line and implement the function'); |
| 89 | super('Buffer empty'); |
| 90 | this.name = 'BufferEmptyError'; |
| 39 | 91 | } |
| 92 | |
| 93 | // Handled Edge Cases: Reading from empty buffer |
| 40 | 94 | } |
| 95 | |
| 96 | // Handled Edge Cases: Buffer initialization with invalid size, Buffer overflow when full, Reading from empty buffer, Wraparound indexing, Buffer clearing |