Skip to content

Commit

Permalink
Merge pull request #3 from shadowcodex/dev-01
Browse files Browse the repository at this point in the history
Dev 01
  • Loading branch information
shadowcodex authored Feb 10, 2019
2 parents 75a072e + 7013ef8 commit e5a3eff
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 66 deletions.
188 changes: 175 additions & 13 deletions benchmark/stacks.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,196 @@
let { stacks } = require('./../index');
let ptime = require('quick-ptime');
const Table = require('cli-table');


// setup stacks
let stack = new stacks.Stack();
let arrayStack = new stacks.ArrayStack();
let linkedStack = new stacks.LinkedStack();
let uArrayStack = new stacks.UArrayStack();
let bArrayStack = new stacks.BArrayStack(1100001);

/**
* PUSH STUFF
*/

// push 100,000 items onto the stack
ptime.setTime('stack100000');
for(let i = 0; i < 100000; i++) {
stack.push('value');
linkedStack.push('value');
}
let stackTime = ptime.elapsedTime('stack100000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1,000,000 items onto the stack
ptime.setTime('stack1000000');
for(let i = 0; i < 1000000; i++) {
linkedStack.push('value');
}
let mStackTime = ptime.elapsedTime('stack1000000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1 item onto the stack
ptime.setTime('stack1');
for(let i = 0; i < 1; i++) {
linkedStack.push('value');
}
console.log("Push 100,000 Stack timing:", ptime.elapsedTime('stack100000').formatted);
let sStackTime = ptime.elapsedTime('stack1');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);




ptime.setTime('arraystack100000');

// push 100,000 items onto the stack
ptime.setTime('ustack100000');
for(let i = 0; i < 100000; i++) {
arrayStack.push('value');
uArrayStack.push('value');
}
let ustackTime = ptime.elapsedTime('ustack100000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1,000,000 items onto the stack
ptime.setTime('ustack1000000');
for(let i = 0; i < 1000000; i++) {
uArrayStack.push('value');
}
console.log("Push 100,000 Array Stack timing:", ptime.elapsedTime('arraystack100000').formatted);
let umStackTime = ptime.elapsedTime('ustack1000000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1 item onto the stack
ptime.setTime('ustack1');
for(let i = 0; i < 1; i++) {
uArrayStack.push('value');
}
let usStackTime = ptime.elapsedTime('ustack1');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);



// pop 100,000 items off the stack

// push 100,000 items onto the stack
ptime.setTime('bstack100000');
for(let i = 0; i < 100000; i++) {
bArrayStack.push('value');
}
let bstackTime = ptime.elapsedTime('bstack100000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1,000,000 items onto the stack
ptime.setTime('bstack1000000');
for(let i = 0; i < 1000000; i++) {
bArrayStack.push('value');
}
let bmStackTime = ptime.elapsedTime('bstack1000000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1 item onto the stack
ptime.setTime('bstack1');
for(let i = 0; i < 1; i++) {
bArrayStack.push('value');
}
let bsStackTime = ptime.elapsedTime('bstack1');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);




/**
* POP STUFF
*/

// push 100,000 items onto the stack
ptime.setTime('pstack100000');
for(let i = 0; i < 100000; i++) {
stack.pop()
linkedStack.pop('value');
}
console.log("Pop 100,000 Stack timing:", ptime.elapsedTime('pstack100000').formatted);
let pstackTime = ptime.elapsedTime('pstack100000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

ptime.setTime('parraystack100000');
// push 1,000,000 items onto the stack
ptime.setTime('pstack1000000');
for(let i = 0; i < 1000000; i++) {
linkedStack.pop('value');
}
let pmStackTime = ptime.elapsedTime('pstack1000000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1 item onto the stack
ptime.setTime('pstack1');
for(let i = 0; i < 1; i++) {
linkedStack.pop('value');
}
let psStackTime = ptime.elapsedTime('pstack1');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);



// push 100,000 items onto the stack
ptime.setTime('upstack100000');
for(let i = 0; i < 100000; i++) {
stack.pop()
uArrayStack.pop('value');
}
console.log("Pop 100,000 Array Stack timing:", ptime.elapsedTime('parraystack100000').formatted);
let upstackTime = ptime.elapsedTime('upstack100000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1,000,000 items onto the stack
ptime.setTime('upstack1000000');
for(let i = 0; i < 1000000; i++) {
uArrayStack.pop('value');
}
let upmStackTime = ptime.elapsedTime('upstack1000000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1 item onto the stack
ptime.setTime('upstack1');
for(let i = 0; i < 1; i++) {
uArrayStack.pop('value');
}
let upsStackTime = ptime.elapsedTime('upstack1');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);





// push 100,000 items onto the stack
ptime.setTime('bpstack100000');
for(let i = 0; i < 100000; i++) {
bArrayStack.pop('value');
}
let bpstackTime = ptime.elapsedTime('bpstack100000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1,000,000 items onto the stack
ptime.setTime('bpstack1000000');
for(let i = 0; i < 1000000; i++) {
bArrayStack.pop('value');
}
let bpmStackTime = ptime.elapsedTime('bpstack1000000');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);

// push 1 item onto the stack
ptime.setTime('bpstack1');
for(let i = 0; i < 1; i++) {
bArrayStack.pop('value');
}
let bpsStackTime = ptime.elapsedTime('bpstack1');
// console.log("Push 100,000 Linked Stack timing:", ptime.elapsedTime('stack100000').formatted);


/// Output results

const table = new Table({
head: ['Stack Operation', '100,000 items', '1,000,000 items', '1 item']
});


table.push(
['Linked List Stack Push', stackTime.formatted, mStackTime.formatted, sStackTime.formatted],
['Unbounded Array Stack Push', ustackTime.formatted, umStackTime.formatted, usStackTime.formatted],
['Bounded Array Stack Push', bstackTime.formatted, bmStackTime.formatted, bsStackTime.formatted],
['Linked List Stack Pop', pstackTime.formatted, pmStackTime.formatted, psStackTime.formatted],
['Unbounded Array Stack Pop', upstackTime.formatted, upmStackTime.formatted, upsStackTime.formatted],
['Bounded Array Stack Pop', bpstackTime.formatted, bpmStackTime.formatted, bpsStackTime.formatted],
);

console.log(table.toString());
83 changes: 67 additions & 16 deletions lib/stacks.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
class Stack {
class LinkedStack {
constructor () {
this.top = {};
this.topNode = null;
this.length = 0;
}

push(data) {
let node = {
value: data,
prev: this.top
prev: this.topNode
};
this.top = node;
this.topNode = node;
this.length++;
return this.length;
}

pop() {
if(this.length === 0) {
if(this.isEmpty()) {
return undefined;
}

let value = this.top.value;
this.top = this.top.prev;
this.length--;
return value;
this.topNode = this.topNode.prev;
this.length--;
}

peek() {
return this.top.value;
top() {
return this.topNode.value;
}

height() {
Expand All @@ -38,7 +36,7 @@ class Stack {
}
}

class ArrayStack {
class UArrayStack {
constructor() {
this.stack = [];
}
Expand All @@ -48,10 +46,10 @@ class ArrayStack {
}

pop() {
return this.stack.pop();
this.stack.pop();
}

peek() {
top() {
return this.stack[this.stack.length - 1];
}

Expand All @@ -64,8 +62,61 @@ class ArrayStack {
}
}

class BArrayStack {
constructor(size) {
const DEFAULT_SIZE = 100;
let use = !!size ? size : DEFAULT_SIZE;
this.stack = [];
for(let i = 0; i < use; i++) {
this.stack[i] = null;
}
this.topIndex = -1;
}

height() {
return this.topIndex + 1;
}

isEmpty() {
return this.topIndex === -1 ? true : false;
}

isFull() {
return this.topIndex === this.stack.length - 1 ? true : false;
}

push(item) {
if(!this.isFull()) {
this.topIndex++;
this.stack[this.topIndex] = item;
return this.height();
} else {
throw new Error('Stack too deep');
}
}

pop() {
if(!this.isEmpty()) {
this.stack[this.topIndex] = null;
this.topIndex--;
} else {
throw new Error('Stack already empty');
}
}

top() {
if(!this.isEmpty()) {
return this.stack[this.topIndex];
} else {
return undefined;
}
}

}


module.exports = {
Stack,
ArrayStack
LinkedStack,
UArrayStack,
BArrayStack
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "data-theory",
"version": "0.0.1",
"version": "1.0.0",
"description": "A collection of data theory and structures in JavaScript.",
"main": "index.js",
"repository": "https://github.com/shadowcodex/data-theory.git",
Expand All @@ -17,6 +17,7 @@
"mocha": "^5.2.0"
},
"dependencies": {
"cli-table": "^0.3.1",
"quick-ptime": "^1.0.0"
}
}
5 changes: 3 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ As theories and structures are built, I'll list them under this heading.

> You won't see a todo list or a target list this time around. I'll take suggestions as issues on the repository, but I'm going to focus on building commong structures and items one at a time. One thing I have learned as a developer is never get ahead of yourself, you can get paralyzed with planning and expectations.
- `Stack`: based on a linked list (slow)
- `ArrayStack`: based on JS array (fast)
- `LinkedStack`: based on a linked list (slow)
- `UArrayStack`: based on JS unbounded array (fast)
- `BArrayStack`: based on JS bounded array (fastest)
- `Queue`: based on a linked list (fast)
- `ArrayQueue`: based on JS array (slow)

Expand Down
Loading

0 comments on commit e5a3eff

Please sign in to comment.