|
| 1 | +class Router { |
| 2 | + constructor(memoryLimit) { |
| 3 | + this.memoryLimit = memoryLimit; |
| 4 | + this.storage = []; |
| 5 | + this.packetSet = new Set(); |
| 6 | + this.destMap = new Map(); |
| 7 | + } |
| 8 | + |
| 9 | + addPacket(source, destination, timestamp) { |
| 10 | + const packet = [source, destination, timestamp]; |
| 11 | + if (this.packetSet.has(packet.toString())) { |
| 12 | + return false; |
| 13 | + } |
| 14 | + |
| 15 | + if (this.storage.length >= this.memoryLimit) { |
| 16 | + this._removeOldest(); |
| 17 | + } |
| 18 | + |
| 19 | + this.storage.push(packet); |
| 20 | + this.packetSet.add(packet.toString()); |
| 21 | + |
| 22 | + if (!this.destMap.has(destination)) { |
| 23 | + this.destMap.set(destination, []); |
| 24 | + } |
| 25 | + |
| 26 | + this.destMap.get(destination).push(timestamp); |
| 27 | + return true; |
| 28 | + } |
| 29 | + |
| 30 | + forwardPacket() { |
| 31 | + if (this.storage.length === 0) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + |
| 35 | + const packet = this.storage.shift(); |
| 36 | + const [source, destination, timestamp] = packet; |
| 37 | + this.packetSet.delete(packet.toString()); |
| 38 | + |
| 39 | + const tsList = this.destMap.get(destination); |
| 40 | + const idx = this._binarySearch(tsList, timestamp); |
| 41 | + if (idx < tsList.length && tsList[idx] === timestamp) { |
| 42 | + tsList.splice(idx, 1); |
| 43 | + } |
| 44 | + |
| 45 | + if (tsList.length === 0) { |
| 46 | + this.destMap.delete(destination); |
| 47 | + } |
| 48 | + |
| 49 | + return [source, destination, timestamp]; |
| 50 | + } |
| 51 | + |
| 52 | + getCount(destination, startTime, endTime) { |
| 53 | + if (!this.destMap.has(destination)) { |
| 54 | + return 0; |
| 55 | + } |
| 56 | + |
| 57 | + const tsList = this.destMap.get(destination); |
| 58 | + const leftIndex = this._binarySearch(tsList, startTime); |
| 59 | + const rightIndex = this._binarySearch(tsList, endTime, true); |
| 60 | + return rightIndex - leftIndex; |
| 61 | + } |
| 62 | + |
| 63 | + _removeOldest() { |
| 64 | + if (this.storage.length > 0) { |
| 65 | + const packet = this.storage.shift(); |
| 66 | + const [source, destination, timestamp] = packet; |
| 67 | + this.packetSet.delete(packet.toString()); |
| 68 | + |
| 69 | + const tsList = this.destMap.get(destination); |
| 70 | + const idx = this._binarySearch(tsList, timestamp); |
| 71 | + if (idx < tsList.length && tsList[idx] === timestamp) { |
| 72 | + tsList.splice(idx, 1); |
| 73 | + } |
| 74 | + |
| 75 | + if (tsList.length === 0) { |
| 76 | + this.destMap.delete(destination); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + _binarySearch(arr, target, isRight = false) { |
| 82 | + let left = 0; |
| 83 | + let right = arr.length; |
| 84 | + |
| 85 | + while (left < right) { |
| 86 | + const mid = Math.floor((left + right) / 2); |
| 87 | + if (arr[mid] < target || (isRight && arr[mid] === target)) { |
| 88 | + left = mid + 1; |
| 89 | + } else { |
| 90 | + right = mid; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return left; |
| 95 | + } |
| 96 | +} |
0 commit comments