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 */
1010function 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 */
3232function 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 */
5555DoubleLinkedList . 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 */
7272DoubleLinkedList . 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 */
8888DoubleLinkedList . 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 */
105105DoubleLinkedList . 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 */
123123DoubleLinkedList . 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 */
147147DoubleLinkedList . 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 */
167167DoubleLinkedList . 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 */
180181DoubleLinkedList . 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 */
245248DoubleLinkedList . 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 */
291294DoubleLinkedList . 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 */
302306DoubleLinkedList . 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+ } ;
0 commit comments