Skip to content

Commit 04f6a6f

Browse files
make-github-pseudonymous-againAurélien Ooms
authored andcommitted
🐛 fix: Correctly wrap measure of middle tree...
when creating trees from digits and small lists. Fixes #73.
1 parent b1e53a0 commit 04f6a6f

File tree

11 files changed

+424
-24
lines changed

11 files changed

+424
-24
lines changed

package.json

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,56 @@
1818
"@babel/preset-env"
1919
],
2020
"env": {
21+
"test": {
22+
"presets": [
23+
"babel-preset-power-assert"
24+
],
25+
"plugins": [
26+
[
27+
"transform-remove-console",
28+
{
29+
"exclude": [
30+
"log",
31+
"error",
32+
"warn"
33+
]
34+
}
35+
]
36+
],
37+
"sourceMaps": "inline"
38+
},
2139
"development": {
40+
"presets": [
41+
"babel-preset-power-assert"
42+
],
43+
"plugins": [
44+
[
45+
"transform-remove-console",
46+
{
47+
"exclude": [
48+
"log",
49+
"error",
50+
"warn"
51+
]
52+
}
53+
]
54+
],
55+
"sourceMaps": "inline"
56+
},
57+
"production": {
58+
"plugins": [
59+
"babel-plugin-unassert",
60+
[
61+
"transform-remove-console",
62+
{
63+
"exclude": [
64+
"log",
65+
"error",
66+
"warn"
67+
]
68+
}
69+
]
70+
],
2271
"sourceMaps": "inline"
2372
}
2473
}
@@ -37,6 +86,9 @@
3786
"@babel/register": "7.10.4",
3887
"argparse": "1.0.10",
3988
"ava": "3.9.0",
89+
"babel-plugin-transform-remove-console": "^6.9.4",
90+
"babel-plugin-unassert": "^3.0.1",
91+
"babel-preset-power-assert": "^3.0.0",
4092
"benchmark": "2.1.4",
4193
"coveralls": "3.1.0",
4294
"esdoc": "1.1.0",
@@ -47,6 +99,7 @@
4799
"microtime": "3.0.0",
48100
"np": "6.2.5",
49101
"nyc": "15.1.0",
102+
"power-assert": "^1.6.1",
50103
"regenerator-runtime": "^0.13.5",
51104
"xo": "0.32.1"
52105
},
@@ -72,7 +125,7 @@
72125
"url": "https://github.com/aureooms/js-fingertree"
73126
},
74127
"scripts": {
75-
"build": "rm -rf lib && babel src -d lib",
128+
"build": "babel --delete-dir-on-start --env-name production src -d lib",
76129
"cover": "nyc --reporter=lcov npm test",
77130
"esdoc": "esdoc",
78131
"prepare": "npm run build",

src/0-core/_fast/_from_digit.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import {One, Two, Three, Four} from '../../1-digit';
22
import {Empty, Single, Deep} from '../../3-tree';
3+
import {cache} from '../measure';
34

45
export function _from_digit(M, digit) {
56
if (digit instanceof One) return new Single(M, digit.a);
67
if (digit instanceof Two || digit instanceof Three || digit instanceof Four) {
7-
return new Deep(M, digit.init(), new Empty(M), new One(digit.last()));
8+
return new Deep(
9+
M,
10+
digit.init(),
11+
new Empty(cache(M)),
12+
new One(digit.last())
13+
);
814
}
915

1016
// Potential optimization by commenting out this section

src/0-core/_fast/_from_small_list.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {Empty, Single, Deep} from '../../3-tree';
22
import {One, Two} from '../../1-digit';
3+
import {cache} from '../measure';
34

45
export function _from_small_list(M, list) {
56
switch (list.length) {
@@ -8,12 +9,17 @@ export function _from_small_list(M, list) {
89
case 1:
910
return new Single(M, list[0]);
1011
case 2:
11-
return new Deep(M, new One(list[0]), new Empty(M), new One(list[1]));
12+
return new Deep(
13+
M,
14+
new One(list[0]),
15+
new Empty(cache(M)),
16+
new One(list[1])
17+
);
1218
case 3:
1319
return new Deep(
1420
M,
1521
new Two(list[0], list[1]),
16-
new Empty(M),
22+
new Empty(cache(M)),
1723
new One(list[2])
1824
);
1925
default:

src/2-node/2-Node2.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,3 @@ Node2.prototype.push = function (value) {
4141
Node2.prototype.cons = function (value) {
4242
throw new Error('trying to call cons of Node2');
4343
};
44-
45-
export function node2(M, a, b) {
46-
return new Node2(M.plus(M.measure(a), M.measure(b)), a, b);
47-
}

src/2-node/3-Node3.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,3 @@ Node3.prototype.push = function (value) {
4242
Node3.prototype.cons = function (value) {
4343
throw new Error('trying to call cons of Node3');
4444
};
45-
46-
export function node3(M, a, b, c) {
47-
return new Node3(
48-
M.plus(M.measure(a), M.plus(M.measure(b), M.measure(c))),
49-
a,
50-
b,
51-
c
52-
);
53-
}

src/2-node/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
export * from './2-Node2';
22
export * from './3-Node3';
3+
export * from './node2';
4+
export * from './node3';

src/2-node/node2.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import assert from 'assert';
2+
3+
import {CachedMeasure} from '../0-core';
4+
import {Node2} from './2-Node2';
5+
import {Node3} from './3-Node3';
6+
7+
export function node2(M, a, b) {
8+
assert(
9+
!(a instanceof Node2 || a instanceof Node3) || M instanceof CachedMeasure
10+
);
11+
assert(
12+
a instanceof Node2 || a instanceof Node3 || !(M instanceof CachedMeasure)
13+
);
14+
return new Node2(M.plus(M.measure(a), M.measure(b)), a, b);
15+
}

src/2-node/node3.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import assert from 'assert';
2+
3+
import {CachedMeasure} from '../0-core';
4+
import {Node2} from './2-Node2';
5+
import {Node3} from './3-Node3';
6+
7+
export function node3(M, a, b, c) {
8+
assert(
9+
!(a instanceof Node2 || a instanceof Node3) || M instanceof CachedMeasure
10+
);
11+
assert(
12+
a instanceof Node2 || a instanceof Node3 || !(M instanceof CachedMeasure)
13+
);
14+
return new Node3(
15+
M.plus(M.measure(a), M.plus(M.measure(b), M.measure(c))),
16+
a,
17+
b,
18+
c
19+
);
20+
}

src/3-tree/implementations/2-Deep.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@ import {
66
_from_small_list,
77
_deepL,
88
_deepR,
9+
CachedMeasure,
910
Split
1011
} from '../../0-core';
1112
import {One, Two, Four} from '../../1-digit';
12-
import {delay} from '../../4-lazy';
13+
import {delay, Lazy} from '../../4-lazy';
14+
15+
import assert from 'assert';
1316

1417
export function Deep(M, left, middle, right) {
18+
assert(middle instanceof Lazy || middle.M instanceof CachedMeasure);
1519
this.M = M;
1620
this.left = left;
1721
this.middle = middle;

test/src/github-issue-73.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import test from 'ava';
2+
3+
import {Measures} from '@aureooms/js-measure';
4+
const {COUNTER} = Measures;
5+
6+
import {map, list, chain, range, reversed} from '@aureooms/js-itertools';
7+
8+
import {gt} from '@aureooms/js-predicate';
9+
10+
import {empty, from} from '../../src';
11+
12+
function set(tree, index, value) {
13+
if (index < 0 || index >= tree.measure())
14+
throw new Error(`wrong index '${index}'`);
15+
16+
const split = tree.splitTree((m) => m > index, tree.M.zero());
17+
18+
return split.left.push(value).concat(split.right);
19+
}
20+
21+
test('github issue #73', (t) => {
22+
let T = from(COUNTER, 'abcde');
23+
T = set(T, 2, 'C');
24+
t.deepEqual(list(T), list('abCde'));
25+
T = set(T, 2, 'Z');
26+
t.deepEqual(list(T), list('abZde'));
27+
});

0 commit comments

Comments
 (0)