This repository has been archived by the owner on Jul 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathot_toy.js
279 lines (256 loc) · 6.93 KB
/
ot_toy.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// Copyright 2016 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// A testbed for operational transformation ideas.
function size_of(tree) {
return tree == null ? 0 : tree.size;
}
function xi(tree, i) {
var base = 0;
while (tree != null) {
var left = tree.left;
var x = tree.value - size_of(left);
if (i < x) {
tree = left;
} else {
i = 1 + i - x;
base += tree.value;
tree = tree.right;
}
}
return base + i;
}
// precondition: i is not a member of the set
function xi_inv(tree, i) {
var result = i;
var x = 0;
while (tree != null) {
if (i < tree.value) {
tree = tree.left;
} else {
i -= tree.value;
result -= size_of(tree.left) + 1;
tree = tree.right;
}
}
return result;
}
function contains(tree, i) {
while (tree != null) {
if (i < tree.value) {
tree = tree.left;
} else if (i == tree.value) {
return true;
} else { // i > tree.value
i -= tree.value;
tree = tree.right;
}
}
return false;
}
function mk_tree_raw(left, value, right) {
var size = size_of(left) + 1 + size_of(right);
var left_height = left == null ? 0 : left.height;
var right_height = right == null ? 0 : right.height;
var height = Math.max(left_height, right_height) + 1;
return {left: left, value: value, right: right, size: size, height: height};
}
function mk_tree(left, value, right) {
var left_height = left == null ? 0 : left.height;
var right_height = right == null ? 0 : right.height;
if (left_height > right_height + 1) {
// unbalanced, rotate right
var new_right = mk_tree_raw(left.right, value - left.value, right);
return mk_tree_raw(left.left, left.value, new_right);
} else if (right_height > left_height + 1) {
// unbalanced, rotate left
var new_left = mk_tree_raw(left, value, right.left);
return mk_tree_raw(new_left, value + right.value, right.right);
}
return mk_tree_raw(left, value, right);
}
function union_one(tree, i) {
if (tree == null) {
return mk_tree(null, i, null);
} else if (i < tree.value) {
var left_union = union_one(tree.left, i);
return mk_tree(left_union, tree.value, tree.right);
} else if (i == tree.value) {
return tree;
} else { // i > tree.value
var right_union = union_one(tree.right, i - tree.value);
return mk_tree(tree.left, tree.value, right_union);
}
}
// \Xi_{i}(S)
function xi_one(tree, i) {
if (tree == null) {
return null;
} else if (i <= tree.value) {
var left_seq = xi_one(tree.left, i);
return mk_tree(left_seq, tree.value + 1, tree.right);
} else {
var right_seq = xi_one(tree.right, i - tree.value);
return mk_tree(tree.left, tree.value, right_seq);
}
}
// for debugging
function to_array(tree) {
var result = [];
function rec(tree, base) {
if (tree != null) {
rec(tree.left, base);
base += tree.value;
result.push(base);
rec(tree.right, base);
}
}
rec(tree, 0);
return result;
}
function tree_toy() {
tree = null;
tree = union_one(tree, 7);
tree = union_one(tree, 2);
tree = union_one(tree, 0);
tree = union_one(tree, 2);
tree = union_one(tree, 3);
for (var i = 200; i > 100; i--) {
tree = union_one(tree, i);
}
console.log(to_array(tree));
console.log(tree.size, tree.height);
for (var i = 0; i < 10; i++) {
console.log(i, xi(tree, i), xi_inv(tree, xi(tree, i)), contains(tree, i));
}
console.log(to_array(xi_one(tree, 5)));
}
//tree_toy();
// transformations of operations
// All operations have 'ty' and 'ix' properties
// also 'id'
// sample operations:
// {ty: 'ins', ix: 1, ch: 'x', pri: 0}
// {ty: 'del', ix: 1}
// Note: mutating in place is appealing, to avoid allocations.
function transform(op1, op2) {
if (op2.ty != 'ins') { return op1; }
return transform_ins(op1, op2.ix, op2.pri);
}
function transform_ins(op1, ix, pri) {
if (op1.ty == 'ins') {
if (op1.ix < ix || (op1.ix == ix && op1.pri < pri)) {
return op1;
}
return {ty: op1.ty, ix: op1.ix + 1, ch: op1.ch, pri: op1.pri, id: op1.id};
} else { // op1.ty is del
if (op1.ix < ix) {
return op1;
}
return {ty: op1.ty, ix: op1.ix + 1, id: op1.id};
}
}
class DocState {
constructor() {
this.ops = [];
this.dels = null;
this.str = "";
this.points = []; // in user-visible string coordinates
}
add(op) {
this.ops.push(op);
if (op.ty == 'del') {
if (!contains(this.dels, op.ix)) {
var ix = xi_inv(this.dels, op.ix);
this.dels = union_one(this.dels, op.ix);
this.str = this.str.slice(0, ix) + this.str.slice(ix + 1);
for (var i = 0; i < this.points.length; i++) {
if (this.points[i] > ix) {
this.points[i] -= 1;
}
}
}
} else if (op.ty == 'ins') {
this.dels = xi_one(this.dels, op.ix);
var ix = xi_inv(this.dels, op.ix);
this.str = this.str.slice(0, ix) + op.ch + this.str.slice(ix);
for (var i = 0; i < this.points.length; i++) {
if (this.points[i] > ix) {
this.points[i] += 1;
}
}
}
}
xform_ix(ix) {
return xi(this.dels, ix);
}
get_str() {
return this.str;
}
}
class Peer {
constructor() {
this.rev = 0;
this.context = new Set();
}
merge_op(doc_state, op) {
var id = op.id;
var ops = doc_state.ops;
if (this.rev < ops.length && ops[this.rev].id == id) {
// we already have this, roll rev forward
this.rev++;
while (this.rev < ops.length && this.context.has(ops[this.rev].id)) {
this.context.delete(ops[this.rev].id);
this.rev++;
}
return;
}
for (var ix = this.rev; ix < ops.length; ix++) {
if (ops[ix].id == id) {
// we already have this, but can't roll rev forward
this.context.add(id);
return;
}
}
// we don't have it, need to merge
var ins_list = [];
var S = null;
var T = null;
for (var ix = ops.length - 1; ix >= this.rev; ix--) {
var my_op = ops[ix];
if (my_op.ty == 'ins') {
var i = xi(S, my_op.ix);
if (!this.context.has(my_op.id)) {
ins_list.push([xi_inv(T, i), my_op.pri]);
T = union_one(T, i);
}
S = union_one(S, i);
}
}
for (var i = ins_list.length - 1; i >= 0; i--) {
op = transform_ins(op, ins_list[i][0], ins_list[i][1]);
}
var current = (this.rev == ops.length);
doc_state.add(op);
if (current) {
this.rev++;
} else {
this.context.add(id);
}
}
}
// Export as a module for node, but don't bother namespacing for browser
if (typeof exports !== 'undefined') {
exports.DocState = DocState;
exports.Peer = Peer;
}