-
Notifications
You must be signed in to change notification settings - Fork 1
/
braidvis004.html
1674 lines (1467 loc) · 59.1 KB
/
braidvis004.html
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<script src="https://dglittle.github.io/cdn/random001.js"></script>
<script src="https://dglittle.github.io/cdn/utils004.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/2.6.1/gl-matrix-min.js"></script>
<script src="https://invisible-college.github.io/universal-sync/diffsync.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
<script src="https://unpkg.com/d3-dag"></script>
<body style="margin:0px"></body>
<script>
// 55
Math.randomSeed(12)
var BOX_X = 20
var BOX_Y = 30
var FOREGROUNDS = {
m : '#C8C8C8', // AE
'0' : '#F15E5E',
'1' : '#0167FF',
'2' : '#24B04B',
'3' : '#B04BB0'
}
var BACKGROUNDS = {
m : '#F5F5F580', // EF
'0' : '#FFEFEBA0',
'1' : '#C5E8FFA0',
'2' : '#CAE9CFA0',
'3' : '#E9CFE9A0'
}
var top_grid = document.createElement('div')
document.body.append(top_grid)
top_grid.style.display = 'grid'
top_grid.style['grid-template-columns'] = 'auto auto'
var left = document.createElement('div')
top_grid.append(left)
var right = document.createElement('div')
top_grid.append(right)
function create_line(x1, y1, x2, y2, stroke, dasharray) {
var line = document.createElementNS('http://www.w3.org/2000/svg', 'line')
line.setAttribute('x1', x1)
line.setAttribute('y1', y1)
line.setAttribute('x2', x2)
line.setAttribute('y2', y2)
line.style['stroke-width'] = 1
line.setAttribute('stroke', stroke)
if (dasharray) line.setAttribute('stroke-dasharray', dasharray)
return line
}
function create_rect(x, y, w, h, stroke) {
var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
rect.setAttribute('x', x)
rect.setAttribute('y', y)
rect.setAttribute('width', w)
rect.setAttribute('height', h)
rect.style['stroke-width'] = 1
rect.style.stroke = stroke
rect.style.fill = '#FFFFFF80'
return rect
}
function create_char(x, y, text, fill, bold) {
var char = document.createElementNS('http://www.w3.org/2000/svg', 'text')
char.style.fill = fill
char.style.fontSize = 19
char.style.fontFamily = 'avenir, sans-serif'
if (bold) char.style.fontWeight = 'bold'
char.setAttribute('x', x)
char.setAttribute('y', y)
char.setAttribute('text-anchor', 'middle')
char.setAttribute('alignment-baseline', 'baseline')
char.appendChild(document.createTextNode(text))
return char
}
var space_dag_div = null
function update_space_dag(merge_info) {
if (!space_dag_div) {
space_dag_div = document.createElement('div')
document.body.append(space_dag_div)
}
space_dag_div.innerHTML = ''
svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
space_dag_div.append(svg)
svg.setAttribute('width', 1000)
svg.setAttribute('height', BOX_Y * 3)
var size = BOX_Y
// work here
// svg.append(create_line(size, size, size, size * 2, FOREGROUNDS.m))
// svg.append(create_line(size, size + size/2, size * 2, size * 1.5, FOREGROUNDS.m))
svg.append(create_line(size + 0.5, size, size + 0.5, size * 2, 'black'))
svg.append(create_line(size, size * 1.5 + 0.5, size * 2, size * 1.5 + 0.5, 'black'))
function draw_char_box(x, y, char, deleted) {
svg.append(create_char(x + size/2, y + size*0.75, char, deleted ? FOREGROUNDS.m : 'black', false))
}
function draw_char_boxes(x, y, text, deleted) {
// work here
// svg.append(create_rect(x, y, size * text.length, size, FOREGROUNDS.m))
svg.append(create_rect(x, y, size * text.length, size, 'black'))
for (var i = 0; i < text.length; i++) {
draw_char_box(x + i * size, y, text[i], deleted)
}
}
var next_top_x = size * 2
var next_bottom_x = size * 2
var last_top_was_middle = true
var last_bottom_was_middle = true
var very_first = true
var on_a = true
var ai = 0
var bi = 0
var a_text = merge_info.a_text
var b_text = merge_info.b_text
var a_regions = merge_info.a_regions
var b_regions = merge_info.b_regions
function draw_t(t, deleted, middle) {
if (middle) {
var x = Math.max(next_top_x, next_bottom_x)
if (!last_top_was_middle) {
if (next_top_x < x)
svg.append(create_line(next_top_x, size*0.75, x, size*0.75, 'black'))
svg.append(create_line(x, size*0.75, x + size/2 + 0.5, size*1.5 + 0.5, 'black'))
}
if (!last_bottom_was_middle) {
if (next_bottom_x < x)
svg.append(create_line(next_bottom_x, size*2.25, x, size*2.25, 'black'))
svg.append(create_line(x, size*2.25, x + size/2 + 0.5, size*1.5 + 0.5, 'black'))
}
if (!last_top_was_middle || !last_bottom_was_middle) x += size/2
if (last_top_was_middle && last_bottom_was_middle && t && !very_first) {
svg.append(create_line(x, size*1.5 + 0.5, x + size/2 + 0.5, size*1.5 + 0.5, 'black'))
x += size/2
}
if (t == '') {
svg.append(create_line(x, size * 1.5 + 0.5, x + size, size * 1.5 + 0.5, 'black'))
svg.append(create_line(x + size + 0.5, size, x + size + 0.5, size * 2, 'black'))
} else {
draw_char_boxes(x + 0.5, size + 0.5, t, deleted)
}
next_top_x = next_bottom_x = x + t.length * size
last_top_was_middle = true
last_bottom_was_middle = true
} else if (on_a) {
if (last_top_was_middle) {
svg.append(create_line(next_top_x, size*1.5 + 0.5, next_top_x + size/2 + 0.5, size*0.75, 'black'))
} else {
svg.append(create_line(next_top_x, size*0.75, next_top_x + size/2 + 0.5, size*0.75, 'black'))
}
draw_char_boxes(next_top_x + size/2 + 0.5, size*0.25, t, deleted)
next_top_x += size/2 + t.length * size
last_top_was_middle = false
} else {
if (last_bottom_was_middle) {
svg.append(create_line(next_bottom_x, size*1.5 + 0.5, next_bottom_x + size/2 + 0.5, size*2.25, 'black'))
} else {
svg.append(create_line(next_bottom_x, size*2.25, next_bottom_x + size/2 + 0.5, size*2.25, 'black'))
}
draw_char_boxes(next_bottom_x + size/2 + 0.5, size*1.75, t, deleted)
next_bottom_x += size/2 + t.length * size
last_bottom_was_middle = false
}
very_first = false
}
while (true) {
var aa = a_regions[ai]
var bb = b_regions[bi]
if (!aa && !bb) break
if (!aa) on_a = false
if (!bb) on_a = true
var ci = on_a ? ai : bi
var di = on_a ? bi : ai
var cc = on_a ? aa : bb
var dd = on_a ? bb : aa
var c_text = on_a ? a_text : b_text
var d_text = on_a ? b_text : a_text
if (cc[5] < di) {
var t = c_text.substr(cc[0], cc[1])
if (cc[2]) {
if (cc[3] < di) {
draw_t(t, true)
} else if (cc[3] == di) {
draw_t(t, false, true)
if (on_a) { bi++ } else { ai++ }
} else {
draw_t(t)
}
} else {
draw_t(t)
}
if (on_a) { ai++ } else { bi++ }
} else if (dd && dd[5] < ci) {
on_a = !on_a
} else {
throw 'failure'
}
}
draw_t('', false, true)
// svg.append(create_line(size*2, size*1.5, size*2.5, size*0.75, FOREGROUNDS.m))
// for (var i = 0; i < merge_info.a_text.length; i++) {
// svg.append(create_rect(size * 2.5 + i * size, size/4, size, size, FOREGROUNDS.m))
// svg.append(create_char(size * 2.5 + i * size + size/2, size/4 + size*0.75, merge_info.a_text[i], 'black', false))
// }
// svg.append(create_line(size*2, size*1.5, size*2.5, size*2.25, FOREGROUNDS.m))
// for (var i = 0; i < merge_info.b_text.length; i++) {
// svg.append(create_rect(size * 2.5 + i * size, size*2 - size/4, size, size, FOREGROUNDS.m))
// svg.append(create_char(size * 2.5 + i * size + size/2, size*2 - size/4 + size*0.75, merge_info.b_text[i], 'black', false))
// }
console.log('merge_info: ' + JSON.stringify(merge_info, null, 4))
// var d = document.createElement('div')
// d.textContent = JSON.stringify(merge_info, null, ' ')
// space_dag_div.append(d)
}
function sync7_create_simple_dag(s7) {
var selected = {}
var d = document.createElement('div')
var svg_div = document.createElement('div')
svg_div.style.cursor = 'pointer'
d.append(svg_div)
function create_arrow(x1, y1, x2, y2, stroke, dasharray, uid) {
var line = document.createElementNS('http://www.w3.org/2000/svg', 'line')
var start = vec3.fromValues(x1, y1, 0)
var end = vec3.fromValues(x2, y2, 0)
var to_end = vec3.subtract(vec3.create(), end, start)
var len = vec3.length(to_end)
var arrow = vec3.scale(vec3.create(), to_end, 1.0 / len * (len - 21))
line.setAttribute('marker-end', 'url(#marker_id_' + uid + ')')
line.setAttribute('x1', x1)
line.setAttribute('y1', y1)
line.setAttribute('x2', x1 + arrow[0])
line.setAttribute('y2', y1 + arrow[1])
line.style['stroke-width'] = 2
line.setAttribute('stroke', stroke)
if (dasharray) line.setAttribute('stroke-dasharray', dasharray)
return line
}
var svg = null
function redraw() {
if (svg) svg.remove()
svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg_div.append(svg)
svg.setAttribute('width', s7.width)
svg.setAttribute('height', s7.height)
var defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs')
svg.append(defs)
each(FOREGROUNDS, function (color, user) {
var marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker')
marker.setAttribute('id', 'marker_id_' + user)
marker.setAttribute('orient', 'auto')
marker.setAttribute('markerWidth', '10')
marker.setAttribute('markerHeight', '10')
marker.setAttribute('refX', '0')
marker.setAttribute('refY', '5')
defs.append(marker)
var path = document.createElementNS('http://www.w3.org/2000/svg', 'path')
path.setAttribute('d', 'M0,3 L5,5 L0,7 Z')
path.setAttribute('fill', color)
marker.append(path)
})
each(s7.commits, function (c, id) {
var parents = Object.keys(c.to_parents).length
var merge_node = parents == 0 || parents > 1
var uid = merge_node ? 'm' : c.uid
var foreground = FOREGROUNDS[uid]
each(c.to_parents, function (d, pid) {
svg.append(create_arrow(s7.commits[pid].x || 0, s7.commits[pid].y || 0, c.x || 0, c.y || 0, foreground, null, uid))
})
})
each(s7.commits, function (c, id) {
var parents = Object.keys(c.to_parents).length
var merge_node = parents == 0 || parents > 1
var uid = merge_node ? 'm' : c.uid
var foreground = FOREGROUNDS[uid]
var dot = document.createElementNS('http://www.w3.org/2000/svg', 'circle')
dot.style.fill = foreground
dot.setAttribute('cx', c.x || 0)
dot.setAttribute('cy', c.y || 0)
dot.setAttribute('r', '10')
svg.append(dot)
})
}
redraw()
return d
}
function sync7_create_fancy_dag(s7) {
var selected = {}
var d = document.createElement('div')
var svg_div = document.createElement('div')
svg_div.style.cursor = 'pointer'
d.append(svg_div)
function calc_specials(id) {
var specials = {}
each(s7.commits[id].to_parents, function (d, p) {
var offset = 0
each(d, function (d) {
if (typeof(d) == 'number') {
offset += d
} else {
for (var i = 0; i < d[0].length; i++) {
specials[offset++] = true
}
}
})
})
return specials
}
function create_char_boxes(x, y, text, selected, specials, merge_node, commit) {
var g = document.createElementNS('http://www.w3.org/2000/svg', 'g')
if (selected) {
var rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect')
rect.style.fill = 'rgba(255, 255, 0, 0.5)'
rect.setAttribute('x', x)
rect.setAttribute('y', y)
rect.setAttribute('width', text.length * BOX_X)
rect.setAttribute('height', BOX_Y)
g.append(rect)
}
g.append(create_line(x, Math.floor(y + BOX_Y) + 0.5, x + BOX_X * text.length, Math.floor(y + BOX_Y) + 0.5, FOREGROUNDS.m, '3'))
for (var i = 0; i < text.length; i++) {
var uid = merge_node ? 'm' : commit.uid
var foreground = FOREGROUNDS[uid]
g.append(create_char(Math.floor(x + i*BOX_X + BOX_X/2), y + BOX_Y*0.75, text[i], specials[i] ? foreground : FOREGROUNDS.m, specials[i] && !merge_node))
}
return g
}
function create_polygon(points, fill) {
var poly = document.createElementNS('http://www.w3.org/2000/svg', 'polygon')
poly.style.fill = fill
var points_text = []
for (var i = 0; i < points.length; i++) {
points_text.push((i == 0 ? '' : ' ') + points[i][0])
points_text.push(',' + points[i][1])
}
poly.setAttribute('points', points_text.join(''))
return poly
}
function create_polyline(points, stroke) {
var poly = document.createElementNS('http://www.w3.org/2000/svg', 'polyline')
poly.style.fill = 'none'
poly.style.stroke = stroke
poly.style['stroke-width'] = 1
var points_text = []
for (var i = 0; i < points.length; i++) {
points_text.push((i == 0 ? '' : ' ') + points[i][0])
points_text.push(',' + points[i][1])
}
poly.setAttribute('points', points_text.join(''))
return poly
}
function create_diff_lines(x1, y1, x2, y2, diff, merge_node, uid) {
var g = document.createElementNS('http://www.w3.org/2000/svg', 'g')
for (var pass = 0; pass <= 1; pass++) {
var child_offset = 0
var parent_offset = 0
each(diff, function (d) {
var points = []
points.push([x1 + child_offset + 0.5, y1 + BOX_Y])
points.push([x1 + child_offset + 0.5, y1])
points.push([x2 + parent_offset + 0.5, y2 + BOX_Y])
if (typeof(d) == 'number') {
child_offset += d * BOX_X
parent_offset += d * BOX_X
} else {
child_offset += d[0].length * BOX_X
parent_offset += d[1].length * BOX_X
}
points.push([x2 + parent_offset + 0.5, y2 + BOX_Y])
points.push([x1 + child_offset + 0.5, y1])
points.push([x1 + child_offset + 0.5, y1 + BOX_Y])
var foreground = null
var background = null
if (pass == 0 && merge_node && typeof(d) == 'number') {
foreground = FOREGROUNDS.m
background = BACKGROUNDS.m
} else if (pass == 1 && !merge_node && typeof(d) != 'number') {
foreground = FOREGROUNDS[uid]
background = BACKGROUNDS[uid]
} else if (pass == 0 && !merge_node && typeof(d) == 'number') {
foreground = FOREGROUNDS.m
background = BACKGROUNDS.m
}
if (foreground) {
g.append(create_polygon(points, background))
g.append(create_polyline(points.slice(0, 3), foreground))
g.append(create_polyline(points.slice(3), foreground))
}
})
}
return g
}
var svg = null
function redraw() {
if (svg) svg.remove()
svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg')
svg_div.append(svg)
svg.setAttribute('width', s7.width)
svg.setAttribute('height', s7.height)
each(s7.commits, function (c, id) {
var merge_node = Object.keys(c.to_parents).length > 1
var uid = merge_node ? 'm' : c.uid
each(c.to_parents, function (d, pid) {
svg.append(create_diff_lines(c.x || 0, c.y || 0, s7.commits[pid].x || 0, s7.commits[pid].y || 0, d, merge_node, uid))
})
})
each(s7.commits, function (c, id) {
var merge_node = Object.keys(c.to_parents).length > 1
svg.append(create_char_boxes(c.x || 0, c.y || 0, c.text || '', selected[id], calc_specials(id), merge_node, c))
})
}
redraw()
function getPos(e) {
var x = 0, y = 0
while (e != null) {
x += e.offsetLeft
y += e.offsetTop
e = e.offsetParent
}
return {x : x, y : y}
}
function getRelPos(to, positionedObject) {
var pos = getPos(to)
return {
x : positionedObject.pageX - pos.x,
y : positionedObject.pageY - pos.y
}
}
svg_div.onmousedown = function (e) {
e.preventDefault()
var pos = getRelPos(svg_div, e)
var grabbed = null
each(s7.commits, function (c, id) {
if (pos.x >= (c.x || 0) && pos.x < (c.x || 0) + (c.text || 'Q!?').length * BOX_X && pos.y >= (c.y || 0) && pos.y < (c.y || 0) + BOX_Y) {
if (!e.metaKey)
selected = {}
selected[id] = true
grabbed = {
id : id,
hot_x : pos.x - (c.x || 0),
hot_y : pos.y - (c.y || 0)
}
redraw()
return false
}
})
if (!grabbed) {
selected = {}
redraw()
}
if (grabbed) {
var commit = s7.commits[grabbed.id]
update_space_dag(commit.merge_info)
}
var oldMove = document.onmousemove
document.onmousemove = function (e) {
var pos = getRelPos(svg_div, e)
if (grabbed) {
s7.commits[grabbed.id].x = pos.x - grabbed.hot_x
s7.commits[grabbed.id].y = pos.y - grabbed.hot_y
redraw()
}
}
var oldUp = document.onmouseup
document.onmouseup = function (e) {
document.onmousemove = oldMove
document.onmouseup = oldUp
}
}
return d
}
var sync7 = {}
sync7.create = function () {
return {
commits : {
'root' : { to_parents : {}, from_kids : {} }
},
waiting_commits : {},
temp_commits : {},
real_leaves : ['root'],
leaf : 'root',
text : ''
}
}
sync7.commit = function (s7, s) {
if (s == s7.text) { return }
var cs = s7.temp_commits
s7.temp_commits = {}
var id = guid()
var to_parents = {}
s7.commits[s7.leaf].from_kids[id] = to_parents[s7.leaf] = sync7_diff(s, s7.text)
s7.commits[id] = cs[id] = { to_parents : to_parents, from_kids : {} }
s7.leaf = id
s7.real_leaves = [id]
s7.text = s
return cs
}
sync7.merge = function (s7, cs, cursors, custom_merge_func) {
if (!cursors) cursors = {}
if (!custom_merge_func) custom_merge_func = default_custom_merge_func
var cant_add_these = {}
function find_commit(id) {
return s7.commits[id] || cs[id] || s7.waiting_commits[id]
}
function mark_this_and_kids_as_unaddable(c, id) {
if (!cant_add_these[id]) {
cant_add_these[id] = true
each(c.from_kids, function (d, kid) {
var kidc = find_commit(kid)
if (kidc)
mark_this_and_kids_as_unaddable(kidc, kid)
})
}
}
function process_commit(c, id) {
var missing_parent = false
each(c.to_parents, function (d, p) {
if (!find_commit(p)) {
missing_parent = true
return false
}
})
if (missing_parent)
mark_this_and_kids_as_unaddable(c, id)
}
each(cs, process_commit)
each(s7.waiting_commits, process_commit)
if (Object.keys(cs).length + Object.keys(s7.waiting_commits).length - Object.keys(cant_add_these).length <= 0) {
Object.assign(s7.waiting_commits, cs)
return cursors
}
var projected_cursors = map(cursors, function (cursor) {
var node = s7.leaf
while (s7.temp_commits[node]) {
var old_node = node
each(s7.commits[node].to_parents, function (d, p) {
var offset = 0
var poffset = 0
each(d, function (d) {
if (typeof(d) == 'number') {
if (cursor <= offset + d) {
cursor = cursor - offset + poffset
node = p
return false
}
offset += d
poffset += d
} else {
offset += d[0].length
poffset += d[1].length
}
})
if (old_node != node) return false
})
if (old_node == node) throw 'failed to project cursor up'
}
return [cursor, node]
})
function process_commit2(c, id) {
if (cant_add_these[id]) {
if (cs[id])
s7.waiting_commits[id] = c
} else {
s7.commits[id] = c
if (s7.waiting_commits[id])
delete s7.waiting_commits[id]
each(c.to_parents, function (d, p) {
if (s7.commits[p]) {
s7.commits[p].from_kids[id] = d
}
})
}
}
each(cs, process_commit2)
each(s7.waiting_commits, process_commit2)
s7.real_leaves = sync7_get_leaves(s7.commits, s7.temp_commits)
var leaves = Object.keys(s7.real_leaves).sort()
var texts = {}
each(leaves, function (leaf) {
texts[leaf] = sync7_get_text(s7, leaf)
})
each(s7.temp_commits, function (c, k) {
each(c.to_parents, function (d, p) {
if (!s7.temp_commits[p]) {
delete s7.commits[p].from_kids[k]
}
})
delete s7.commits[k]
})
s7.temp_commits = {}
var prev_merge_node = leaves[0]
var ancestors = sync7_get_ancestors(s7, prev_merge_node)
for (var i = 1; i < leaves.length; i++) {
var leaf = leaves[i]
var i_ancestors = sync7_get_ancestors(s7, leaf)
var CAs = sync7_intersection(ancestors, i_ancestors)
var LCAs = sync7_get_leaves(CAs)
each(i_ancestors, function (v, k) {
ancestors[k] = v
})
function get_nodes_on_path_to_LCAs(node) {
var agg = {}
function helper(x) {
var hit_LCA = LCAs[x]
if (!CAs[x]) {
each(s7.commits[x].to_parents, function (d, p) {
hit_LCA = helper(p) || hit_LCA
})
}
if (hit_LCA) {
agg[x] = true
return true
}
}
helper(node)
return agg
}
function calc_dividers_and_such_for_node(node, nodes_on_path_to_LCAs, dividers, untouched_regions_for_node) {
untouched_regions_for_node[node] = [[0, texts[node].length, 0]]
function helper(node) {
if (untouched_regions_for_node[node]) return untouched_regions_for_node[node]
var pur = {}
each(s7.commits[node].from_kids, function (d, k) {
if (!nodes_on_path_to_LCAs[k]) { return }
var untouched = helper(k)
var ui = 0
var uo = 0
var offset = 0
var poffset = 0
each(d, function (r) {
var end_point = offset + ((typeof(r) == 'number') ? r : r[0].length)
while (untouched[ui] && end_point >= untouched[ui][2] + untouched[ui][1]) {
if (typeof(r) == 'number') {
var x = untouched[ui][2] + uo - offset + poffset
pur[x] = [untouched[ui][0] + uo, untouched[ui][1] - uo, x]
}
ui++
uo = 0
}
if (!untouched[ui]) { return false }
if (end_point > untouched[ui][2] + uo) {
if (typeof(r) == 'number') {
var x = untouched[ui][2] + uo - offset + poffset
pur[x] = [untouched[ui][0] + uo, end_point - (untouched[ui][2] + uo), x]
}
uo = end_point - untouched[ui][2]
dividers[untouched[ui][0] + uo] = untouched[ui][0] + uo
}
offset = end_point
poffset += (typeof(r) == 'number') ? r : r[1].length
})
})
return untouched_regions_for_node[node] = Object.values(pur).sort(function (a, b) { return a[2] - b[2] })
}
each(LCAs, function (_, lca) { helper(lca) })
}
var prev_nodes_on_path_to_LCAs = get_nodes_on_path_to_LCAs(prev_merge_node)
var prev_dividers = {}
var prev_untouched_regions_for_node = {}
calc_dividers_and_such_for_node(prev_merge_node, prev_nodes_on_path_to_LCAs, prev_dividers, prev_untouched_regions_for_node)
var leaf_nodes_on_path_to_LCAs = get_nodes_on_path_to_LCAs(leaf)
var leaf_dividers = {}
var leaf_untouched_regions_for_node = {}
calc_dividers_and_such_for_node(leaf, leaf_nodes_on_path_to_LCAs, leaf_dividers, leaf_untouched_regions_for_node)
each(LCAs, function (_, lca) {
function do_one_against_the_other(a, b, dividers) {
var bb, bi = 0
each(a, function (aa) {
while ((bb = b[bi]) && (bb[2] + bb[1] <= aa[2])) bi++
if (bb && bb[2] < aa[2]) {
var x = aa[2] - bb[2] + bb[0]
dividers[x] = x
}
while ((bb = b[bi]) && (bb[2] + bb[1] <= aa[2] + aa[1])) bi++
if (bb && bb[2] < aa[2] + aa[1]) {
var x = aa[2] + aa[1] - bb[2] + bb[0]
dividers[x] = x
}
})
}
var a = prev_untouched_regions_for_node[lca]
var b = leaf_untouched_regions_for_node[lca]
do_one_against_the_other(a, b, leaf_dividers)
do_one_against_the_other(b, a, prev_dividers)
})
function calc_endpoints(dividers, node) {
var endpoints = []
endpoints.push([0, 0, 0])
each(Object.values(dividers).sort(function (a, b) { return a - b }), function (offset) {
endpoints.push([offset, 1, offset])
endpoints.push([offset, 0, offset])
})
endpoints.push([texts[node].length, 1, texts[node].length])
return endpoints
}
var prev_endpoints = calc_endpoints(prev_dividers, prev_merge_node)
var leaf_endpoints = calc_endpoints(leaf_dividers, leaf)
function project_endpoints_to_LCAs(endpoints, node, nodes_on_path_to_LCAs) {
var endpoints_for_node = {}
endpoints_for_node[node] = endpoints
function helper(node) {
if (endpoints_for_node[node]) return endpoints_for_node[node]
var agg = {}
function add_to_agg(endpoint, projected_pos) {
var key = '[' + endpoint[0] + ',' + endpoint[1] + ']'
if (endpoint[1] == 0)
agg[key] = Math.min(agg[key] || Infinity, projected_pos)
else
agg[key] = Math.max(agg[key] || -Infinity, projected_pos)
}
each(s7.commits[node].from_kids, function (d, k) {
if (!nodes_on_path_to_LCAs[k]) { return }
var endpoints = helper(k)
var ei = 0
var offset = 0
var poffset = 0
each(d, function (d) {
var end = offset + ((typeof(d) == 'number') ? d : d[0].length)
while (endpoints[ei] && (endpoints[ei][2] < end || (endpoints[ei][1] == 1 && endpoints[ei][2] <= end))) {
if (typeof(d) == 'number') {
add_to_agg(endpoints[ei], endpoints[ei][2] - offset + poffset)
} else if (endpoints[ei][1] == 0) {
add_to_agg(endpoints[ei], poffset)
} else {
add_to_agg(endpoints[ei], poffset + d[1].length)
}
ei++
}
offset = end
poffset += (typeof(d) == 'number') ? d : d[1].length
})
while (endpoints[ei]) {
add_to_agg(endpoints[ei], poffset)
ei++
}
})
var endpoints = []
each(agg, function (v, k) {
var kk = eval(k)
endpoints.push([kk[0], kk[1], v])
})
return endpoints_for_node[node] = endpoints.sort(function (a, b) {
if (a[2] != b[2])
return a[2] - b[2]
return b[1] - a[1]
})
}
var regions_for_node = {}
var lookup_by_begin = {}
var lookup_by_end = {}
var base_regions = []
regions_for_node[node] = base_regions
for (var i = 0; i < endpoints.length; i += 2) {
var e0 = endpoints[i][0]
var e1 = endpoints[i + 1][0]
base_regions.push([e0, e1 - e0])
lookup_by_begin[e0] = base_regions.length - 1
lookup_by_end[e1] = base_regions.length - 1
}
each(LCAs, function (_, lca) {
var endpoints = helper(lca)
var regions = []
regions_for_node[lca] = regions
each(endpoints, function (e) {
if (e[1] == 0) {
var i = lookup_by_begin[e[0]];
(regions[i] = regions[i] || [])[0] = e[2]
} else {
var i = lookup_by_end[e[0]];
(regions[i] = regions[i] || [])[1] = e[2]
}
})
each(regions, function (r) {
r[1] = r[1] - r[0]
})
})
return regions_for_node
}
var prev_regions_per_node = project_endpoints_to_LCAs(prev_endpoints, prev_merge_node, prev_nodes_on_path_to_LCAs)
var leaf_regions_per_node = project_endpoints_to_LCAs(leaf_endpoints, leaf, leaf_nodes_on_path_to_LCAs)
var prev_regions = prev_regions_per_node[prev_merge_node]
var leaf_regions = leaf_regions_per_node[leaf]
var prev_untouched_regions_for_LCA_by_position = {}
var leaf_untouched_regions_for_LCA_by_position = {}
each(LCAs, function (_, lca) {
function process(base, regions, untouched, _by_position) {
_by_position[lca] = {}
var ri = 0
var r
each(untouched, function (u) {
while ((r = regions[ri]) && r[0] + r[1] <= u[2]) { ri++ }
while ((r = regions[ri]) && r[0] < u[2] + u[1]) {
_by_position[lca][r[0]] = ri
base[ri][2] = true
r[2] = true
ri++
}
})
}
process(prev_regions_per_node[prev_merge_node], prev_regions_per_node[lca], prev_untouched_regions_for_node[lca], prev_untouched_regions_for_LCA_by_position)
process(leaf_regions_per_node[leaf], leaf_regions_per_node[lca], leaf_untouched_regions_for_node[lca], leaf_untouched_regions_for_LCA_by_position)
})
function mark_deletes_and_more(regions_for_node, node, other_untouched_for_LCA_by_position) {
each(regions_for_node[node], function (r, ri) {
r[4] = r[5] = -1 // <-- the "more"
if (r[2]) {
r[3] = -1
each(LCAs, function (_, lca) {
var rr = regions_for_node[lca][ri]
var other_ri = other_untouched_for_LCA_by_position[lca][rr[0]]
if (rr[2] && (typeof(other_ri) == 'number')) {
r[3] = other_ri
return false
}
})
}
})
}
mark_deletes_and_more(prev_regions_per_node, prev_merge_node, leaf_untouched_regions_for_LCA_by_position)
mark_deletes_and_more(leaf_regions_per_node, leaf, prev_untouched_regions_for_LCA_by_position)
function is_definitely_before(a_regions, a_node, ai, b_regions, b_node, bi) {
var a_before_b = false
var b_before_a = false
each(LCAs, function (_, lca) {
var ar = a_regions[lca][ai]
var br = b_regions[lca][bi]
if ((ar[1] || br[1]) && (ar[0] + ar[1] <= br[0]))
a_before_b = true
if ((!ar[1] && !br[1]) && (ar[0] < br[0]))
a_before_b = true
if ((ar[1] || br[1]) && (br[0] + br[1] <= ar[0]))
b_before_a = true
if ((!ar[1] && !br[1]) && (br[0] < ar[0]))
b_before_a = true
})
return a_before_b && !b_before_a
}
function calc_known_orderings(a_regions, a_node, b_regions, b_node) {
var bi = 0
each(a_regions[a_node], function (ar, ai) {
for ( ; bi < b_regions[b_node].length; bi++) {
if (is_definitely_before(a_regions, a_node, ai, b_regions, b_node, bi)) {
ar[4] = bi
b_regions[b_node][bi][5] = ai
return
}
}
})
}
calc_known_orderings(prev_regions_per_node, prev_merge_node, leaf_regions_per_node, leaf)
calc_known_orderings(leaf_regions_per_node, leaf, prev_regions_per_node, prev_merge_node)
var m = custom_merge_func(s7, prev_merge_node, leaf, texts[prev_merge_node], texts[leaf], prev_regions, leaf_regions)
var id = guid()
var to_parents = {}
s7.commits[prev_merge_node].from_kids[id] = to_parents[prev_merge_node] = m.to_a
s7.commits[leaf].from_kids[id] = to_parents[leaf] = m.to_b
s7.commits[id] = s7.temp_commits[id] = {
to_parents : to_parents,
from_kids : {},
merge_info : {
a_text : texts[prev_merge_node],
b_text : texts[leaf],
a_regions : prev_regions,
b_regions : leaf_regions
}
}
prev_merge_node = id
texts[prev_merge_node] = m.text
}
s7.leaf = prev_merge_node
s7.text = texts[prev_merge_node]
return map(projected_cursors, function (cursor) {
while (cursor[1] != s7.leaf) {
var old_node = cursor[1]
var kids = s7.commits[cursor[1]].from_kids
var kid = Object.keys(kids)[0]
var d = kids[kid]