@pxtrick/queues
v0.0.7
Published
A Queue factory for creating various Queue data structures.
Downloads
2
Maintainers
Readme
Queues
A JavaScript module providing the implementations of various Queue data structures.
Simple Queue
A simple (first-in, first-out) Queue. Items are removed in the order in which they were added.
Methods
push(item)
- Adds a new item to the Queue.pop()
- Removes and returns the next item from the Queue.peek()
- Returns, but does not remove, the next item in the Queue.size()
- Returns the number of items in the Queue.empty()
- Empties the Queue of all items.
Sample Usage
// Load the Queue factory.
var Queues = require('@pxtrick/Queues');
// Create a new (FIFO) Queue object.
var queue = Queues.newSimpleQueue();
queue.push( {id:'firstItem'} );
queue.push( {id:'secondItem'} );
queue.push( {id:'thirdItem'} );
queue.size(); // 3
queue.pop(); // 'firstItem'
queue.pop(); // 'secondItem'
queue.pop(); // 'thirdItem'
Stack
A simple (last-in, first-out) Stack. Items are removed in the reverse order in which they were added.
Methods
push(item)
- Adds a new item to the Queue.pop()
- Removes and returns the next item from the Queue.peek()
- Returns, but does not remove, the next item in the Queue.size()
- Returns the number of items in the Queue.empty()
- Empties the Queue of all items.
Sample Usage
// Load the Queue factory.
var Queues = require('@pxtrick/Queues');
// Create a new Stack object.
var stack = Queues.newStack();
stack.push( {id:'firstItem'} );
stack.push( {id:'secondItem'} );
stack.push( {id:'thirdItem'} );
stack.pop(); // 'thirdItem'
stack.pop(); // 'secondItem'
stack.pop(); // 'firstItem'
Priority Queue
A Queue in which items are stored sorted by priority, and which are removed in the order of highest priority.
NOTE: Larger numerical values will have a higher priority than smaller values. It is possible to create a custom comparator function (specific to sorting your own data) and pass it to the factory function: Queues.newPriorityQueue(customComparator)
.
Methods
push(item)
- Adds a new item to the Queue.pop()
- Removes and returns the next highest priority item from the Queue.peek()
- Returns, but does not remove, the next highest priority item in the Queue.size()
- Returns the number of items in the Queue.empty()
- Empties the Queue of all items.
Sample Usage
// Load the Queue factory.
var Queues = require('@pxtrick/Queues');
// Create a new Priority Queue object.
var priorityQueue = Queues.newPriorityQueue();
priorityQueue.push( {name:'Priority 2', priority:2} );
priorityQueue.push( {name:'Priority 4', priority:4} );
priorityQueue.push( {name:'Priority 1', priority:1} );
priorityQueue.push( {name:'Priority 3', priority:3} );
priorityQueue.pop(); // 'Priority 4'
priorityQueue.pop(); // 'Priority 3'
priorityQueue.pop(); // 'Priority 2'
priorityQueue.pop(); // 'Priority 1'
Circular Queue
A Queue in which items can be iterated in a circular fashion. i.e when the "end" of the queue is encountered, the next item will be the one positioned at the "start" of the queue.
Methods
push(item)
- Adds a new item to the end of the Queue, regardless of the iterator position.next()
- Returns, but does not remove, the next item in the Queue. This also advances the iterator position. (Useful for read-only iterating over a fixed set of items.)pop()
- Removes and returns the next item in the Queue.peek()
- Returns, but does not remove, the next item in the Queue. This does not advance the iterator positionsetIndex(value)
- Moves the iterator to the given index value. Values outside of the Queue bounds are ignored.reset()
- Moves the iterator to the first index in the Queue.size()
- Returns the number of items in the Queue.empty()
- Empties the Queue of all items.
Sample Usage
// Load the Queue factory.
var Queues = require('@pxtrick/Queues');
// Create a new Circular Queue object.
var circularQueue = Queues.newCircularQueue();
circularQueue.push( {id:'firstItem'} );
circularQueue.push( {id:'secondItem'} );
circularQueue.push( {id:'thirdItem'} );
circularQueue.next(); // 'firstItem'
circularQueue.next(); // 'secondItem'
circularQueue.next(); // 'thirdItem'
circularQueue.next(); // 'firstItem'
circularQueue.next(); // 'secondItem'