Skip to content

Commit ee876e7

Browse files
committed
JSDoc updated
1 parent ce0cea2 commit ee876e7

File tree

10 files changed

+189
-70
lines changed

10 files changed

+189
-70
lines changed

.gitignore_global

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

CircularBuffer/CircularBuffer.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* Class for managing a circular buffer.
7-
* @param size The size of the buffer.
7+
* @param size {Number} The size of the buffer.
88
* @constructor
99
*/
1010
function CircularBuffer(size) {
@@ -20,7 +20,7 @@ function CircularBuffer(size) {
2020
this.tail = 0;
2121
/**
2222
* The items stored in the buffer.
23-
* @type {Array}
23+
* @type {Array<*>}
2424
*/
2525
this.items = new Array(size);
2626
/**
@@ -42,44 +42,44 @@ function CircularBuffer(size) {
4242

4343
/**
4444
* Write the item at the head of the buffer.
45-
* @param item The item to write.
45+
* @param item {*} The item to write.
4646
* @return {void}
4747
*/
48-
CircularBuffer.prototype.write = function(item) {
48+
CircularBuffer.prototype.write = function (item) {
4949
this.empty = false;
50-
if(this.full)
51-
//if buffer is full tail must be set forward
50+
if (this.full)
51+
//if buffer is full tail must be set forward
5252
this.tail = (this.tail + 1) % this.size;
5353
this.items[this.head] = item;
5454
//head is set forward
5555
this.head = (this.head + 1) % this.size;
56-
if(this.tail === this.head)
56+
if (this.tail === this.head)
5757
this.full = true;
5858
};
5959

6060
/**
6161
* Free the buffer between indexes from and to.
6262
* If from > to, positions between from and the end of the buffer and between the start and to will be free.
63-
* @param from The index from which start to free (inclusive index)
64-
* @param to The index where stop to free (exclusive index)
63+
* @param from {Number} The index from which start to free (inclusive index)
64+
* @param to {Number} The index where stop to free (exclusive index)
6565
* @return {void}
6666
*/
67-
CircularBuffer.prototype.free = function(from, to) {
68-
if(from < 0)
67+
CircularBuffer.prototype.free = function (from, to) {
68+
if (from < 0)
6969
from = 0;
70-
if(from > this.size - 1)
70+
if (from > this.size - 1)
7171
from = this.size - 1;
72-
if(to < 0)
72+
if (to < 0)
7373
to = 0;
74-
if(to > this.size - 1)
74+
if (to > this.size - 1)
7575
to = this.size - 1;
7676
//if from < to then will be free allocation between from and to
7777
//otherwise will be free allocations between from and the end and between the start and to
78-
for(; from < to; from = (from + 1) % this.size)
78+
for (; from < to; from = (from + 1) % this.size)
7979
delete this.items[from];
8080
//free could make buffer empty
81-
for(var i = 0; i < this.size; i++)
82-
if(this.items[i] !== undefined) {
81+
for (var i = 0; i < this.size; i++)
82+
if (this.items[i] !== undefined) {
8383
this.empty = false;
8484
return;
8585
}
@@ -90,33 +90,33 @@ CircularBuffer.prototype.free = function(from, to) {
9090
* Free all the buffer.
9191
* @return {void}
9292
*/
93-
CircularBuffer.prototype.freeAll = function() {
94-
for(var i = 0; i < this.size; i++)
93+
CircularBuffer.prototype.freeAll = function () {
94+
for (var i = 0; i < this.size; i++)
9595
delete this.items[i];
9696
this.empty = true;
9797
};
9898

9999
/**
100100
* Read the item stored at the position index.
101-
* @param index The position of the item to read.
102-
* @return {Object} The item read.
101+
* @param index {Number} The position of the item to read.
102+
* @return {*} The item read.
103103
*/
104-
CircularBuffer.prototype.read = function(index) {
104+
CircularBuffer.prototype.read = function (index) {
105105
return this.items[index % this.size];
106106
};
107107

108108
/**
109109
* Return true if the buffer is empty, false otherwise.
110110
* @return {boolean}
111111
*/
112-
CircularBuffer.prototype.isEmpty = function() {
112+
CircularBuffer.prototype.isEmpty = function () {
113113
return this.empty;
114114
};
115115

116116
/**
117117
* Return true if the buffer is full, false otherwise.
118118
* @return {boolean}
119119
*/
120-
CircularBuffer.prototype.isFull = function() {
120+
CircularBuffer.prototype.isFull = function () {
121121
return this.full;
122122
};

DoubleLinkedList/DoubleLinkedList.js

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
/**
66
* The single node of the list.
7-
* @param item The item to store in the node.
7+
* @param item {*} The item to store in the node.
88
* @constructor
99
*/
1010
function Node(item) {
1111
/**
1212
* The item stored.
13-
* @type {Object}
13+
* @type {*}
1414
*/
1515
this.item = item;
1616
/**
@@ -26,7 +26,7 @@ function Node(item) {
2626
}
2727

2828
/**
29-
* Class for managing a linked list.
29+
* Class for managing a double linked list.
3030
* @constructor
3131
*/
3232
function DoubleLinkedList() {
@@ -49,7 +49,7 @@ function DoubleLinkedList() {
4949

5050
/**
5151
* Add an item at the head of the list.
52-
* @param item The item to add.
52+
* @param item {*} The item to add.
5353
* @return {void}
5454
*/
5555
DoubleLinkedList.prototype.pushFront = function (item) {
@@ -66,7 +66,7 @@ DoubleLinkedList.prototype.pushFront = function (item) {
6666

6767
/**
6868
* Add an item at the tail of the list.
69-
* @param item The item to add.
69+
* @param item {*} The item to add.
7070
* @return {void}
7171
*/
7272
DoubleLinkedList.prototype.pushBack = function (item) {
@@ -83,7 +83,7 @@ DoubleLinkedList.prototype.pushBack = function (item) {
8383

8484
/**
8585
* Remove the first element of the list.
86-
* @return {Object|undefined} The element removed. It's undefined if the list is empty.
86+
* @return {*} The element removed. It's undefined if the list is empty.
8787
*/
8888
DoubleLinkedList.prototype.popFront = function () {
8989
if (this.length) {
@@ -100,7 +100,7 @@ DoubleLinkedList.prototype.popFront = function () {
100100

101101
/**
102102
* Remove the last element of the list.
103-
* @return {Object|undefined} The element removed. It's undefined if the list is empty.
103+
* @return {*} The element removed. It's undefined if the list is empty.
104104
*/
105105
DoubleLinkedList.prototype.popBack = function () {
106106
if (this.length) {
@@ -117,8 +117,8 @@ DoubleLinkedList.prototype.popBack = function () {
117117

118118
/**
119119
* Remove the item at the position index.
120-
* @param index The position of the item to remove.
121-
* @return {Object|undefined}
120+
* @param index {Number} The position of the item to remove.
121+
* @return {*} The item stored at the position index. It's undefined if the index is out of bounds.
122122
*/
123123
DoubleLinkedList.prototype.removeAt = function (index) {
124124
if (index < 0 || index > this.length - 1)
@@ -141,8 +141,8 @@ DoubleLinkedList.prototype.removeAt = function (index) {
141141

142142
/**
143143
* Get the item at the position index.
144-
* @param index The position of the item.
145-
* @return {Object|undefined}. It's undefined if index isn't in the queue bounds.
144+
* @param index {Number} The position of the item.
145+
* @return {*}. It's undefined if index isn't in the queue bounds.
146146
*/
147147
DoubleLinkedList.prototype.getItem = function (index) {
148148
if (index < 0 || index > this.length - 1)
@@ -160,8 +160,8 @@ DoubleLinkedList.prototype.getItem = function (index) {
160160

161161
/**
162162
* Get the node at the position index relative from the node.
163-
* @param node The node from which start the search.
164-
* @param index The index, relative to the node, of the node to return.
163+
* @param node {Node} The node from which start the search.
164+
* @param index {Number} The index, relative to the node, of the node to return.
165165
* @return {Node} The node at the position index.
166166
*/
167167
DoubleLinkedList.prototype.getNode = function (node, index) {
@@ -176,6 +176,7 @@ DoubleLinkedList.prototype.getNode = function (node, index) {
176176
* Sort the list using web workers.
177177
* Using this method is discouraged. Many web browser set a limit to the maximum number of workers instantiated.
178178
* The items of the list, due to web workers implementation, will be serialized so they will lost own methods.
179+
* @return {void}
179180
*/
180181
DoubleLinkedList.prototype.parallelSort = function () {
181182

@@ -205,7 +206,7 @@ DoubleLinkedList.prototype.parallelSort = function () {
205206
_array[data.index] = data.value;
206207
break;
207208
}
208-
}
209+
};
209210
workerRight.onmessage = function (event) {
210211
var data = event.data;
211212
switch (data.cmd) {
@@ -235,12 +236,14 @@ DoubleLinkedList.prototype.parallelSort = function () {
235236
case 'replace':
236237
_array[data.index] = data.value;
237238
}
238-
}
239+
};
239240
partialSort(0, this.length - 1, 0);
240241
};
241242

242243
/**
243244
* Sort the list.
245+
* @param callback {function} The function invoked in order to get the value for the evaluation of the sort criteria. That parameter could be omitted. In that case, will be returned the same item.
246+
* @return {void}
244247
*/
245248
DoubleLinkedList.prototype.sort = function (callback) {
246249

@@ -286,18 +289,19 @@ DoubleLinkedList.prototype.sort = function (callback) {
286289

287290
/**
288291
* Transform the list into an array.
289-
* @return {Array} The array built.
292+
* @return {Array<*>} The array built.
290293
*/
291294
DoubleLinkedList.prototype.toArray = function () {
292295
var array = [];
293296
for (var node = this.first, i = 0; node; node = node.next, i++)
294297
array[i] = node.item;
295298
return array;
296-
}
299+
};
297300

298301
/**
299302
* Build the list from the array.
300-
* @param array The array from which build the list.
303+
* @param array {Array<*>} The array from which build the list.
304+
* @return {void}
301305
*/
302306
DoubleLinkedList.prototype.fromArray = function (array) {
303307
var node = this.first;
@@ -309,4 +313,30 @@ DoubleLinkedList.prototype.fromArray = function (array) {
309313
else
310314
for (var k = array.length; k < this.length;)
311315
this.popBack();
312-
}
316+
};
317+
318+
/**
319+
* Return the items that satisfy the condition determined by the callback.
320+
* @param callback {function} The function that implements the condition.
321+
* @return {Array<Object>} The array that contains the items that satisfy the condition.
322+
*/
323+
DoubleLinkedList.prototype.filter = function (callback) {
324+
var result = [];
325+
for (var node = this.first; node; node = node.next) {
326+
if (callback(node.item))
327+
result.push(node.item);
328+
}
329+
return result;
330+
};
331+
332+
/**
333+
* Reverse the list. This method reverses only the items, not the nodes.
334+
* @return {void}
335+
*/
336+
DoubleLinkedList.prototype.reverse = function () {
337+
for (var start = this.first, end = this.last; start !== end && start.previous !== end; start = start.next, end = end.previous) {
338+
var item = start.item;
339+
start.item = end.item;
340+
end.item = item;
341+
}
342+
};

DoubleLinkedList/WorkerSort.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ onmessage = function (event) {
1616
break;
1717
case 'finished':
1818
this.finished++;
19-
if(this.finished > 1) {
19+
if (this.finished > 1) {
2020
this.array = data.array;
2121
merge(this.from, this.to, this.array);
2222
this.postMessage({cmd: 'finished', worker: this.worker});
@@ -26,18 +26,19 @@ onmessage = function (event) {
2626
default :
2727
this.postMessage('Something went wrong');
2828
}
29-
}
29+
};
3030

31+
//noinspection FunctionWithMultipleLoopsJS
3132
function merge(from, to, array) {
3233
var m = Math.floor((from + to) / 2);
3334
var left = [];
3435
var right = [];
3536
for (var i = 0; i < m - from + 1; i++)
3637
left[i] = array[from + i];
37-
for (var j = 0; j < to - m ; j++)
38+
for (var j = 0; j < to - m; j++)
3839
right[j] = array[m + j + 1];
3940
var x = 0, y = 0;
40-
for(var k = from; k < to + 1; k++) {
41+
for (var k = from; k < to + 1; k++) {
4142
if (y > to - m - 1 || (left[x] <= right[y] && x < m - from + 1)) {
4243
this.postMessage({cmd: 'replace', index: k, value: left[x]});
4344
x++;

Queue/Queue.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,29 @@ function Queue() {
1919
* @param item The item to add.
2020
* @return {void}
2121
*/
22-
Queue.prototype.enqueue = function(item) {
22+
Queue.prototype.enqueue = function (item) {
2323
this.items.push(item);
2424
};
2525

26+
//noinspection FunctionWithMultipleReturnPointsJS
2627
/**
2728
* Remove the item at the head of the queue.
2829
* @return {Object|undefined} The item at the head of the queue. It's undefined if the queue is empty.
2930
*/
30-
Queue.prototype.dequeue = function() {
31-
if(!this.items.length)
31+
Queue.prototype.dequeue = function () {
32+
if (!this.items.length)
3233
return undefined;
3334
return this.items.splice(0, 1)[0]; //remove the first item and return it
3435
};
3536

37+
//noinspection FunctionWithMultipleReturnPointsJS
3638
/**
3739
* Return the item at the position index.
3840
* @param index The position of the item.
3941
* @return {Object|undefined} The item at the position. It's undefined if index isn't in the queue bounds.
4042
*/
41-
Queue.prototype.getItem = function(index) {
42-
if(index < 0 || index > this.items.length - 1)
43+
Queue.prototype.getItem = function (index) {
44+
if (index < 0 || index > this.items.length - 1)
4345
return undefined;
4446
return this.items[index];
4547
};
@@ -48,6 +50,6 @@ Queue.prototype.getItem = function(index) {
4850
* Return the length of the queue.
4951
* @return {Number} The length of the queue.
5052
*/
51-
Queue.prototype.getLength = function() {
53+
Queue.prototype.getLength = function () {
5254
return this.items.length;
5355
};

0 commit comments

Comments
 (0)