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 1
1
/**
2
2
* Base node class.
3
3
*
4
- * @param {Object } value - The value to hold.
4
+ * @class
5
+ * @param {any } value The value to hold.
5
6
*/
6
7
export 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 ;
10
14
}
Original file line number Diff line number Diff line change 1
1
import assert from 'assert' ;
2
+ import Node from './Node.js' ;
2
3
3
4
/**
4
5
* Optimization of {@link _concat} when <code>x</code> contains a single element.
5
6
* Works even if <code>x</code> has dangling pointers <code>x.next</code> and <code>x.prev</code>.
6
7
*
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.
9
10
*/
10
11
export 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 ) ;
13
16
// FROM a - b - c - d - a x
14
17
// TO a - b - c - d - x - a
15
18
const d = a . prev ;
Original file line number Diff line number Diff line change 1
1
import assert from 'assert' ;
2
+ import Node from './Node.js' ;
2
3
3
4
/**
4
5
* Concatenate two input lists.
5
6
*
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.
8
9
*/
9
10
export default function _concat ( x , y ) {
10
- assert ( x !== null && y !== null ) ;
11
+ assert ( x instanceof Node ) ;
12
+ assert ( y instanceof Node ) ;
11
13
// FROM x - b - c - d - x y - f - g - h - y
12
14
// TO x - b - c - d - y - f - g - h - x
13
15
const d = x . prev ;
Original file line number Diff line number Diff line change 1
1
import assert from 'assert' ;
2
+ import Node from './Node.js' ;
2
3
3
4
/**
4
5
* Generator of nodes in list in order.
5
6
*
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.
8
11
*/
9
12
export default function * _iter ( first ) {
10
- assert ( first !== null ) ;
13
+ assert ( first instanceof Node ) ;
11
14
let next = first ;
12
15
13
16
do {
Original file line number Diff line number Diff line change 1
1
import assert from 'assert' ;
2
+ import Node from './Node.js' ;
2
3
3
4
/**
4
5
* Removes input {@link Node} from its list.
5
6
*
6
7
* /!\ Pointers in the extracted node are left unchanged.
7
8
* /!\ <code>x</code> will have dangling pointers after removal if not single element.
8
9
*
9
- * @param {Node } x - Node to remove.
10
+ * @param {Node } x Node to remove.
10
11
*/
11
12
export default function _remove ( x ) {
12
- assert ( x !== null ) ;
13
+ assert ( x instanceof Node ) ;
13
14
x . prev . next = x . next ;
14
15
x . next . prev = x . prev ;
15
16
}
Original file line number Diff line number Diff line change @@ -5,8 +5,9 @@ import _concat from './_concat.js';
5
5
/**
6
6
* Concatenate two input lists.
7
7
*
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).
10
11
*/
11
12
export default function concat ( x , y ) {
12
13
if ( x === null ) return y ;
Original file line number Diff line number Diff line change @@ -4,7 +4,8 @@ import _append from './_append.js';
4
4
/**
5
5
* Creates a list from an input iterable.
6
6
*
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).
8
9
*/
9
10
export default function from ( iterable ) {
10
11
const it = iterable [ Symbol . iterator ] ( ) ;
Original file line number Diff line number Diff line change
1
+ import assert from 'assert' ;
1
2
import _iter from './_iter.js' ;
3
+ import Node from './Node.js' ;
4
+
2
5
/**
3
6
* Generator of nodes in list in order.
4
7
*
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 }
7
10
*/
8
11
export default function * iter ( first ) {
12
+ assert ( first === null || first instanceof Node ) ;
9
13
if ( first !== null ) yield * _iter ( first ) ;
10
14
}
Original file line number Diff line number Diff line change @@ -3,9 +3,9 @@ import Node from './Node.js';
3
3
import _remove from './_remove.js' ;
4
4
5
5
/**
6
- * Removes last {@link Node} from a list.
6
+ * Removes last {@link Node} from a list. Throws if input list is empty.
7
7
*
8
- * @param {Node } x - First node .
8
+ * @param {Node } x First node.
9
9
* @return {[Node, Node] } New list (possibly null) and popped node.
10
10
*/
11
11
export default function pop ( x ) {
Original file line number Diff line number Diff line change @@ -5,8 +5,10 @@ import _append from './_append.js';
5
5
/**
6
6
* Push value to list.
7
7
*
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).
10
12
*/
11
13
export default function push ( x , value ) {
12
14
if ( x === null ) return new Node ( value ) ;
You can’t perform that action at this time.
0 commit comments