-
Notifications
You must be signed in to change notification settings - Fork 208
/
strSetMathHelpers.js
129 lines (124 loc) · 3.69 KB
/
strSetMathHelpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// @ts-check
import { passStyleOf } from '@agoric/marshal';
import { assert, details } from '@agoric/assert';
const identity = harden([]);
const assertUniqueSorted = list => {
const len = list.length;
for (let i = 1; i < len; i += 1) {
const leftStr = list[i - 1];
const rightStr = list[i];
assert(leftStr !== rightStr, details`value has duplicates: ${list}`);
assert(leftStr < rightStr, details`value not sorted ${list}`);
}
};
/**
* Operations for arrays with unique string elements. More information
* about these assets might be provided by some other mechanism, such as
* an off-chain API or physical records. strSetMathHelpers are highly
* efficient, but if the users rely on an external resource to learn
* more about the digital assets (for example, looking up a string ID
* in a database), the administrator of the external resource could
* potentially change the external definition at any time.
*
* @type {MathHelpers}
*/
const strSetMathHelpers = harden({
doCoerce: list => {
assert(passStyleOf(list) === 'copyArray', 'value must be an array');
list.forEach(elem => assert.typeof(elem, 'string'));
assertUniqueSorted(list);
return harden(list);
},
doGetEmpty: _ => identity,
doIsEmpty: list => passStyleOf(list) === 'copyArray' && list.length === 0,
doIsGTE: (left, right) => {
let leftI = 0;
let rightI = 0;
const leftLen = left.length;
const rightLen = right.length;
while (leftI < leftLen && rightI < rightLen) {
const leftStr = left[leftI];
const rightStr = right[rightI];
if (leftStr < rightStr) {
// an element of left not in right. Fine
leftI += 1;
} else if (leftStr > rightStr) {
// an element of right not in left.
return false;
} else {
leftI += 1;
rightI += 1;
}
}
// Are there no elements of right remaining?
return rightI >= rightLen;
},
doIsEqual: (left, right) => {
if (left.length !== right.length) {
return false;
}
return left.every((leftStr, i) => leftStr === right[i]);
},
doAdd: (left, right) => {
const result = [];
let leftI = 0;
let rightI = 0;
const leftLen = left.length;
const rightLen = right.length;
while (leftI < leftLen && rightI < rightLen) {
const leftStr = left[leftI];
const rightStr = right[rightI];
assert(
leftStr !== rightStr,
details`left and right have same element ${leftStr}`,
);
if (leftStr < rightStr) {
result.push(leftStr);
leftI += 1;
} else {
result.push(rightStr);
rightI += 1;
}
}
if (leftI < leftLen) {
result.push(left[leftI]);
} else if (rightI < rightLen) {
result.push(right[rightI]);
}
return harden(result);
},
doSubtract: (left, right) => {
const result = [];
let leftI = 0;
let rightI = 0;
const leftLen = left.length;
const rightLen = right.length;
while (leftI < leftLen && rightI < rightLen) {
const leftStr = left[leftI];
const rightStr = right[rightI];
assert(
leftStr <= rightStr,
details`element of right not present in left ${rightStr}`,
);
if (leftStr < rightStr) {
// an element of left not in right. Keep.
result.push(leftStr);
leftI += 1;
} else {
// element in both. Skip.
leftI += 1;
rightI += 1;
}
}
assert(
rightI >= rightLen,
details`some of the elements in right (${right}) were not present in left (${left})`,
);
if (leftI < leftLen) {
result.push(left[leftI]);
}
return harden(result);
},
});
harden(strSetMathHelpers);
export default strSetMathHelpers;