[Pkg-javascript-commits] [node-dequeue] 01/12: Imported Upstream version 1.0.3

Mike Gabriel sunweaver at debian.org
Tue Aug 19 17:49:51 UTC 2014


This is an automated email from the git hooks/post-receive script.

sunweaver pushed a commit to branch master
in repository node-dequeue.

commit a854a8ff08fd4fa3a86e7ca37fd94ea51086b1a5
Author: Mike Gabriel <mike.gabriel at das-netzwerkteam.de>
Date:   Fri May 10 01:43:37 2013 +0200

    Imported Upstream version 1.0.3
---
 .gitignore     |  1 +
 README.md      | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/dequeue.js | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/index.js   |  1 +
 package.json   | 17 +++++++++++++
 5 files changed, 162 insertions(+)

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..587b028
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+*.tmproj
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..5a5323b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,67 @@
+A Simple Double Ended Queue Datastructure
+=========================================
+
+Dequeue is implemented as a doubly linked circular list with a titular head
+node. By "titular head node", I mean an empty node to designate the beginning
+and end of the circularly linked list. I first saw this construction in the
+linux kernel source and it seem simple and elegant. I added the `.length`
+property to use it like I was using an Array.
+
+I was using a javascript Array as a FIFO. Somewhere between 100,000 and
+200,000 entries the program performance went to hell (dev host is a MBP
+w/8GB RAM). 15 minutes later, I implemented a simple dequeue and my FIFO
+scales up to millions of entries.
+
+It is a drop-in replacement for javascript-arrays-as-fifo.
+
+## Example: Dequeue as a replacement for an Array as a FIFO
+
+    var Dequeue = require('dequeue')
+    
+    //var fifo = []
+    var fifo = new Dequeue()
+    
+    fifo.length === 0 //=> true
+    
+    fifo.push(d1)
+    fifo.length === 1 //=> true
+    
+    fifo.unshift(d2)
+    
+    fifo.pop() === d1 //=> true
+    
+    fifo.push(d3)
+    
+    fifo.shift() === d2 //=> true
+    
+    fifo.length === 1 //=> true; only d3 is in the dequeue
+    
+## API
+
+### `deque = new Dequeue()`
+
+### `deque.push(value)`
+Push a value on the end.
+
+### `value = deque.pop()`
+Remove a value off the end.
+
+### `deque.unshift(value)`
+Push a value on the beginning.
+
+### `value = deque.shift()`
+Remove a value off the beginning.
+
+### `deque.empty()`
+Remove all entries. This is NOT a test for an empty dequeue; use `deque.length`
+for that.
+
+## Future Development
+Something this simple does not really need a roadmap. However, I am thinking
+of adding APIs to facilitate walking the Linked List via an iterator. It will
+be simple and fully backward compatible.
+
+## About the Code
+
+I was convinced by [a blog posting](http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding) [by Issac Z. Schlueter](http://blog.izs.me/) that I don't need
+semicolons. So I don't use them.
diff --git a/lib/dequeue.js b/lib/dequeue.js
new file mode 100644
index 0000000..dc032ed
--- /dev/null
+++ b/lib/dequeue.js
@@ -0,0 +1,76 @@
+
+var Dequeue = exports = module.exports = function Dequeue() {
+  this.head = new Node()
+  this.length = 0
+}
+
+Dequeue.prototype.push = function(d){
+  var n = new Node(d)
+  this.head.prepend(n)
+  this.length += 1
+  return this
+}
+
+Dequeue.prototype.unshift = function(d){
+  var n = new Node(d)
+  this.head.append(n)
+  this.length += 1
+  return this
+}
+
+Dequeue.prototype.pop = function(){
+  if (this.head.prev === this.head) return
+  var n = this.head.prev.remove()
+  this.length -= 1
+  return n.data
+}
+
+Dequeue.prototype.shift = function(){
+  if (this.head.next === this.head) return
+  var n = this.head.next.remove()
+  this.length -= 1
+  return n.data
+}
+
+Dequeue.prototype.empty = function(){
+  if (this.length === 0 ) return
+
+  //no node points to head; not necessary for GC, but it makes me feel better.
+  this.head.next.prev = null
+  this.head.prev.next = null
+
+  //head only points to itself; as a fresh node would
+  this.head.next = this.head
+  this.head.prev = this.head
+  
+  this.length = 0
+
+  return
+}
+function Node(d) {
+  this.data = d
+  this.next = this
+  this.prev = this
+}
+
+Node.prototype.append = function(n) {
+  n.next = this.next
+  n.prev = this
+  this.next.prev = n
+  this.next = n
+  return n
+}
+
+Node.prototype.prepend = function(n) {
+  n.prev = this.prev
+  n.next = this
+  this.prev.next = n
+  this.prev = n
+  return n
+}
+
+Node.prototype.remove = function() {
+  this.next.prev = this.prev
+  this.prev.next = this.next
+  return this
+}
\ No newline at end of file
diff --git a/lib/index.js b/lib/index.js
new file mode 100644
index 0000000..9db1227
--- /dev/null
+++ b/lib/index.js
@@ -0,0 +1 @@
+exports = module.exports = require("./dequeue")
\ No newline at end of file
diff --git a/package.json b/package.json
new file mode 100644
index 0000000..c012500
--- /dev/null
+++ b/package.json
@@ -0,0 +1,17 @@
+{
+  "name"  : "dequeue"
+, "main"  : "./lib/index.js"
+, "version" : "1.0.3"
+, "description" : "A simple double ended queue datastructure"
+, "keywords"    : ["datastructure", "queue", "double ended queue", "fifo", "FIFO", "linked list"]
+, "homepage"    : "https://github.com/lleo/node-dequeue"
+, "repository"  : { "type": "git"
+                  , "url": "https://github.com/lleo/node-dequeue" }
+, "bugs"        : { "url": "https://github.com/lleo/node-dequeue/issues"
+                  , "email": "lleoem at gmail.com" }
+, "author"      : { "name": "LLeo"
+                  , "email": "lleoem at gmail.com"
+                  , "url": "http://lleo-blog.blogspot.com/" }
+, "engines" : {"node" : "*"}
+, "dependencies" : {}
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-dequeue.git



More information about the Pkg-javascript-commits mailing list