-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 93e4298
Showing
6 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Ignore all files starting with . | ||
.* | ||
# Don't commit node modules | ||
node_modules | ||
# Un-ignore .gitignore and custom config files for services | ||
!/.gitignore | ||
!/.travis.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "5" | ||
script: "npm run-script test-coverage" | ||
# Send coverage data to Coveralls | ||
after_script: "istanbul cover ./node_modules/mocha/bin/_mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# Queue | ||
|
||
Queue data structure implementation in javascript. | ||
|
||
While you can mimic queue(FIFO) behavior via arrays like so: | ||
|
||
```Javascript | ||
|
||
const arr = []; | ||
// "queue" some data up | ||
arr.push(1); | ||
arr.push(2); | ||
arr.push(3); | ||
|
||
console.log(arr.shift()) // 1 | ||
|
||
``` | ||
|
||
This operation causes the array indices to be recalculated resulting in O(n) | ||
time for each dequeue made (push is 0(1) time operation). | ||
|
||
## Alternative | ||
|
||
To avoid O(n) dequeue times we can instead use a hash table and keep track of | ||
of head and tail indexes of the table. This allows enqueue and dequeue to | ||
operate in O(1) time! | ||
|
||
Check out index.js to see a very simple implementation of this behavior. | ||
|
||
|
||
## Usage | ||
|
||
```Javascript | ||
|
||
const q = new Queue(); | ||
|
||
// NOTE: enqueue returns the queue object so we can chain enqueue methods | ||
q.enqueue('some').enqueue('test').enqueue('data'); | ||
|
||
console.log(q.dequeue()) // 'some' | ||
console.log(q.dequeue()) // 'test' | ||
console.log(q.dequeue()) // 'data' | ||
console.log(q.dequeue()) // false | ||
|
||
console.log(q.size) // 0 | ||
|
||
q.enqueue('something'); | ||
|
||
// peek lets us check whats next without dequeuing it | ||
console.log(q.peek()) // 'something' | ||
|
||
q.enqueue('some').enqueue('more').enqueue.('data'); | ||
|
||
console.log(q.size) // 4 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict'; | ||
|
||
// Simple queue data struture | ||
// Javascript arrays can mimic the exact behavior via push and shift but come | ||
// with performance overhead of resizing array after each shift operation. This Queue | ||
// therefore will operate in O(1) time instead of O(N) from resizing. | ||
class Queue { | ||
|
||
constructor() { | ||
this.newIndex = 0; | ||
this.oldIndex = 0; | ||
this.storage = {}; | ||
} | ||
|
||
// Queue up some data | ||
enqueue(data) { | ||
this.storage[this.newIndex] = data; | ||
this.newIndex++; | ||
|
||
return this; | ||
} | ||
|
||
// Dequeue some data | ||
dequeue() { | ||
let temp; | ||
|
||
if (this.oldIndex !== this.newIndex) { | ||
temp = this.storage[this.oldIndex]; | ||
delete this.storage[this.oldIndex]; | ||
this.oldIndex++; | ||
|
||
return temp; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
// Whats the next value to | ||
peek() { | ||
return this.storage[this.oldIndex]; | ||
} | ||
|
||
// Get the size of the array | ||
get size() { | ||
return this.newIndex - this.oldIndex; | ||
} | ||
} | ||
|
||
module.exports = Queue; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "queue", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test/*", | ||
"test-coverage": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha -- -R spec ./test/*" | ||
}, | ||
"author": "Stefan Woskowiak", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"coveralls": "^2.11.9", | ||
"istanbul": "^0.4.3", | ||
"mocha": "^2.4.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
const expect = require('chai').expect, | ||
Queue = require('../index'); | ||
|
||
describe('Queue', function () { | ||
let queue; | ||
|
||
describe('Methods', () => { | ||
|
||
beforeEach(() => { | ||
queue = new Queue(); | ||
}); | ||
|
||
it ('Queues data', () => { | ||
expect(queue.enqueue('test')).to.have.property('size').eql(1); | ||
expect(queue.enqueue('more')).to.have.property('size').eql(2); | ||
}); | ||
|
||
it ('Dequeues data and returns false when empty', () => { | ||
queue.enqueue('some').enqueue('test').enqueue('data'); | ||
|
||
expect(queue.dequeue()).to.equal('some'); | ||
expect(queue.dequeue()).to.equal('test'); | ||
expect(queue.dequeue()).to.equal('data'); | ||
expect(queue.dequeue()).to.be.false; | ||
expect(queue).to.have.property('size').eql(0); | ||
}); | ||
|
||
it ('Peeks into queue', () => { | ||
queue.enqueue('some').enqueue('test').enqueue('data'); | ||
|
||
expect(queue.peek()).to.equal('some'); | ||
}); | ||
}); | ||
|
||
}); |