Comparing version
65
index.js
@@ -1,2 +0,2 @@ | ||
var Node = function(list, val) { | ||
function Node (list, val) { | ||
this.prev = this.next = this | ||
@@ -7,3 +7,3 @@ this.value = val | ||
Node.prototype.link = function(next) { | ||
Node.prototype.link = function (next) { | ||
this.next = next | ||
@@ -14,3 +14,3 @@ next.prev = this | ||
var FIFO = function() { | ||
function FIFO () { | ||
if (!(this instanceof FIFO)) return new FIFO() | ||
@@ -21,3 +21,3 @@ this.node = null | ||
FIFO.prototype.set = function(node, value) { | ||
FIFO.prototype.set = function (node, value) { | ||
if (!node || node.list !== this) return null | ||
@@ -28,3 +28,13 @@ node.value = value | ||
FIFO.prototype.get = function(node) { | ||
FIFO.prototype.next = function (node) { | ||
if (!node) return this.node | ||
return node.next === this.node ? null : node.next | ||
} | ||
FIFO.prototype.prev = function (node) { | ||
if (!node) return this.node | ||
return node === this.node ? null : node.prev | ||
} | ||
FIFO.prototype.get = function (node) { | ||
if (!node || node.list !== this) return null | ||
@@ -34,3 +44,3 @@ return node.value | ||
FIFO.prototype.remove = function(node) { | ||
FIFO.prototype.remove = function (node) { | ||
if (!node || node.list !== this) return null | ||
@@ -44,8 +54,16 @@ this.length-- | ||
FIFO.prototype.unshift = function(value) { | ||
FIFO.prototype.unshift = function (value) { | ||
return this.node = this.push(value) | ||
} | ||
FIFO.prototype.push = function(value) { | ||
var node = new Node(this, value) | ||
FIFO.prototype.push = function (value) { | ||
return this.add(new Node(this, value)) | ||
} | ||
FIFO.prototype.bump = function (node) { | ||
this.remove(node) | ||
this.add(node) | ||
} | ||
FIFO.prototype.add = function (node) { | ||
this.length++ | ||
@@ -58,23 +76,24 @@ if (!this.node) return this.node = node | ||
FIFO.prototype.first = function() { | ||
FIFO.prototype.first = function () { | ||
return this.node && this.node.value | ||
} | ||
FIFO.prototype.last = function() { | ||
FIFO.prototype.last = function () { | ||
return this.node && this.node.prev.value | ||
} | ||
FIFO.prototype.shift = function() { | ||
FIFO.prototype.shift = function () { | ||
return this.node && this.remove(this.node) | ||
} | ||
FIFO.prototype.pop = function() { | ||
FIFO.prototype.pop = function () { | ||
return this.node && this.remove(this.node.prev) | ||
} | ||
FIFO.prototype.isEmpty = function() { | ||
FIFO.prototype.isEmpty = function () { | ||
return this.length === 0 || this.node === null | ||
} | ||
FIFO.prototype.removeAll = function() { | ||
FIFO.prototype.removeAll = | ||
FIFO.prototype.clear = function () { | ||
if (this.length !== 0 && this.node !== null) { | ||
@@ -86,10 +105,12 @@ this.length = 0 | ||
FIFO.prototype.toArray = function() { | ||
FIFO.prototype.forEach = function (fn) { | ||
for (var node = this.node; node; node = this.next(node)) { | ||
fn(node.value, node) | ||
} | ||
} | ||
FIFO.prototype.toArray = function () { | ||
var list = [] | ||
var node = this.node | ||
var first = node | ||
while (node) { | ||
for (var node = this.node; node; node = this.next(node)) { | ||
list.push(node.value) | ||
node = node.next | ||
if (node === first) return list | ||
} | ||
@@ -99,2 +120,2 @@ return list | ||
module.exports = FIFO | ||
module.exports = FIFO |
{ | ||
"name": "fifo", | ||
"version": "2.1.0", | ||
"repository": "git://github.com/mafintosh/fifo", | ||
"version": "2.2.0", | ||
"description": "FIFO queue implemented using a double linked-list", | ||
"main": "index.js", | ||
"dependencies": {}, | ||
"devDependencies": { | ||
"tape": "^4.2.2" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/mafintosh/fifo.git" | ||
}, | ||
"author": "Mathias Buus (@mafintosh)", | ||
"license": "MIT", | ||
"scripts": { | ||
"test": "tape test.js" | ||
"bugs": { | ||
"url": "https://github.com/mafintosh/fifo/issues" | ||
}, | ||
"description": "FIFO queue", | ||
"keywords": [ | ||
"queue", | ||
"linked", | ||
"list", | ||
"fifo", | ||
"data", | ||
"structure" | ||
], | ||
"author": "Mathias Buus Madsen <mathiasbuus@gmail.com>", | ||
"devDependencies": { | ||
"tape": "^3.0.3" | ||
} | ||
"homepage": "https://github.com/mafintosh/fifo" | ||
} |
112
README.md
@@ -1,4 +0,4 @@ | ||
# FIFO | ||
# fifo | ||
Javascript FIFO queue implemented using a double linked-list | ||
FIFO queue implemented using a double linked-list | ||
@@ -11,33 +11,107 @@ ``` | ||
# Usage is simple | ||
## Usage | ||
``` js | ||
var fifo = require('fifo')(); | ||
var fifo = require('fifo')() | ||
fifo.push('hello'); | ||
fifo.push('world'); | ||
fifo.push('hello') | ||
fifo.push('world') | ||
console.log(fifo.first()); // prints hello | ||
console.log(fifo.last()); // prints world | ||
console.log(fifo.first()) // prints hello | ||
console.log(fifo.last()) // prints world | ||
console.log(fifo.shift()); // prints hello | ||
console.log(fifo.shift()); // prints world | ||
console.log(fifo.shift()) // prints hello | ||
console.log(fifo.shift()) // prints world | ||
var node = fifo.push('meh'); | ||
var node = fifo.push('meh') | ||
fifo.remove(node); // remove 'meh' from the stack | ||
fifo.unshift('hello'); // insert at the beginning | ||
fifo.remove(node) // remove 'meh' from the stack | ||
fifo.unshift('hello') // insert at the beginning | ||
``` | ||
fifo.removeAll(); // Clear stack | ||
`fifo` uses a linked list behind the scene so all list manipulation methods run in O(1) | ||
if (!fifo.isEmpty()) { // Check if stack is empty | ||
fifo.shift(); | ||
## API | ||
#### `fifo = FIFO()` | ||
Create a new instance | ||
#### `fifo.node` | ||
Contains the first node on the list. | ||
#### `fifo.length` | ||
Number of nodes in the list. | ||
#### `node = fifo.push(value)` | ||
Push a new value to the end of the list. Returns a node that contains this value. | ||
The value can be accessed by accessing `node.value`. | ||
#### `value = fifo.shift()` | ||
Removes the first node and returns the value | ||
#### `value = fifo.pop()` | ||
Removes the last node and returns the value | ||
#### `value = fifo.remove(node)` | ||
Removes the node and returns the value | ||
#### `fifo.add(node)` | ||
Readds a node. Should only be done with a node that has been removed. | ||
#### `value = fifo.first()` | ||
Peek at the first value | ||
#### `value = fifo.last()` | ||
Peek at the last value | ||
#### `node = fifo.unshift(value)` | ||
Inserts a value at the beginning of the list | ||
#### `node = fifo.next(node)` | ||
Returns the next node relative to the node you pass. | ||
If the node was the last node in the list `null` is returned. | ||
#### `node = fifo.prev(node)` | ||
Returns the previous node relative to the node you pass. | ||
If the node was the first node in the list `null` is returned. | ||
#### `fifo.bump(node)` | ||
Moves a node to the end of the list | ||
#### `fifo.clear()` | ||
Clears the list. | ||
#### `fifo.forEach(fn)` | ||
Iterate over all values in the list. Calls the function with `value, node`. | ||
## Iteration | ||
To iterate the list simply use the following for loop | ||
``` js | ||
for (var node = fifo.node; node; node = fifo.next(node)) { | ||
console.log('value is', node.value) | ||
} | ||
``` | ||
`fifo` uses a linked list behind the scene so `push`, `shift`, `unshift`, and `remove` all run in O(1) | ||
Optionally you can call `fifo.forEach(fn)` which does the above internally. | ||
# License | ||
## License | ||
MIT |
40
test.js
var test = require('tape') | ||
var FIFO = require('./') | ||
test('basic ops', function(t){ | ||
var fifo = new FIFO() | ||
t.equal(fifo.isEmpty(), true); | ||
test('basic ops', function (t) { | ||
var fifo = FIFO() | ||
t.equal(fifo.isEmpty(), true) | ||
@@ -43,4 +43,4 @@ fifo.push('foo') | ||
test('toArray', function(t) { | ||
var fifo = new FIFO() | ||
test('toArray', function (t) { | ||
var fifo = FIFO() | ||
@@ -58,2 +58,30 @@ fifo.push('foo') | ||
t.end() | ||
}) | ||
}) | ||
test('iteration', function (t) { | ||
var fifo = FIFO() | ||
fifo.push('foo') | ||
fifo.push('bar') | ||
fifo.push('baz') | ||
var expected = fifo.toArray() | ||
for (var node = fifo.node; node; node = fifo.next(node)) { | ||
t.same(node.value, expected.shift()) | ||
} | ||
t.end() | ||
}) | ||
test('bump', function (t) { | ||
var fifo = FIFO() | ||
var node = fifo.push('foo') | ||
fifo.push('bar') | ||
fifo.push('baz') | ||
fifo.bump(node) | ||
t.same(fifo.toArray(), ['bar', 'baz', 'foo']) | ||
t.end() | ||
}) |
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
7843
82.18%7
16.67%158
31.67%0
-100%0
-100%117
172.09%0
-100%