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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
|
const std = @import("std");
const assert = std.debug.assert;
const build_config = @import("../build_config.zig");
const ArenaAllocator = std.heap.ArenaAllocator;
const Allocator = std.mem.Allocator;
/// SplitTree represents a tree of view types that can be divided.
///
/// Concretely for Ghostty, it represents a tree of terminal views. In
/// its basic state, there are no splits and it is a single full-sized
/// terminal. However, it can be split arbitrarily many times among two
/// axes (horizontal and vertical) to create a tree of terminal views.
///
/// This is an immutable tree structure, meaning all operations on it
/// will return a new tree with the operation applied. This allows us to
/// store versions of the tree in a history for easy undo/redo. To facilitate
/// this, the stored View type must implement reference counting; this is left
/// as an implementation detail of the View type.
///
/// The View type will be stored as a pointer within the tree and must
/// implement a number of functions to work properly:
///
/// - `fn ref(*View, Allocator) Allocator.Error!*View` - Increase a
/// reference count of the view. The Allocator will be the allocator provided
/// to the tree operation. This is allowed to copy the value if it wants to;
/// the returned value is expected to be a new reference (but that may
/// just be a copy).
///
/// - `fn unref(*View, Allocator) void` - Decrease the reference count of a
/// view. The Allocator will be the allocator provided to the tree
/// operation.
///
/// - `fn eql(*const View, *const View) bool` - Check if two views are equal.
///
/// Optionally the following functions can also be implemented:
///
/// - `fn splitTreeLabel(*const View) []const u8` - Return a label that is used
/// for the debug view. If this isn't specified then the node handle
/// will be used.
///
/// Note: for both the ref and unref functions, the allocator is optional.
/// If the functions take less arguments, then the allocator will not be
/// passed.
pub fn SplitTree(comptime V: type) type {
return struct {
const Self = @This();
/// The view that this tree contains.
pub const View = V;
/// The arena allocator used for all allocations in the tree.
/// Since the tree is an immutable structure, this lets us
/// cleanly free all memory when the tree is deinitialized.
arena: ArenaAllocator,
/// All the nodes in the tree. Node at index 0 is always the root.
nodes: []const Node,
/// The handle of the zoomed node. A "zoomed" node is one that is
/// expected to be made the full size of the split tree. Various
/// operations may unzoom (e.g. resize).
zoomed: ?Node.Handle,
/// An empty tree.
pub const empty: Self = .{
// Arena can be undefined because we have zero allocated nodes.
// If our nodes are empty our deinit function doesn't touch the
// arena.
.arena = undefined,
.nodes = &.{},
.zoomed = null,
};
pub const Node = union(enum) {
leaf: *View,
split: Split,
/// A handle into the nodes array. This lets us keep track of
/// nodes with 16-bit handles rather than full pointer-width
/// values.
pub const Handle = enum(Backing) {
root = 0,
_,
pub const Backing = u16;
pub inline fn idx(self: Handle) usize {
return @intFromEnum(self);
}
/// Offset the handle by a given amount.
pub fn offset(self: Handle, v: usize) Handle {
const self_usize: usize = @intCast(@intFromEnum(self));
const final = self_usize + v;
assert(final < std.math.maxInt(Backing));
return @enumFromInt(final);
}
};
};
pub const Split = struct {
layout: Layout,
ratio: f16,
left: Node.Handle,
right: Node.Handle,
pub const Layout = enum { horizontal, vertical };
pub const Direction = enum { left, right, down, up };
};
/// Initialize a new tree with a single view.
pub fn init(gpa: Allocator, view: *View) Allocator.Error!Self {
var arena = ArenaAllocator.init(gpa);
errdefer arena.deinit();
const alloc = arena.allocator();
const nodes = try alloc.alloc(Node, 1);
nodes[0] = .{ .leaf = try viewRef(view, gpa) };
errdefer viewUnref(view, gpa);
return .{
.arena = arena,
.nodes = nodes,
.zoomed = null,
};
}
pub fn deinit(self: *Self) void {
// Important: only free memory if we have memory to free,
// because we use an undefined arena for empty trees.
if (self.nodes.len > 0) {
// Unref all our views
const gpa: Allocator = self.arena.child_allocator;
for (self.nodes) |node| switch (node) {
.leaf => |view| viewUnref(view, gpa),
.split => {},
};
self.arena.deinit();
}
self.* = undefined;
}
/// Clone this tree, returning a new tree with the same nodes.
pub fn clone(self: *const Self, gpa: Allocator) Allocator.Error!Self {
// If we're empty then return an empty tree.
if (self.isEmpty()) return .empty;
// Create a new arena allocator for the clone.
var arena = ArenaAllocator.init(gpa);
errdefer arena.deinit();
const alloc = arena.allocator();
// Allocate a new nodes array and copy the existing nodes into it.
const nodes = try alloc.dupe(Node, self.nodes);
// Increase the reference count of all the views in the nodes.
try refNodes(gpa, nodes);
return .{
.arena = arena,
.nodes = nodes,
.zoomed = self.zoomed,
};
}
/// Returns true if this is an empty tree.
pub fn isEmpty(self: *const Self) bool {
// An empty tree has no nodes.
return self.nodes.len == 0;
}
/// An iterator over all the views in the tree.
pub fn iterator(
self: *const Self,
) Iterator {
return .{ .nodes = self.nodes };
}
pub const ViewEntry = struct {
handle: Node.Handle,
view: *View,
};
pub const Iterator = struct {
i: Node.Handle = .root,
nodes: []const Node,
pub fn next(self: *Iterator) ?ViewEntry {
// If we have no nodes, return null.
if (@intFromEnum(self.i) >= self.nodes.len) return null;
// Get the current node and increment the index.
const handle = self.i;
self.i = @enumFromInt(handle.idx() + 1);
const node = self.nodes[handle.idx()];
return switch (node) {
.leaf => |v| .{ .handle = handle, .view = v },
.split => self.next(),
};
}
};
/// Change the zoomed state to the given node. Assumes the handle
/// is valid.
pub fn zoom(self: *Self, handle: ?Node.Handle) void {
if (handle) |v| {
assert(@intFromEnum(v) >= 0);
assert(@intFromEnum(v) < self.nodes.len);
}
self.zoomed = handle;
}
pub const Goto = union(enum) {
/// Previous view, null if we're the first view.
previous,
/// Next view, null if we're the last view.
next,
/// Previous view, but wrapped around to the last view. May
/// return the same view if this is the first view.
previous_wrapped,
/// Next view, but wrapped around to the first view. May return
/// the same view if this is the last view.
next_wrapped,
/// A spatial direction. "Spatial" means that the direction is
/// based on the nearest surface in the given direction visually
/// as the surfaces are laid out on a 2D grid.
spatial: Spatial.Direction,
};
/// Goto a view from a certain point in the split tree. Returns null
/// if the direction results in no visitable view.
///
/// Allocator is only used for temporary state for spatial navigation.
pub fn goto(
self: *const Self,
alloc: Allocator,
from: Node.Handle,
to: Goto,
) Allocator.Error!?Node.Handle {
return switch (to) {
.previous => self.previous(from),
.next => self.next(from),
.previous_wrapped => self.previous(from) orelse self.deepest(.right, .root),
.next_wrapped => self.next(from) orelse self.deepest(.left, .root),
.spatial => |d| spatial: {
// Get our spatial representation.
var sp = try self.spatial(alloc);
defer sp.deinit(alloc);
break :spatial self.nearest(sp, from, d);
},
};
}
pub const Side = enum { left, right };
/// Returns the deepest view in the tree in the given direction.
/// This can be used to find the leftmost/rightmost surface within
/// a given split structure.
pub fn deepest(
self: *const Self,
side: Side,
from: Node.Handle,
) Node.Handle {
var current: Node.Handle = from;
while (true) {
switch (self.nodes[current.idx()]) {
.leaf => return current,
.split => |s| current = switch (side) {
.left => s.left,
.right => s.right,
},
}
}
}
/// Returns the previous view from the given node handle (which itself
/// doesn't need to be a view). If there is no previous (this is the
/// most previous view) then this will return null.
///
/// "Previous" is defined as the previous node in an in-order
/// traversal of the tree. This isn't a perfect definition and we
/// may want to change this to something that better matches a
/// spatial view of the tree later.
fn previous(self: *const Self, from: Node.Handle) ?Node.Handle {
return switch (self.previousBacktrack(from, .root)) {
.result => |v| v,
.backtrack, .deadend => null,
};
}
/// Same as `previous`, but returns the next view instead.
fn next(self: *const Self, from: Node.Handle) ?Node.Handle {
return switch (self.nextBacktrack(from, .root)) {
.result => |v| v,
.backtrack, .deadend => null,
};
}
// Design note: we use a recursive backtracking search because
// split trees are never that deep, so we can abuse the stack as
// a safe allocator (stack overflow unlikely unless the kernel is
// tuned in some really weird way).
const Backtrack = union(enum) {
deadend,
backtrack,
result: Node.Handle,
};
fn previousBacktrack(
self: *const Self,
from: Node.Handle,
current: Node.Handle,
) Backtrack {
// If we reached the point that we're trying to find the previous
// value of, then we need to backtrack from here.
if (from == current) return .backtrack;
return switch (self.nodes[current.idx()]) {
// If we hit a leaf that isn't our target, then deadend.
.leaf => .deadend,
.split => |s| switch (self.previousBacktrack(from, s.left)) {
.result => |v| .{ .result = v },
// Backtrack from the left means we have to continue
// backtracking because we can't see what's before the left.
.backtrack => .backtrack,
// If we hit a deadend on the left then let's move right.
.deadend => switch (self.previousBacktrack(from, s.right)) {
.result => |v| .{ .result = v },
// Deadend means its not in this split at all since
// we already tracked the left.
.deadend => .deadend,
// Backtrack means that its in our left view because
// we can see the immediate previous and there MUST
// be leaves (we can't have split-only leaves).
.backtrack => .{ .result = self.deepest(.right, s.left) },
},
},
};
}
// See previousBacktrack for detailed comments. This is a mirror
// of that.
fn nextBacktrack(
self: *const Self,
from: Node.Handle,
current: Node.Handle,
) Backtrack {
if (from == current) return .backtrack;
return switch (self.nodes[current.idx()]) {
.leaf => .deadend,
.split => |s| switch (self.nextBacktrack(from, s.right)) {
.result => |v| .{ .result = v },
.backtrack => .backtrack,
.deadend => switch (self.nextBacktrack(from, s.left)) {
.result => |v| .{ .result = v },
.deadend => .deadend,
.backtrack => .{ .result = self.deepest(.left, s.right) },
},
},
};
}
/// Returns the nearest leaf node (view) in the given direction.
fn nearest(
self: *const Self,
sp: Spatial,
from: Node.Handle,
direction: Spatial.Direction,
) ?Node.Handle {
const target = sp.slots[from.idx()];
var result: ?struct {
handle: Node.Handle,
distance: f16,
} = null;
for (sp.slots, 0..) |slot, handle| {
// Never match ourself
if (handle == from.idx()) continue;
// Only match leaves
switch (self.nodes[handle]) {
.leaf => {},
.split => continue,
}
// Ensure it is in the proper direction
if (!switch (direction) {
.left => slot.maxX() <= target.x,
.right => slot.x >= target.maxX(),
.up => slot.maxY() <= target.y,
.down => slot.y >= target.maxY(),
}) continue;
// Track our distance
const dx = slot.x - target.x;
const dy = slot.y - target.y;
const distance = @sqrt(dx * dx + dy * dy);
// If we have a nearest it must be closer.
if (result) |n| {
if (distance >= n.distance) continue;
}
result = .{
.handle = @enumFromInt(handle),
.distance = distance,
};
}
return if (result) |n| n.handle else null;
}
/// Resize the given node in place. The node MUST be a split (asserted).
///
/// In general, this is an immutable data structure so this is
/// heavily discouraged. However, this is provided for convenience
/// and performance reasons where its very important for GUIs to
/// update the ratio during a live resize than to redraw the entire
/// widget tree.
pub fn resizeInPlace(
self: *Self,
at: Node.Handle,
ratio: f16,
) void {
// Let's talk about this constCast. Our member are const but
// we actually always own their memory. We don't want consumers
// who directly access the nodes to be able to modify them
// (without nasty stuff like this), but given this is internal
// usage its perfectly fine to modify the node in-place.
const s: *Split = @constCast(&self.nodes[at.idx()].split);
s.ratio = ratio;
}
/// Insert another tree into this tree at the given node in the
/// specified direction. The other tree will be inserted in the
/// new direction. For example, if the direction is "right" then
/// `insert` is inserted right of the existing node.
///
/// The allocator will be used for the newly created tree.
/// The previous trees will not be freed, but reference counts
/// for the views will be increased accordingly for the new tree.
pub fn split(
self: *const Self,
gpa: Allocator,
at: Node.Handle,
direction: Split.Direction,
ratio: f16,
insert: *const Self,
) Allocator.Error!Self {
// The new arena for our new tree.
var arena = ArenaAllocator.init(gpa);
errdefer arena.deinit();
const alloc = arena.allocator();
// We know we're going to need the sum total of the nodes
// between the two trees plus one for the new split node.
const nodes = try alloc.alloc(Node, self.nodes.len + insert.nodes.len + 1);
if (nodes.len > std.math.maxInt(Node.Handle.Backing)) return error.OutOfMemory;
// We can copy our nodes exactly as they are, since they're
// mostly not changing (only `at` is changing).
@memcpy(nodes[0..self.nodes.len], self.nodes);
// We can copy the destination nodes as well directly next to
// the source nodes. We just have to go through and offset
// all the handles in the destination tree to account for
// the shift.
const nodes_inserted = nodes[self.nodes.len..][0..insert.nodes.len];
@memcpy(nodes_inserted, insert.nodes);
for (nodes_inserted) |*node| switch (node.*) {
.leaf => {},
.split => |*s| {
// We need to offset the handles in the split
s.left = s.left.offset(self.nodes.len);
s.right = s.right.offset(self.nodes.len);
},
};
// Determine our split layout and if we're on the left
const layout: Split.Layout, const left: bool = switch (direction) {
.left => .{ .horizontal, true },
.right => .{ .horizontal, false },
.up => .{ .vertical, true },
.down => .{ .vertical, false },
};
// Copy our previous value to the end of the nodes list and
// create our new split node.
nodes[nodes.len - 1] = nodes[at.idx()];
nodes[at.idx()] = .{ .split = .{
.layout = layout,
.ratio = ratio,
.left = @enumFromInt(if (left) self.nodes.len else nodes.len - 1),
.right = @enumFromInt(if (left) nodes.len - 1 else self.nodes.len),
} };
// We need to increase the reference count of all the nodes.
try refNodes(gpa, nodes);
return .{
.arena = arena,
.nodes = nodes,
// Splitting always resets zoom state.
.zoomed = null,
};
}
/// Remove a node from the tree.
pub fn remove(
self: *Self,
gpa: Allocator,
at: Node.Handle,
) Allocator.Error!Self {
assert(at.idx() < self.nodes.len);
// If we're removing node zero then we're clearing the tree.
if (at == .root) return .empty;
// The new arena for our new tree.
var arena = ArenaAllocator.init(gpa);
errdefer arena.deinit();
const alloc = arena.allocator();
// Allocate our new nodes list with the number of nodes we'll
// need after the removal.
const nodes = try alloc.alloc(Node, self.countAfterRemoval(
.root,
at,
0,
));
var result: Self = .{
.arena = arena,
.nodes = nodes,
.zoomed = null,
};
// Traverse the tree and copy all our nodes into place.
assert(self.removeNode(
&result,
0,
.root,
at,
) != 0);
// Increase the reference count of all the nodes.
try refNodes(gpa, nodes);
return result;
}
fn removeNode(
old: *Self,
new: *Self,
new_offset: usize,
current: Node.Handle,
target: Node.Handle,
) usize {
assert(current != target);
// If we have a zoomed node and this is it then we migrate it.
if (old.zoomed) |v| {
if (v == current) {
assert(new.zoomed == null);
new.zoomed = @enumFromInt(new_offset);
}
}
// Let's talk about this constCast. Our member are const but
// we actually always own their memory. We don't want consumers
// who directly access the nodes to be able to modify them
// (without nasty stuff like this), but given this is internal
// usage its perfectly fine to modify the node in-place.
const new_nodes: []Node = @constCast(new.nodes);
switch (old.nodes[current.idx()]) {
// Leaf is simple, just copy it over. We don't ref anything
// yet because it'd make undo (errdefer) harder. We do that
// all at once later.
.leaf => |view| {
new_nodes[new_offset] = .{ .leaf = view };
return 1;
},
.split => |s| {
// If we're removing one of the split node sides then
// we remove the split node itself as well and only add
// the other (non-removed) side.
if (s.left == target) return old.removeNode(
new,
new_offset,
s.right,
target,
);
if (s.right == target) return old.removeNode(
new,
new_offset,
s.left,
target,
);
// Neither side is being directly removed, so we traverse.
const left = old.removeNode(
new,
new_offset + 1,
s.left,
target,
);
assert(left != 0);
const right = old.removeNode(
new,
new_offset + left + 1,
s.right,
target,
);
assert(right != 0);
new_nodes[new_offset] = .{ .split = .{
.layout = s.layout,
.ratio = s.ratio,
.left = @enumFromInt(new_offset + 1),
.right = @enumFromInt(new_offset + 1 + left),
} };
return left + right + 1;
},
}
}
/// Returns the number of nodes that would be needed to store
/// the tree if the target node is removed.
fn countAfterRemoval(
self: *Self,
current: Node.Handle,
target: Node.Handle,
acc: usize,
) usize {
assert(current != target);
return switch (self.nodes[current.idx()]) {
// Leaf is simple, always takes one node.
.leaf => acc + 1,
// Split is slightly more complicated. If either side is the
// target to remove, then we remove the split node as well
// so our count is just the count of the other side.
//
// If neither side is the target, then we count both sides
// and add one to account for the split node itself.
.split => |s| if (s.left == target) self.countAfterRemoval(
s.right,
target,
acc,
) else if (s.right == target) self.countAfterRemoval(
s.left,
target,
acc,
) else self.countAfterRemoval(
s.left,
target,
acc,
) + self.countAfterRemoval(
s.right,
target,
acc,
) + 1,
};
}
/// Reference all the nodes in the given slice, handling unref if
/// any fail. This should be called LAST so you don't have to undo
/// the refs at any further point after this.
fn refNodes(gpa: Allocator, nodes: []Node) Allocator.Error!void {
// We need to increase the reference count of all the nodes.
// Careful accounting here so that we properly unref on error
// only the nodes we referenced.
var reffed: usize = 0;
errdefer for (0..reffed) |i| {
switch (nodes[i]) {
.split => {},
.leaf => |view| viewUnref(view, gpa),
}
};
for (0..nodes.len) |i| {
switch (nodes[i]) {
.split => {},
.leaf => |view| nodes[i] = .{ .leaf = try viewRef(view, gpa) },
}
reffed = i;
}
assert(reffed == nodes.len - 1);
}
/// Equalize this node and all its children, returning a new node with splits
/// adjusted so that each split's ratio is based on the relative weight
/// (number of leaves) of its children.
pub fn equalize(
self: *const Self,
gpa: Allocator,
) Allocator.Error!Self {
if (self.isEmpty()) return .empty;
// Create a new arena allocator for the clone.
var arena = ArenaAllocator.init(gpa);
errdefer arena.deinit();
const alloc = arena.allocator();
// Allocate a new nodes array and copy the existing nodes into it.
const nodes = try alloc.dupe(Node, self.nodes);
// Go through and equalize our ratios based on weights.
for (nodes) |*node| switch (node.*) {
.leaf => {},
.split => |*s| {
const weight_left = self.weight(s.left, s.layout, 0);
const weight_right = self.weight(s.right, s.layout, 0);
assert(weight_left > 0);
assert(weight_right > 0);
const total_f16: f16 = @floatFromInt(weight_left + weight_right);
const weight_left_f16: f16 = @floatFromInt(weight_left);
s.ratio = weight_left_f16 / total_f16;
},
};
// Increase the reference count of all the views in the nodes.
try refNodes(gpa, nodes);
return .{
.arena = arena,
.nodes = nodes,
.zoomed = self.zoomed,
};
}
fn weight(
self: *const Self,
from: Node.Handle,
layout: Split.Layout,
acc: usize,
) usize {
return switch (self.nodes[from.idx()]) {
.leaf => acc + 1,
.split => |s| if (s.layout == layout)
self.weight(s.left, layout, acc) +
self.weight(s.right, layout, acc)
else
1,
};
}
/// Resize the nearest split matching the layout by the given ratio.
/// Positive is right and down.
///
/// The ratio is a value between 0 and 1 representing the percentage
/// to move the divider in the given direction. The percentage is
/// of the entire grid size, not just the specific split size.
/// We use the entire grid size because that's what Ghostty's
/// `resize_split` keybind does, because it maps to a general human
/// understanding of moving a split relative to the entire window
/// (generally).
///
/// For example, a ratio of 0.1 and a layout of `vertical` will find
/// the nearest vertical split and move the divider down by 10% of
/// the total grid height.
///
/// If no matching split is found, this does nothing, but will always
/// still return a cloned tree.
pub fn resize(
self: *const Self,
gpa: Allocator,
from: Node.Handle,
layout: Split.Layout,
ratio: f16,
) Allocator.Error!Self {
assert(ratio >= 0 and ratio <= 1);
assert(!std.math.isNan(ratio));
assert(!std.math.isInf(ratio));
// Fast path empty trees.
if (self.isEmpty()) return .empty;
// From this point forward worst case we return a clone.
var result = try self.clone(gpa);
errdefer result.deinit();
// Find our nearest parent split node matching the layout.
const parent_handle = switch (self.findParentSplit(
layout,
from,
.root,
)) {
.deadend, .backtrack => return result,
.result => |v| v,
};
// Get our spatial layout, because we need the dimensions of this
// split with regards to the entire grid.
var sp = try result.spatial(gpa);
defer sp.deinit(gpa);
// Get the ratio of the split relative to the full grid.
const full_ratio = full_ratio: {
// Our scale is the amount we need to multiply our individual
// ratio by to get the full ratio. Its actually a ratio on its
// own but I'm trying to avoid that word: its the ratio of
// our spatial width/height to the total.
const scale = switch (layout) {
.horizontal => sp.slots[parent_handle.idx()].width / sp.slots[0].width,
.vertical => sp.slots[parent_handle.idx()].height / sp.slots[0].height,
};
const current = result.nodes[parent_handle.idx()].split.ratio;
break :full_ratio current * scale;
};
// Set the final new ratio, clamping it to [0, 1]
result.resizeInPlace(
parent_handle,
@min(@max(full_ratio + ratio, 0), 1),
);
return result;
}
fn findParentSplit(
self: *const Self,
layout: Split.Layout,
from: Node.Handle,
current: Node.Handle,
) Backtrack {
if (from == current) return .backtrack;
return switch (self.nodes[current.idx()]) {
.leaf => .deadend,
.split => |s| switch (self.findParentSplit(
layout,
from,
s.left,
)) {
.result => |v| .{ .result = v },
.backtrack => if (s.layout == layout)
.{ .result = current }
else
.backtrack,
.deadend => switch (self.findParentSplit(
layout,
from,
s.right,
)) {
.deadend => .deadend,
.result => |v| .{ .result = v },
.backtrack => if (s.layout == layout)
.{ .result = current }
else
.backtrack,
},
},
};
}
/// Spatial representation of the split tree. See spatial.
pub const Spatial = struct {
/// The slots of the spatial representation in the same order
/// as the tree it was created from.
slots: []const Slot,
pub const empty: Spatial = .{ .slots = &.{} };
pub const Direction = enum { left, right, down, up };
const Slot = struct {
x: f16,
y: f16,
width: f16,
height: f16,
fn maxX(self: *const Slot) f16 {
return self.x + self.width;
}
fn maxY(self: *const Slot) f16 {
return self.y + self.height;
}
};
pub fn deinit(self: *Spatial, alloc: Allocator) void {
alloc.free(self.slots);
self.* = undefined;
}
};
/// Spatial representation of the split tree. This can be used to
/// better understand the layout of the tree in a 2D space.
///
/// The bounds of the representation are always based on the total
/// 2D space being 1x1. The x/y coordinates and width/height dimensions
/// of each individual split and leaf are relative to this.
/// This means that the spatial representation is a normalized
/// representation of the actual space.
///
/// The top-left corner of the tree is always (0, 0).
///
/// We use a normalized form because we can calculate it without
/// accessing to the actual rendered view sizes. These actual sizes
/// may not be available at various times because GUI toolkits often
/// only make them available once they're part of a widget tree and
/// a SplitTree can represent views that aren't currently visible.
pub fn spatial(
self: *const Self,
alloc: Allocator,
) Allocator.Error!Spatial {
// No nodes, empty spatial representation.
if (self.nodes.len == 0) return .empty;
// Get our total dimensions.
const dim = self.dimensions(.root);
// Create our slots which will match our nodes exactly.
const slots = try alloc.alloc(Spatial.Slot, self.nodes.len);
errdefer alloc.free(slots);
slots[0] = .{
.x = 0,
.y = 0,
.width = @floatFromInt(dim.width),
.height = @floatFromInt(dim.height),
};
self.fillSpatialSlots(slots, .root);
// Normalize the dimensions to 1x1 grid.
for (slots) |*slot| {
slot.x /= @floatFromInt(dim.width);
slot.y /= @floatFromInt(dim.height);
slot.width /= @floatFromInt(dim.width);
slot.height /= @floatFromInt(dim.height);
}
return .{ .slots = slots };
}
fn fillSpatialSlots(
self: *const Self,
slots: []Spatial.Slot,
current_: Node.Handle,
) void {
const current = current_.idx();
assert(slots[current].width >= 0 and slots[current].height >= 0);
switch (self.nodes[current]) {
// Leaf node, current slot is already filled by caller.
.leaf => {},
.split => |s| {
switch (s.layout) {
.horizontal => {
slots[s.left.idx()] = .{
.x = slots[current].x,
.y = slots[current].y,
.width = slots[current].width * s.ratio,
.height = slots[current].height,
};
slots[s.right.idx()] = .{
.x = slots[current].x + slots[current].width * s.ratio,
.y = slots[current].y,
.width = slots[current].width * (1 - s.ratio),
.height = slots[current].height,
};
},
.vertical => {
slots[s.left.idx()] = .{
.x = slots[current].x,
.y = slots[current].y,
.width = slots[current].width,
.height = slots[current].height * s.ratio,
};
slots[s.right.idx()] = .{
.x = slots[current].x,
.y = slots[current].y + slots[current].height * s.ratio,
.width = slots[current].width,
.height = slots[current].height * (1 - s.ratio),
};
},
}
self.fillSpatialSlots(slots, s.left);
self.fillSpatialSlots(slots, s.right);
},
}
}
/// Get the dimensions of the tree starting from the given node.
///
/// This creates relative dimensions (see Spatial) by assuming each
/// leaf is exactly 1x1 unit in size.
fn dimensions(self: *const Self, current: Node.Handle) struct {
width: u16,
height: u16,
} {
return switch (self.nodes[current.idx()]) {
.leaf => .{ .width = 1, .height = 1 },
.split => |s| split: {
const left = self.dimensions(s.left);
const right = self.dimensions(s.right);
break :split switch (s.layout) {
.horizontal => .{
.width = left.width + right.width,
.height = @max(left.height, right.height),
},
.vertical => .{
.width = @max(left.width, right.width),
.height = left.height + right.height,
},
};
},
};
}
/// Format the tree in a human-readable format. By default this will
/// output a diagram followed by a textual representation.
pub fn format(
self: *const Self,
writer: *std.Io.Writer,
) !void {
if (self.nodes.len == 0) {
try writer.writeAll("empty");
return;
}
self.formatDiagram(writer) catch {};
try self.formatText(writer);
}
pub fn formatText(self: Self, writer: *std.Io.Writer) std.Io.Writer.Error!void {
if (self.nodes.len == 0) {
try writer.writeAll("empty");
return;
}
try self.formatTextInner(writer, .root, 0);
}
fn formatTextInner(
self: Self,
writer: *std.Io.Writer,
current: Node.Handle,
depth: usize,
) std.Io.Writer.Error!void {
for (0..depth) |_| try writer.writeAll(" ");
if (self.zoomed) |zoomed| if (zoomed == current) {
try writer.writeAll("(zoomed) ");
};
switch (self.nodes[current.idx()]) {
.leaf => |v| if (@hasDecl(View, "splitTreeLabel"))
try writer.print("leaf: {s}\n", .{v.splitTreeLabel()})
else
try writer.print("leaf: {d}\n", .{current}),
.split => |s| {
try writer.print("split (layout: {t}, ratio: {d:.2})\n", .{
s.layout,
s.ratio,
});
try self.formatTextInner(writer, s.left, depth + 1);
try self.formatTextInner(writer, s.right, depth + 1);
},
}
}
pub fn formatDiagram(
self: Self,
writer: *std.Io.Writer,
) std.Io.Writer.Error!void {
if (self.nodes.len == 0) {
try writer.writeAll("empty");
return;
}
// Use our arena's GPA to allocate some intermediate memory.
// Requiring allocation for formatting is nasty but this is really
// only used for debugging and testing and shouldn't hit OOM
// scenarios.
var arena: ArenaAllocator = .init(self.arena.child_allocator);
defer arena.deinit();
const alloc = arena.allocator();
// Get our spatial representation.
const sp = spatial: {
const sp = self.spatial(alloc) catch return error.WriteFailed;
// Scale our spatial representation to have minimum width/height 1.
var min_w: f16 = 1;
var min_h: f16 = 1;
for (sp.slots) |slot| {
if (slot.width > 0) min_w = @min(min_w, slot.width);
if (slot.height > 0) min_h = @min(min_h, slot.height);
}
const ratio_w: f16 = 1 / min_w;
const ratio_h: f16 = 1 / min_h;
const slots = alloc.dupe(Spatial.Slot, sp.slots) catch return error.WriteFailed;
for (slots) |*slot| {
slot.x *= ratio_w;
slot.y *= ratio_h;
slot.width *= ratio_w;
slot.height *= ratio_h;
}
break :spatial .{ .slots = slots };
};
// The width we need for the largest label.
const max_label_width: usize = max_label_width: {
if (!@hasDecl(View, "splitTreeLabel")) {
break :max_label_width std.math.log10(sp.slots.len) + 1;
}
var max: usize = 0;
for (self.nodes) |node| switch (node) {
.split => {},
.leaf => |view| {
const label = view.splitTreeLabel();
max = @max(max, label.len);
},
};
break :max_label_width max;
};
// We need space for whitespace and ASCII art so add that.
// We need to accommodate the leaf handle, whitespace, and
// then the border.
const cell_width = cell_width: {
// Border + whitespace + label + whitespace + border.
break :cell_width 2 + max_label_width + 2;
};
const cell_height = cell_height: {
// Border + label + border. No whitespace needed on the
// vertical axis.
break :cell_height 1 + 1 + 1;
};
// Make a grid that can fit our entire ASCII diagram. We know
// the width/height based on node 0.
const grid = grid: {
// Get our initial width/height. Each leaf is 1x1 in this.
// We round up for this because partial widths/heights should
// take up an extra cell.
var width: usize = @intFromFloat(@ceil(sp.slots[0].width));
var height: usize = @intFromFloat(@ceil(sp.slots[0].height));
// We need space for whitespace and ASCII art so add that.
// We need to accommodate the leaf handle, whitespace, and
// then the border.
width *= cell_width;
height *= cell_height;
const rows = alloc.alloc([]u8, height) catch return error.WriteFailed;
for (0..rows.len) |y| {
rows[y] = alloc.alloc(u8, width + 1) catch return error.WriteFailed;
@memset(rows[y], ' ');
rows[y][width] = '\n';
}
break :grid rows;
};
// Draw each node
for (sp.slots, 0..) |slot, handle| {
// We only draw leaf nodes. Splits are only used for layout.
const node = self.nodes[handle];
switch (node) {
.leaf => {},
.split => continue,
}
// If our width/height is zero then we skip this.
if (slot.width == 0 or slot.height == 0) continue;
var x: usize = @intFromFloat(@floor(slot.x));
var y: usize = @intFromFloat(@floor(slot.y));
var width: usize = @intFromFloat(@max(@floor(slot.width), 1));
var height: usize = @intFromFloat(@max(@floor(slot.height), 1));
x *= cell_width;
y *= cell_height;
width *= cell_width;
height *= cell_height;
// Top border
{
const top = grid[y][x..][0..width];
top[0] = '+';
for (1..width - 1) |i| top[i] = '-';
top[width - 1] = '+';
}
// Bottom border
{
const bottom = grid[y + height - 1][x..][0..width];
bottom[0] = '+';
for (1..width - 1) |i| bottom[i] = '-';
bottom[width - 1] = '+';
}
// Left border
for (y + 1..y + height - 1) |y_cur| grid[y_cur][x] = '|';
for (y + 1..y + height - 1) |y_cur| grid[y_cur][x + width - 1] = '|';
// Get our label text
var buf: [10]u8 = undefined;
const label: []const u8 = if (@hasDecl(View, "splitTreeLabel"))
node.leaf.splitTreeLabel()
else
std.fmt.bufPrint(&buf, "{d}", .{handle}) catch return error.WriteFailed;
// Draw the handle in the center
const x_mid = width / 2 + x;
const y_mid = height / 2 + y;
const label_width = label.len;
const label_start = x_mid - label_width / 2;
const row = grid[y_mid][label_start..];
_ = std.fmt.bufPrint(row, "{s}", .{label}) catch return error.WriteFailed;
}
// Output every row
for (grid) |row| {
// We currently have a bug in our height calculation that
// results in trailing blank lines. Ignore those. We should
// really fix our height calculation instead. If someone wants
// to do that just remove this line and see the tests that fail
// and go from there.
if (row[0] == ' ') break;
try writer.writeAll(row);
}
}
fn viewRef(view: *View, gpa: Allocator) Allocator.Error!*View {
const func = @typeInfo(@TypeOf(View.ref)).@"fn";
return switch (func.params.len) {
1 => view.ref(),
2 => try view.ref(gpa),
else => @compileError("invalid view ref function"),
};
}
fn viewUnref(view: *View, gpa: Allocator) void {
const func = @typeInfo(@TypeOf(View.unref)).@"fn";
switch (func.params.len) {
1 => view.unref(),
2 => view.unref(gpa),
else => @compileError("invalid view unref function"),
}
}
/// Make this a valid gobject if we're in a GTK environment.
pub const getGObjectType = switch (build_config.app_runtime) {
.gtk => @import("gobject").ext.defineBoxed(
Self,
.{
// To get the type name we get the non-qualified type name
// of the view and append that to `GhosttySplitTree`.
.name = name: {
const type_name = @typeName(View);
const last = if (std.mem.lastIndexOfScalar(
u8,
type_name,
'.',
)) |idx|
type_name[idx + 1 ..]
else
type_name;
assert(last.len > 0);
break :name "GhosttySplitTree" ++ last;
},
.funcs = .{
.copy = &struct {
fn copy(self: *Self) callconv(.c) *Self {
const ptr = @import("glib").ext.create(Self);
ptr.* = if (self.nodes.len == 0)
.empty
else
self.clone(self.arena.child_allocator) catch @panic("oom");
return ptr;
}
}.copy,
.free = &struct {
fn free(self: *Self) callconv(.c) void {
self.deinit();
@import("glib").ext.destroy(self);
}
}.free,
},
},
),
.none => void,
};
};
}
const TestTree = SplitTree(TestView);
const TestView = struct {
const Self = @This();
label: []const u8,
pub fn ref(self: *Self, alloc: Allocator) Allocator.Error!*Self {
const ptr = try alloc.create(Self);
ptr.* = self.*;
return ptr;
}
pub fn unref(self: *Self, alloc: Allocator) void {
alloc.destroy(self);
}
pub fn splitTreeLabel(self: *const Self) []const u8 {
return self.label;
}
};
test "SplitTree: empty tree" {
const testing = std.testing;
const alloc = testing.allocator;
var t: TestTree = .empty;
defer t.deinit();
const str = try std.fmt.allocPrint(alloc, "{f}", .{t});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\empty
);
}
test "SplitTree: single node" {
const testing = std.testing;
const alloc = testing.allocator;
var v: TestTree.View = .{ .label = "A" };
var t: TestTree = try .init(alloc, &v);
defer t.deinit();
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(t, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| A |
\\+---+
\\
);
}
test "SplitTree: split horizontal" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
var t3 = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer t3.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{t3});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---++---+
\\| A || B |
\\+---++---+
\\split (layout: horizontal, ratio: 0.50)
\\ leaf: A
\\ leaf: B
\\
);
}
// Split right at B
var vC: TestTree.View = .{ .label = "C" };
var tC: TestTree = try .init(alloc, &vC);
defer tC.deinit();
var it = t3.iterator();
var t4 = try t3.split(
alloc,
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "B")) {
break entry.handle;
}
} else return error.NotFound,
.right,
0.5,
&tC,
);
defer t4.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{t4});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+--------++---++---+
\\| A || B || C |
\\+--------++---++---+
\\split (layout: horizontal, ratio: 0.50)
\\ leaf: A
\\ split (layout: horizontal, ratio: 0.50)
\\ leaf: B
\\ leaf: C
\\
);
}
// Split right at C
var vD: TestTree.View = .{ .label = "D" };
var tD: TestTree = try .init(alloc, &vD);
defer tD.deinit();
it = t4.iterator();
var t5 = try t4.split(
alloc,
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "C")) {
break entry.handle;
}
} else return error.NotFound,
.right,
0.5,
&tD,
);
defer t5.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{t5});
defer alloc.free(str);
try testing.expectEqualStrings(
\\+------------------++--------++---++---+
\\| A || B || C || D |
\\+------------------++--------++---++---+
\\split (layout: horizontal, ratio: 0.50)
\\ leaf: A
\\ split (layout: horizontal, ratio: 0.50)
\\ leaf: B
\\ split (layout: horizontal, ratio: 0.50)
\\ leaf: C
\\ leaf: D
\\
, str);
}
// Find "previous" from D back.
{
var current: u8 = 'D';
while (current != 'A') : (current -= 1) {
it = t5.iterator();
const handle = t5.previous(
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, &.{current})) {
break entry.handle;
}
} else return error.NotFound,
).?;
const entry = t5.nodes[handle.idx()].leaf;
try testing.expectEqualStrings(
entry.label,
&.{current - 1},
);
}
it = t5.iterator();
try testing.expect(t5.previous(
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, &.{current})) {
break entry.handle;
}
} else return error.NotFound,
) == null);
}
// Find "next" from A forward.
{
var current: u8 = 'A';
while (current != 'D') : (current += 1) {
it = t5.iterator();
const handle = t5.next(
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, &.{current})) {
break entry.handle;
}
} else return error.NotFound,
).?;
const entry = t5.nodes[handle.idx()].leaf;
try testing.expectEqualStrings(
entry.label,
&.{current + 1},
);
}
it = t5.iterator();
try testing.expect(t5.next(
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, &.{current})) {
break entry.handle;
}
} else return error.NotFound,
) == null);
}
}
test "SplitTree: split vertical" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
var t3 = try t1.split(
alloc,
.root, // at root
.down, // split down
0.5,
&t2, // insert t2
);
defer t3.deinit();
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(t3, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| A |
\\+---+
\\+---+
\\| B |
\\+---+
\\
);
}
test "SplitTree: split horizontal with zero ratio" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// A | B horizontal
var splitAB = try t1.split(
alloc,
.root, // at root
.right, // split right
0,
&t2, // insert t2
);
defer splitAB.deinit();
const split = splitAB;
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| B |
\\+---+
\\
);
}
}
test "SplitTree: split vertical with zero ratio" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// A | B horizontal
var splitAB = try t1.split(
alloc,
.root, // at root
.down, // split right
0,
&t2, // insert t2
);
defer splitAB.deinit();
const split = splitAB;
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| B |
\\+---+
\\
);
}
}
test "SplitTree: split horizontal with full width" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// A | B horizontal
var splitAB = try t1.split(
alloc,
.root, // at root
.right, // split right
1,
&t2, // insert t2
);
defer splitAB.deinit();
const split = splitAB;
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| A |
\\+---+
\\
);
}
}
test "SplitTree: split vertical with full width" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// A | B horizontal
var splitAB = try t1.split(
alloc,
.root, // at root
.down, // split right
1,
&t2, // insert t2
);
defer splitAB.deinit();
const split = splitAB;
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| A |
\\+---+
\\
);
}
}
test "SplitTree: remove leaf" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
var t3 = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer t3.deinit();
// Remove "A"
var it = t3.iterator();
var t4 = try t3.remove(
alloc,
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "A")) {
break entry.handle;
}
} else return error.NotFound,
);
defer t4.deinit();
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(t4, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| B |
\\+---+
\\
);
}
test "SplitTree: split twice, remove intermediary" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
var v3: TestTree.View = .{ .label = "C" };
var t3: TestTree = try .init(alloc, &v3);
defer t3.deinit();
// A | B horizontal.
var split1 = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer split1.deinit();
// Insert C below that.
var split2 = try split1.split(
alloc,
.root, // at root
.down, // split down
0.5,
&t3, // insert t3
);
defer split2.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split2, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---++---+
\\| A || B |
\\+---++---+
\\+--------+
\\| C |
\\+--------+
\\
);
}
// Remove "B"
var it = split2.iterator();
var split3 = try split2.remove(
alloc,
while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "B")) {
break entry.handle;
}
} else return error.NotFound,
);
defer split3.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split3, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---+
\\| A |
\\+---+
\\+---+
\\| C |
\\+---+
\\
);
}
// Remove every node from split2 (our most complex one), which should
// never crash. We don't test the result is correct, this just verifies
// we don't hit any assertion failures.
for (0..split2.nodes.len) |i| {
var t = try split2.remove(alloc, @enumFromInt(i));
t.deinit();
}
}
test "SplitTree: spatial goto" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
var v3: TestTree.View = .{ .label = "C" };
var t3: TestTree = try .init(alloc, &v3);
defer t3.deinit();
var v4: TestTree.View = .{ .label = "D" };
var t4: TestTree = try .init(alloc, &v4);
defer t4.deinit();
// A | B horizontal
var splitAB = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer splitAB.deinit();
// A | C vertical
var splitAC = try splitAB.split(
alloc,
at: {
var it = splitAB.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "A")) {
break entry.handle;
}
} else return error.NotFound;
},
.down, // split down
0.8,
&t3, // insert t3
);
defer splitAC.deinit();
// B | D vertical
var splitBD = try splitAC.split(
alloc,
at: {
var it = splitAB.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "B")) {
break entry.handle;
}
} else return error.NotFound;
},
.down, // split down
0.3,
&t4, // insert t4
);
defer splitBD.deinit();
const split = splitBD;
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---++---+
\\| || B |
\\| |+---+
\\| |+---+
\\| A || |
\\| || |
\\| || |
\\| || D |
\\+---+| |
\\+---+| |
\\| C || |
\\+---++---+
\\
);
}
// Spatial C => right
{
const target = (try split.goto(
alloc,
from: {
var it = split.iterator();
break :from while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "C")) {
break entry.handle;
}
} else return error.NotFound;
},
.{ .spatial = .right },
)).?;
const view = split.nodes[target.idx()].leaf;
try testing.expectEqualStrings(view.label, "D");
}
// Spatial D => left
{
const target = (try split.goto(
alloc,
from: {
var it = split.iterator();
break :from while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "D")) {
break entry.handle;
}
} else return error.NotFound;
},
.{ .spatial = .left },
)).?;
const view = split.nodes[target.idx()].leaf;
try testing.expectEqualStrings("A", view.label);
}
// Equalize
var equal = try split.equalize(alloc);
defer equal.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(equal, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---++---+
\\| A || B |
\\+---++---+
\\+---++---+
\\| C || D |
\\+---++---+
\\
);
}
}
test "SplitTree: resize" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// A | B horizontal
var split = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer split.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+---++---+
\\| A || B |
\\+---++---+
\\
);
}
// Resize
{
var resized = try split.resize(
alloc,
at: {
var it = split.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "B")) {
break entry.handle;
}
} else return error.NotFound;
},
.horizontal, // resize right
0.25,
);
defer resized.deinit();
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(resized, .formatDiagram)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\+-------------++---+
\\| A || B |
\\+-------------++---+
\\
);
}
}
test "SplitTree: clone empty tree" {
const testing = std.testing;
const alloc = testing.allocator;
var t: TestTree = .empty;
defer t.deinit();
var t2 = try t.clone(alloc);
defer t2.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{t2});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\empty
);
}
}
test "SplitTree: zoom" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// A | B horizontal
var split = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer split.deinit();
split.zoom(at: {
var it = split.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "B")) {
break entry.handle;
}
} else return error.NotFound;
});
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatText)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\split (layout: horizontal, ratio: 0.50)
\\ leaf: A
\\ (zoomed) leaf: B
\\
);
}
// Clone preserves zoom
var clone = try split.clone(alloc);
defer clone.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(clone, .formatText)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\split (layout: horizontal, ratio: 0.50)
\\ leaf: A
\\ (zoomed) leaf: B
\\
);
}
}
test "SplitTree: split resets zoom" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// Zoom A
t1.zoom(at: {
var it = t1.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "A")) {
break entry.handle;
}
} else return error.NotFound;
});
// A | B horizontal
var split = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer split.deinit();
{
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(split, .formatText)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\split (layout: horizontal, ratio: 0.50)
\\ leaf: A
\\ leaf: B
\\
);
}
}
test "SplitTree: remove and zoom" {
const testing = std.testing;
const alloc = testing.allocator;
var v1: TestTree.View = .{ .label = "A" };
var t1: TestTree = try .init(alloc, &v1);
defer t1.deinit();
var v2: TestTree.View = .{ .label = "B" };
var t2: TestTree = try .init(alloc, &v2);
defer t2.deinit();
// A | B horizontal
var split = try t1.split(
alloc,
.root, // at root
.right, // split right
0.5,
&t2, // insert t2
);
defer split.deinit();
split.zoom(at: {
var it = split.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "A")) {
break entry.handle;
}
} else return error.NotFound;
});
// Remove A, should unzoom
{
var removed = try split.remove(
alloc,
at: {
var it = split.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "A")) {
break entry.handle;
}
} else return error.NotFound;
},
);
defer removed.deinit();
try testing.expect(removed.zoomed == null);
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(removed, .formatText)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\leaf: B
\\
);
}
// Remove B, should keep zoom
{
var removed = try split.remove(
alloc,
at: {
var it = split.iterator();
break :at while (it.next()) |entry| {
if (std.mem.eql(u8, entry.view.label, "B")) {
break entry.handle;
}
} else return error.NotFound;
},
);
defer removed.deinit();
const str = try std.fmt.allocPrint(alloc, "{f}", .{std.fmt.alt(removed, .formatText)});
defer alloc.free(str);
try testing.expectEqualStrings(str,
\\(zoomed) leaf: A
\\
);
}
}
|