functionalscript 0.0.287 → 0.0.288
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/package.json +1 -1
- package/types/btree/set/index.js +81 -27
- package/types/btree/set/test.js +19 -0
- package/types/btree/test.js +1 -0
package/package.json
CHANGED
package/types/btree/set/index.js
CHANGED
|
@@ -1,39 +1,93 @@
|
|
|
1
1
|
const _ = require('..')
|
|
2
|
-
const
|
|
2
|
+
const find = require('../find')
|
|
3
3
|
const cmp = require('../../function/compare')
|
|
4
|
-
const
|
|
4
|
+
const list = require('../../list')
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
/**
|
|
7
|
+
* @template T
|
|
8
|
+
* @typedef {_.Branch1<T> | _.Branch3<T>} Result
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** @type {<T>(b: _.Branch5<T> | _.Branch7<T>) => Result<T>} */
|
|
12
|
+
const b57 = b => b.length === 5 ? [b] : [[b[0], b[1], b[2]], b[3], [b[4], b[5], b[6]]]
|
|
13
|
+
|
|
14
|
+
/** @type {<T>(a: Result<T>) => (i: find.PathItem<T>) => Result<T>} */
|
|
15
|
+
const reduce = a => i => {
|
|
16
|
+
switch (i[0]) {
|
|
10
17
|
case 0: {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
// x4
|
|
17
|
-
const x = first[1];
|
|
18
|
-
return todo()
|
|
18
|
+
const x = i[1]
|
|
19
|
+
switch (x.length) {
|
|
20
|
+
case 3: { return [[...a, x[1], x[2]]] }
|
|
21
|
+
case 5: { return b57([...a, x[1], x[2], x[3], x[4]]) }
|
|
22
|
+
}
|
|
19
23
|
}
|
|
20
24
|
case 2: {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
// x2
|
|
27
|
-
const x = first[1];
|
|
28
|
-
return todo()
|
|
25
|
+
const x = i[1]
|
|
26
|
+
switch (x.length) {
|
|
27
|
+
case 3: { return [[x[0], x[1], ...a]] }
|
|
28
|
+
case 5: { return b57([x[0], x[1], ...a, x[3], x[4]]) }
|
|
29
|
+
}
|
|
29
30
|
}
|
|
30
31
|
case 4: {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
const x = i[1]
|
|
33
|
+
return b57([x[0], x[1], x[2], x[3], ...a])
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** @type {<T>(c: cmp.Compare<T>) => (value: T) => (node: _.Node<T>) => _.Node<T>} */
|
|
39
|
+
const set = c => value => node => {
|
|
40
|
+
const { first, tail } = find.find(c)(node)
|
|
41
|
+
/** @typedef {typeof value} T */
|
|
42
|
+
/** @type {() => Result<T>} */
|
|
43
|
+
const f = () => {
|
|
44
|
+
switch (first[0]) {
|
|
45
|
+
case 0: {
|
|
46
|
+
// insert
|
|
47
|
+
const x = first[1]
|
|
48
|
+
switch (x.length) {
|
|
49
|
+
case 1: { return [[value, x[0]]] }
|
|
50
|
+
case 2: { return [[value], x[0], [x[1]]] }
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
case 1: {
|
|
54
|
+
// replace
|
|
55
|
+
const x = first[1];
|
|
56
|
+
switch (x.length) {
|
|
57
|
+
case 1: { return [[value]] }
|
|
58
|
+
case 2: { return [[value, x[1]]] }
|
|
59
|
+
case 3: { return [[x[0], value, x[2]]] }
|
|
60
|
+
case 5: { return [[x[0], value, x[2], x[3], x[4]]] }
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
case 2: {
|
|
64
|
+
// insert
|
|
65
|
+
const x = first[1];
|
|
66
|
+
switch (x.length) {
|
|
67
|
+
case 1: { return [[x[0], value]] }
|
|
68
|
+
case 2: { return [[x[0]], value, [x[1]]] }
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
case 3: {
|
|
72
|
+
// replace
|
|
73
|
+
const x = first[1];
|
|
74
|
+
switch (x.length) {
|
|
75
|
+
case 2: { return [[x[0], value]] }
|
|
76
|
+
case 5: { return [[x[0], x[1], x[2], value, x[4]]]}
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
case 4: {
|
|
80
|
+
// insert
|
|
81
|
+
const [v0, v1] = first[1];
|
|
82
|
+
return [[v0], v1, [value]]
|
|
83
|
+
}
|
|
34
84
|
}
|
|
35
85
|
}
|
|
36
|
-
|
|
86
|
+
const r = list.reduce(reduce)(f())(tail)
|
|
87
|
+
return r.length === 1 ? r[0] : r
|
|
37
88
|
}
|
|
38
89
|
|
|
39
|
-
module.exports = {
|
|
90
|
+
module.exports = {
|
|
91
|
+
/** @readonly */
|
|
92
|
+
set,
|
|
93
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const _ = require('.')
|
|
2
|
+
const btree = require('..')
|
|
3
|
+
const { stringCmp } = require('../../function/compare')
|
|
4
|
+
const json = require('../../../json')
|
|
5
|
+
const { sort } = require('../../object')
|
|
6
|
+
|
|
7
|
+
/** @type {(node: btree.Node<string>) => (value: string) => btree.Node<string>} */
|
|
8
|
+
const set = node => value => _.set(stringCmp(value))(value)(node)
|
|
9
|
+
|
|
10
|
+
const jsonStr = json.stringify(sort)
|
|
11
|
+
|
|
12
|
+
{
|
|
13
|
+
/** @type {btree.Node<string>} */
|
|
14
|
+
let _map = ['1']
|
|
15
|
+
for (let i = 2; i <= 10; i++)
|
|
16
|
+
_map = set(_map)((i * i).toString())
|
|
17
|
+
const r = jsonStr(_map)
|
|
18
|
+
if (r !== '[[["1","100"],"16",["25","36"]],"4",[["49"],"64",["81","9"]]]') { throw r }
|
|
19
|
+
}
|