Browse files

change front to be a value, change all references to length to be size

  • Loading branch information...
1 parent ca33a98 commit fc5184812ab264c8c548ee75f568c339afc1b0aa @Benvie committed Nov 27, 2012
Showing with 27 additions and 31 deletions.
  1. +1 −1 README.md
  2. +20 −24 listish.js
  3. +5 −5 listish.min.js
  4. +1 −1 package.json
View
2 README.md
@@ -76,7 +76,7 @@ FIFO (First in, first out) data structure.
enqueue
|-----length----|
-* __front()__ Front value of the list which will be removed and returned by dequeue
+* __front__ Front value of the list which will be removed and returned by dequeue
* __enqueue(value)__ Add an item to the end of the queue
* __dequeue()__ Remove an item from the front of the queue and return it
View
44 listish.js
@@ -734,7 +734,7 @@ var listish = (function(global, exports, undefined){
* ^--|____|____|____|
* ^-- \___\
* enqueue
- * |-----length----|
+ * |-----size-----|
*/
// #############
@@ -758,11 +758,12 @@ var listish = (function(global, exports, undefined){
function Queue(iterable){
- this.index = this.length = 0;
+ this.index = this.size = 0;
+ this.front = undefined;
if (iterable != null) {
if (iterable instanceof Queue) {
this.items = iterable.items.slice(iterable.front);
- this.length = this.items.length;
+ this.size = this.items.size;
} else {
this.items = [];
iterate(iterable, this.enqueue, this);
@@ -776,21 +777,25 @@ var listish = (function(global, exports, undefined){
/**
* Add an item to the end of the queue.
* @param {Any} item new end of the queue
- * @return {Number} the queue's new length
+ * @return {Number} the queue's new size
*/
function enqueue(item){
this.items.push(item);
- return ++this.length;
+ if (!this.size) {
+ this.front = item;
+ }
+ return ++this.size;
},
/**
* Remove an item from the front of the queue and return it.
* @return {Any} Old queue front
*/
function dequeue(){
- if (this.length) {
+ if (this.size) {
var item = this.items[this.index];
this.items[this.index++] = null;
- this.length--;
+ this.front = this.items[this.index];
+ this.size--;
if (this.index === 500) {
this.items = this.items.slice(this.index);
this.index = 0;
@@ -802,19 +807,12 @@ var listish = (function(global, exports, undefined){
* Reset the queue
*/
function empty(){
- this.length = 0;
+ this.size = 0;
this.index = 0;
this.items = [];
return this;
},
/**
- * Peak at the front item without removing it.
- * @return {Any}
- */
- function front(){
- return this.items[this.index];
- },
- /**
* Peak at an item.
* @param {Number} depth Distance from the front of the queue
* @return {Any}
@@ -843,7 +841,7 @@ var listish = (function(global, exports, undefined){
* \__\ /__/
* --> top --^ _
* |___| |
- * |___| length
+ * |___| size
* |___| _|
*/
@@ -854,7 +852,7 @@ var listish = (function(global, exports, undefined){
exporter(function(){
function StackIterator(stack){
this.stack = stack;
- this.index = stack.length;
+ this.index = stack.size;
}
inherit(StackIterator, Iterator, [
@@ -873,16 +871,14 @@ var listish = (function(global, exports, undefined){
}
}
-
defineMethods(Stack.prototype, [
/**
* Add an item to the top of the stack
* @param {Any} item new stack top
- * @return {Number} the stack's new length
+ * @return {Number} the stack's new size
*/
function push(item){
- this.items.push(item);
- this.length++;
+ this.items[this.size++] = item;
this.top = item;
return this;
},
@@ -891,15 +887,15 @@ var listish = (function(global, exports, undefined){
* @return {Any} old stack top
*/
function pop(){
- this.length--;
- this.top = this.items[this.length - 1];
+ this.size--;
+ this.top = this.items[this.size - 1];
return this.items.pop();
},
/**
* Reset the stack
*/
function empty(){
- this.length = 0;
+ this.size = 0;
this.items = [];
this.top = undefined;
},
View
10 listish.min.js
@@ -30,9 +30,9 @@ return a&&(a.unlink(),this.size--),this.size},function replace(c,d){var a=find(t
while(c--)b.push(a[c]);return b},function __iterator__(){return new LinkedListIterator(this)}]),LinkedList}),exporter(function(){function DoublyLinkedListIterator(a){this.item=a.sentinel,this.sentinel=a.sentinel}function Item(a,b){this.data=a,this.after(b)}function Sentinel(a){this.next=this,this.prev=this}function find(a,c){if(a.lastFind&&a.lastFind.data===c)return a.lastFind;var b=a.sentinel,d=0;while((b=b.next)!==a.sentinel)if(b.data===c)return a.lastFind=b}function DoublyLinkedList(a){this.size=
0,define(this,{sentinel:new Sentinel,lastFind:null}),a&&iterate(a,this.append,this)}return inherit(DoublyLinkedListIterator,Iterator,[function next(){this.item=this.item.next;if(this.item===this.sentinel)throw a;return this.item.data}]),defineMethods(Item.prototype,[function after(a){return this.relink(a),this},function before(a){return this.prev.relink(a),this},function relink(a){return this.next&&(this.next.prev=this.prev,this.prev.next=this.next),this.prev=a,this.next=a.next,a.next.prev=this,a.next=
this,this},function unlink(){return this.next&&(this.next.prev=this.prev),this.prev&&(this.prev.next=this.next),this.prev=this.next=null,this},function empty(){var a=this.data;return this.next=this.prev=this.data=null,a}]),inherit(Sentinel,Item,[function unlink(){return this}]),defineMethods(DoublyLinkedList.prototype,[function first(){return this.sentinel.next.data},function last(){return this.sentinel.prev.data},function prepend(a){var b=new Item(a,this.sentinel);return this.size++},function append(a){var b=
-new Item(a,this.sentinel.prev);return this.size++},function insert(b,c){var a=find(this,c);return a?(a=new Item(b,a),this.size++):!1},function replace(b,c){var a=find(this,c);return a?(new Item(b,a),a.unlink(),!0):!1},function insertBefore(b,c){var a=find(this,c);return a?(a=new Item(b,a.prev),this.size++):!1},function pop(){if(this.size)return this.size--,this.sentinel.prev.unlink().data},function shift(){if(this.size)return this.size--,this.sentinel.next.unlink().data},function remove(b){var a=
+new Item(a,this.sentinel.prev);return this.size++},function insertAfter(b,c){var a=find(this,c);return a?(a=new Item(b,a),this.size++):!1},function insertBefore(b,c){var a=find(this,c);return a?(a=new Item(b,a.prev),this.size++):!1},function replace(b,c){var a=find(this,c);return a?(new Item(b,a),a.unlink(),!0):!1},function pop(){if(this.size)return this.size--,this.sentinel.prev.unlink().data},function shift(){if(this.size)return this.size--,this.sentinel.next.unlink().data},function remove(b){var a=
find(this,b);return a?(a.unlink(),!0):!1},function has(a){return!!find(this,a)},function items(){var a=this.sentinel,b=[];while((a=a.next)!==this.sentinel)b.push(a.data);return b},function empty(){var b,a=this.sentinel.next;while(a!==this.sentinel)b=a.next,a.empty(),a=b;return this.lastFind=null,this.size=0,this},function clone(){var a=this.sentinel,b=new DoublyLinkedList;while((a=a.next)!==this.sentinel)b.push(a.data);return b},function __iterator__(){return new DoublyLinkedListIterator(this)}]),
-DoublyLinkedList}),exporter(function(){function QueueIterator(a){this.queue=a,this.index=a.index}function Queue(a){this.index=this.length=0,a!=null?a instanceof Queue?(this.items=a.items.slice(a.front),this.length=this.items.length):(this.items=[],iterate(a,this.enqueue,this)):this.items=[]}return inherit(QueueIterator,Iterator,[function next(){if(this.index===this.queue.items.length)throw a;return this.queue.items[this.index++]}]),defineMethods(Queue.prototype,[function enqueue(a){return this.items.push(a),
-++this.length},function dequeue(){if(this.length){var a=this.items[this.index];return this.items[this.index++]=null,this.length--,this.index===500&&(this.items=this.items.slice(this.index),this.index=0),a}},function empty(){return this.length=0,this.index=0,this.items=[],this},function front(){return this.items[this.index]},function item(a){return this.items[this.index+a]},function __iterator__(){return new QueueIterator(this)}]),Queue}),exporter(function(){function StackIterator(a){this.stack=a,
-this.index=a.length}function Stack(a){this.empty(),a!=null&&iterate(a,this.push,this)}return inherit(StackIterator,Iterator,[function next(){if(!this.index)throw a;return this.stack[--this.index]}]),defineMethods(Stack.prototype,[function push(a){return this.items.push(a),this.length++,this.top=a,this},function pop(){return this.length--,this.top=this.items[this.length-1],this.items.pop()},function empty(){this.length=0,this.items=[],this.top=b},function __iterator__(){return new StackIterator(this)}]),
-Stack}),f}((0,eval)("this"),typeof exports==="undefined"?{}:exports);
+DoublyLinkedList}),exporter(function(){function QueueIterator(a){this.queue=a,this.index=a.index}function Queue(a){this.index=this.size=0,this.front=b,a!=null?a instanceof Queue?(this.items=a.items.slice(a.front),this.size=this.items.size):(this.items=[],iterate(a,this.enqueue,this)):this.items=[]}return inherit(QueueIterator,Iterator,[function next(){if(this.index===this.queue.items.length)throw a;return this.queue.items[this.index++]}]),defineMethods(Queue.prototype,[function enqueue(a){return this.items.push(a),
+!this.size&&(this.front=a),++this.size},function dequeue(){if(this.size){var a=this.items[this.index];return this.items[this.index++]=null,this.front=this.items[this.index],this.size--,this.index===500&&(this.items=this.items.slice(this.index),this.index=0),a}},function empty(){return this.size=0,this.index=0,this.items=[],this},function item(a){return this.items[this.index+a]},function __iterator__(){return new QueueIterator(this)}]),Queue}),exporter(function(){function StackIterator(a){this.stack=
+a,this.index=a.size}function Stack(a){this.empty(),a!=null&&iterate(a,this.push,this)}return inherit(StackIterator,Iterator,[function next(){if(!this.index)throw a;return this.stack[--this.index]}]),defineMethods(Stack.prototype,[function push(a){return this.items[this.size++]=a,this.top=a,this},function pop(){return this.size--,this.top=this.items[this.size-1],this.items.pop()},function empty(){this.size=0,this.items=[],this.top=b},function __iterator__(){return new StackIterator(this)}]),Stack}),
+f}((0,eval)("this"),typeof exports==="undefined"?{}:exports);
View
2 package.json
@@ -2,7 +2,7 @@
"author": "Brandon Benvie <http://bbenvie.com>",
"name": "listish",
"description": "Queue, Stack, LinkedList implemented using Maps",
- "version": "0.0.8",
+ "version": "0.0.9",
"keywords": ["list", "collection", "queue", "stack", "data", "linked list", "map", "weakmap"],
"repository": {
"type": "git",

0 comments on commit fc51848

Please sign in to comment.