collect-your-stuff 1.4.0 → 2.1.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.
Files changed (35) hide show
  1. package/dist/collections/linked-tree-list/LinkedTreeList.d.ts +31 -19
  2. package/dist/collections/linked-tree-list/LinkedTreeList.js +63 -24
  3. package/dist/collections/linked-tree-list/LinkedTreeList.min.js +1 -1
  4. package/dist/collections/linked-tree-list/TreeLinker.d.ts +3 -1
  5. package/dist/collections/linked-tree-list/TreeLinker.js +14 -5
  6. package/dist/collections/linked-tree-list/TreeLinker.min.js +1 -1
  7. package/dist/collections/queue/Queue.d.ts +35 -42
  8. package/dist/collections/queue/Queue.js +64 -80
  9. package/dist/collections/queue/Queue.min.js +1 -1
  10. package/dist/collections/queue/TaskQueue.d.ts +68 -0
  11. package/dist/collections/queue/TaskQueue.js +137 -0
  12. package/dist/collections/queue/TaskQueue.min.js +1 -0
  13. package/dist/collections/stack/Stack.d.ts +40 -40
  14. package/dist/collections/stack/Stack.js +73 -52
  15. package/dist/collections/stack/Stack.min.js +1 -1
  16. package/dist/collections/stack/TaskStack.d.ts +65 -0
  17. package/dist/collections/stack/TaskStack.js +107 -0
  18. package/dist/collections/stack/TaskStack.min.js +1 -0
  19. package/dist/main.d.ts +8 -2
  20. package/dist/main.js +16 -0
  21. package/dist/main.min.js +1 -1
  22. package/dist/recipes/IsQueue.d.ts +37 -0
  23. package/dist/recipes/IsQueue.js +5 -0
  24. package/dist/recipes/IsQueue.min.js +1 -0
  25. package/dist/recipes/IsStack.d.ts +36 -0
  26. package/dist/recipes/IsStack.js +5 -0
  27. package/dist/recipes/IsStack.min.js +1 -0
  28. package/dist/recipes/TreeLinkerIterator.d.ts +3 -1
  29. package/dist/recipes/TreeLinkerIterator.js +4 -2
  30. package/dist/recipes/TreeLinkerIterator.min.js +1 -1
  31. package/dist/services/parseTreeNext.d.ts +4 -1
  32. package/dist/services/parseTreeNext.js +14 -19
  33. package/dist/services/parseTreeNext.min.js +1 -1
  34. package/dist/services/services.d.ts +1 -1
  35. package/package.json +1 -1
@@ -25,6 +25,8 @@ export declare class LinkedTreeList implements IsTree, Iterable<TreeLinker> {
25
25
  private tailCache;
26
26
  /** 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). */
27
27
  private countCache;
28
+ /** The node these linkers are the children of, remembered so that it is known even while the list is empty (undefined until it is known). */
29
+ private ownerNode;
28
30
  /**
29
31
  * Create the new LinkedTreeList instance, configure the list class.
30
32
  * @param {TreeLinker} [linkerClass=TreeLinker] The class used to wrap given data as tree linkers.
@@ -58,15 +60,17 @@ export declare class LinkedTreeList implements IsTree, Iterable<TreeLinker> {
58
60
  */
59
61
  get length(): number;
60
62
  /**
61
- * Get the parent of this tree list.
62
- * @return {TreeLinker}
63
+ * Get the parent of this tree list: the node these linkers are the children of (remembered even while the list is
64
+ * empty), or null for the linkers at the top of a tree.
65
+ * @return {TreeLinker|null}
63
66
  */
64
- get parent(): TreeLinker;
67
+ get parent(): TreeLinker | null;
65
68
  /**
66
- * Set the parent of this tree list
67
- * @param {TreeLinker} parent The new node to use as the parent for this group of children
69
+ * Set the parent of this tree list: every linker in it gets the node as its parent, and the node gets this list as its
70
+ * children. Linkers added to the list later get this parent too.
71
+ * @param {TreeLinker|null} parent The new node to use as the parent for this group of children
68
72
  */
69
- set parent(parent: TreeLinker);
73
+ set parent(parent: TreeLinker | null);
70
74
  /**
71
75
  * Return the root parent of the entire tree.
72
76
  * @return {TreeLinker}
@@ -74,24 +78,31 @@ export declare class LinkedTreeList implements IsTree, Iterable<TreeLinker> {
74
78
  get rootParent(): TreeLinker;
75
79
  /**
76
80
  * Set the children on a parent item.
77
- * @param {TreeLinker} item The TreeLinker node that will be the parent of the children
78
- * @param {LinkedTreeList} children The LinkedTreeList which has the child nodes to use
81
+ * @param {TreeLinker} item The TreeLinker node (one of the linkers of this list) that will be the parent of the children
82
+ * @param {LinkedTreeList|null} [children=null] The LinkedTreeList which has the child nodes to use, or null to remove the children of the item
83
+ * @throws {Error} When the item is not one of the linkers of this list
79
84
  */
80
- setChildren(item: TreeLinker, children?: LinkedTreeList): void;
85
+ setChildren(item: TreeLinker, children?: LinkedTreeList | null): void;
81
86
  /**
82
- * Insert a new node (or data) after a node.
83
- * @param {TreeLinker|*} node The existing node as reference
87
+ * Make a linker of the given node (or data) and make this list's parent its parent.
88
+ * @param {TreeLinker|*} newNode The node (or data) which is being added to this list
89
+ * @returns {TreeLinker}
90
+ */
91
+ private adopt;
92
+ /**
93
+ * Insert a new node (or data) after a node. The new node gets the parent of this list.
94
+ * @param {TreeLinker|*} node The existing node as reference, or null to insert at the start of the list
84
95
  * @param {TreeLinker|*} newNode The new node to go after the existing node
85
96
  * @returns {LinkedTreeList}
86
97
  */
87
- insertAfter(node: TreeLinker, newNode: TreeLinker | any): LinkedTreeList;
98
+ insertAfter(node: TreeLinker | null, newNode: TreeLinker | any): LinkedTreeList;
88
99
  /**
89
- * Insert a new node (or data) before a node.
90
- * @param {TreeLinker|*} node The existing node as reference
100
+ * Insert a new node (or data) before a node. The new node gets the parent of this list.
101
+ * @param {TreeLinker|*} node The existing node as reference, or null to insert at the end of the list
91
102
  * @param {TreeLinker|*} newNode The new node to go before the existing node
92
103
  * @returns {LinkedTreeList}
93
104
  */
94
- insertBefore(node: TreeLinker, newNode: TreeLinker | any): LinkedTreeList;
105
+ insertBefore(node: TreeLinker | null, newNode: TreeLinker | any): LinkedTreeList;
95
106
  /**
96
107
  * Add a node (or data) after the given (or last) node in the list.
97
108
  * @param {TreeLinker|*} node The new node to add to the end of the list
@@ -107,11 +118,11 @@ export declare class LinkedTreeList implements IsTree, Iterable<TreeLinker> {
107
118
  */
108
119
  prepend(node: TreeLinker | any, before?: TreeLinker): LinkedTreeList;
109
120
  /**
110
- * Remove a linker from this linked list.
121
+ * Remove a linker from this linked list. The removed node no longer has a parent.
111
122
  * @param {TreeLinker} node The node we wish to remove (and it will be returned after removal)
112
- * @return {TreeLinker}
123
+ * @return {TreeLinker|null} The removed node, or null when there was nothing to remove
113
124
  */
114
- remove(node: TreeLinker): TreeLinker;
125
+ remove(node: TreeLinker): TreeLinker | null;
115
126
  /**
116
127
  * Refresh all references (the head, the end and the length) by walking the list once, and return the head. The
117
128
  * list's own methods keep these up to date, so this is only needed after linkers were changed directly.
@@ -132,7 +143,8 @@ export declare class LinkedTreeList implements IsTree, Iterable<TreeLinker> {
132
143
  */
133
144
  forEach(callback: forEachCallback, thisArg?: LinkedTreeList): LinkedTreeList;
134
145
  /**
135
- * Be able to iterate over this class.
146
+ * Be able to iterate over this class: the linkers of this list and everything below them (left-first). It stays within
147
+ * this list (it does not start at, or climb up to, the parents), use the parseTree service to parse a whole tree.
136
148
  * @returns {Iterator}
137
149
  */
138
150
  [Symbol.iterator](): Iterator<TreeLinker>;
@@ -4,6 +4,8 @@ Object.defineProperty(exports, '__esModule', {
4
4
  value: true
5
5
  })
6
6
  exports.LinkedTreeList = void 0
7
+ require('core-js/modules/esnext.iterator.constructor.js')
8
+ require('core-js/modules/esnext.iterator.for-each.js')
7
9
  var _TreeLinker = require('./TreeLinker')
8
10
  var _TreeLinkerIterator = require('../../recipes/TreeLinkerIterator')
9
11
  var _DoublyLinkedList = require('../doubly-linked-list/DoublyLinkedList')
@@ -41,6 +43,8 @@ class LinkedTreeList {
41
43
  this.tailCache = null
42
44
  /** 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). */
43
45
  this.countCache = null
46
+ /** The node these linkers are the children of, remembered so that it is known even while the list is empty (undefined until it is known). */
47
+ this.ownerNode = undefined
44
48
  this.linkerClass = linkerClass
45
49
  }
46
50
 
@@ -93,22 +97,25 @@ class LinkedTreeList {
93
97
  }
94
98
 
95
99
  /**
96
- * Get the parent of this tree list.
97
- * @return {TreeLinker}
100
+ * Get the parent of this tree list: the node these linkers are the children of (remembered even while the list is
101
+ * empty), or null for the linkers at the top of a tree.
102
+ * @return {TreeLinker|null}
98
103
  */
99
104
  get parent () {
100
- const first = this.first
101
- if (first === null) {
102
- return null
105
+ if (this.ownerNode !== undefined) {
106
+ return this.ownerNode
103
107
  }
104
- return this.first.parent
108
+ const first = this.first
109
+ return first === null ? null : first.parent
105
110
  }
106
111
 
107
112
  /**
108
- * Set the parent of this tree list
109
- * @param {TreeLinker} parent The new node to use as the parent for this group of children
113
+ * Set the parent of this tree list: every linker in it gets the node as its parent, and the node gets this list as its
114
+ * children. Linkers added to the list later get this parent too.
115
+ * @param {TreeLinker|null} parent The new node to use as the parent for this group of children
110
116
  */
111
117
  set parent (parent) {
118
+ this.ownerNode = parent
112
119
  let current = this.first
113
120
  while (current !== null) {
114
121
  current.parent = parent
@@ -138,34 +145,57 @@ class LinkedTreeList {
138
145
 
139
146
  /**
140
147
  * Set the children on a parent item.
141
- * @param {TreeLinker} item The TreeLinker node that will be the parent of the children
142
- * @param {LinkedTreeList} children The LinkedTreeList which has the child nodes to use
148
+ * @param {TreeLinker} item The TreeLinker node (one of the linkers of this list) that will be the parent of the children
149
+ * @param {LinkedTreeList|null} [children=null] The LinkedTreeList which has the child nodes to use, or null to remove the children of the item
150
+ * @throws {Error} When the item is not one of the linkers of this list
143
151
  */
144
152
  setChildren (item, children = null) {
145
- if (Array.from(this).indexOf(item) < 0) {
146
- console.error('item is not a child of this')
153
+ // The item must be one of the linkers of this list (only the siblings are checked, not the whole tree)
154
+ let isChild = false
155
+ this.forEach(linker => {
156
+ if (linker === item) {
157
+ isChild = true
158
+ }
159
+ })
160
+ if (!isChild) {
161
+ throw new Error('The item is not one of the linkers of this list.')
162
+ }
163
+ if (children === null || typeof children === 'undefined') {
164
+ item.children = null
165
+ return
147
166
  }
148
167
  children.parent = item
149
168
  }
150
169
 
151
170
  /**
152
- * Insert a new node (or data) after a node.
153
- * @param {TreeLinker|*} node The existing node as reference
171
+ * Make a linker of the given node (or data) and make this list's parent its parent.
172
+ * @param {TreeLinker|*} newNode The node (or data) which is being added to this list
173
+ * @returns {TreeLinker}
174
+ */
175
+ adopt (newNode) {
176
+ const linker = this.linkerClass.make(newNode, this.linkerClass)
177
+ linker.parent = this.parent
178
+ return linker
179
+ }
180
+
181
+ /**
182
+ * Insert a new node (or data) after a node. The new node gets the parent of this list.
183
+ * @param {TreeLinker|*} node The existing node as reference, or null to insert at the start of the list
154
184
  * @param {TreeLinker|*} newNode The new node to go after the existing node
155
185
  * @returns {LinkedTreeList}
156
186
  */
157
187
  insertAfter (node, newNode) {
158
- return _DoublyLinkedList.DoublyLinkedList.prototype.insertAfter.call(this, node, newNode)
188
+ return _DoublyLinkedList.DoublyLinkedList.prototype.insertAfter.call(this, node, this.adopt(newNode))
159
189
  }
160
190
 
161
191
  /**
162
- * Insert a new node (or data) before a node.
163
- * @param {TreeLinker|*} node The existing node as reference
192
+ * Insert a new node (or data) before a node. The new node gets the parent of this list.
193
+ * @param {TreeLinker|*} node The existing node as reference, or null to insert at the end of the list
164
194
  * @param {TreeLinker|*} newNode The new node to go before the existing node
165
195
  * @returns {LinkedTreeList}
166
196
  */
167
197
  insertBefore (node, newNode) {
168
- return _DoublyLinkedList.DoublyLinkedList.prototype.insertBefore.call(this, node, newNode)
198
+ return _DoublyLinkedList.DoublyLinkedList.prototype.insertBefore.call(this, node, this.adopt(newNode))
169
199
  }
170
200
 
171
201
  /**
@@ -189,12 +219,19 @@ class LinkedTreeList {
189
219
  }
190
220
 
191
221
  /**
192
- * Remove a linker from this linked list.
222
+ * Remove a linker from this linked list. The removed node no longer has a parent.
193
223
  * @param {TreeLinker} node The node we wish to remove (and it will be returned after removal)
194
- * @return {TreeLinker}
224
+ * @return {TreeLinker|null} The removed node, or null when there was nothing to remove
195
225
  */
196
226
  remove (node) {
197
- return _DoublyLinkedList.DoublyLinkedList.prototype.remove.call(this, node)
227
+ const owner = this.parent
228
+ const removed = _DoublyLinkedList.DoublyLinkedList.prototype.remove.call(this, node)
229
+ if (removed && removed.parent === owner) {
230
+ // Remember whose children these are (the list may now be empty), the removed node no longer has that parent
231
+ this.ownerNode = owner
232
+ removed.parent = null
233
+ }
234
+ return removed
198
235
  }
199
236
 
200
237
  /**
@@ -233,12 +270,14 @@ class LinkedTreeList {
233
270
  }
234
271
 
235
272
  /**
236
- * Be able to iterate over this class.
273
+ * Be able to iterate over this class: the linkers of this list and everything below them (left-first). It stays within
274
+ * this list (it does not start at, or climb up to, the parents), use the parseTree service to parse a whole tree.
237
275
  * @returns {Iterator}
238
276
  */
239
277
  [Symbol.iterator] () {
240
- const root = this.rootParent
241
- return new _TreeLinkerIterator.TreeLinkerIterator(root)
278
+ // The linkers of this list and everything below them, left-first. It stays within this list: it does not start at,
279
+ // or climb up to, the parents (use the parseTree service to parse a whole tree)
280
+ return new _TreeLinkerIterator.TreeLinkerIterator(this.first, this.parent)
242
281
  }
243
282
  }
244
283
  /**
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LinkedTreeList=void 0;var _TreeLinker=require("./TreeLinker"),_TreeLinkerIterator=require("../../recipes/TreeLinkerIterator"),_DoublyLinkedList=require("../doubly-linked-list/DoublyLinkedList");const borrowedGetter=(e,t)=>Object.getOwnPropertyDescriptor(_DoublyLinkedList.DoublyLinkedList.prototype,e).get.call(t);class LinkedTreeList{constructor(e=_TreeLinker.TreeLinker){this.classType=LinkedTreeList,this.innerList=null,this.initialized=!1,this.tailCache=null,this.countCache=null,this.linkerClass=e}initialize(e){return this.initialized?(console.warn("Attempt to initialize LinkedTreeList which is not empty."),this):(this.initialized=!0,this.innerList=e,this)}get list(){return this.innerList}get first(){return borrowedGetter("first",this)}get last(){return borrowedGetter("last",this)}get length(){return borrowedGetter("length",this)}get parent(){return null===this.first?null:this.first.parent}set parent(e){let t=this.first;for(;null!==t;)t.parent=e,t=t.next;e&&(e.children=this)}get rootParent(){let e=this.first;if(!e)return null;let t=this.first.parent;for(;null!==t;)e=t,t=e.parent;return e}setChildren(e,t=null){Array.from(this).indexOf(e)<0&&console.error("item is not a child of this"),t.parent=e}insertAfter(e,t){return _DoublyLinkedList.DoublyLinkedList.prototype.insertAfter.call(this,e,t)}insertBefore(e,t){return _DoublyLinkedList.DoublyLinkedList.prototype.insertBefore.call(this,e,t)}append(e,t=this.last){return _DoublyLinkedList.DoublyLinkedList.prototype.append.call(this,e,t)}prepend(e,t=this.first){return _DoublyLinkedList.DoublyLinkedList.prototype.prepend.call(this,e,t)}remove(e){return _DoublyLinkedList.DoublyLinkedList.prototype.remove.call(this,e)}reset(){return _DoublyLinkedList.DoublyLinkedList.prototype.reset.call(this)}item(e){return _DoublyLinkedList.DoublyLinkedList.prototype.item.call(this,e)}forEach(e,t=this){let r=0,i=t.first;for(;null!==i;)e(i,r,t),i=i.next,++r;return t}[Symbol.iterator](){let e=this.rootParent;return new _TreeLinkerIterator.TreeLinkerIterator(e)}}exports.LinkedTreeList=LinkedTreeList,LinkedTreeList.fromArray=(e=[],t=_TreeLinker.TreeLinker,r=LinkedTreeList)=>new r(t).initialize(t.fromArray(e).head);
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.LinkedTreeList=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.for-each.js");var _TreeLinker=require("./TreeLinker"),_TreeLinkerIterator=require("../../recipes/TreeLinkerIterator"),_DoublyLinkedList=require("../doubly-linked-list/DoublyLinkedList");const borrowedGetter=(e,t)=>Object.getOwnPropertyDescriptor(_DoublyLinkedList.DoublyLinkedList.prototype,e).get.call(t);class LinkedTreeList{constructor(e=_TreeLinker.TreeLinker){this.classType=LinkedTreeList,this.innerList=null,this.initialized=!1,this.tailCache=null,this.countCache=null,this.ownerNode=void 0,this.linkerClass=e}initialize(e){return this.initialized?(console.warn("Attempt to initialize LinkedTreeList which is not empty."),this):(this.initialized=!0,this.innerList=e,this)}get list(){return this.innerList}get first(){return borrowedGetter("first",this)}get last(){return borrowedGetter("last",this)}get length(){return borrowedGetter("length",this)}get parent(){if(void 0!==this.ownerNode)return this.ownerNode;const e=this.first;return null===e?null:e.parent}set parent(e){this.ownerNode=e;let t=this.first;for(;null!==t;)t.parent=e,t=t.next;e&&(e.children=this)}get rootParent(){let e=this.first;if(!e)return null;let t=this.first.parent;for(;null!==t;)e=t,t=e.parent;return e}setChildren(e,t=null){let r=!1;if(this.forEach((t=>{t===e&&(r=!0)})),!r)throw new Error("The item is not one of the linkers of this list.");null!=t?t.parent=e:e.children=null}adopt(e){const t=this.linkerClass.make(e,this.linkerClass);return t.parent=this.parent,t}insertAfter(e,t){return _DoublyLinkedList.DoublyLinkedList.prototype.insertAfter.call(this,e,this.adopt(t))}insertBefore(e,t){return _DoublyLinkedList.DoublyLinkedList.prototype.insertBefore.call(this,e,this.adopt(t))}append(e,t=this.last){return _DoublyLinkedList.DoublyLinkedList.prototype.append.call(this,e,t)}prepend(e,t=this.first){return _DoublyLinkedList.DoublyLinkedList.prototype.prepend.call(this,e,t)}remove(e){const t=this.parent,r=_DoublyLinkedList.DoublyLinkedList.prototype.remove.call(this,e);return r&&r.parent===t&&(this.ownerNode=t,r.parent=null),r}reset(){return _DoublyLinkedList.DoublyLinkedList.prototype.reset.call(this)}item(e){return _DoublyLinkedList.DoublyLinkedList.prototype.item.call(this,e)}forEach(e,t=this){let r=0,i=t.first;for(;null!==i;)e(i,r,t),i=i.next,++r;return t}[Symbol.iterator](){return new _TreeLinkerIterator.TreeLinkerIterator(this.first,this.parent)}}exports.LinkedTreeList=LinkedTreeList,LinkedTreeList.fromArray=(e=[],t=_TreeLinker.TreeLinker,r=LinkedTreeList)=>new r(t).initialize(t.fromArray(e).head);
@@ -43,7 +43,9 @@ export declare class TreeLinker implements IsTreeNode {
43
43
  listClass?: any;
44
44
  });
45
45
  /**
46
- * Create the children for this tree from an array.
46
+ * Create the children for this tree from an array. Each child becomes a tree linker with this node as its parent: an
47
+ * existing linker is kept as it is, an object with a data property gives the settings of the linker, and anything
48
+ * else is the data of the linker.
47
49
  * @param {Array|null} children Provide an array of data / linker references to be children of this tree node.
48
50
  * @param {IsArrayable<IsTreeNode>} listClass Give the type of list to use for storing the children
49
51
  * @return {LinkedTreeList|null}
@@ -51,7 +51,9 @@ class TreeLinker {
51
51
  }
52
52
 
53
53
  /**
54
- * Create the children for this tree from an array.
54
+ * Create the children for this tree from an array. Each child becomes a tree linker with this node as its parent: an
55
+ * existing linker is kept as it is, an object with a data property gives the settings of the linker, and anything
56
+ * else is the data of the linker.
55
57
  * @param {Array|null} children Provide an array of data / linker references to be children of this tree node.
56
58
  * @param {IsArrayable<IsTreeNode>} listClass Give the type of list to use for storing the children
57
59
  * @return {LinkedTreeList|null}
@@ -60,10 +62,17 @@ class TreeLinker {
60
62
  if (children === null) {
61
63
  return null
62
64
  }
63
- // Creates a linked-tree-list to store the children.
64
- return listClass.fromArray(children.map(child => Object.assign({}, child, {
65
- parent: this
66
- })), this.classType)
65
+ // Every child is made into a tree linker (an existing one is kept as it is, and a plain value is the data) and is
66
+ // given this node as its parent
67
+ const nodes = children.map(child => {
68
+ const linker = this.classType.make(child, this.classType)
69
+ linker.parent = this
70
+ return linker
71
+ })
72
+ // Creates a linked-tree-list to store the children, which remembers this node as its parent even when it is empty
73
+ const list = listClass.fromArray(nodes, this.classType)
74
+ list.parent = this
75
+ return list
67
76
  }
68
77
  }
69
78
  /**
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TreeLinker=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.map.js");var _DoubleLinker=require("../doubly-linked-list/DoubleLinker"),_LinkedTreeList=require("./LinkedTreeList");class TreeLinker{constructor({data:e=null,next:r=null,prev:i=null,children:n=null,parent:t=null,listClass:l=_LinkedTreeList.LinkedTreeList}={}){this.classType=TreeLinker,this.data=null,this.next=null,this.prev=null,this.parent=null,this.children=null,this.data=e,this.next=r,this.prev=i,this.parent=t,this.children=this.childrenFromArray(n,l)}childrenFromArray(e=null,r=_LinkedTreeList.LinkedTreeList){return null===e?null:r.fromArray(e.map((e=>Object.assign({},e,{parent:this}))),this.classType)}}exports.TreeLinker=TreeLinker,TreeLinker.make=(e,r=TreeLinker)=>_DoubleLinker.DoubleLinker.make(e,r),TreeLinker.fromArray=(e=[],r=TreeLinker)=>_DoubleLinker.DoubleLinker.fromArray(e,r);
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.TreeLinker=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.map.js");var _DoubleLinker=require("../doubly-linked-list/DoubleLinker"),_LinkedTreeList=require("./LinkedTreeList");class TreeLinker{constructor({data:e=null,next:r=null,prev:i=null,children:n=null,parent:t=null,listClass:s=_LinkedTreeList.LinkedTreeList}={}){this.classType=TreeLinker,this.data=null,this.next=null,this.prev=null,this.parent=null,this.children=null,this.data=e,this.next=r,this.prev=i,this.parent=t,this.children=this.childrenFromArray(n,s)}childrenFromArray(e=null,r=_LinkedTreeList.LinkedTreeList){if(null===e)return null;const i=e.map((e=>{const r=this.classType.make(e,this.classType);return r.parent=this,r})),n=r.fromArray(i,this.classType);return n.parent=this,n}}exports.TreeLinker=TreeLinker,TreeLinker.make=(e,r=TreeLinker)=>_DoubleLinker.DoubleLinker.make(e,r),TreeLinker.fromArray=(e=[],r=TreeLinker)=>_DoubleLinker.DoubleLinker.fromArray(e,r);
@@ -1,66 +1,59 @@
1
- /**
2
- * @file queue
3
- * @author Joshua Heagle <joshuaheagle@gmail.com>
4
- * @version 1.1.0
5
- * @memberOf module:collect-your-stuff
6
- */
7
- import { Queueable } from './Queueable';
1
+ import { Linker } from '../linked-list/Linker';
8
2
  import { IsArrayable } from '../../recipes/IsArrayable';
9
- import { IsLinker } from '../../recipes/IsLinker';
10
- import { completeResponse } from '../../recipes/Runnable';
3
+ import { IsQueue } from '../../recipes/IsQueue';
11
4
  /**
12
- * Maintain a series of queued items.
5
+ * A first-in-first-out collection: items are added to the back with enqueue and taken from the front with dequeue.
6
+ * Any value can be queued (it is stored as it is, whether it is a function, an object or null), and adding and taking
7
+ * are constant time. To queue tasks which are run as they are taken use TaskQueue.
13
8
  */
14
- export declare class Queue {
15
- /** The list which stores the queueables, the first is next to be dequeued. */
9
+ export declare class Queue<T = any> implements IsQueue<T>, Iterable<T> {
10
+ /** The list which stores the queued items, the first is next to be dequeued. */
16
11
  queuedList: IsArrayable<any>;
17
- private listClass;
18
- private queueableClass;
12
+ private readonly linkerClass;
19
13
  /**
20
- * Instantiate the queue with the given queue list.
21
- * @param {Iterable|LinkedList} queuedList Give the list of queueables to start in this queue.
22
- * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no queued list is given.
23
- * @param {Queueable} [queueableClass=Queueable] The class used to wrap queued items.
14
+ * Instantiate the queue, optionally with a list of items to start from.
15
+ * @param {IsArrayable|null} [queuedList=null] The list of linkers to start in this queue (the first is the front)
16
+ * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no queued list is given
17
+ * @param {Linker} [linkerClass=Linker] The class used to hold each queued item
24
18
  */
25
- constructor(queuedList?: IsArrayable<any>, listClass?: any, queueableClass?: typeof Queueable);
19
+ constructor(queuedList?: IsArrayable<any> | null, listClass?: any, linkerClass?: typeof Linker);
26
20
  /**
27
- * Take a queued task from the front of the queue and run it if ready. A task which is not ready yet is kept in the
28
- * queue (never dropped), a task which is still running is reported as blocking and left to finish on its own, and
29
- * completed tasks are discarded.
30
- * @return {completeResponse|*}
21
+ * Take the item from the front of the queue.
22
+ * @return {*|null} The item, or null when the queue is empty
31
23
  */
32
- dequeue(): completeResponse | any;
24
+ dequeue(): T | null;
33
25
  /**
34
- * Return true if the queue is empty (there are no tasks in the queue list)
26
+ * Check whether the queue has no items.
35
27
  * @return {boolean}
36
28
  */
37
29
  empty(): boolean;
38
30
  /**
39
- * Add a queued task to the end of the queue
40
- * @param {Queueable} queueable Add a new queueable to the end of the queue
41
- */
42
- enqueue(queueable: Queueable): void;
43
- /**
44
- * Take a look at the next queued task
45
- * @return {Queueable}
31
+ * Add an item to the back of the queue.
32
+ * @param {*} data The item to add
33
+ * @return {Queue} This queue, so that adding can be chained
46
34
  */
47
- peek(): IsLinker;
35
+ enqueue(data: T): this;
48
36
  /**
49
- * Remove the next queued item and return it.
50
- * @return {Queueable|null}
37
+ * Look at the item at the front of the queue, without removing it.
38
+ * @return {*|null} The item, or null when the queue is empty
51
39
  */
52
- remove(): Queueable | null;
40
+ peek(): T | null;
53
41
  /**
54
- * Get the length of the current queue.
42
+ * Count the items in the queue.
55
43
  * @return {number}
56
44
  */
57
45
  size(): number;
58
46
  /**
59
- * Convert an array to a Queue.
60
- * @param {Array} values An array of values which will be converted to queueables in this queue
61
- * @param {Queueable} queueableClass The class to use for each queueable
62
- * @param {Queue|Iterable} listClass The class to use to manage the queueables
47
+ * Iterate over the items from the front of the queue to the back, without removing them.
48
+ * @return {Iterator}
49
+ */
50
+ [Symbol.iterator](): Iterator<T>;
51
+ /**
52
+ * Convert an array to a Queue, the first value is at the front.
53
+ * @param {Array} [values=[]] The items to queue
54
+ * @param {IsArrayable} [listClass=LinkedList] The type of list used to store the items
55
+ * @param {Linker} [linkerClass=Linker] The class used to hold each queued item
63
56
  * @returns {Queue}
64
57
  */
65
- static fromArray: (values?: Array<any>, queueableClass?: typeof Queueable, listClass?: any) => Queue;
58
+ static fromArray: <T_1 = any>(values?: Array<T_1>, listClass?: any, linkerClass?: typeof Linker) => Queue<T_1>;
66
59
  }
@@ -4,81 +4,49 @@ Object.defineProperty(exports, '__esModule', {
4
4
  value: true
5
5
  })
6
6
  exports.Queue = void 0
7
- var _Queueable = require('./Queueable')
7
+ require('core-js/modules/esnext.iterator.constructor.js')
8
+ require('core-js/modules/esnext.iterator.for-each.js')
8
9
  var _LinkedList = require('../linked-list/LinkedList')
10
+ var _Linker = require('../linked-list/Linker')
9
11
  /**
10
12
  * @file queue
11
13
  * @author Joshua Heagle <joshuaheagle@gmail.com>
12
- * @version 1.1.0
14
+ * @version 2.0.0
13
15
  * @memberOf module:collect-your-stuff
14
16
  */
15
17
 
16
18
  /**
17
- * Maintain a series of queued items.
19
+ * A first-in-first-out collection: items are added to the back with enqueue and taken from the front with dequeue.
20
+ * Any value can be queued (it is stored as it is, whether it is a function, an object or null), and adding and taking
21
+ * are constant time. To queue tasks which are run as they are taken use TaskQueue.
18
22
  */
19
23
  class Queue {
20
24
  /**
21
- * Instantiate the queue with the given queue list.
22
- * @param {Iterable|LinkedList} queuedList Give the list of queueables to start in this queue.
23
- * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no queued list is given.
24
- * @param {Queueable} [queueableClass=Queueable] The class used to wrap queued items.
25
+ * Instantiate the queue, optionally with a list of items to start from.
26
+ * @param {IsArrayable|null} [queuedList=null] The list of linkers to start in this queue (the first is the front)
27
+ * @param {IsArrayable} [listClass=LinkedList] The type of list to create when no queued list is given
28
+ * @param {Linker} [linkerClass=Linker] The class used to hold each queued item
25
29
  */
26
- constructor (queuedList = null, listClass = _LinkedList.LinkedList, queueableClass = _Queueable.Queueable) {
27
- this.listClass = listClass
28
- this.queueableClass = queueableClass
29
- if (queuedList === null) {
30
- queuedList = new listClass(queueableClass)
31
- }
32
- this.queuedList = queuedList
30
+ constructor (queuedList = null, listClass = _LinkedList.LinkedList, linkerClass = _Linker.Linker) {
31
+ this.linkerClass = linkerClass
32
+ this.queuedList = queuedList === null ? new listClass(linkerClass) : queuedList
33
33
  }
34
34
 
35
35
  /**
36
- * Take a queued task from the front of the queue and run it if ready. A task which is not ready yet is kept in the
37
- * queue (never dropped), a task which is still running is reported as blocking and left to finish on its own, and
38
- * completed tasks are discarded.
39
- * @return {completeResponse|*}
36
+ * Take the item from the front of the queue.
37
+ * @return {*|null} The item, or null when the queue is empty
40
38
  */
41
39
  dequeue () {
42
- let next = this.remove()
43
- // Tasks which already completed are discarded when they reach the front of the queue
44
- while (next && next.complete) {
45
- next = this.remove()
46
- }
47
- if (!next) {
48
- return {
49
- success: 'No more queueable tasks in the queue',
50
- error: false,
51
- context: this.queuedList
52
- }
53
- }
54
- if (next.running) {
55
- // The unfinished task reports back through its own complete callback, so it is not kept in the queue
56
- return {
57
- success: false,
58
- error: 'The queue has been blocked by an unfinished task.',
59
- context: next
60
- }
61
- }
62
- if (!next.isReady) {
63
- // Keep the task (at the back, so the next dequeue can try the other tasks) rather than losing it
64
- this.enqueue(next)
65
- // We could go check the next in queue here but if we end up in a state where nothing is ready it would infinite loop
66
- // Also, we want the loop handled externally
67
- return {
68
- success: false,
69
- error: 'Unable to find ready task.',
70
- context: next
71
- }
72
- }
73
- if (!this.empty()) {
74
- // Place back in queue to be checked once again next time, only if the queue will not be empty
75
- this.enqueue(next)
40
+ const front = this.queuedList.first
41
+ if (front === null || typeof front === 'undefined') {
42
+ return null
76
43
  }
77
- return next.run.call(next)
44
+ this.queuedList.remove(front)
45
+ return front.data
78
46
  }
79
47
 
80
48
  /**
81
- * Return true if the queue is empty (there are no tasks in the queue list)
49
+ * Check whether the queue has no items.
82
50
  * @return {boolean}
83
51
  */
84
52
  empty () {
@@ -86,50 +54,66 @@ class Queue {
86
54
  }
87
55
 
88
56
  /**
89
- * Add a queued task to the end of the queue
90
- * @param {Queueable} queueable Add a new queueable to the end of the queue
57
+ * Add an item to the back of the queue.
58
+ * @param {*} data The item to add
59
+ * @return {Queue} This queue, so that adding can be chained
91
60
  */
92
- enqueue (queueable) {
93
- this.queuedList.append(queueable)
61
+ enqueue (data) {
62
+ // The item is wrapped here rather than left to the list, since the list treats objects that look like a linker's
63
+ // settings (they have a data property) as such, and a queue must give back exactly what it was given
64
+ this.queuedList.append(new this.linkerClass({
65
+ data
66
+ }))
67
+ return this
94
68
  }
95
69
 
96
70
  /**
97
- * Take a look at the next queued task
98
- * @return {Queueable}
71
+ * Look at the item at the front of the queue, without removing it.
72
+ * @return {*|null} The item, or null when the queue is empty
99
73
  */
100
74
  peek () {
101
- return this.queuedList.first
75
+ const front = this.queuedList.first
76
+ return front === null || typeof front === 'undefined' ? null : front.data
102
77
  }
103
78
 
104
79
  /**
105
- * Remove the next queued item and return it.
106
- * @return {Queueable|null}
80
+ * Count the items in the queue.
81
+ * @return {number}
107
82
  */
108
- remove () {
109
- if (this.empty()) {
110
- return null
111
- }
112
- return this.queuedList.remove(this.queuedList.first)
83
+ size () {
84
+ return this.queuedList.length
113
85
  }
114
86
 
115
87
  /**
116
- * Get the length of the current queue.
117
- * @return {number}
88
+ * Iterate over the items from the front of the queue to the back, without removing them.
89
+ * @return {Iterator}
118
90
  */
119
- size () {
120
- return this.queuedList.length
91
+ [Symbol.iterator] () {
92
+ const linkers = this.queuedList[Symbol.iterator]()
93
+ return {
94
+ next: () => {
95
+ const result = linkers.next()
96
+ return result.done ? {
97
+ done: true,
98
+ value: undefined
99
+ } : {
100
+ done: false,
101
+ value: result.value.data
102
+ }
103
+ }
104
+ }
121
105
  }
122
106
  }
123
107
  /**
124
- * Convert an array to a Queue.
125
- * @param {Array} values An array of values which will be converted to queueables in this queue
126
- * @param {Queueable} queueableClass The class to use for each queueable
127
- * @param {Queue|Iterable} listClass The class to use to manage the queueables
108
+ * Convert an array to a Queue, the first value is at the front.
109
+ * @param {Array} [values=[]] The items to queue
110
+ * @param {IsArrayable} [listClass=LinkedList] The type of list used to store the items
111
+ * @param {Linker} [linkerClass=Linker] The class used to hold each queued item
128
112
  * @returns {Queue}
129
113
  */
130
114
  exports.Queue = Queue
131
- Queue.fromArray = (values = [], queueableClass = _Queueable.Queueable, listClass = _LinkedList.LinkedList) => {
132
- const list = new listClass(queueableClass)
133
- list.initialize(queueableClass.fromArray(values, queueableClass).head)
134
- return new Queue(list, listClass, queueableClass)
115
+ Queue.fromArray = (values = [], listClass = _LinkedList.LinkedList, linkerClass = _Linker.Linker) => {
116
+ const queue = new Queue(null, listClass, linkerClass)
117
+ values.forEach(value => queue.enqueue(value))
118
+ return queue
135
119
  }
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Queue=void 0;var _Queueable=require("./Queueable"),_LinkedList=require("../linked-list/LinkedList");class Queue{constructor(e=null,u=_LinkedList.LinkedList,t=_Queueable.Queueable){this.listClass=u,this.queueableClass=t,null===e&&(e=new u(t)),this.queuedList=e}dequeue(){let e=this.remove();for(;e&&e.complete;)e=this.remove();return e?e.running?{success:!1,error:"The queue has been blocked by an unfinished task.",context:e}:e.isReady?(this.empty()||this.enqueue(e),e.run.call(e)):(this.enqueue(e),{success:!1,error:"Unable to find ready task.",context:e}):{success:"No more queueable tasks in the queue",error:!1,context:this.queuedList}}empty(){return this.size()<=0}enqueue(e){this.queuedList.append(e)}peek(){return this.queuedList.first}remove(){return this.empty()?null:this.queuedList.remove(this.queuedList.first)}size(){return this.queuedList.length}}exports.Queue=Queue,Queue.fromArray=(e=[],u=_Queueable.Queueable,t=_LinkedList.LinkedList)=>{const s=new t(u);return s.initialize(u.fromArray(e,u).head),new Queue(s,t,u)};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.Queue=void 0,require("core-js/modules/esnext.iterator.constructor.js"),require("core-js/modules/esnext.iterator.for-each.js");var _LinkedList=require("../linked-list/LinkedList"),_Linker=require("../linked-list/Linker");class Queue{constructor(e=null,t=_LinkedList.LinkedList,r=_Linker.Linker){this.linkerClass=r,this.queuedList=null===e?new t(r):e}dequeue(){const e=this.queuedList.first;return null==e?null:(this.queuedList.remove(e),e.data)}empty(){return this.size()<=0}enqueue(e){return this.queuedList.append(new this.linkerClass({data:e})),this}peek(){const e=this.queuedList.first;return null==e?null:e.data}size(){return this.queuedList.length}[Symbol.iterator](){const e=this.queuedList[Symbol.iterator]();return{next:()=>{const t=e.next();return t.done?{done:!0,value:void 0}:{done:!1,value:t.value.data}}}}}exports.Queue=Queue,Queue.fromArray=(e=[],t=_LinkedList.LinkedList,r=_Linker.Linker)=>{const u=new Queue(null,t,r);return e.forEach((e=>u.enqueue(e))),u};