|
| 1 | +# Round Linked Queue |
| 2 | + |
| 3 | +[](https://coveralls.io/github/hbarcelos/round-linked-queue?branch=master) |
| 4 | + |
| 5 | +Implements a fixed-size linked-list based queue. When trying to add an element to a full queue, it will evict the first added item in order to make room for the new element. This queue preserves the First In/First Out operation mode. |
| 6 | + |
| 7 | +## How it works? |
| 8 | + |
| 9 | +Given a queue of capacity 3, initially empty: |
| 10 | + |
| 11 | +``` |
| 12 | +(Nil) |
| 13 | +``` |
| 14 | + |
| 15 | +Calling `queue.add(1)` will put `1` at the beginning of the queue: |
| 16 | + |
| 17 | +``` |
| 18 | +(1) -> (Nil) |
| 19 | +``` |
| 20 | + |
| 21 | +Then `queue.add(2)` will turn it into: |
| 22 | + |
| 23 | +``` |
| 24 | +(1) -> (2) -> (Nil) |
| 25 | +``` |
| 26 | + |
| 27 | +Once more, with `queue.add(3)` we will have: |
| 28 | + |
| 29 | +``` |
| 30 | +(1) -> (2) -> (3) -> (Nil) |
| 31 | +``` |
| 32 | + |
| 33 | +Now the queue is at capacity. If we try to add another element, it will remove the element that was added first and shift all elements to the right. Consider `queue.add(4)`, the final result will be: |
| 34 | + |
| 35 | +``` |
| 36 | +(2) -> (3) -> (4) -> (Nil) |
| 37 | +
|
| 38 | +(1) // <--- removed element will be returned |
| 39 | +``` |
| 40 | + |
| 41 | +## Installation |
| 42 | + |
| 43 | +```sh |
| 44 | +yarn add round-linked-queue |
| 45 | +# or with NPM |
| 46 | +# npm install --save round-linked-queue |
| 47 | +``` |
| 48 | + |
| 49 | +## API |
| 50 | + |
| 51 | +### constructor |
| 52 | + |
| 53 | +``` |
| 54 | +new RoundLinkedQueue(maxLength: number) => RoudLinkedQueue |
| 55 | +``` |
| 56 | + |
| 57 | +Params: |
| 58 | +- `maxLength: number`: the capacity of the queue. |
| 59 | + |
| 60 | +### #add() |
| 61 | + |
| 62 | +``` |
| 63 | +add(element: Any) => Any |
| 64 | +``` |
| 65 | + |
| 66 | +Params: |
| 67 | +- `element: Any`: the element to add to the queue. |
| 68 | + |
| 69 | +Returns: |
| 70 | +- `undefined`: if the queue is not at capacity, because no element was removed. |
| 71 | +- `Any`: the removed element if the queue is at capacity. |
| 72 | + |
| 73 | +### #remove() |
| 74 | + |
| 75 | +``` |
| 76 | +remove() => Any, throws Error |
| 77 | +``` |
| 78 | + |
| 79 | +Returns: |
| 80 | +- `Any`: the removed element if the queue is not empty. |
| 81 | + |
| 82 | +Throws: |
| 83 | +- `Error`: if the queue is empty. |
| 84 | + |
| 85 | + |
| 86 | +### #toArray() |
| 87 | + |
| 88 | +``` |
| 89 | +toArray() => [Any] |
| 90 | +``` |
| 91 | + |
| 92 | +Returns: |
| 93 | +- `[Any]`: an Array with all elements in the queue. |
| 94 | + |
| 95 | +### #*\[Symbol.iterator\]() |
| 96 | + |
| 97 | +``` |
| 98 | +*[Symbol.iterator]() => [Any] |
| 99 | +``` |
| 100 | + |
| 101 | +Returns: |
| 102 | +- `Iterator`: an iterator for the queue. |
| 103 | + |
| 104 | + |
| 105 | +### .maxLength |
| 106 | + |
| 107 | +``` |
| 108 | +get maxLength => Number |
| 109 | +``` |
| 110 | + |
| 111 | +Returns: |
| 112 | +- `Number`: the capacity of the queue. |
| 113 | + |
| 114 | +### .first |
| 115 | + |
| 116 | +``` |
| 117 | +get first => Any, throws Error |
| 118 | +``` |
| 119 | + |
| 120 | +Returns: |
| 121 | +- `Any`: the first element in the queue if it is not empty. |
| 122 | + |
| 123 | +Throws: |
| 124 | +- `Error`: if the queue is empty. |
| 125 | + |
| 126 | + |
| 127 | +### .last |
| 128 | + |
| 129 | +``` |
| 130 | +get last => Any, throws Error |
| 131 | +``` |
| 132 | + |
| 133 | +Returns: |
| 134 | +- `Any`: the last element in the queue if it is not empty. |
| 135 | + |
| 136 | +Throws: |
| 137 | +- `Error`: if the queue is empty. |
| 138 | + |
| 139 | + |
| 140 | +### static fromArray |
| 141 | + |
| 142 | +``` |
| 143 | +RoundLinkedQueue.fromArray(inputArray: [Any]) => RoudLinkedQueue |
| 144 | +``` |
| 145 | + |
| 146 | +Params: |
| 147 | +- `inputArray: [Any]`: an arbitrary array of elements. |
| 148 | + |
| 149 | +Returns: |
| 150 | +- `RoundLinkedQueue`: a new instance of the queue containing all elements in the array, respecting the insertion order. |
| 151 | + |
| 152 | +## Contributing |
| 153 | + |
| 154 | +### Running tests |
| 155 | + |
| 156 | +After clonnig this repo, run: |
| 157 | + |
| 158 | +``` |
| 159 | +yarn test **.test.js |
| 160 | +# or with NPM |
| 161 | +# npm run test **.test.js |
| 162 | +``` |
0 commit comments