├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── index.d.ts ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fifo 2 | 3 | FIFO queue implemented using a double linked-list 4 | 5 | ``` 6 | npm install fifo 7 | ``` 8 | 9 | [![build status](http://img.shields.io/travis/mafintosh/fifo.svg?style=flat)](http://travis-ci.org/mafintosh/fifo) 10 | 11 | ## Usage 12 | 13 | ``` js 14 | var fifo = require('fifo')() 15 | 16 | fifo.push('hello') 17 | fifo.push('world') 18 | 19 | console.log(fifo.first()) // prints hello 20 | console.log(fifo.last()) // prints world 21 | 22 | console.log(fifo.shift()) // prints hello 23 | console.log(fifo.shift()) // prints world 24 | 25 | var node = fifo.push('meh') 26 | 27 | fifo.remove(node) // remove 'meh' from the stack 28 | fifo.unshift('hello') // insert at the beginning 29 | ``` 30 | 31 | `fifo` uses a linked list behind the scene so all list manipulation methods run in O(1) 32 | 33 | ## API 34 | 35 | #### `fifo = FIFO()` 36 | 37 | Create a new instance 38 | 39 | #### `fifo.node` 40 | 41 | Contains the first node on the list. 42 | 43 | #### `fifo.length` 44 | 45 | Number of nodes in the list. 46 | 47 | #### `node = fifo.push(value)` 48 | 49 | Push a new value to the end of the list. Returns a node that contains this value. 50 | The value can be accessed by accessing `node.value`. 51 | 52 | #### `value = fifo.shift()` 53 | 54 | Removes the first node and returns the value 55 | 56 | #### `value = fifo.pop()` 57 | 58 | Removes the last node and returns the value 59 | 60 | #### `value = fifo.remove(node)` 61 | 62 | Removes the node and returns the value 63 | 64 | #### `fifo.add(node)` 65 | 66 | Readds a node. Should only be done with a node that has been removed. 67 | 68 | #### `value = fifo.first()` 69 | 70 | Peek at the first value 71 | 72 | #### `value = fifo.last()` 73 | 74 | Peek at the last value 75 | 76 | #### `node = fifo.unshift(value)` 77 | 78 | Inserts a value at the beginning of the list 79 | 80 | #### `node = fifo.next(node)` 81 | 82 | Returns the next node relative to the node you pass. 83 | If the node was the last node in the list `null` is returned. 84 | 85 | #### `node = fifo.prev(node)` 86 | 87 | Returns the previous node relative to the node you pass. 88 | If the node was the first node in the list `null` is returned. 89 | 90 | #### `fifo.bump(node)` 91 | 92 | Moves a node to the end of the list 93 | 94 | #### `fifo.clear()` 95 | 96 | Clears the list. 97 | 98 | #### `fifo.forEach(fn)` 99 | 100 | Iterate over all values in the list. Calls the function with `value, node`. 101 | 102 | ## Iteration 103 | 104 | To iterate the list simply use the following for loop 105 | 106 | ``` js 107 | for (var node = fifo.node; node; node = fifo.next(node)) { 108 | console.log('value is', node.value) 109 | } 110 | ``` 111 | 112 | Optionally you can call `fifo.forEach(fn)` which does the above internally. 113 | 114 | ## License 115 | 116 | MIT 117 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export = FIFO 2 | 3 | declare class Node { 4 | next: Node 5 | prev: Node 6 | list: FIFO 7 | value: T 8 | constructor(list: FIFO, val: T) 9 | link(next: Node): Node 10 | } 11 | 12 | /** Create a new instance */ 13 | declare function FIFO(): FIFO 14 | 15 | declare class FIFO { 16 | /** Contains the first node on the list */ 17 | node: null|Node 18 | 19 | /** Number of nodes in the list */ 20 | length: number 21 | 22 | /** Sets the value of a node */ 23 | set(node: null|Node, value: T): null|Node 24 | 25 | /** 26 | * Returns the next node relative to the node you pass. If the node was the 27 | * last node in the list `null` is returned 28 | */ 29 | next(node?: null|Node): null|Node 30 | 31 | /** 32 | * Returns the previous node relative to the node you pass. If the node was 33 | * the first node in the list null is returned 34 | */ 35 | prev(node?: null|Node): null|Node 36 | 37 | /** Returns the value of the node */ 38 | get(node?: null|Node): null|T 39 | 40 | /** Removes the node and returns the value */ 41 | remove(node?: null|Node): null|T 42 | 43 | /** Inserts a value at the beginning of the list */ 44 | unshift(value: T): Node 45 | 46 | /** 47 | * Push a new value to the end of the list. Returns a node that contains this 48 | * value. The value can be accessed by accessing `node.value` 49 | */ 50 | push(value: T): Node 51 | 52 | /** Moves a node to the end of the list */ 53 | bump(node: Node): boolean 54 | 55 | /** Readds a node. Should only be done with a node that has been removed */ 56 | add(node: Node): Node 57 | 58 | /** Peek at the first value */ 59 | first(): null|T 60 | 61 | /** Peek at the last value */ 62 | last(): null|T 63 | 64 | /** Removes the first node and returns the value */ 65 | shift(): null|Node 66 | 67 | /** Removes the last node and returns the value */ 68 | pop(): null|Node 69 | 70 | /** Returns whether the list is empty */ 71 | isEmpty(): boolean 72 | 73 | /** Alias for `clear` */ 74 | removeAll: () => void 75 | 76 | /** Clears the list */ 77 | clear(): void 78 | 79 | /** 80 | * Iterate over all values in the list. Calls the function with `value, node` 81 | */ 82 | forEach(fn: (value: T, node: Node) => void): void 83 | 84 | /** Converts the list to an array and returns it */ 85 | toArray(): T[] 86 | } 87 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function Node (list, val) { 2 | this.prev = this.next = this 3 | this.value = val 4 | this.list = list 5 | } 6 | 7 | Node.prototype.link = function (next) { 8 | this.next = next 9 | next.prev = this 10 | return next 11 | } 12 | 13 | function FIFO () { 14 | if (!(this instanceof FIFO)) return new FIFO() 15 | this.node = null 16 | this.length = 0 17 | } 18 | 19 | FIFO.prototype.set = function (node, value) { 20 | if (!node || node.list !== this) return null 21 | node.value = value 22 | return node 23 | } 24 | 25 | FIFO.prototype.next = function (node) { 26 | if (!node) return this.node 27 | return node.next === this.node ? null : node.next 28 | } 29 | 30 | FIFO.prototype.prev = function (node) { 31 | if (!node) return this.node 32 | return node === this.node ? null : node.prev 33 | } 34 | 35 | FIFO.prototype.get = function (node) { 36 | if (!node || node.list !== this) return null 37 | return node.value 38 | } 39 | 40 | FIFO.prototype.remove = function (node) { 41 | if (!node || node.list !== this) return null 42 | this.length-- 43 | node.list = null 44 | node.prev.link(node.next) 45 | if (node === this.node) this.node = node.next === node ? null : node.next 46 | return node.link(node).value 47 | } 48 | 49 | FIFO.prototype.unshift = function (value) { 50 | return this.node = this.push(value) 51 | } 52 | 53 | FIFO.prototype.push = function (value) { 54 | return this.add(new Node(this, value)) 55 | } 56 | 57 | FIFO.prototype.bump = function (node) { 58 | if (node.list !== this) return false 59 | this.remove(node) 60 | this.add(node) 61 | return true 62 | } 63 | 64 | FIFO.prototype.add = function (node) { 65 | this.length++ 66 | if (!node.list) node.list = this 67 | if (!this.node) return this.node = node 68 | this.node.prev.link(node) 69 | node.link(this.node) 70 | return node 71 | } 72 | 73 | FIFO.prototype.first = function () { 74 | return this.node && this.node.value 75 | } 76 | 77 | FIFO.prototype.last = function () { 78 | return this.node && this.node.prev.value 79 | } 80 | 81 | FIFO.prototype.shift = function () { 82 | return this.node && this.remove(this.node) 83 | } 84 | 85 | FIFO.prototype.pop = function () { 86 | return this.node && this.remove(this.node.prev) 87 | } 88 | 89 | FIFO.prototype.isEmpty = function () { 90 | return this.length === 0 || this.node === null 91 | } 92 | 93 | FIFO.prototype.removeAll = 94 | FIFO.prototype.clear = function () { 95 | if (this.length !== 0 && this.node !== null) { 96 | this.length = 0 97 | this.node = null 98 | } 99 | } 100 | 101 | FIFO.prototype.forEach = function (fn) { 102 | for (var node = this.node; node; node = this.next(node)) { 103 | fn(node.value, node) 104 | } 105 | } 106 | 107 | FIFO.prototype.toArray = function () { 108 | var list = [] 109 | for (var node = this.node; node; node = this.next(node)) { 110 | list.push(node.value) 111 | } 112 | return list 113 | } 114 | 115 | module.exports = FIFO 116 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fifo", 3 | "version": "2.4.1", 4 | "description": "FIFO queue implemented using a double linked-list", 5 | "main": "index.js", 6 | "dependencies": {}, 7 | "devDependencies": { 8 | "tape": "^4.2.2" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/mafintosh/fifo.git" 13 | }, 14 | "author": "Mathias Buus (@mafintosh)", 15 | "contributors": [ 16 | "Gabriel Castilho " 17 | ], 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/mafintosh/fifo/issues" 21 | }, 22 | "homepage": "https://github.com/mafintosh/fifo" 23 | } 24 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var FIFO = require('./') 3 | 4 | test('basic ops', function (t) { 5 | var fifo = FIFO() 6 | t.equal(fifo.isEmpty(), true) 7 | 8 | fifo.push('foo') 9 | t.equal(fifo.length, 1) 10 | t.equal(fifo.first(), 'foo') 11 | t.equal(fifo.last(), 'foo') 12 | 13 | fifo.push('bar') 14 | t.equal(fifo.length, 2) 15 | t.equal(fifo.first(), 'foo') 16 | t.equal(fifo.last(), 'bar') 17 | 18 | var foo = fifo.shift() 19 | t.equal(fifo.length, 1) 20 | t.equal(foo, 'foo') 21 | t.equal(fifo.first(), 'bar') 22 | 23 | var bar = fifo.pop() 24 | t.equal(fifo.length, 0) 25 | t.equal(bar, 'bar') 26 | t.equal(fifo.pop(), null) 27 | t.equal(fifo.shift(), null) 28 | t.equal(fifo.first(), null) 29 | t.equal(fifo.last(), null) 30 | 31 | fifo.push('foo') 32 | fifo.push('bar') 33 | fifo.push('foo1') 34 | t.equal(fifo.isEmpty(), false) 35 | fifo.removeAll() 36 | t.equal(fifo.length, 0) 37 | t.equal(fifo.first(), null) 38 | t.equal(fifo.last(), null) 39 | 40 | t.end() 41 | }) 42 | 43 | test('toArray', function (t) { 44 | var fifo = FIFO() 45 | 46 | fifo.push('foo') 47 | fifo.push('bar') 48 | fifo.push('baz') 49 | 50 | var list = fifo.toArray() 51 | 52 | t.equal(list.length, 3) 53 | t.equal(list[0], 'foo') 54 | t.equal(list[1], 'bar') 55 | t.equal(list[2], 'baz') 56 | t.end() 57 | }) 58 | 59 | test('iteration', function (t) { 60 | var fifo = FIFO() 61 | 62 | fifo.push('foo') 63 | fifo.push('bar') 64 | fifo.push('baz') 65 | 66 | var expected = fifo.toArray() 67 | 68 | for (var node = fifo.node; node; node = fifo.next(node)) { 69 | t.same(node.value, expected.shift()) 70 | } 71 | 72 | t.end() 73 | }) 74 | 75 | test('bump', function (t) { 76 | var fifo = FIFO() 77 | 78 | var node = fifo.push('foo') 79 | fifo.push('bar') 80 | fifo.push('baz') 81 | fifo.bump(node) 82 | 83 | t.same(fifo.length, 3) 84 | t.same(fifo.toArray(), ['bar', 'baz', 'foo']) 85 | t.end() 86 | }) 87 | 88 | test('bump twice', function (t) { 89 | var fifo = FIFO() 90 | 91 | var node = fifo.push('bar') 92 | fifo.push('baz') 93 | fifo.bump(node) 94 | fifo.bump(node) 95 | 96 | t.same(fifo.length, 2) 97 | t.same(fifo.toArray(), ['baz', 'bar']) 98 | t.end() 99 | }) 100 | --------------------------------------------------------------------------------