Package Exports
- double-linkedlist
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (double-linkedlist) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
DOUBLE LINKED LIST
Getting Started
This is a simple double linked list. It has some tests but still use discretion.
You will have to create a new object to use it so for browsers...
var linkedList = new DoubleLinkedList()
and for node...
var linkedList = new require('double-linkedlist')();
Classes
- DoubleLinkedList
- Comparitor ⇒
boolean
- Callback ⇒
boolean
- DoubleLinkedList
- new DoubleLinkedList()
- .onChange(func)
- .canUndo() ⇒
boolean
- .undo()
- .clearUndo()
- .isEmpty() ⇒
boolean
- .getSize() ⇒
Number
- .insertAtStart(data)
- .insertAtEnd(data)
- .insertAtPosition(data)
- .deleteAtPosition(position)
- .deleteAll()
- .removeNode(comparitor, isReversed)
- .getTail() ⇒
Object
- .getHead() ⇒
Object
- .move(oldIdx, newIdx)
- .cycle(callback, isReversed)
- .toArray() ⇒
Array
- .findAll(comparitor)
new DoubleLinkedList()
Double Linked List
doubleLinkedList.onChange(func)
Will trigger all the functions given to it when objects are added, removed or moved.
Kind: instance method of DoubleLinkedList
Param | Type | Description |
---|---|---|
func | function |
function to call when a change occurs |
doubleLinkedList.canUndo() ⇒ boolean
determines if there are any more undo left
Kind: instance method of DoubleLinkedList
doubleLinkedList.undo()
will undo the last modifying command
Kind: instance method of DoubleLinkedList
doubleLinkedList.clearUndo()
removes all the undo that the user can perform
Kind: instance method of DoubleLinkedList
doubleLinkedList.isEmpty() ⇒ boolean
is this data list empty?
Kind: instance method of DoubleLinkedList
doubleLinkedList.getSize() ⇒ Number
Returns the size of this list
Kind: instance method of DoubleLinkedList
doubleLinkedList.insertAtStart(data)
Inserts data at the end of the list
Kind: instance method of DoubleLinkedList
Summary: Speed indicated that this method was far superior than the native array at with greater data
With 10000 small objects the native array was slightly faster by 3ms
With 100000 small object the double linked list more than 2x's faster.
With 1000000 small object the double linked list more than ~100x's faster.
Param | Type | Description |
---|---|---|
data | Object | Array |
Data to store into the array |
doubleLinkedList.insertAtEnd(data)
Inserts data at the end of the list
Kind: instance method of DoubleLinkedList
Summary: Speed indicated that this method was extremely slow in comparison to the native array
With 100000 small objects the native array was 12x's faster
With 1000000 small object the native array was 6x's faster
Param | Type | Description |
---|---|---|
data | Object | Array |
Data to store into the array |
doubleLinkedList.insertAtPosition(data)
Inserts data at the end of the list
Kind: instance method of DoubleLinkedList
Summary: Speed indicated that this method was comparable to the native array
With 1000 small objects both were equal
With 100000 small object the double linked list was slightly slower by ms
With 1000000 small object the double linked list was still a little slower by about 30ms
Param | Type | Description |
---|---|---|
data | Object | Array |
Data to store into the array |
doubleLinkedList.deleteAtPosition(position)
removes a node at the specified position
Kind: instance method of DoubleLinkedList
Param | Type | Description |
---|---|---|
position | Number |
index of the node you want to remove |
doubleLinkedList.deleteAll()
removes all the nodes
Kind: instance method of DoubleLinkedList
doubleLinkedList.removeNode(comparitor, isReversed)
removes a node at the specified position
Kind: instance method of DoubleLinkedList
Param | Type | Description |
---|---|---|
comparitor | Comparitor |
function that cycles through each element returning the node and index. The user must return true or false to indicate whether or not the node should be removed. |
isReversed | Boolean |
to cycle through the list in reverse |
Example
list.removeNode(function(node, idx){
return node.id === 4
},true)
doubleLinkedList.getTail() ⇒ Object
Kind: instance method of DoubleLinkedList
Returns: Object
- the node at the end of the list
doubleLinkedList.getHead() ⇒ Object
Kind: instance method of DoubleLinkedList
Returns: Object
- the node at the start of the list
doubleLinkedList.move(oldIdx, newIdx)
moves the object
Kind: instance method of DoubleLinkedList
Todo
- optimize (Method is brute force)
Param | Type | Description |
---|---|---|
oldIdx | Number |
the index of the object you want to move |
newIdx | Number |
the index you want to move the old object to |
doubleLinkedList.cycle(callback, isReversed)
cycles through each node and returns it along with the index to the callback To break free from the cycle the user can return false.
Kind: instance method of DoubleLinkedList
Param | Type | Description |
---|---|---|
callback | Callback |
function that cycles through each element returning the node and index. |
isReversed | Boolean |
to cycle through the list in reverse |
Example
list.cycle(function(node, idx){
// Do something with the node
})
doubleLinkedList.toArray() ⇒ Array
returns an array of the data
Kind: instance method of DoubleLinkedList
Returns: Array
- the internal data as an array
doubleLinkedList.findAll(comparitor)
removes a node at the specified position
Kind: instance method of DoubleLinkedList
Param | Type | Description |
---|---|---|
comparitor | Comparitor | Object |
function that cycles through each element returning the node and index. The user must return true or false to indicate whether or not the node should be removed. Or it can be an object and the method will find any node that matches the attribute's data |
Example
var array = list.findAll(function(node, idx){
return node.id === 4
})
var list = list.findAll({id:4})
Comparitor ⇒ boolean
Kind: global typedef
Returns: boolean
- the user should return true any time a condition is met while comparing the node
Param | Type | Description |
---|---|---|
node | Object |
object in the list |
idx | Number |
index of the object in the list |
Callback ⇒ boolean
Kind: global typedef
Returns: boolean
- Optionally the user can return false to break free from the cycle early
Param | Type | Description |
---|---|---|
node | Object |
object in the list |
idx | Number |
index of the object in the list |
~Node
new Node(data, nextNode, previousNode)
class that represents the nodes that make up the list. Each of the node are referenced to at most two other nodes - a previous and a next.
Param | Description |
---|---|
data | any object to store |
nextNode | a node to reference as next |
previousNode | a node to reference as previous |
Change Log
Version 0.0.6
Bug Fixes:
- Fixed cycle to require true to be explicitly returned to make the cycle continue.
- Fixed the Move method to properly rearrange the the nodes
Version 0.0.7
Bug Fixes:
- Fixed issue where adding attributes to the Node object via the dot notation did not propagate properly by restricting alterations to the inner node class attributes through only 4 methods: setData, getDataForKey, appendData & getProtectedData;