collect-your-stuff 1.3.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +802 -77
- package/dist/collections/arrayable/ArrayElement.d.ts +2 -0
- package/dist/collections/arrayable/ArrayElement.js +4 -2
- package/dist/collections/arrayable/ArrayElement.min.js +1 -1
- package/dist/collections/arrayable/Arrayable.d.ts +27 -13
- package/dist/collections/arrayable/Arrayable.js +47 -12
- package/dist/collections/arrayable/Arrayable.min.js +1 -1
- package/dist/collections/doubly-linked-list/DoubleLinker.d.ts +5 -1
- package/dist/collections/doubly-linked-list/DoubleLinker.js +5 -1
- package/dist/collections/doubly-linked-list/DoublyLinkedList.d.ts +25 -13
- package/dist/collections/doubly-linked-list/DoublyLinkedList.js +106 -53
- package/dist/collections/doubly-linked-list/DoublyLinkedList.min.js +1 -1
- package/dist/collections/linked-list/LinkedList.d.ts +27 -10
- package/dist/collections/linked-list/LinkedList.js +106 -43
- package/dist/collections/linked-list/LinkedList.min.js +1 -1
- package/dist/collections/linked-list/Linker.d.ts +5 -2
- package/dist/collections/linked-list/Linker.js +9 -5
- package/dist/collections/linked-list/Linker.min.js +1 -1
- package/dist/collections/linked-tree-list/LinkedTreeList.d.ts +16 -4
- package/dist/collections/linked-tree-list/LinkedTreeList.js +25 -22
- package/dist/collections/linked-tree-list/LinkedTreeList.min.js +1 -1
- package/dist/collections/linked-tree-list/TreeLinker.d.ts +7 -1
- package/dist/collections/linked-tree-list/TreeLinker.js +7 -1
- package/dist/collections/queue/Queue.d.ts +6 -3
- package/dist/collections/queue/Queue.js +23 -18
- package/dist/collections/queue/Queue.min.js +1 -1
- package/dist/collections/queue/Queueable.d.ts +10 -4
- package/dist/collections/queue/Queueable.js +11 -6
- package/dist/collections/queue/Queueable.min.js +1 -1
- package/dist/collections/stack/Stack.d.ts +4 -3
- package/dist/collections/stack/Stack.js +4 -4
- package/dist/collections/stack/Stack.min.js +1 -1
- package/dist/collections/stack/Stackable.d.ts +4 -1
- package/dist/collections/stack/Stackable.js +7 -5
- package/dist/collections/stack/Stackable.min.js +1 -1
- package/dist/main.d.ts +2 -2
- package/dist/recipes/ArrayIterator.d.ts +10 -0
- package/dist/recipes/ArrayIterator.js +10 -0
- package/dist/recipes/DoubleLinkerIterator.d.ts +9 -0
- package/dist/recipes/DoubleLinkerIterator.js +9 -0
- package/dist/recipes/LinkerIterator.d.ts +9 -0
- package/dist/recipes/LinkerIterator.js +9 -0
- package/dist/recipes/Runnable.d.ts +3 -2
- package/dist/recipes/Runnable.js +3 -2
- package/dist/recipes/TreeLinkerIterator.d.ts +9 -0
- package/dist/recipes/TreeLinkerIterator.js +9 -0
- package/package.json +2 -1
|
@@ -9,7 +9,9 @@ import { IsElement } from '../../recipes/IsElement';
|
|
|
9
9
|
* Element represents a node in an Arrayable.
|
|
10
10
|
*/
|
|
11
11
|
export declare class ArrayElement implements IsElement {
|
|
12
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
12
13
|
readonly classType: typeof ArrayElement;
|
|
14
|
+
/** The data stored in this element. */
|
|
13
15
|
data: any;
|
|
14
16
|
/**
|
|
15
17
|
* Create the new Element instance, provide the data and optionally configure the type of Element.
|
|
@@ -15,7 +15,9 @@ class ArrayElement {
|
|
|
15
15
|
* @param {*} [data=null] The data to be stored in this element.
|
|
16
16
|
*/
|
|
17
17
|
constructor (data = null) {
|
|
18
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
18
19
|
this.classType = ArrayElement
|
|
20
|
+
/** The data stored in this element. */
|
|
19
21
|
this.data = null
|
|
20
22
|
this.data = data
|
|
21
23
|
}
|
|
@@ -28,8 +30,8 @@ class ArrayElement {
|
|
|
28
30
|
*/
|
|
29
31
|
exports.ArrayElement = ArrayElement
|
|
30
32
|
ArrayElement.make = (element, classType = ArrayElement) => {
|
|
31
|
-
if (typeof element !== 'object') {
|
|
32
|
-
// It is not an object, so instantiate the Element with element as the data
|
|
33
|
+
if (element === null || typeof element !== 'object') {
|
|
34
|
+
// It is not an object (or it is null), so instantiate the Element with element as the data
|
|
33
35
|
return new classType(element)
|
|
34
36
|
}
|
|
35
37
|
if (element.classType) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ArrayElement=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.reduce.js");class ArrayElement{constructor(e=null){this.classType=ArrayElement,this.data=null,this.data=e}}exports.ArrayElement=ArrayElement,ArrayElement.make=(e,r=ArrayElement)=>"object"!=typeof e?new r(e):e.classType?e:new r(e),ArrayElement.fromArray=(e=[],r=ArrayElement)=>e.reduce(((e,t)=>{const a=r.make(t,r);return e.head.length?(e.head.push(a),e.tail=a,e):{head:[a],tail:a}}),{head:[],tail:null});
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.ArrayElement=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.reduce.js");class ArrayElement{constructor(e=null){this.classType=ArrayElement,this.data=null,this.data=e}}exports.ArrayElement=ArrayElement,ArrayElement.make=(e,r=ArrayElement)=>null===e||"object"!=typeof e?new r(e):e.classType?e:new r(e),ArrayElement.fromArray=(e=[],r=ArrayElement)=>e.reduce(((e,t)=>{const a=r.make(t,r);return e.head.length?(e.head.push(a),e.tail=a,e):{head:[a],tail:a}}),{head:[],tail:null});
|
|
@@ -11,14 +11,26 @@ import { IsElement } from '../../recipes/IsElement';
|
|
|
11
11
|
* Arrayable represents a collection stored as an array.
|
|
12
12
|
*/
|
|
13
13
|
export declare class Arrayable implements IsArrayable<ArrayElement>, Iterable<ArrayElement> {
|
|
14
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
14
15
|
readonly classType: typeof Arrayable;
|
|
16
|
+
/** The array which stores the elements of this Arrayable. */
|
|
15
17
|
innerList: Array<ArrayElement>;
|
|
18
|
+
/** Whether the inner list has been initialized (it can only be initialized once). */
|
|
16
19
|
initialized: boolean;
|
|
20
|
+
/** The class used to wrap the data given to this Arrayable as elements. */
|
|
17
21
|
elementClass: typeof ArrayElement;
|
|
18
22
|
/**
|
|
19
23
|
* Create the new Arrayable instance, configure the Arrayable class.
|
|
24
|
+
* @param {ArrayElement} [elementClass=ArrayElement] The class used to wrap given data as elements.
|
|
20
25
|
*/
|
|
21
26
|
constructor(elementClass?: typeof ArrayElement);
|
|
27
|
+
/**
|
|
28
|
+
* Find the position of an element which must be in this list.
|
|
29
|
+
* @param {ArrayElement} node The element to find
|
|
30
|
+
* @returns {number}
|
|
31
|
+
* @throws {Error} When the element is not in this list
|
|
32
|
+
*/
|
|
33
|
+
private indexOfElement;
|
|
22
34
|
/**
|
|
23
35
|
* Initialize the inner list, should only run once.
|
|
24
36
|
* @param {Array<ArrayElement>} initialList Give the array of elements to start in this Arrayable.
|
|
@@ -26,20 +38,20 @@ export declare class Arrayable implements IsArrayable<ArrayElement>, Iterable<Ar
|
|
|
26
38
|
*/
|
|
27
39
|
initialize(initialList: Array<ArrayElement>): Arrayable;
|
|
28
40
|
/**
|
|
29
|
-
* Retrieve
|
|
41
|
+
* Retrieve the innerList used (the list itself, not a copy).
|
|
30
42
|
* @returns {Array<ArrayElement>}
|
|
31
43
|
*/
|
|
32
44
|
get list(): Array<ArrayElement>;
|
|
33
45
|
/**
|
|
34
46
|
* Retrieve the first Element from the Arrayable
|
|
35
|
-
* @returns {ArrayElement}
|
|
47
|
+
* @returns {ArrayElement|null} The first element, or null when the Arrayable is empty
|
|
36
48
|
*/
|
|
37
|
-
get first(): ArrayElement;
|
|
49
|
+
get first(): ArrayElement | null;
|
|
38
50
|
/**
|
|
39
51
|
* Retrieve the last Element from the Arrayable
|
|
40
|
-
* @returns {ArrayElement}
|
|
52
|
+
* @returns {ArrayElement|null} The last element, or null when the Arrayable is empty
|
|
41
53
|
*/
|
|
42
|
-
get last(): ArrayElement;
|
|
54
|
+
get last(): ArrayElement | null;
|
|
43
55
|
/**
|
|
44
56
|
* Return the length of the list.
|
|
45
57
|
* @returns {number}
|
|
@@ -47,38 +59,40 @@ export declare class Arrayable implements IsArrayable<ArrayElement>, Iterable<Ar
|
|
|
47
59
|
get length(): number;
|
|
48
60
|
/**
|
|
49
61
|
* Insert a new node (or data) after a node.
|
|
50
|
-
* @param {ArrayElement
|
|
62
|
+
* @param {ArrayElement|null} node The existing node as reference, or null to insert at the start of the list
|
|
51
63
|
* @param {ArrayElement|*} newNode The new node to go after the existing node
|
|
52
64
|
* @returns {Arrayable}
|
|
65
|
+
* @throws {Error} When the reference node is not in this list
|
|
53
66
|
*/
|
|
54
|
-
insertAfter(node: ArrayElement, newNode: ArrayElement | any): Arrayable;
|
|
67
|
+
insertAfter(node: ArrayElement | null, newNode: ArrayElement | any): Arrayable;
|
|
55
68
|
/**
|
|
56
69
|
* Insert a new node (or data) before a node.
|
|
57
|
-
* @param {ArrayElement
|
|
70
|
+
* @param {ArrayElement|null} node The existing node as reference, or null to insert at the end of the list
|
|
58
71
|
* @param {ArrayElement|*} newNode The new node to go before the existing node
|
|
59
72
|
* @returns {Arrayable}
|
|
73
|
+
* @throws {Error} When the reference node is not in this list
|
|
60
74
|
*/
|
|
61
|
-
insertBefore(node: ArrayElement, newNode: ArrayElement | any): Arrayable;
|
|
75
|
+
insertBefore(node: ArrayElement | null, newNode: ArrayElement | any): Arrayable;
|
|
62
76
|
/**
|
|
63
77
|
* Add a node (or data) after the given (or last) node in the list.
|
|
64
78
|
* @param {ArrayElement|*} node The new node to add to the end of the list
|
|
65
79
|
* @param {ArrayElement} after The existing last node
|
|
66
80
|
* @returns {Arrayable}
|
|
67
81
|
*/
|
|
68
|
-
append(node: ArrayElement | any, after?: ArrayElement): Arrayable;
|
|
82
|
+
append(node: ArrayElement | any, after?: ArrayElement | null): Arrayable;
|
|
69
83
|
/**
|
|
70
84
|
* Add a node (or data) before the given (or first) node in the list.
|
|
71
85
|
* @param {ArrayElement|*} node The new node to add to the start of the list
|
|
72
86
|
* @param {ArrayElement} before The existing first node
|
|
73
87
|
* @returns {Arrayable}
|
|
74
88
|
*/
|
|
75
|
-
prepend(node: ArrayElement | any, before?: ArrayElement): Arrayable;
|
|
89
|
+
prepend(node: ArrayElement | any, before?: ArrayElement | null): Arrayable;
|
|
76
90
|
/**
|
|
77
91
|
* Remove an element from this arrayable.
|
|
78
92
|
* @param {ArrayElement} node The node we wish to remove (and it will be returned after removal)
|
|
79
|
-
* @return {ArrayElement}
|
|
93
|
+
* @return {ArrayElement|null} The removed node, or null when it was not in this list (nothing is removed)
|
|
80
94
|
*/
|
|
81
|
-
remove(node: ArrayElement): ArrayElement;
|
|
95
|
+
remove(node: ArrayElement): ArrayElement | null;
|
|
82
96
|
/**
|
|
83
97
|
* Retrieve an ArrayElement item from this list by numeric index, otherwise return null.
|
|
84
98
|
* @param {number} index The integer number for retrieving a node by position.
|
|
@@ -19,14 +19,32 @@ var _ArrayIterator = require('../../recipes/ArrayIterator')
|
|
|
19
19
|
class Arrayable {
|
|
20
20
|
/**
|
|
21
21
|
* Create the new Arrayable instance, configure the Arrayable class.
|
|
22
|
+
* @param {ArrayElement} [elementClass=ArrayElement] The class used to wrap given data as elements.
|
|
22
23
|
*/
|
|
23
24
|
constructor (elementClass = _ArrayElement.ArrayElement) {
|
|
25
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
24
26
|
this.classType = Arrayable
|
|
27
|
+
/** The array which stores the elements of this Arrayable. */
|
|
25
28
|
this.innerList = []
|
|
29
|
+
/** Whether the inner list has been initialized (it can only be initialized once). */
|
|
26
30
|
this.initialized = false
|
|
27
31
|
this.elementClass = elementClass
|
|
28
32
|
}
|
|
29
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Find the position of an element which must be in this list.
|
|
36
|
+
* @param {ArrayElement} node The element to find
|
|
37
|
+
* @returns {number}
|
|
38
|
+
* @throws {Error} When the element is not in this list
|
|
39
|
+
*/
|
|
40
|
+
indexOfElement (node) {
|
|
41
|
+
const index = this.innerList.indexOf(node)
|
|
42
|
+
if (index < 0) {
|
|
43
|
+
throw new Error('The reference element is not in this list.')
|
|
44
|
+
}
|
|
45
|
+
return index
|
|
46
|
+
}
|
|
47
|
+
|
|
30
48
|
/**
|
|
31
49
|
* Initialize the inner list, should only run once.
|
|
32
50
|
* @param {Array<ArrayElement>} initialList Give the array of elements to start in this Arrayable.
|
|
@@ -43,7 +61,7 @@ class Arrayable {
|
|
|
43
61
|
}
|
|
44
62
|
|
|
45
63
|
/**
|
|
46
|
-
* Retrieve
|
|
64
|
+
* Retrieve the innerList used (the list itself, not a copy).
|
|
47
65
|
* @returns {Array<ArrayElement>}
|
|
48
66
|
*/
|
|
49
67
|
get list () {
|
|
@@ -52,18 +70,18 @@ class Arrayable {
|
|
|
52
70
|
|
|
53
71
|
/**
|
|
54
72
|
* Retrieve the first Element from the Arrayable
|
|
55
|
-
* @returns {ArrayElement}
|
|
73
|
+
* @returns {ArrayElement|null} The first element, or null when the Arrayable is empty
|
|
56
74
|
*/
|
|
57
75
|
get first () {
|
|
58
|
-
return this.innerList[0]
|
|
76
|
+
return this.length ? this.innerList[0] : null
|
|
59
77
|
}
|
|
60
78
|
|
|
61
79
|
/**
|
|
62
80
|
* Retrieve the last Element from the Arrayable
|
|
63
|
-
* @returns {ArrayElement}
|
|
81
|
+
* @returns {ArrayElement|null} The last element, or null when the Arrayable is empty
|
|
64
82
|
*/
|
|
65
83
|
get last () {
|
|
66
|
-
return this.innerList[this.length - 1]
|
|
84
|
+
return this.length ? this.innerList[this.length - 1] : null
|
|
67
85
|
}
|
|
68
86
|
|
|
69
87
|
/**
|
|
@@ -76,25 +94,29 @@ class Arrayable {
|
|
|
76
94
|
|
|
77
95
|
/**
|
|
78
96
|
* Insert a new node (or data) after a node.
|
|
79
|
-
* @param {ArrayElement
|
|
97
|
+
* @param {ArrayElement|null} node The existing node as reference, or null to insert at the start of the list
|
|
80
98
|
* @param {ArrayElement|*} newNode The new node to go after the existing node
|
|
81
99
|
* @returns {Arrayable}
|
|
100
|
+
* @throws {Error} When the reference node is not in this list
|
|
82
101
|
*/
|
|
83
102
|
insertAfter (node, newNode) {
|
|
84
|
-
|
|
85
|
-
|
|
103
|
+
// With no reference element, the new one goes after nothing: at the start of the list
|
|
104
|
+
const insertAt = node === null || typeof node === 'undefined' ? -1 : this.indexOfElement(node)
|
|
105
|
+
this.innerList.splice(insertAt + 1, 0, this.elementClass.make(newNode, this.elementClass))
|
|
86
106
|
return this
|
|
87
107
|
}
|
|
88
108
|
|
|
89
109
|
/**
|
|
90
110
|
* Insert a new node (or data) before a node.
|
|
91
|
-
* @param {ArrayElement
|
|
111
|
+
* @param {ArrayElement|null} node The existing node as reference, or null to insert at the end of the list
|
|
92
112
|
* @param {ArrayElement|*} newNode The new node to go before the existing node
|
|
93
113
|
* @returns {Arrayable}
|
|
114
|
+
* @throws {Error} When the reference node is not in this list
|
|
94
115
|
*/
|
|
95
116
|
insertBefore (node, newNode) {
|
|
96
|
-
|
|
97
|
-
this.
|
|
117
|
+
// With no reference element, the new one goes before nothing: at the end of the list
|
|
118
|
+
const insertAt = node === null || typeof node === 'undefined' ? this.length : this.indexOfElement(node)
|
|
119
|
+
this.innerList.splice(insertAt, 0, this.elementClass.make(newNode, this.elementClass))
|
|
98
120
|
return this
|
|
99
121
|
}
|
|
100
122
|
|
|
@@ -105,6 +127,11 @@ class Arrayable {
|
|
|
105
127
|
* @returns {Arrayable}
|
|
106
128
|
*/
|
|
107
129
|
append (node, after = this.last) {
|
|
130
|
+
if (after === this.last) {
|
|
131
|
+
// Adding to the end does not need to search for where that is
|
|
132
|
+
this.innerList.push(this.elementClass.make(node, this.elementClass))
|
|
133
|
+
return this
|
|
134
|
+
}
|
|
108
135
|
return this.insertAfter(after, node)
|
|
109
136
|
}
|
|
110
137
|
|
|
@@ -115,16 +142,24 @@ class Arrayable {
|
|
|
115
142
|
* @returns {Arrayable}
|
|
116
143
|
*/
|
|
117
144
|
prepend (node, before = this.first) {
|
|
145
|
+
if (before === this.first) {
|
|
146
|
+
// Adding to the start does not need to search for where that is
|
|
147
|
+
this.innerList.unshift(this.elementClass.make(node, this.elementClass))
|
|
148
|
+
return this
|
|
149
|
+
}
|
|
118
150
|
return this.insertBefore(before, node)
|
|
119
151
|
}
|
|
120
152
|
|
|
121
153
|
/**
|
|
122
154
|
* Remove an element from this arrayable.
|
|
123
155
|
* @param {ArrayElement} node The node we wish to remove (and it will be returned after removal)
|
|
124
|
-
* @return {ArrayElement}
|
|
156
|
+
* @return {ArrayElement|null} The removed node, or null when it was not in this list (nothing is removed)
|
|
125
157
|
*/
|
|
126
158
|
remove (node) {
|
|
127
159
|
const deleteAt = this.innerList.indexOf(node)
|
|
160
|
+
if (deleteAt < 0) {
|
|
161
|
+
return null
|
|
162
|
+
}
|
|
128
163
|
this.innerList.splice(deleteAt, 1)
|
|
129
164
|
return node
|
|
130
165
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Arrayable=void 0;var _ArrayElement=require("./ArrayElement"),_ArrayIterator=require("../../recipes/ArrayIterator");class Arrayable{constructor(
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Arrayable=void 0;var _ArrayElement=require("./ArrayElement"),_ArrayIterator=require("../../recipes/ArrayIterator");class Arrayable{constructor(e=_ArrayElement.ArrayElement){this.classType=Arrayable,this.innerList=[],this.initialized=!1,this.elementClass=e}indexOfElement(e){const t=this.innerList.indexOf(e);if(t<0)throw new Error("The reference element is not in this list.");return t}initialize(e){return this.initialized?(console.warn("Attempt to initialize non-empty list."),this):(this.initialized=!0,this.innerList=e,this)}get list(){return this.innerList}get first(){return this.length?this.innerList[0]:null}get last(){return this.length?this.innerList[this.length-1]:null}get length(){return this.innerList.length}insertAfter(e,t){const i=null==e?-1:this.indexOfElement(e);return this.innerList.splice(i+1,0,this.elementClass.make(t,this.elementClass)),this}insertBefore(e,t){const i=null==e?this.length:this.indexOfElement(e);return this.innerList.splice(i,0,this.elementClass.make(t,this.elementClass)),this}append(e,t=this.last){return t===this.last?(this.innerList.push(this.elementClass.make(e,this.elementClass)),this):this.insertAfter(t,e)}prepend(e,t=this.first){return t===this.first?(this.innerList.unshift(this.elementClass.make(e,this.elementClass)),this):this.insertBefore(t,e)}remove(e){const t=this.innerList.indexOf(e);return t<0?null:(this.innerList.splice(t,1),e)}item(e){if(e>=this.length)return null;if(e>=0)return this.innerList[e];const t=this.length+e;return t<0?null:this.innerList[t]}forEach(e,t=this){for(let i=0;i<t.length;++i)e(t.item(i),i,t);return t}[Symbol.iterator](){return new _ArrayIterator.ArrayIterator(this.innerList,0)}}exports.Arrayable=Arrayable,Arrayable.fromArray=(e=[],t=_ArrayElement.ArrayElement,i=Arrayable)=>new i(t).initialize(t.fromArray(e).head);
|
|
@@ -10,13 +10,17 @@ import { IsDoubleLinker } from '../../recipes/IsDoubleLinker';
|
|
|
10
10
|
* @extends Linker
|
|
11
11
|
*/
|
|
12
12
|
export declare class DoubleLinker implements IsDoubleLinker {
|
|
13
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
13
14
|
readonly classType: typeof DoubleLinker;
|
|
15
|
+
/** The data stored in this linker. */
|
|
14
16
|
data: any;
|
|
17
|
+
/** The linker after this one, or null when this is the last. */
|
|
15
18
|
next: DoubleLinker | null;
|
|
19
|
+
/** The linker before this one, or null when this is the first. */
|
|
16
20
|
prev: DoubleLinker | null;
|
|
17
21
|
/**
|
|
18
22
|
* Create the new DoubleLinker instance, provide the data and optionally the next and prev references.
|
|
19
|
-
* @param {Object} [nodeData={}]
|
|
23
|
+
* @param {Object} [nodeData={}] The settings for the new linker.
|
|
20
24
|
* @param {*} [nodeData.data=null] The data to be stored in this linker
|
|
21
25
|
* @param {DoubleLinker|null} [nodeData.next=null] The reference to the next linker if any
|
|
22
26
|
* @param {DoubleLinker|null} [nodeData.prev=null] The reference to the previous linker if any
|
|
@@ -14,7 +14,7 @@ var _Linker = require('../linked-list/Linker')
|
|
|
14
14
|
class DoubleLinker {
|
|
15
15
|
/**
|
|
16
16
|
* Create the new DoubleLinker instance, provide the data and optionally the next and prev references.
|
|
17
|
-
* @param {Object} [nodeData={}]
|
|
17
|
+
* @param {Object} [nodeData={}] The settings for the new linker.
|
|
18
18
|
* @param {*} [nodeData.data=null] The data to be stored in this linker
|
|
19
19
|
* @param {DoubleLinker|null} [nodeData.next=null] The reference to the next linker if any
|
|
20
20
|
* @param {DoubleLinker|null} [nodeData.prev=null] The reference to the previous linker if any
|
|
@@ -24,9 +24,13 @@ class DoubleLinker {
|
|
|
24
24
|
next = null,
|
|
25
25
|
prev = null
|
|
26
26
|
} = {}) {
|
|
27
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
27
28
|
this.classType = DoubleLinker
|
|
29
|
+
/** The data stored in this linker. */
|
|
28
30
|
this.data = null
|
|
31
|
+
/** The linker after this one, or null when this is the last. */
|
|
29
32
|
this.next = null
|
|
33
|
+
/** The linker before this one, or null when this is the first. */
|
|
30
34
|
this.prev = null
|
|
31
35
|
this.data = data
|
|
32
36
|
this.next = next
|
|
@@ -12,12 +12,21 @@ import { IsDoubleLinker } from '../../recipes/IsDoubleLinker';
|
|
|
12
12
|
* @extends LinkedList
|
|
13
13
|
*/
|
|
14
14
|
export declare class DoublyLinkedList implements IsArrayable<DoubleLinker>, Iterable<DoubleLinker> {
|
|
15
|
+
/** The class used to create this instance, so that it can be recognized as valid without an instanceof check. */
|
|
15
16
|
readonly classType: typeof DoublyLinkedList;
|
|
17
|
+
/** A linker of the list (null when the list is empty); the head is found by walking back from it. */
|
|
16
18
|
innerList: DoubleLinker;
|
|
19
|
+
/** Whether the inner list has been initialized (it can only be initialized once). */
|
|
17
20
|
initialized: boolean;
|
|
21
|
+
/** The class used to wrap the data given to this list as linkers. */
|
|
18
22
|
linkerClass: typeof DoubleLinker;
|
|
23
|
+
/** The last linker, remembered so that adding to the end does not need to walk the whole list (null when not known yet). */
|
|
24
|
+
private tailCache;
|
|
25
|
+
/** The number of linkers, kept up to date by the list's own methods so that the length does not need to walk the whole list (null when not known yet). */
|
|
26
|
+
private countCache;
|
|
19
27
|
/**
|
|
20
28
|
* Create the new DoublyLinkedList instance.
|
|
29
|
+
* @param {DoubleLinker} [linkerClass=DoubleLinker] The class used to wrap given data as linkers.
|
|
21
30
|
*/
|
|
22
31
|
constructor(linkerClass?: typeof DoubleLinker);
|
|
23
32
|
/**
|
|
@@ -27,7 +36,7 @@ export declare class DoublyLinkedList implements IsArrayable<DoubleLinker>, Iter
|
|
|
27
36
|
*/
|
|
28
37
|
initialize(initialList: DoubleLinker): DoublyLinkedList;
|
|
29
38
|
/**
|
|
30
|
-
* Retrieve
|
|
39
|
+
* Retrieve the innerList used (the list itself, not a copy).
|
|
31
40
|
* @returns {DoubleLinker}
|
|
32
41
|
*/
|
|
33
42
|
get list(): DoubleLinker;
|
|
@@ -35,31 +44,32 @@ export declare class DoublyLinkedList implements IsArrayable<DoubleLinker>, Iter
|
|
|
35
44
|
* Retrieve the first DoubleLinker in the list.
|
|
36
45
|
* @returns {DoubleLinker}
|
|
37
46
|
*/
|
|
38
|
-
get first(): DoubleLinker;
|
|
47
|
+
get first(): DoubleLinker | null;
|
|
39
48
|
/**
|
|
40
|
-
* Retrieve the last DoubleLinker in the list.
|
|
49
|
+
* Retrieve the last DoubleLinker in the list. The end is remembered, so this does not walk the list.
|
|
41
50
|
* @returns {DoubleLinker}
|
|
42
51
|
*/
|
|
43
|
-
get last(): DoubleLinker;
|
|
52
|
+
get last(): DoubleLinker | null;
|
|
44
53
|
/**
|
|
45
|
-
* Return the length of the list.
|
|
54
|
+
* Return the length of the list. It is kept up to date by the list's own methods, so this does not walk the list
|
|
55
|
+
* (call reset() after linkers were changed directly).
|
|
46
56
|
* @returns {number}
|
|
47
57
|
*/
|
|
48
58
|
get length(): number;
|
|
49
59
|
/**
|
|
50
60
|
* Insert a new node (or data) after a node.
|
|
51
|
-
* @param {DoubleLinker|*} node The existing node as reference
|
|
61
|
+
* @param {DoubleLinker|*} node The existing node as reference (which must be in this list, this is not checked), or null to insert at the start of the list
|
|
52
62
|
* @param {DoubleLinker|*} newNode The new node to go after the existing node
|
|
53
63
|
* @returns {DoublyLinkedList}
|
|
54
64
|
*/
|
|
55
|
-
insertAfter(node: DoubleLinker, newNode: DoubleLinker | any): DoublyLinkedList;
|
|
65
|
+
insertAfter(node: DoubleLinker | null, newNode: DoubleLinker | any): DoublyLinkedList;
|
|
56
66
|
/**
|
|
57
67
|
* Insert a new node (or data) before a node.
|
|
58
|
-
* @param {DoubleLinker|*} node The existing node as reference
|
|
68
|
+
* @param {DoubleLinker|*} node The existing node as reference (which must be in this list, this is not checked), or null to insert at the end of the list
|
|
59
69
|
* @param {DoubleLinker|*} newNode The new node to go before the existing node
|
|
60
70
|
* @returns {DoublyLinkedList}
|
|
61
71
|
*/
|
|
62
|
-
insertBefore(node: DoubleLinker, newNode: DoubleLinker | any): DoublyLinkedList;
|
|
72
|
+
insertBefore(node: DoubleLinker | null, newNode: DoubleLinker | any): DoublyLinkedList;
|
|
63
73
|
/**
|
|
64
74
|
* Add a node (or data) after the given (or last) node in the list.
|
|
65
75
|
* @param {DoubleLinker|*} node The new node to add to the end of the list
|
|
@@ -79,12 +89,13 @@ export declare class DoublyLinkedList implements IsArrayable<DoubleLinker>, Iter
|
|
|
79
89
|
* @param {DoubleLinker} node The node we wish to remove (and it will be returned after removal)
|
|
80
90
|
* @return {DoubleLinker}
|
|
81
91
|
*/
|
|
82
|
-
remove(node: DoubleLinker): DoubleLinker;
|
|
92
|
+
remove(node: DoubleLinker | null): DoubleLinker | null;
|
|
83
93
|
/**
|
|
84
|
-
* Refresh all references and return head
|
|
85
|
-
*
|
|
94
|
+
* Refresh all references (the head, the end and the length) by walking the list once, and return the head. The list's
|
|
95
|
+
* own methods keep these up to date, so this is only needed after linkers were changed directly.
|
|
96
|
+
* @return {DoubleLinker|null}
|
|
86
97
|
*/
|
|
87
|
-
reset(): DoubleLinker;
|
|
98
|
+
reset(): DoubleLinker | null;
|
|
88
99
|
/**
|
|
89
100
|
* Retrieve a DoubleLinker item from this list by numeric index, otherwise return null.
|
|
90
101
|
* @param {number} index The integer number for retrieving a node by position.
|
|
@@ -95,6 +106,7 @@ export declare class DoublyLinkedList implements IsArrayable<DoubleLinker>, Iter
|
|
|
95
106
|
* Be able to run forEach on this DoublyLinkedList to iterate over the DoubleLinker Items.
|
|
96
107
|
* @param {forEachCallback} callback The function to call for-each double linker
|
|
97
108
|
* @param {DoublyLinkedList} thisArg Optional, 'this' reference
|
|
109
|
+
* @return {DoublyLinkedList} The list which was iterated.
|
|
98
110
|
*/
|
|
99
111
|
forEach(callback: forEachCallback, thisArg?: DoublyLinkedList): DoublyLinkedList;
|
|
100
112
|
/**
|