-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathxform.c
291 lines (227 loc) · 10.5 KB
/
xform.c
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
280
281
282
283
284
285
286
287
288
289
290
291
#include <assert.h>
#include "xform.h"
typedef struct delta_pair {
size_t delta1;
size_t delta2;
} delta_pair;
static size_t min(size_t s1, size_t s2) {
if (s1 < s2) {
return s1;
} else {
return s2;
}
}
static delta_pair ot_xform_skip_skip(ot_comp_skip skip1, size_t offset1,
ot_comp_skip skip2, size_t offset2,
ot_xform_pair xform) {
size_t skip1_count = (size_t)skip1.count - offset1;
size_t skip2_count = (size_t)skip2.count - offset2;
size_t min_len = min(skip1_count, skip2_count);
ot_skip(xform.op1_prime, (uint32_t)min_len);
ot_skip(xform.op2_prime, (uint32_t)min_len);
return (delta_pair){min_len, min_len};
}
static delta_pair ot_xform_skip_insert(ot_comp_insert ins, size_t ins_offset,
ot_xform_pair xform) {
size_t ins_len = utf8_length(ins.text) - ins_offset;
ot_skip(xform.op1_prime, (uint32_t)ins_len);
char* str_start = ins.text + utf8_bytes(ins.text, ins_offset);
size_t copy_len = strlen(str_start);
char* substr = malloc(sizeof(char) * copy_len + 1);
memcpy(substr, str_start, copy_len);
substr[copy_len] = '\0';
ot_insert(xform.op2_prime, substr);
free(substr);
return (delta_pair){0, ins_len};
}
static delta_pair ot_xform_insert_skip(ot_comp_insert ins, size_t ins_offset,
ot_xform_pair xform) {
ot_xform_pair flip = (ot_xform_pair){xform.op2_prime, xform.op1_prime};
delta_pair p = ot_xform_skip_insert(ins, ins_offset, flip);
return (delta_pair){p.delta2, p.delta1};
}
static delta_pair ot_xform_skip_delete(ot_comp_skip skip, size_t skip_offset,
ot_comp_delete del, size_t del_offset,
ot_xform_pair xform) {
size_t skip_len = (size_t)skip.count - skip_offset;
size_t del_len = (size_t)del.count - del_offset;
size_t min_len = min(skip_len, del_len);
ot_delete(xform.op2_prime, (uint32_t)min_len);
return (delta_pair){min_len, min_len};
}
static delta_pair ot_xform_delete_skip(ot_comp_delete del, size_t del_offset,
ot_comp_skip skip, size_t skip_offset,
ot_xform_pair xform) {
ot_xform_pair flip = (ot_xform_pair){xform.op2_prime, xform.op1_prime};
delta_pair p =
ot_xform_skip_delete(skip, skip_offset, del, del_offset, flip);
return (delta_pair){p.delta2, p.delta1};
}
static delta_pair ot_xform_insert_insert(ot_comp_insert op1_insert,
size_t op1_offset,
ot_xform_pair xform) {
size_t len = utf8_length(op1_insert.text) - op1_offset;
ot_skip(xform.op2_prime, (uint32_t)len);
char* str_start = op1_insert.text + utf8_bytes(op1_insert.text, op1_offset);
size_t copy_len = strlen(str_start);
char* substr = malloc(sizeof(char) * copy_len + 1);
memcpy(substr, str_start, copy_len);
substr[copy_len] = '\0';
ot_insert(xform.op1_prime, substr);
free(substr);
return (delta_pair){len, 0};
}
static delta_pair ot_xform_insert_delete(ot_comp_insert ins, size_t ins_offset,
ot_xform_pair xform) {
size_t ins_len = utf8_length(ins.text) - ins_offset;
ot_skip(xform.op2_prime, (uint32_t)ins_len);
char* str_start = ins.text + utf8_bytes(ins.text, ins_offset);
size_t copy_len = strlen(str_start);
char* substr = malloc(sizeof(char) * copy_len + 1);
memcpy(substr, str_start, copy_len);
substr[copy_len] = '\0';
ot_insert(xform.op1_prime, substr);
free(substr);
return (delta_pair){ins_len, 0};
}
static delta_pair ot_xform_delete_insert(ot_comp_insert ins, size_t ins_offset,
ot_xform_pair xform) {
ot_xform_pair flip = (ot_xform_pair){xform.op2_prime, xform.op1_prime};
delta_pair p = ot_xform_insert_delete(ins, ins_offset, flip);
return (delta_pair){p.delta2, p.delta1};
}
static delta_pair ot_xform_delete_delete(ot_comp_delete del1, size_t offset1,
ot_comp_delete del2, size_t offset2) {
size_t len1 = (size_t)del1.count - offset1;
size_t len2 = (size_t)del2.count - offset2;
size_t min_len = min(len1, len2);
return (delta_pair){min_len, min_len};
}
ot_xform_pair ot_xform(ot_op* op1, ot_op* op2) {
ot_op* op1_prime = ot_new_op();
op1_prime->client_id = op1->client_id;
memcpy(op1_prime->parent, op2->hash, 20);
ot_op* op2_prime = ot_new_op();
op2_prime->client_id = op2->client_id;
memcpy(op2_prime->parent, op1->hash, 20);
ot_xform_pair xform = (ot_xform_pair){op1_prime, op2_prime};
ot_comp* op1_comps = op1->comps.data;
ot_comp* op2_comps = op2->comps.data;
ot_iter op1_iter;
ot_iter_init(&op1_iter, op1);
ot_iter op2_iter;
ot_iter_init(&op2_iter, op2);
bool op1_next = ot_iter_next(&op1_iter);
bool op2_next = ot_iter_next(&op2_iter);
while (op1_next || op2_next) {
ot_comp* op1_comp;
if (op1_next) {
op1_comp = op1_comps + op1_iter.pos;
} else {
op1_comp = NULL;
}
ot_comp* op2_comp;
if (op2_next) {
op2_comp = op2_comps + op2_iter.pos;
} else {
op2_comp = NULL;
}
if (op1_comp == NULL) {
// If we've reached the end of the first operation but not the
// second, then just act like the first operation is skipping
// whatever the second operation did.
if (op2_comp == NULL) {
assert(!"Both op components should never be NULL.");
} else if (op2_comp->type == OT_INSERT) {
ot_comp_insert op2_insert = op2_comp->value.insert;
delta_pair p =
ot_xform_skip_insert(op2_insert, op2_iter.offset, xform);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
} else {
ot_free_op(op1_prime);
ot_free_op(op2_prime);
return (ot_xform_pair){NULL, NULL};
}
} else if (op2_comp == NULL) {
if (op1_comp == NULL) {
assert(!"Both op components should never be NULL.");
} else if (op1_comp->type == OT_INSERT) {
// If we've reached the end of the second operation but not the
// first, then just act like the second operation is skipping
// whatever the first operation did.
ot_comp_insert op1_insert = op1_comp->value.insert;
delta_pair p =
ot_xform_insert_skip(op1_insert, op1_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
} else {
ot_free_op(op1_prime);
ot_free_op(op2_prime);
return (ot_xform_pair){NULL, NULL};
}
} else if (op1_comp->type == OT_SKIP) {
ot_comp_skip op1_skip = op1_comp->value.skip;
if (op2_comp->type == OT_SKIP) {
ot_comp_skip op2_skip = op2_comp->value.skip;
delta_pair p =
ot_xform_skip_skip(op1_skip, op1_iter.offset, op2_skip,
op2_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
} else if (op2_comp->type == OT_INSERT) {
ot_comp_insert op2_insert = op2_comp->value.insert;
delta_pair p =
ot_xform_skip_insert(op2_insert, op2_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
} else if (op2_comp->type == OT_DELETE) {
ot_comp_delete op2_delete = op2_comp->value.delete;
delta_pair p =
ot_xform_skip_delete(op1_skip, op1_iter.offset, op2_delete,
op2_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
}
} else if (op1_comp->type == OT_INSERT) {
ot_comp_insert op1_insert = op1_comp->value.insert;
if (op2_comp->type == OT_SKIP) {
delta_pair p =
ot_xform_insert_skip(op1_insert, op1_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
} else if (op2_comp->type == OT_INSERT) {
delta_pair p =
ot_xform_insert_insert(op1_insert, op1_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
} else if (op2_comp->type == OT_DELETE) {
delta_pair p =
ot_xform_insert_delete(op1_insert, op1_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
}
} else if (op1_comp->type == OT_DELETE) {
ot_comp_delete op1_delete = op1_comp->value.delete;
if (op2_comp->type == OT_SKIP) {
ot_comp_skip op2_skip = op2_comp->value.skip;
delta_pair p =
ot_xform_delete_skip(op1_delete, op1_iter.offset, op2_skip,
op2_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
} else if (op2_comp->type == OT_INSERT) {
ot_comp_insert op2_insert = op2_comp->value.insert;
delta_pair p =
ot_xform_delete_insert(op2_insert, op2_iter.offset, xform);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
} else if (op2_comp->type == OT_DELETE) {
ot_comp_delete op2_delete = op2_comp->value.delete;
delta_pair p = ot_xform_delete_delete(
op1_delete, op1_iter.offset, op2_delete, op2_iter.offset);
op1_next = ot_iter_skip(&op1_iter, p.delta1);
op2_next = ot_iter_skip(&op2_iter, p.delta2);
}
}
}
return xform;
}