linkd
v3.0.0
Published
A fast and extendable doubly linked list
Downloads
55
Readme
linkd
A fast and extendable doubly linked list
for node 6+
API
The linked list implements the
ES6 Iterable Interface.
You may use the ES6 for (var x of list)
loop on the list. If you like to convert the list into an array you may do so using
the Array.from(list)
method or the ES6 spread operator
[...list]
.
See Iterables and iterators in ECMAScript 6
on how to use them.
You may use any type as the hash value for your items, not only strings.
Attention: many methods work have similar names as those on native arrays but may actually work not the same: the push method on an array adds an element to the end of the array, the push method on the linkd list adds an element to the beginning of the list.
Method Overview
Getting Nodes
get(hash)
: get a value by its hashgetNode(hash)
: get a node by its hashgetFirst()
: get the first value by its hashgetFirstNode()
: get the first node by its hashgetLast()
: get the last value by its hashgetLastNode()
: get the last node by its hash
Checking if a node exists
has(hash)
: checks if a given hash exists in the list
Adding Nodes
push(hash|node, [value])
: adds a node to the begining of the listunshift(hash|node, [value])
: adds a node to the end of the listaddBefore(hash|node, [value], beforeHash|node)
: adds a node before another nodeaddAfter(hash|node, [value], beforeHash|node)
: adds a node after another node
Moving Nodes
moveBefore(hash|node, hash|node)
: move a node before another nodemoveAfter(hash|node, hash|node)
: move a node after another nodemoveToBegin(hash|node)
: move a node to the begin of the listmoveToEnd(hash|node)
: move a node to the end of a list
Removing Nodes
remove(node|hash)
: removes a specific node. returns the value of the removed node or undefined if the node was not foundremoveNode(node|hash)
: removes a specific node, returns the node or nulll it the node was not foundpop()
: remove the first node, returns the value or undefined if the node was not foundpopNode()
: remove the first node, returns the node or null if the node was not foundshift()
: removes the last node, returns the value or undefined if the node was not foundshiftNode()
: removes the last node, returns the node or null if the node was not found
Node Methods
hasNext()
: returns true if the node has a next nodehasPrevious()
: returns true if the node has a previous nodeisFrist()
: returns true if the node is the first nodeisLast()
: returns true if the node is the last nodegetNext()
: returns the next nodegetPrevious()
: returns the previous node
Getting All Keys
keys()
: Returns an ES6 Iterator containing all keys. The keys are not ordered.
Properties
length
: integer, the number of items in the linked list
Events
The list emits events for each action performed on it.
Unspecific events
drain
: is emitted if the list is empty after an item was removed
Events that have the value of the item passed to
remove
: emitted when a node was removed using one of theremove
,pop
,shift
,removeNode
,popNode
orshiftNode
methodsadd
: emitted when a node was added using one of theadd
,addAfter
,addBefore
,unshift
,push
,addNode
,addAfterNode
,addBeforeNode
,unshiftNode
orpushNode
methodspop
: emitted when a node was removed using thepop
orpopNode
methodunshift
: emitted when a node was added using theunshift
methodshift
: emitted when a node was removed using theshift
orshiftNode
methodpush
: emitted when a node was added using thepush
methodaddBefore
: emitted when a node was added using theaddBefore
methodaddAfter
: emitted when a node was added using theaddAfter
methodget
: emitted when a node was returned using theget
orgetNode
method
Events that have the node of the item passed to
removeNode
: emitted when a node was removed using one of theremove
,pop
,shift
,removeNode
,popNode
orshiftNode
methodsaddNode
: emitted when a node was added using one of theadd
,addAfter
,addBefore
,unshift
,push
,addNode
,addAfterNode
,addBeforeNode
,unshiftNode
orpushNode
methodspopNode
: emitted when a node was removed using thepop
orpopNode
methodunshiftNode
: emitted when a node was added using theunshift
methodshiftNode
: emitted when a node was removed using theshift
orshiftNode
methodpushNode
: emitted when a node was added using thepush
methodaddBeforeNode
: emitted when a node was added using theaddBefore
methodaddAfterNode
: emitted when a node was added using theaddAfter
methodgetNode
: emitted when a node was returned using theget
orgetNode
method
Examples
Constructor
Create an instance fo the linked list
var list = new Linkd();
If you like to override the internal node representation you may inherit from the Node class found on the Linked Lsit constructor.
var Class = require('ee-class')
, Linkd = require('linkd')
, Node = Linkd.Node;
var MyNodeImplementation = new Class({
inherits: Node
, init: function init(hash, value, previousNode, nextNode) {
// you should call the super contructor
init.super.call(this, hash, value, previousNode, nextNode);
}
, myCustomMethod: function() {
}
});
var list = new Linkd(MyNodeImplementation);
Get
Returns the value for a given hash. It returns the item or undefined if the hash was not found.
list.get(hash);
GetNode
Returns the node for a given hash. It returns the node or null if the hash was not found. The node is the internal representation of any item and provides an easy to use interface for traversing the list.
list.getNode(hash);
Has
Checks if a given hash is stored in the linked list
list.has(hash);
Push
Add an item or a node at the beginning of the list.
list.push(hash, value);
list.push(node);
AddBefore
Add an item or a node before another item.
list.addBefore(hash, value, hashOfItemBefore);
list.addBefore(node, hashOfItemBefore);
AddAfter
Add an item or a node after another item.
list.addAfter(hash, value, hashOfItemAftrer);
list.addAfter(node, hashOfItemAftrer);
Unshift
Add an item or a node at the end of the list.
list.unshift(hash, value);
list.unshift(node);
Shift
Removes the last item from the list
var removedValue = list.shift(hash);
If you want the removed node instead of the value
var removedNode = list.shiftNode(hash);
Pop
Removes the first item from the list
var removedValue = list.pop(hash);
If you want the removed node instead of the value
var removedNode = list.popNode(hash);
Remove
Removes an item addressed by hash
var removedValue = list.remove(hash);
If you want the removed node instead of the value
var removedNode = list.removeNode(hash);
Iterating over all values
Use the Iterable interface:
for (var value of list) {
log(value);
}
Iterating over all hashes
Use the Iterable interface:
for (var key of list.keys()) {
log(key);
}
Node
The node retreived via the getNode
method has the following interface
Hash
the hash property holds the hash this node is stored on
HasNext
Returns true if the node has a next node in the list
if (node.hasNext()) ...
HasPrevious
Returns true if the node has a previous node in the list
if (node.hasPrevious()) ...
IsFirst
Returns true if the node is the first node in the list
if (node.isFirst()) ...
IsLast
Returns true if the node is the last node in the list
if (node.isLast()) ...
GetNext
Returns the next node in the list
var nextNode = node.getNext();
GetPrevious
Returns the previous node in the list
var previousNode = node.getPrevious();