forked from nayuki/QR-Code-generator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.js
126 lines (126 loc) · 3.78 KB
/
misc.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
var misc;
(function (misc) {
function union(setA, setB) {
let _union = new Set(setA);
for (let elem of setB) {
_union.add(elem);
}
return _union;
}
misc.union = union;
function intersection(setA, setB) {
let _intersection = new Set();
for (let elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem);
}
}
return _intersection;
}
misc.intersection = intersection;
function difference(setA, setB) {
let _difference = new Set(setA);
for (let elem of setB) {
_difference.delete(elem);
}
return _difference;
}
misc.difference = difference;
class ObjectSet {
constructor(list, isEq, hash) {
this.isEq = isEq;
this.hash = hash;
this.map = new Map();
for (let l of list) {
this.add(l);
}
}
add(t) {
let lst = this.map.get(this.hash(t));
if (lst) {
if (lst.every(l => !this.isEq(l, t))) {
lst.push(t);
}
}
else {
this.map.set(this.hash(t), [t]);
}
}
has(t) {
let lst = this.map.get(this.hash(t));
return !!lst && lst.some(l => this.isEq(l, t));
}
toList() {
return Array.from(this.map.entries(), ([_, ts]) => ts).flat();
}
intersect(set) {
let res = new ObjectSet(new Array(), this.isEq, this.hash);
this.map.forEach((ts, _) => {
ts.forEach(element => {
if (set.has(element)) {
res.add(element);
}
});
});
return res;
}
union(set) {
let res = new ObjectSet(new Array(), this.isEq, this.hash);
res.map = new Map(this.map);
set.map.forEach((ts, _) => {
ts.forEach(element => {
res.add(element);
});
});
return res;
}
minus(set) {
let res = new ObjectSet(new Array(), this.isEq, this.hash);
this.map.forEach((ts, _) => {
ts.forEach(element => {
if (!set.has(element)) {
res.add(element);
}
});
});
return res;
}
}
misc.ObjectSet = ObjectSet;
function binaryInsert(entry, sortedArray, comparator) {
let left = 0;
let right = sortedArray.length - 1;
let middle = left + Math.floor((right - left) / 2);
while (left < right) {
if (comparator(entry, sortedArray[middle]) < 0) {
right = middle - 1;
}
else if (comparator(entry, sortedArray[middle]) > 0) {
left = middle + 1;
}
else {
left = middle;
right = middle;
}
middle = left + Math.floor((right - left) / 2);
}
if (comparator(entry, sortedArray[left]) < 0) {
sortedArray.splice(left, 0, entry);
}
else {
sortedArray.splice(left + 1, 0, entry);
}
}
misc.binaryInsert = binaryInsert;
function shuffle(array) {
if (array.length <= 1)
return array;
for (let i = 0; i < array.length; i++) {
const randomChoiceIndex = Math.floor(Math.random() * array.length);
[array[i], array[randomChoiceIndex]] = [array[randomChoiceIndex], array[i]];
}
return array;
}
misc.shuffle = shuffle;
})(misc || (misc = {}));
//# sourceMappingURL=misc.js.map