File tree Expand file tree Collapse file tree 12 files changed +54
-29
lines changed Expand file tree Collapse file tree 12 files changed +54
-29
lines changed Original file line number Diff line number Diff line change 11/**
22 * Base node class.
33 *
4- * @param {Object } value - The value to hold.
4+ * @class
5+ * @param {any } value The value to hold.
56 */
67export default function Node ( value ) {
7- this . value = value ; // Key
8- this . prev = this ; // Pointer to previous (left) sibling
9- this . next = this ; // Pointer to next (right) sibling
8+ /** @member {any} The value/key held by this node. */
9+ this . value = value ;
10+ /** @member {Node} Pointer to previous (left) sibling */
11+ this . prev = this ;
12+ /** @member {Node} Pointer to next (right) sibling */
13+ this . next = this ;
1014}
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
2+ import Node from './Node.js' ;
23
34/**
45 * Optimization of {@link _concat} when <code>x</code> contains a single element.
56 * Works even if <code>x</code> has dangling pointers <code>x.next</code> and <code>x.prev</code>.
67 *
7- * @param {Node } a - First node of list.
8- * @param {Node } x - Node to be inserted at the end of list.
8+ * @param {Node } a First node of list.
9+ * @param {Node } x Node to be inserted at the end of list.
910 */
1011export default function _append ( a , x ) {
11- assert ( a !== null && x !== null ) ;
12- assert ( x . next === x && x . prev === x ) ;
12+ assert ( a instanceof Node ) ;
13+ assert ( x instanceof Node ) ;
14+ assert ( x . next === x ) ;
15+ assert ( x . prev === x ) ;
1316 // FROM a - b - c - d - a x
1417 // TO a - b - c - d - x - a
1518 const d = a . prev ;
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
2+ import Node from './Node.js' ;
23
34/**
45 * Concatenate two input lists.
56 *
6- * @param {Node } x - First node of first input list.
7- * @param {Node } y - First node of second input list.
7+ * @param {Node } x First node of first input list.
8+ * @param {Node } y First node of second input list.
89 */
910export default function _concat ( x , y ) {
10- assert ( x !== null && y !== null ) ;
11+ assert ( x instanceof Node ) ;
12+ assert ( y instanceof Node ) ;
1113 // FROM x - b - c - d - x y - f - g - h - y
1214 // TO x - b - c - d - y - f - g - h - x
1315 const d = x . prev ;
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
2+ import Node from './Node.js' ;
23
34/**
45 * Generator of nodes in list in order.
56 *
6- * @param {Node } first - First node of the list.
7- * @return {Iterable }
7+ * TODO yield values/keys instead?
8+ *
9+ * @param {Node } first First node of the list.
10+ * @return {IterableIterator } Yields nodes of a list in order.
811 */
912export default function * _iter ( first ) {
10- assert ( first !== null ) ;
13+ assert ( first instanceof Node ) ;
1114 let next = first ;
1215
1316 do {
Original file line number Diff line number Diff line change 11import assert from 'assert' ;
2+ import Node from './Node.js' ;
23
34/**
45 * Removes input {@link Node} from its list.
56 *
67 * /!\ Pointers in the extracted node are left unchanged.
78 * /!\ <code>x</code> will have dangling pointers after removal if not single element.
89 *
9- * @param {Node } x - Node to remove.
10+ * @param {Node } x Node to remove.
1011 */
1112export default function _remove ( x ) {
12- assert ( x !== null ) ;
13+ assert ( x instanceof Node ) ;
1314 x . prev . next = x . next ;
1415 x . next . prev = x . prev ;
1516}
Original file line number Diff line number Diff line change @@ -5,8 +5,9 @@ import _concat from './_concat.js';
55/**
66 * Concatenate two input lists.
77 *
8- * @param {Node } x - First node of first input list (can be null).
9- * @param {Node } y - First node of second input list (can be null).
8+ * @param {Node } x First node of first input list (can be null).
9+ * @param {Node } y First node of second input list (can be null).
10+ * @return {Node } First node of the output list (or null if empty).
1011 */
1112export default function concat ( x , y ) {
1213 if ( x === null ) return y ;
Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ import _append from './_append.js';
44/**
55 * Creates a list from an input iterable.
66 *
7- * @param {Iterable } iterable - The input iterable.
7+ * @param {Iterable } iterable The input iterable.
8+ * @return {Node } First node of the newly created list (or null if empty list).
89 */
910export default function from ( iterable ) {
1011 const it = iterable [ Symbol . iterator ] ( ) ;
Original file line number Diff line number Diff line change 1+ import assert from 'assert' ;
12import _iter from './_iter.js' ;
3+ import Node from './Node.js' ;
4+
25/**
36 * Generator of nodes in list in order.
47 *
5- * @param {Node } first - First node of the list (can be null).
6- * @return {Iterable }
8+ * @param {Node } first First node of the list (can be null).
9+ * @return {IterableIterator }
710 */
811export default function * iter ( first ) {
12+ assert ( first === null || first instanceof Node ) ;
913 if ( first !== null ) yield * _iter ( first ) ;
1014}
Original file line number Diff line number Diff line change @@ -3,9 +3,9 @@ import Node from './Node.js';
33import _remove from './_remove.js' ;
44
55/**
6- * Removes last {@link Node} from a list.
6+ * Removes last {@link Node} from a list. Throws if input list is empty.
77 *
8- * @param {Node } x - First node .
8+ * @param {Node } x First node.
99 * @return {[Node, Node] } New list (possibly null) and popped node.
1010 */
1111export default function pop ( x ) {
Original file line number Diff line number Diff line change @@ -5,8 +5,10 @@ import _append from './_append.js';
55/**
66 * Push value to list.
77 *
8- * @param {Node } x - First node of first input list (can be null).
9- * @param {Object } value - Value to push.
8+ * @param {Node } x First node of first input list (can be null).
9+ * @param {any } value Value to push.
10+ * @return {Node } The node at the front of the list (new node if empty, input
11+ * node otherwise).
1012 */
1113export default function push ( x , value ) {
1214 if ( x === null ) return new Node ( value ) ;
You can’t perform that action at this time.
0 commit comments