Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
SWoskowiak committed Apr 16, 2016
0 parents commit 93e4298
Show file tree
Hide file tree
Showing 6 changed files with 173 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
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
6 changes: 6 additions & 0 deletions .travis.yml
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"
56 changes: 56 additions & 0 deletions README.md
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

```
49 changes: 49 additions & 0 deletions index.js
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;
18 changes: 18 additions & 0 deletions package.json
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"
}
}
37 changes: 37 additions & 0 deletions test/test.js
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');
});
});

});

0 comments on commit 93e4298

Please sign in to comment.