forked from automerge/hypermerge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClock.js
105 lines (105 loc) · 2.42 KB
/
Clock.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
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function gte(a, b) {
for (let id in a) {
if (a[id] < (b[id] || 0))
return false;
}
for (let id in b) {
if (b[id] > (a[id] || 0))
return false;
}
return true;
}
exports.gte = gte;
function cmp(a, b) {
const aGTE = gte(a, b);
const bGTE = gte(b, a);
if (aGTE && bGTE) {
return "EQ";
}
else if (aGTE && !bGTE) {
return "GT";
}
else if (!aGTE && bGTE) {
return "LT";
}
return "CONCUR";
}
exports.cmp = cmp;
function strs2clock(input) {
if (typeof input === "string") {
return { [input]: Infinity };
}
else {
let ids = input;
let clock = {};
ids
.map(str => str.split(":"))
.forEach(([id, max]) => {
clock[id] = max ? parseInt(max) : Infinity;
});
return clock;
}
}
exports.strs2clock = strs2clock;
function clock2strs(clock) {
let ids = [];
for (let id in clock) {
const max = clock[id];
if (max === Infinity) {
ids.push(id);
}
else {
ids.push(id + ":" + max);
}
}
return ids;
}
exports.clock2strs = clock2strs;
function clockDebug(c) {
const d = {};
Object.keys(c).forEach(actor => {
const short = actor.substr(0, 5);
d[short] = c[actor];
});
return JSON.stringify(d);
}
exports.clockDebug = clockDebug;
function equivalent(c1, c2) {
const actors = new Set([...Object.keys(c1), ...Object.keys(c2)]);
for (let actor of actors) {
if (c1[actor] != c2[actor]) {
return false;
}
}
return true;
}
exports.equivalent = equivalent;
function union(c1, c2) {
let acc = Object.assign({}, c1);
for (let id in c2) {
acc[id] = Math.max(acc[id] || 0, c2[id]);
}
return acc;
}
exports.union = union;
function addTo(acc, clock) {
for (let actor in clock) {
acc[actor] = Math.max(acc[actor] || 0, clock[actor]);
}
}
exports.addTo = addTo;
function intersection(c1, c2) {
const actors = new Set([...Object.keys(c1), ...Object.keys(c2)]);
let tmp = {};
actors.forEach(actor => {
const val = Math.min(c1[actor] || 0, c2[actor] || 0);
if (val > 0) {
tmp[actor] = val;
}
});
return tmp;
}
exports.intersection = intersection;
//# sourceMappingURL=Clock.js.map