dynamic-array
View on GitHub
View on npmjs.org
View on Tonic
Dynamic array extension for Node.js offering better performance for use as a stack or queue
Purpose: This extension wraps a C++ std::deque giving Node.js developers asymptotically optimal queues and stacks.
Requirements: Node.js, node-gyp, gcc and g++
Build instructions: cd to download directory, node-gyp configure, node-gyp build
NPM: Can be installed from NPM via: npm install dynamic-array
Usage:
var arr = require('dynamic-array').DynamicArray();
// use as a queue
console.log("Use as a queue");
console.log("Should list 0 - 9");
for (var i = 0; i < 10; i++)
arr.enqueue(i);
while(arr.size() > 0)
console.log(arr.dequeue());
// use as a stack
console.log("Use as a stack");
console.log("Should list 9 - 0");
for (var i = 0; i < 10; i++)
arr.push(i);
while(arr.size() > 0)
console.log(arr.pop());
// iteration
console.log("Iteration");
console.log("Should list 0 - 9");
for (var i = 0; i < 10; i++)
arr.push_back(i);
arr.each(function(item) {
console.log(item);
});
arr.clear();
// sorting
console.log("Sorting");
console.log("Should list 0 - 9");
for (var i = 0; i < 10; i++)
arr.push_back(i);
arr.each(function(item) {
console.log(item);
});
console.log("Should now be reversed")
arr.sort(function(a, b) {
return (a > b);
});
arr.each(function(item) {
console.log(item);
});
arr.clear();
// assignment
console.log("Assignment");
console.log("Should list 0 - 8, 8");
for(var i = 0; i < 10; i++)
arr.push_back(i);
arr.assign(9, 8);
arr.each(function(item) {
console.log(item);
});
// retrieval
console.log("Retrieval");
console.log("Should list 7, 8");
console.log(arr.at(7));
console.log(arr.at(8));
arr.clear();
// bounds checking
console.log("Should throw exceptions for being out of bounds");
console.log("This bounds checking is to prevent memory leaks and other programming errors");
try {
arr.at(1);
} catch (e) {
console.log(e);
}
try {
arr.assign(1, 0);
} catch (e) {
console.log(e);
}