Skip to content

Commit fae9633

Browse files
committed
Merge pull request #1 from markpete/markpete/indices
Implementing skip lists to improve large data set performance
2 parents 5b6ccb2 + 77a2b15 commit fae9633

4 files changed

Lines changed: 86 additions & 7 deletions

File tree

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[![build status](https://travis-ci.org/markpete/SortedLinkedList.svg?branch=master)](http://travis-ci.org/markpete/SortedLinkedList)
22

33
# Sorted Linked List Library #
4-
_This repository contains a library for managing a sorted, singly-linked, linked list. The concept behind a linked-list is that developers can maintain an in-memory data structure to store a set of items without initially knowing the size and therefore without having to re-allocate arrays each time a limit is reached. Furthermore in this implementation the overall sort is maintained on the data._
4+
_This repository contains a library for managing a sorted, singly-linked, linked list. The concept behind a linked-list is that developers can maintain an in-memory data structure to store a set of items without initially knowing the size and therefore without having to re-allocate arrays each time a limit is reached. Furthermore in this implementation the overall sort is maintained on the data and as the list get large, it converts to a skip-list to improve performance._
55

66
### Installation ###
77
```
@@ -70,4 +70,10 @@ var current = myList.iterator();
7070
while(!(x = current.next()).done) {
7171
console.log(x.value);
7272
};
73+
```
74+
### - toArray ###
75+
Converts the LinkedList to an Array
76+
```
77+
var myArray = myList.toArray();
78+
console.log(myArray.toString());
7379
```

index.js

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,37 @@ function LinkedList(parameters) {
55
( Object.prototype.toString.call( parameters.values ) !== '[object Array]' )) {
66
return;
77
}
8-
for(var i=0; i<parameters.values.length; i++) {
8+
for (var i=0; i<parameters.values.length; i++) {
99
this.insert(parameters.values[i]);
1010
}
11+
this._actionCount = 0;
1112
}
13+
1214
LinkedList.prototype = {
1315
length: 0,
14-
head: null
16+
head: null,
17+
_indices: [],
18+
_actionCount: 0
1519
};
1620

1721
LinkedList.prototype.insert = function(data) {
1822
var node = new LinkedList.Node(data);
23+
this.actionCount();
1924
if (!this.head || node.data < this.head.data) {
2025
node.next = this.head;
2126
this.head = node;
27+
if(this._indices.length > 0) {
28+
this._indices.shift();
29+
this._indices.unshift(node);
30+
}
2231
} else {
23-
current = this.head;
32+
if(this._indices.length > 0) {
33+
var i;
34+
for(i=0; (i < this._indices.length) && (node.data > this._indices[i].data); i++);
35+
current = this._indices[i-1];
36+
} else {
37+
current = this.head;
38+
}
2439
while (current.next && current.next.data < node.data) {
2540
current = current.next;
2641
}
@@ -34,10 +49,32 @@ LinkedList.prototype.remove = function(data) {
3449
if (!this.head) {
3550
return false;
3651
}
52+
this.actionCount();
3753
if (data === this.head.data) {
3854
this.head = this.head.next;
55+
if(this._indices.length > 0) {
56+
this._indices.shift();
57+
if(this.head) {
58+
this._indices.unshift(this.head);
59+
} else {
60+
this.actionCount(true);
61+
}
62+
}
3963
} else {
40-
current = this.head;
64+
if(this._indices.length > 0) {
65+
var i;
66+
for(i=0; (i < this._indices.length) && (data > this._indices[i].data); i++);
67+
current = this._indices[i-1];
68+
if(current === this._indices[i]) {
69+
if(current.next) {
70+
this._indices.splice(i,1, current.next);
71+
} else {
72+
this._indices.splice(i,1);
73+
}
74+
}
75+
} else {
76+
current = this.head;
77+
}
4178
while (current.next && current.next.data < data) {
4279
current = current.next;
4380
}
@@ -70,6 +107,7 @@ LinkedList.prototype.contains = function(data) {
70107
LinkedList.prototype.clear = function () {
71108
this.head = null;
72109
this.length = 0;
110+
this.actionCount(true);
73111
};
74112

75113
LinkedList.prototype.iterator = function* () {
@@ -78,7 +116,39 @@ LinkedList.prototype.iterator = function* () {
78116
yield item.data;
79117
item = item.next;
80118
}
81-
}
119+
};
120+
121+
LinkedList.prototype.actionCount = function (empty) {
122+
if (empty) {
123+
this._actionCount = 0;
124+
return;
125+
}
126+
this._actionCount++;
127+
if(this._actionCount > 2000) {
128+
this._actionCount = 0;
129+
this._indices = [];
130+
if(this.length >= 2000) {
131+
var index_point = Math.floor(this.length / 100);
132+
var curr_node = this.head;
133+
while(curr_node) {
134+
this._indices.push(curr_node);
135+
for(i=0;curr_node && i<index_point;i++) {
136+
curr_node = curr_node.next;
137+
}
138+
}
139+
}
140+
}
141+
};
142+
143+
LinkedList.prototype.toArray = function() {
144+
var arr = [];
145+
var current = this.head;
146+
while(current) {
147+
arr.push(current.data);
148+
current = current.next;
149+
}
150+
return arr;
151+
};
82152

83153
LinkedList.Node = function(data) {
84154
this.next = null;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"keywords": [
1313
"linked list",
1414
"sorted linked list",
15+
"skip list",
1516
"data structure",
1617
"abstract data type",
1718
"adt"

test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@ describe('sorted-linked-list', function() {
4242
expect(myList.length).to.equal(0);
4343
});
4444

45-
it('create a list via array', function () {
45+
it('list to and from array', function () {
4646
myList = new LinkedList({values: ['dog', 'liger', 'cat', 'donkey']});
4747
expect(myList).to.not.be.null;
4848
expect(myList).to.be.ok;
4949
expect(myList.length).to.equal(4);
5050
expect(myList.head.data).to.equal('cat');
51+
var myArray = myList.toArray();
52+
expect(myArray).to.deep.equal(['cat', 'dog', 'donkey', 'liger']);
5153
});
5254

5355
it('validate contains', function () {

0 commit comments

Comments
 (0)