simple_components_4.68.0_da9b0f3a/persistent-memory_pools-streams-generic_external_b_tree.adb

   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
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
--                                                                    --
--  package                          Copyright (c) Dmitry A. Kazakov  --
--     Persistent.Memory_Pools.Streams.            Luebeck            --
--        Generic_External_B_Tree                  Spring, 2014       --
--  Implementation                                                    --
--                                Last revision :  18:00 18 Aug 2022  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.IO_Exceptions;  use Ada.IO_Exceptions;
with Ada.Tags;           use Ada.Tags;

with Persistent.Blocking_Files.Text_IO;
use  Persistent.Blocking_Files.Text_IO;

with Persistent.Memory_Pools.Streams;
use  Persistent.Memory_Pools.Streams;

with Persistent.Memory_Pools.Dump;
use  Persistent.Memory_Pools.Dump;

package body Persistent.Memory_Pools.Streams.Generic_External_B_Tree is

   Left_Half     : constant Positive := (Width + 1) / 2 - 1;
   Right_Half    : constant Positive := Width - Left_Half;

   type Byte_Index_Array is array (Positive range <>) of Byte_Index;

   function To_Child (No : Integer) return Block_Offset is
      pragma Inline (To_Child);
   begin
      return Child_Offset + (Block_Offset (No) - 1) * 8;
   end To_Child;

   function To_Key (No : Integer) return Block_Offset is
      pragma Inline (To_Key);
   begin
      return Key_Offset + (Block_Offset (No) - 1) * 8;
   end To_Key;

   function To_Value (No : Integer) return Block_Offset is
      pragma Inline (To_Value);
   begin
      return Value_Offset + (Block_Offset (No) - 1) * 8;
   end To_Value;

   function Get_Child
            (  Block : Block_Type;
               No    : Positive
            )  return Byte_Index is
      pragma Inline (Get_Child);
   begin
      return Get (Block, To_Child (No));
   end Get_Child;

   function Get_Key
            (  Block : Block_Type;
               No    : Positive
            )  return Byte_Index is
      pragma Inline (Get_Key);
   begin
      return Get (Block, To_Key (No));
   end Get_Key;

   function Get_Length (Block : Block_Type) return Natural is
      pragma Inline (Get_Length);
   begin
      return Natural (Unsigned_16'(Get (Block, Length_Offset)));
   end Get_Length;

   function Get_Node_Tag (Block : Block_Type) return Byte_Index is
      pragma Inline (Get_Node_Tag);
   begin
      return Get (Block, Node_Tag_Offset);
   end Get_Node_Tag;

   function Get_Parent_Address (Block : Block_Type) return Byte_Index is
      pragma Inline (Get_Parent_Address);
   begin
      return Get (Block, Parent_Address_Offset);
   end Get_Parent_Address;

   function Get_Parent_Index (Block : Block_Type) return Natural is
      pragma Inline (Get_Parent_Index);
   begin
      return Natural (Unsigned_16'(Get (Block, Parent_Index_Offset)));
   end Get_Parent_Index;

   function Get_Root_Address (Container : B_Tree) return Byte_Index is
      Lock : Holder (Container.Pool);
   begin
      return Container.Root_Bucket;
   end Get_Root_Address;

   function Get_Value
            (  Block : Block_Type;
               No    : Positive
            )  return Byte_Index is
      pragma Inline (Get_Value);
   begin
      return Get (Block, To_Value (No));
   end Get_Value;

   function New_Key
            (  Pool : access Persistent_Pool'Class;
               Key  : Key_Type
            )  return Byte_Index is
      Stream : aliased Unchecked_Output_Stream (Pool);
   begin
      Output_Key (Stream'Access, Key);
      return Get_First (Stream);
   end New_Key;

   function New_Value
            (  Pool  : access Persistent_Pool'Class;
               Value : Object_Type
            )  return Byte_Index is
      Stream : aliased Unchecked_Output_Stream (Pool);
   begin
      Output_Value (Stream'Access, Value);
      return Get_First (Stream);
   end New_Value;

   procedure Set_Child
             (  Block : in out Block_Type;
                No    : Positive;
                Child : Byte_Index
             )  is
   begin
      Put (Block, To_Child (No), Child);
   end Set_Child;

   procedure Set_Key
             (  Block : in out Block_Type;
                No    : Positive;
                Key   : Byte_Index
             )  is
   begin
      Put (Block, To_Key (No), Key);
   end Set_Key;

   procedure Set_Length
             (  Block  : in out Block_Type;
                Length : Natural
             )  is
   begin
      Put (Block, Length_Offset, Unsigned_16 (Length));
   end Set_Length;

   procedure Set_Node_Tag
             (  Block : in out Block_Type;
                Tag   : Byte_Index
             )  is
   begin
      Put (Block, Node_Tag_Offset, Tag);
   end Set_Node_Tag;

   procedure Set_Parent_Address
             (  Block  : in out Block_Type;
                Parent : Byte_Index
             )  is
   begin
      Put (Block, Parent_Address_Offset, Parent);
   end Set_Parent_Address;

   procedure Set_Parent_Index
             (  Block : in out Block_Type;
                Index : Positive
             )  is
   begin
      Put (Block, Parent_Index_Offset, Unsigned_16 (Index));
   end Set_Parent_Index;

   procedure Set_Value
             (  Block : in out Block_Type;
                No    : Positive;
                Value : Byte_Index
             )  is
   begin
      Put (Block, To_Value (No), Value);
   end Set_Value;

--     procedure Dump
--               (  Pool   : B_Tree'Class;
--                  Node   : Byte_Index;
--                  Prefix : String := ""
--               )  is
--        use Ada.Text_IO;
--        use Strings_Edit.Integers;
--        Length : Natural;
--        Child  : Byte_Index;
--     begin
--        Put (Prefix);
--        Put ("at ");
--        Put (Image (Node));
--        declare
--           Block : Block_Type renames Load (Pool.File, Node).all;
--        begin
--           Length := Get_Length (Block);
--           if Get_Parent_Address (Block) /= 0 then
--              Put (" [");
--              Put (Image (Get_Parent_Address (Block)));
--              Put ("|");
--              Put (Image (Get_Parent_Index (Block)));
--              Put ("]");
--           end if;
--        end;
--        Put (":");
--        declare
--           Key : Byte_Index;
--        begin
--           for Index in 1..Length loop
--              declare
--                 Block : Block_Type renames Load (Pool.File, Node).all;
--              begin
--                 Child := Get_Child (Block, Index);
--                 if Child = 0 then
--                    Put (" .");
--                 else
--                    Put (' ' & Image (Child));
--                 end if;
--                 Put (" /");
--                 Key := Get_Key (Block, Index);
--              end;
--              Put (Image (Get_Key (Pool, Key)));
--              Put ("\");
--           end loop;
--           declare
--              Block : Block_Type renames Load (Pool.File, Node).all;
--           begin
--              Child := Get_Child (Block, Length + 1);
--              if Child = 0 then
--                 Put (" .");
--              else
--                 Put (' ' & Image (Child));
--              end if;
--           end;
--        end;
--        New_Line;
--        for Index in 1..Length + 1 loop
--           declare
--              Block : Block_Type renames Load (Pool.File, Node).all;
--           begin
--              Child := Get_Child (Block, Index);
--           end;
--           if Child /= 0 then
--              Dump (Pool, Child, "  " & Prefix & Image (Index) & ".");
--           end if;
--        end loop;
--     end Dump;

   procedure Set_Parent
             (  File      : access Persistent_Array'Class;
                Child     : Byte_Index;
                Parent    : Byte_Index;
                Index     : Positive
             )  is
   begin
      if Child /= 0 then
         declare
            Block : Block_Type renames Update (File, Child).all;
         begin
            Set_Parent_Address (Block, Parent);
            Set_Parent_Index   (Block, Index);
         end;
      end if;
   end Set_Parent;

   procedure Copy
             (  File       : access Persistent_Array'Class;
                To_Node    : Byte_Index;
                To_First   : Positive;
                To_Last    : Natural;
                From_Node  : Byte_Index;
                From_First : Positive;
                Tail       : Natural := 1
             )  is
      From_Last : constant Integer := From_First + To_Last - To_First;
      Children  : Byte_Index_Array (To_First..To_Last + Tail);
      Keys   : Byte_Array (To_Key (To_First)..To_Key (To_Last) + 7);
      Values : Byte_Array (To_Value (To_First)..To_Value (To_Last) + 7);
   begin
      declare
         Block : Block_Type renames Load (File, From_Node).all;
      begin
         Keys :=
            Block
            (  To_Key (From_First)
            .. To_Key (From_Last) + 7
            );
         Values :=
            Block
            (  To_Value (From_First)
            .. To_Value (From_Last) + 7
            );
         for Index in Children'Range loop
            Children (Index) :=
               Get_Child (Block, From_First + Index - To_First);
         end loop;
      end;
      declare
         Block : Block_Type renames Update (File, To_Node).all;
      begin
         Block (Keys'Range)   := Keys;
         Block (Values'Range) := Values;
         for Index in Children'Range loop
            Set_Child (Block, Index, Children (Index));
         end loop;
      end;
      for Index in Children'Range loop
         if Children (Index) /= 0 then
            Set_Parent (File, Children (Index), To_Node, Index);
         end if;
      end loop;
   end Copy;

   procedure Copy
             (  File       : access Persistent_Array'Class;
                To_Node    : Byte_Index;
                To_Index   : Positive;
                From_Node  : Byte_Index;
                From_Index : Positive;
                Increment  : Boolean := False
             )  is
      Key   : Byte_Index;
      Value : Byte_Index;
   begin
      declare
         Block : Block_Type renames Load (File, From_Node).all;
      begin
         Key := Get_Key (Block, From_Index);
         Value := Get_Value (Block, From_Index);
      end;
      declare
         Block : Block_Type renames Update (File, To_Node).all;
      begin
         Set_Key (Block, To_Index, Key);
         Set_Value (Block, To_Index, Value);
         if Increment then
            Set_Length (Block, Get_Length (Block) + 1);
         end if;
      end;
   end Copy;

   procedure Move
             (  File       : access Persistent_Array'Class;
                Node       : Byte_Index;
                To_First   : Positive;
                To_Last    : Natural;
                From_First : Positive;
                Tail       : Natural := 1
             )  is
      pragma Inline (Move);
      From_Last : constant Integer := From_First + To_Last - To_First;
      Children  : Byte_Index_Array (To_First..To_Last + Tail);
   begin
      declare
         Block : Block_Type renames Update (File, Node).all;
      begin
         Block (To_Key (To_First)..To_Key (To_Last) + 7) :=
            Block (To_Key (From_First)..To_Key (From_Last) + 7);
         Block (To_Value (To_First)..To_Value (To_Last) + 7) :=
            Block (To_Value (From_First)..To_Value (From_Last) + 7);
         Block (To_Child (To_First)..To_Child (To_Last + Tail) + 7) :=
            Block
            (  To_Child (From_First)
            .. To_Child (From_Last + Tail) + 7
            );
         for Index in Children'Range loop
            Children (Index) := Get_Child (Block, Index);
         end loop;
      end;
      for Index in Children'Range loop
         if Children (Index) /= 0 then -- The same parent only index
            Set_Parent_Index           -- change
            (  Update (File, Children (Index)).all,
               Index
            );
         end if;
      end loop;
   end Move;

   function Is_Empty (Container : B_Tree) return Boolean is
      Lock : Holder (Container.Pool);
   begin
      if Container.Root_Bucket = 0 then
         return True;
      else
         declare
            Block : Block_Type renames
                    Load (Container.Pool.File, Container.Root_Bucket).all;
         begin
            return Get_Length (Block) = 0;
         end;
      end if;
   end Is_Empty;

   function Is_In (Container : B_Tree; Key : Key_Type)
      return Boolean is
      Lock  : Holder (Container.Pool);
      Node  : Byte_Index;
      Index : Integer;
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      return Node /= 0 and then Index > 0;
   end Is_In;

   procedure Add
             (  Container : in out B_Tree;
                Key       : Key_Type;
                Value     : Object_Type
             )  is
      Pool : Persistent_Pool'Class renames Container.Pool.all;
      Lock : Holder (Container.Pool);
   begin
      if Container.Root_Bucket = 0 then
         New_Root (Container, Key, Value, 0, 0);
      else
         declare
            Node  : Byte_Index;
            Index : Integer;
         begin
            Search (Container, Container.Root_Bucket, Key, Node, Index);
            if Index < 0 then
               declare
                  Key_Index   : Byte_Index := 0;
                  Value_Index : Byte_Index := 0;
               begin
                  Key_Index   := New_Key   (Container.Pool, Key);
                  Value_Index := New_Value (Container.Pool, Value);
                  Insert
                  (  Key         => Key,
                     Key_Index   => Key_Index,
                     Value_Index => Value_Index,
                     Child       => 0,
                     Parent      => (  Container'Unchecked_Access,
                                       Node,
                                      -Index
                  )                 );
               exception
                  when others =>
                     if Key_Index /= 0 then
                        Unchecked_Deallocate (Pool, Key_Index);
                     end if;
                     if Value_Index /= 0 then
                        Unchecked_Deallocate (Pool, Value_Index);
                     end if;
                     raise;
               end;
            else
               Raise_Exception (Constraint_Error'Identity, Key_In_Use);
            end if;
         end;
      end if;
   end Add;

   procedure Add
             (  Container : in out B_Tree;
                Key       : Key_Type;
                Producer  : in out Abstract_Value_Access'Class
             )  is
      Pool   : Persistent_Pool'Class renames Container.Pool.all;
      Lock   : Holder (Container.Pool);
      Stream : aliased Unchecked_Output_Stream (Container.Pool);
   begin
      if Container.Root_Bucket = 0 then
         declare
            Index : constant Byte_Index :=
                    New_Key (Container.Pool, Key);
         begin
            Put (Producer, Stream);
            New_Root
            (  Container,
               Index,
               Get_First (Stream),
               0,
               0
            );
         exception
            when others =>
               if Index /= 0 then
                  Unchecked_Deallocate (Pool, Index);
               end if;
               if Get_First (Stream) /= 0 then
                  Unchecked_Deallocate (Pool, Get_First (Stream));
               end if;
               raise;
         end;
      else
         declare
            Node  : Byte_Index;
            Where : Integer;
         begin
            Search (Container, Container.Root_Bucket, Key, Node, Where);
            if Where < 0 then
               declare
                  Index : Byte_Index := 0;
               begin
                  Index := New_Key (Container.Pool, Key);
                  Put (Producer, Stream);
                  Insert
                  (  Key         => Key,
                     Key_Index   => Index,
                     Value_Index => Get_First (Stream),
                     Child       => 0,
                     Parent      => (  Container'Unchecked_Access,
                                       Node,
                                      -Where
                  )                 );
               exception
                  when others =>
                     if Index /= 0 then
                        Unchecked_Deallocate (Pool, Index);
                     end if;
                     if Get_First (Stream) /= 0 then
                        Unchecked_Deallocate (Pool, Get_First (Stream));
                     end if;
                     raise;
               end;
            else
               Raise_Exception (Constraint_Error'Identity, Key_In_Use);
            end if;
         end;
      end if;
   end Add;

   procedure Erase
             (  Container : in out B_Tree;
                Root      : Byte_Index
             )  is
      Pool   : Persistent_Pool'Class renames Container.Pool.all;
      Length : Integer;
   begin
      if Root /= 0 then
         declare
            Block : Block_Type renames Load (Pool.File, Root).all;
         begin
            Length := Get_Length (Block);
            declare
               Ptr : Byte_Index_Array (1..Length * 2);
            begin
               for Index in 1..Length loop
                  Ptr (Index) := Get_Key (Block, Index);
                  Ptr (Length + Index) := Get_Value (Block, Index);
               end loop;
               for Index in Ptr'Range loop
                  Unchecked_Deallocate (Pool, Ptr (Index));
               end loop;
            end;
         end;
         for Index in 1..Length + 1 loop
            Erase
            (  Container,
               Get_Child (Load (Pool.File, Root).all, Index)
            );
         end loop;
         Unchecked_Deallocate (Pool, Root);
      end if;
   end Erase;

   procedure Erase (Container : in out B_Tree) is
      Lock : Holder (Container.Pool);
   begin
      Erase (Container, Container.Root_Bucket);
      Container.Root_Bucket := 0;
   end Erase;

   procedure Finalize (Container : in out B_Tree) is
      use Ada.Finalization;
   begin
--        if (  Exception_Identity
--              (  GNAT.Most_Recent_Exception.Occurrence
--              )
--           /= Null_ID
--           )
--        then
--           Ada.Text_IO.Put_Line
--           (  "Finalize entered with exception "
--           &  Exception_Information
--              (  GNAT.Most_Recent_Exception.Occurrence
--           )  );
--        end if;
      if Is_Writable (Container.Pool.File.all) then
         Erase (Container);
      end if;
      Finalize (Limited_Controlled (Container));
   end Finalize;

   function Find
            (  Container : B_Tree;
               Block     : Block_Type;
               Key       : Key_Type
            )  return Integer is
      pragma Inline (Find);
      Size : constant Natural := Get_Length (Block);
      From : Natural := 0;
      To   : Natural := Size + 1;
      This : Natural;
   begin
      if Size = 0 then
         return -1;
      end if;
      declare
         Keys : constant Byte_Array :=
                   Block
                   (  Key_Offset
                   .. Key_Offset - 1 + Block_Offset (Size) * 8
                   );
      begin
         loop
            This := (From + To) / 2;
            declare
               Current : constant Key_Type :=
                  Get_Key
                  (  Container,
                     Get
                     (  Keys,
                        Keys'First + (Block_Offset (This) - 1) * 8
                  )  );
            begin
               if Key = Current then
                  return This;
               elsif Key < Current then
                  if This - From <= 1 then
                     return -This;
                  end if;
                  To := This;
               else
                  if To - This <= 1 then
                     return - This - 1;
                  end if;
                  From := This;
               end if;
            end;
         end loop;
      end;
   end Find;

   function Find (Container : B_Tree; Key : Key_Type) return Item_Ptr is
      Lock  : Holder (Container.Pool);
      Node  : Byte_Index;
      Index : Integer;
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      if Index <= 0 then
         return No_Item;
      else
         return (Container.Self, Node, Index);
      end if;
   end Find;

   type Children_Location is (Left, Right);

   procedure Generic_Traverse
             (  Container : B_Tree;
                From      : Item_Ptr;
                To        : Key_Type
             )  is
      Stop : Boolean  := False;
      This : Item_Ptr := From;
      Next : Item_Ptr;

      function Overshot (Children_At : Children_Location)
         return Boolean is
         Key  : constant Key_Type := Get_Key (Next);
         Skip : Item_Ptr;
      begin
         if To < Key then
            return True;
         end if;
         case Children_At is
            when Left =>
               Skip := Get_Left_Child (Next);
            when Right =>
               Skip := Get_Right_Child (This);
         end case;
         if Skip.Node /= 0 then
            case Visit_Range (Container, Skip) is
               when Quit =>
                  Stop := True;
                  return True;
               when Step_Over =>
                  null;
               when Step_In =>
                  loop
                     This := (Skip.Tree, Skip.Node, 1); -- The first
                     Skip := Get_Left_Child (This);     -- item in the
                     if Skip.Node = 0 then              -- bucket
                        Stop := not Visit_Item          -- Leaf visit it
                                    (  Container,       -- and return
                                       Get_Key (This),
                                       This
                                    );
                        return Stop;
                     end if;
                     case Visit_Range (Container, Skip) is
                        when Quit =>
                           Stop := True;
                           return True;
                        when Step_Over =>
                           This := Skip;
                           return False;
                        when Step_In =>
                           null;
                     end case;
                  end loop;
            end case;
         end if;
         Stop := not Visit_Item (Container, Key, Next);
         This := Next;
         return Stop;
      end Overshot;
   begin
      if This = No_Item then
         return;
      end if;
      declare
         Key : constant Key_Type := Get_Key (This);
      begin
         if To < Key or else not Visit_Item (Container, Key, This) then
            return;
         end if;
      end;
      loop
         Next := Get_Item (This, This.Index + 1);
         if Next.Node = 0 then
            declare -- Searching fpr a right parent item
               Current : Item_Ptr := This;
            begin
               loop
                  Next := Get_Right_Parent (Current);
                  exit when Next.Node /= 0;
                  Next := Get_Left_Parent (Current);
                  exit when Next.Node = 0;
                  Current := Next;
               end loop;
            end;
            if Next.Node = 0 or else Overshot (Right) then
               if Stop then
                  return;
               end if;
               Next := Get_Right_Child (This);
               if Next.Node = 0 or else Overshot (Left) or else Stop
               then
                  return;
               end if;
            end if;
         else
            if Overshot (Right) then
               exit when Stop;
               Next := Get_Right_Child (This);
               if Next.Node = 0 or else Overshot (Left) or else Stop
               then
                  return;
               end if;
            end if;
         end if;
      end loop;
   end Generic_Traverse;

   function Get
            (  Container : B_Tree;
               Key       : Key_Type
            )  return Object_Type is
      Lock  : Holder (Container.Pool);
      Node  : Byte_Index;
      Index : Integer;
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      if Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      return Get_Value
             (  Container,
                Get_Value (Load (Container.Pool.File, Node).all, Index)
             );
   end Get;

   procedure Get
             (  Container : in out B_Tree;
                Key       : Key_Type;
                Consumer  : in out Abstract_Value_Access'Class
             )  is
      Pool   : Persistent_Pool'Class renames Container.Pool.all;
      Lock   : Holder (Container.Pool);
      Node   : Byte_Index;
      Index  : Integer;
      Stream : aliased Unchecked_Input_Stream (Container.Pool);
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      if Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      Open (Stream, Get_Value (Load (Pool.File, Node).all, Index));
      Get (Consumer, Stream);
   end Get;

   function Get_Bucket_Address (Item : Item_Ptr) return Byte_Index is
   begin
      return Item.Node;
   end Get_Bucket_Address;

   function Get_Bucket_Size (Item : Item_Ptr) return Natural is
   begin
      if Item.Node = 0 then
         return 0;
      else
         declare
            Pool : Persistent_Pool'Class renames Item.Tree.Pool.all;
            Lock : Holder (Item.Tree.Pool);
         begin
            return Get_Length (Load (Pool.File, Item.Node).all);
         end;
      end if;
   end Get_Bucket_Size;

   function Get_First
            (  Container : B_Tree;
               Root      : Byte_Index
            )  return Item_Ptr is
      Pool : Persistent_Pool'Class renames Container.Pool.all;
      This : Byte_Index := Root;
      Next : Byte_Index;
   begin
      if (  This = 0
         or else
            Get_Length (Load (Pool.File, This).all) = 0
         )
      then
         return No_Item;
      else
         loop
            Next := Get_Child (Load (Pool.File, This).all, 1);
            if Next = 0 then
               return (Container.Self, This, 1);
            end if;
            This := Next;
         end loop;
      end if;
   end Get_First;

   function Get_First (Container : B_Tree) return Item_Ptr is
      Lock : Holder (Container.Pool);
   begin
      return Get_First (Container, Container.Root_Bucket);
   end Get_First;

   function Get_First (Item : Item_Ptr) return Item_Ptr is
      Left : Item_Ptr;
   begin
      if Item.Node = 0 then
         return No_Item;
      else
         Left := Get_Left_Child (Item);
         if Left.Node = 0 then
            return Item;
         else
            return Get_First (Left.Tree.all, Left.Node);
         end if;
      end if;
   end Get_First;

   function Get_Index (Item : Item_Ptr) return Positive is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      return Item.Index;
   end Get_Index;

   function Get_Index
            (  Container : B_Tree;
               Key       : Key_Type
            )  return Byte_Index is
      Lock  : Holder (Container.Pool);
      Node  : Byte_Index;
      Index : Integer;
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      if Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      return Get_Value (Load (Container.Pool.File, Node).all, Index);
   end Get_Index;

   function Get_Item (Item : Item_Ptr; Index : Positive)
      return Item_Ptr is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Pool  : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames Load (Pool.File, Item.Node).all;
      begin
         if Index > Get_Length (Block) then
            return No_Item;
         else
            return (Item.Tree, Item.Node, Index);
         end if;
      end;
   end Get_Item;

   function Get_Key
            (  Container : B_Tree;
               Key       : Byte_Index
            )  return Key_Type is
      Stream : aliased Unchecked_Input_Stream (Container.Pool);
   begin
      Open (Stream, Key);
      return Input_Key (Stream'Access);
   end Get_Key;

   function Get_Key (Item : Item_Ptr) return Key_Type is
   begin
      if Item.Node = 0 then
         Raise_Exception
         (  Constraint_Error'Identity,
            No_Item_Error
         );
      end if;
      declare
         Pool  : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames Load (Pool.File, Item.Node).all;
      begin
         if Item.Index > Get_Length (Block) then
            Raise_Exception (Constraint_Error'Identity, Bad_Key_Index);
         end if;
         return Get_Key (Item.Tree.all, Get_Key (Block, Item.Index));
      end;
   end Get_Key;

   function Get_Key_Address (Item : Item_Ptr) return Byte_Index is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Pool  : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames Load (Pool.File, Item.Node).all;
      begin
         if Item.Index > Get_Length (Block) then
            Raise_Exception (Constraint_Error'Identity, Bad_Key_Index);
         end if;
         return Get_Key (Block, Item.Index);
      end;
   end Get_Key_Address;

   function Get_Last
            (  Container : B_Tree;
               Root      : Byte_Index
            )  return Item_Ptr is
      Pool   : Persistent_Pool'Class renames Container.Pool.all;
      This   : Byte_Index := Root;
      Next   : Byte_Index;
      Length : Integer;
   begin
      if This = 0 then
         return No_Item;
      end if;
      Length := Get_Length (Load (Pool.File, This).all);
      if Length < 0 then
         return No_Item;
      else
         loop
            Next :=
               Get_Child
               (  Load (Pool.File, This).all,
                  Length + 1
               );
            if Next = 0 then
               if Length = 0 then
                  return No_Item;
               else
                  return (Container.Self, This, Length);
               end if;
            end if;
            This   := Next;
            Length := Get_Length (Load (Pool.File, This).all);
         end loop;
      end if;
   end Get_Last;

   function Get_Last (Container : B_Tree) return Item_Ptr is
      Lock : Holder (Container.Pool);
   begin
      return Get_Last (Container, Container.Root_Bucket);
   end Get_Last;

   function Get_Last (Item : Item_Ptr) return Item_Ptr is
      Right : Item_Ptr;
   begin
      if Item.Node = 0 then
         return No_Item;
      else
         Right := Get_Right_Child (Item);
         if Right.Node = 0 then
            return Item;
         else
            return Get_Last (Right.Tree.all, Right.Node);
         end if;
      end if;
   end Get_Last;

   function Get_Left_Child (Item : Item_Ptr) return Item_Ptr is
   begin
      if Item.Node = 0 then
         return No_Item;
      end if;
      declare
         Pool   : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock   : Holder (Item.Tree.Pool);
         Block  : Block_Type_Ref := Load (Pool.File, Item.Node);
         Length : Integer := Get_Length (Block.all);
         Result : Item_Ptr;
      begin
         if Item.Index > Length then
            Raise_Exception (Constraint_Error'Identity, Bad_Left_Index);
         end if;
         Result.Tree := Item.Tree;
         Result.Node := Get_Child (Block.all, Item.Index);
         if Result.Node = 0 then
            return No_Item;
         end if;
         Block  := Load (Pool.File, Result.Node);
         Length := Get_Length (Block.all);
         if Length = 0 then
            return No_Item;
         else
            Result.Index := Length;
            return Result;
         end if;
      end;
   end Get_Left_Child;

   function Get_Left_Parent (Item : Item_Ptr) return Item_Ptr is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Pool  : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames Load (Pool.File, Item.Node).all;
         Node  : constant Byte_Index := Get_Parent_Address (Block);
      begin
         if Node = 0 then
            return No_Item;
         else
            declare
               Index : constant Natural := Get_Parent_Index (Block);
            begin
               if Index = 1 then
                  return No_Item;
               else
                  return (Item.Tree, Node, Index - 1);
               end if;
            end;
         end if;
      end;
   end Get_Left_Parent;

   function Get_Next
            (  Container : B_Tree;
               Node      : Byte_Index;
               Index     : Positive
            )  return Item_Ptr is
   begin
      if Node = 0 then
         return No_Item;
      end if;
      declare
         Pool   : Persistent_Pool'Class renames Container.Pool.all;
         Block  : Block_Type_Ref := Load (Pool.File, Node);
         Length : constant Integer := Get_Length (Block.all);
      begin
         if Index > Length then
            if Index = Length + 1 then
               return No_Item;
            else
               Raise_Exception
               (  Constraint_Error'Identity,
                  Bad_Next_Index
               );
            end if;
         end if;
         declare
            First : Item_Ptr :=
                       Get_First
                       (  Container,
                          Get_Child (Block.all, Index + 1)
                       );
         begin
            if First.Node = 0 or else First.Node = Node then
               if Index = Length then
                  Block := Load (Pool.File, Node);
                  First.Tree := Container.Self;
                  First.Node := Get_Parent_Address (Block.all);
                  while First.Node /= 0 loop
                     First.Index := Get_Parent_Index (Block.all);
                     Block := Load (Pool.File, First.Node);
                     if First.Index <= Get_Length (Block.all) then
                        return First;
                     end if;
                     First.Node := Get_Parent_Address (Block.all);
                  end loop;
                  return No_Item;
               else
                  return (Container.Self, Node, Index + 1);
               end if;
            else
               return First;
            end if;
         end;
      end;
   end Get_Next;

   function Get_Next (Item : Item_Ptr) return Item_Ptr is
      Lock : Holder (Item.Tree.Pool);
   begin
      return Get_Next (Item.Tree.all, Item.Node, Item.Index);
   end Get_Next;

   function Get_Previous
            (  Container : B_Tree;
               Node      : Byte_Index;
               Index     : Positive
            )  return Item_Ptr is
   begin
      if Node = 0 then
         return No_Item;
      end if;
      declare
         Pool   : Persistent_Pool'Class renames Container.Pool.all;
         Block  : Block_Type_Ref   := Load (Pool.File, Node);
         Length : constant Integer := Get_Length (Block.all);
      begin
         if Index > Length then
            Raise_Exception (Constraint_Error'Identity, Bad_Prev_Index);
         end if;
         declare
            Last : Item_Ptr :=
                      Get_Last
                      (  Container,
                         Get_Child (Block.all, Index)
                      );
         begin
            if Last.Node = 0 or else Last.Node = Node then
               if Index = 1 then
                  Block := Load (Pool.File, Node);
                  Last.Tree := Container.Self;
                  Last.Node := Get_Parent_Address (Block.all);
                  while Last.Node /= 0 loop
                     Last.Index := Get_Parent_Index (Block.all);
                     if Last.Index > 1 then
                        return
                        (  Container.Self,
                           Last.Node,
                           Last.Index - 1
                        );
                     end if;
                     Block := Load (Pool.File, Last.Node);
                     Last.Node := Get_Parent_Address (Block.all);
                   end loop;
                   return No_Item;
               else
                  return (Container.Self, Node, Index - 1);
               end if;
            else
               return Last;
            end if;
         end;
      end;
   end Get_Previous;

   function Get_Previous (Item : Item_Ptr) return Item_Ptr is
      Lock : Holder (Item.Tree.Pool);
   begin
      return Get_Previous (Item.Tree.all, Item.Node, Item.Index);
   end Get_Previous;

   function Get_Right_Child (Item : Item_Ptr) return Item_Ptr is
   begin
      if Item.Node = 0 then
         return No_Item;
      end if;
      declare
         Pool   : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock   : Holder (Item.Tree.Pool);
         Block  : Block_Type_Ref := Load (Pool.File, Item.Node);
         Length : Integer := Get_Length (Block.all);
         Result : Item_Ptr;
      begin
         if Item.Index > Length then
            Raise_Exception (Constraint_Error'Identity, Bad_Righ_Index);
         end if;
         Result.Tree := Item.Tree;
         Result.Node := Get_Child (Block.all, Item.Index + 1);
         if Result.Node = 0 then
            return No_Item;
         end if;
         Block  := Load (Pool.File, Result.Node);
         Length := Get_Length (Block.all);
         if Length = 0 then
            return No_Item;
         else
            Result.Index := 1;
            return Result;
         end if;
      end;
   end Get_Right_Child;

   function Get_Right_Parent (Item : Item_Ptr) return Item_Ptr is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Pool  : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames Load (Pool.File, Item.Node).all;
         Node  : constant Byte_Index := Get_Parent_Address (Block);
      begin
         if Node = 0 then
            return No_Item;
         else
            declare
               Index : constant Natural := Get_Parent_Index (Block);
            begin
               if Index > Get_Length (Load (Pool.File, Node).all) then
                  return No_Item;
               else
                  return (Item.Tree, Node, Index);
               end if;
            end;
         end if;
      end;
   end Get_Right_Parent;

   function Get_Root (Item : Item_Ptr) return Item_Ptr is
      Container : B_Tree'Class renames Item.Tree.all;
   begin
      if Item.Node = 0 then
         return No_Item;
      else
         declare
            Pool  : Persistent_Pool'Class renames Container.Pool.all;
            Lock  : Holder (Container.Pool);
            Block : Block_Type_Ref := Load (Pool.File, Item.Node);
            Node  : Byte_Index := Item.Node;
            Next  : Byte_Index := Get_Parent_Address (Block.all);
         begin
            while Next /= 0 loop
               Node  := Next;
               Block := Load (Pool.File, Node);
               Next  := Get_Parent_Address (Block.all);
            end loop;
            return (Item.Tree, Node, 1);
         end;
      end if;
   end Get_Root;

   function Get_Root (Container : B_Tree) return Item_Ptr is
      Lock : Holder (Container.Pool);
   begin
      if Container.Root_Bucket = 0 then
         return No_Item;
      else
         return (Container.Self, Container.Root_Bucket, 1);
      end if;
   end Get_Root;

   function Get_Self (Container : B_Tree) return B_Tree_Ptr is
   begin
      return Container.Self;
   end Get_Self;

   function Get_Self (Item : Item_Ptr) return B_Tree_Ptr is
   begin
      return Item.Tree;
   end Get_Self;

   function Get_Tag (Item : Item_Ptr) return Byte_Index is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Pool : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock : Holder (Item.Tree.Pool);
      begin
         return Get_Node_Tag (Load (Pool.File, Item.Node).all);
      end;
   end Get_Tag;

   function Get_Value
            (  Container : B_Tree;
               Value     : Byte_Index
            )  return Object_Type is
      Stream : aliased Unchecked_Input_Stream (Container.Pool);
   begin
      Open (Stream, Value);
      return Input_Value (Stream'Access);
   end Get_Value;

   function Get_Value (Item : Item_Ptr) return Object_Type is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames
                 Load (Item.Tree.Pool.File, Item.Node).all;
      begin
         if Item.Index > Get_Length (Block) then
            Raise_Exception (Constraint_Error'Identity, Bad_Val_Index);
         end if;
         return Get_Value
                (  Item.Tree.all,
                   Get_Value (Block, Item.Index)
                );
      end;
   end Get_Value;

   procedure Get_Value
             (  Item     : Item_Ptr;
                Consumer : in out Abstract_Value_Access'Class
             )  is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames
                 Load (Item.Tree.Pool.File, Item.Node).all;
      begin
         if Item.Index > Get_Length (Block) then
            Raise_Exception (Constraint_Error'Identity, Bad_Val_Index);
         end if;
         declare
            Stream : aliased Unchecked_Input_Stream (Item.Tree.Pool);
         begin
            Open (Stream, Get_Value (Block, Item.Index));
            Get (Consumer, Stream);
         end;
      end;
   end Get_Value;

   function Get_Value_Address (Item : Item_Ptr) return Byte_Index is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Lock  : Holder (Item.Tree.Pool);
         Block : Block_Type renames
                 Load (Item.Tree.Pool.File, Item.Node).all;
      begin
         if Item.Index > Get_Length (Block) then
            Raise_Exception (Constraint_Error'Identity, Bad_Val_Index);
         end if;
         return Get_Value (Block, Item.Index);
      end;
   end Get_Value_Address;

   function Inf (Container : B_Tree; Key : Key_Type) return Item_Ptr is
      Lock  : Holder (Container.Pool);
      Node  : Byte_Index;
      Index : Integer;
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      if Index = 0 then
         return No_Item;
      elsif Index > 0 then
         return (Container.Self, Node, Index);
      else
         declare
            Block : constant Block_Type_Ref :=
                    Load (Container.Pool.File, Node);
         begin
            if -Index > Get_Length (Block.all) then
               if Index < -1 then
                  return (Container.Self, Node, -Index - 1);
               else
                  return No_Item;
               end if;
            else
               return Get_Previous (Container, Node, -Index);
            end if;
         end;
      end if;
   end Inf;

   procedure Initialize (Container : in out B_Tree) is
      use Ada.Finalization;
      Lock : Holder (Container.Pool);
   begin
      Initialize (Limited_Controlled (Container));
   end Initialize;

   procedure Insert
              (  Container   : in out B_Tree;
                 Parent      : Byte_Index;
                 Key         : Key_Type;
                 Key_Index   : Byte_Index;
                 Value_Index : Byte_Index;
                 Child       : Byte_Index
             )  is
   begin
      if Parent = 0 then
         Insert
         (  (Container.Self, 0, 1),
            Key,
            Key_Index,
            Value_Index,
            Child
         );
      else
         declare
            Index : constant Integer :=
                    Find
                    (  Container,
                       Load (Container.Pool.File, Parent).all,
                       Key
                   );
         begin
            if Index > 0 then
               Raise_Exception (Data_Error'Identity, Reinsert_Error);
            end if;
            Insert
            (  (Container.Self, Parent, -Index),
               Key,
               Key_Index,
               Value_Index,
               Child
            );
         end;
      end if;
   end Insert;

   procedure Insert
              (  Container   : in out B_Tree;
                 Parent      : Byte_Index;
                 Key_Index   : Byte_Index;
                 Value_Index : Byte_Index;
                 Child       : Byte_Index
             )  is
   begin
      Insert
      (  Container,
         Parent,
         Get_Key (Container, Key_Index),
         Key_Index,
         Value_Index,
         Child
      );
   end Insert;

   procedure Insert
             (  Parent      : Item_Ptr;
                Key         : Key_Type;
                Key_Index   : Byte_Index;
                Value_Index : Byte_Index;
                Child       : Byte_Index
             )  is
      Container : B_Tree'Class renames Parent.Tree.all;
      Ancestor  : Byte_Index;
      Length    : Integer;
      Split     : Integer;
      Index     : Integer := Parent.Index;
   begin
      if Parent.Node = 0 then -- Creating new root node
         Raise_Exception (Data_Error'Identity, No_Node_Insert);
      end if;
      declare
         Block : Block_Type renames
                 Load (Container.Pool.File, Parent.Node).all;
      begin
         Length := Get_Length (Block);
         if Length < Width then -- Have place in the bucket
            Move
            (  Container.Pool.File,
               Parent.Node,
               Index  + 1,
               Length + 1,
               Index
            );
            Update
            (  Container,
               Parent.Node,
               Index,
               Key_Index,
               Value_Index,
               Child,
               True
            );
            return;
         end if;
         Ancestor := Get_Parent_Address (Block);
         Split    := Get_Parent_Index   (Block);
      end;
      if Index = Width + 1 then
         declare
            Left  : Byte_Index;
            Space : Natural;
         begin
            if Ancestor /= 0 and then Split >= 2 then
               Split := Split - 1;
               Left  := Get_Child
                        (  Load (Container.Pool.File, Ancestor).all,
                           Split
                        );
               if Left /= 0 then
                  Space :=
                     Get_Length (Load (Container.Pool.File, Left).all);
                  if Space < Width then -- Underfilled left
                     Copy
                     (  Container.Pool.File, -- Append pair from the
                        Left,                -- parent to the left node
                        Space + 1,
                        Ancestor,
                        Split,
                        True            -- Increment the left's length
                     );
                     Copy
                     (  Container.Pool.File, -- Copy the first child
                        Left,                -- from the right node as
                        Space + 2,           -- the last child of the
                        Space + 1,           -- left node
                        Parent.Node,
                        1
                     );
                     Copy
                     (  Container.Pool.File, -- Move the first pair from
                        Ancestor,            -- the right node up to the
                        Split,               -- parent at the split
                        Parent.Node,
                        1
                     );
                     Move
                     (  Container.Pool.File, -- Move pairs and children
                        Parent.Node,         -- of the right node to the
                        1,                   -- left
                        Width - 1,
                        2,
                        0
                     );
                     Update
                     (  Container,   -- Insert the key and value at the
                        Parent.Node, -- end of the right node
                        Width,
                        Key_Index,
                        Value_Index,
                        Child
                     );
                     return;
                  end if;
               end if;
            end if;
         end;
      end if;
      if Index = 1 then
         declare
            Right : Byte_Index;
            Space : Natural;
            Block : Block_Type renames
                    Load (Container.Pool.File, Ancestor).all;
         begin
            if Ancestor /= 0 and then Split <= Get_Length (Block) then
               Right := Get_Child (Block, Split + 1);
               if Right /= 0 then
                  Space :=
                     Get_Length (Load (Container.Pool.File, Right).all);
                  if Space < Width then
                     --
                     -- Fill right sibling without splitting the bucket
                     --
                     --                         Index = 1
                     --                    K5               K+ < K1
                     --         [ * | * | L | R | * ]      /
                     --                  /     \         C+
                     --   K1 K2 K3 K4   /       \ K6 K7
                     -- [C1|C2|C3|C4|C5]        [C6|C7|C8|  |  ]   |
                     --                                            |
                     --                    K4                      V
                     --         [ * | * | L | R | * ]
                     --                  /     \
                     --   K+ K1 K2 K3   /       \ K5 K6 K7
                     -- [C+|C1|C2|C3|C4]        [C5|C6|C7|C8|  ]
                     --
                     Move
                     (  Container.Pool.File, -- Move items of the right
                        Right,               -- node to the right
                        2,
                        Space + 1,
                        1
                     );
                     Copy
                     (  Container.Pool.File, -- Move last child from the
                        Right,               -- node to the beginning of
                        1,                   -- the right node
                        0,
                        Parent.Node,
                        Width + 1
                     );
                     Copy
                     (  Container.Pool.File, -- Move pair from the
                        Right,               -- parent node at the
                        1,                   -- split to the first place
                        Ancestor,            -- of the right node
                        Split,
                        True            -- Increment right node length
                     );
                     Copy
                     (  Container.Pool.File, -- Move last pair of the
                        Ancestor,            -- node to the parent node
                        Split,               -- at the split
                        Parent.Node,
                        Width
                     );
                     Move
                     (  Container.Pool.File, -- Move items in the node
                        Parent.Node,         -- to the right
                        2,
                        Width,
                        1
                     );
                     Update
                     (  Container,      -- Insert the new key, value
                        Parent.Node,    -- and the child at the index
                        1,              -- 1, which is now free
                        Key_Index,
                        Value_Index,
                        Child
                     );
                     return;
                  end if;
               end if;
            end if;
         end;
      end if;
      declare
         New_Node : constant Byte_Index :=
                    Unchecked_Allocate (Container.Pool.all, Node_Size);
      begin
         declare
            Block : Block_Type renames
                    Update (Container.Pool.File, New_Node).all;
         begin
            Set_Length (Block, Left_Half);
            Set_Node_Tag (Block, 0);
         end;
         Set_Length
         (  Update (Container.Pool.File, Parent.Node).all,
            Right_Half
         );
         if Index = Left_Half + 1 then -- Insert at the split
            --
            -- The new key is at the split        Left_Half = 2
            --                                   Right_Half = 2
            --   K1 K2 K3 K4     + K2 < K+ < K3       Index = 3
            -- [C1|C2|C3|C4|C5]         C+
            --                  K+
            --   K1 K2         /  K3 K4
            -- [C1|C2|C+|  |  ] [C3|C4|C5|  |  ]
            --
            if Ancestor = 0 then
               New_Root
               (  Container,
                  Key_Index,
                  Value_Index,
                  New_Node,
                  Parent.Node
               );
            else
               Insert
               (  Container,
                  Ancestor,
                  Key,
                  Key_Index,
                  Value_Index,
                  New_Node
               );
            end if;
            Copy
            (  Container.Pool.File, -- Copy half of node to the left
               New_Node,            -- node
               1,
               Left_Half,
               Parent.Node,
               1
            );
            Set_Child
            (  Update (Container.Pool.File, New_Node).all,
               Left_Half + 1,
               Child
            );
            Move               -- Shift right node to the left
            (  Container.Pool.File,
               Parent.Node,
               1,
               Right_Half,
               Left_Half + 1
            );
            Set_Parent
            (  Container.Pool.File,
               Child,
               New_Node,
               Left_Half + 1
            );
         elsif Index <= Left_Half then
            --
            -- The new key is left of the split   Left_Half = 2
            --                                   Right_Half = 2
            --   K1 K2 K3 K4     + K1 < K+ < K2       Index = 1
            -- [C1|C2|C3|C4|C5]        /
            --                       C+
            --
            --                  K2 (the last left key)
            --   K1 K+         /  K3 K4
            -- [C1|C+|C2|  |  ] [C3|C4|C5|  |  ]
            --
            declare
               Block : Block_Type renames
                       Load (Container.Pool.File, Parent.Node).all;
            begin
               if Ancestor = 0 then
                  New_Root
                  (  Container,
                     Get_Key   (Block, Left_Half),
                     Get_Value (Block, Left_Half),
                     New_Node,
                     Parent.Node
                  );
                  else
                     Insert
                     (  Container,
                        Ancestor,
                        Get_Key   (Block, Left_Half),
                        Get_Value (Block, Left_Half),
                        New_Node
                     );
                  end if;
            end;
            Copy
            (  Container.Pool.File,  -- Copy items under the insertion
               New_Node,             -- point from the old node
               1,
               Index - 1,
               Parent.Node,
               1,
               0
            );
            Copy
            (  Container.Pool.File,  -- Copy items after the insetion
               New_Node,             -- point
               Index + 1,
               Left_Half + 1,
               Parent.Node,
               Index
            );
            Update
            (  Container,       -- Insert key-value pair and the
               New_Node,        -- child
               Index,
               Key_Index,
               Value_Index,
               Child
            );
            Move
            (  Container.Pool.File,  -- Move left items in the right
               Parent.Node,          -- node
               1,
               Right_Half,
               Left_Half + 1
            );
         else
            --
            -- The new key is right of the split  Left_Half = 2
            --                                   Right_Half = 2
            --   K1 K2 K3 K4     + K4 < K+            Index = 5
            -- [C1|C2|C3|C4|C5]        /
            --                       C+
            --
            --                  K3 (the first right key)
            --   K1 K2         /  K4 K+
            -- [C1|C2|C3|  |  ] [C4|C+|C5|  |  ]
            --
            declare
               Block : Block_Type renames
                       Load (Container.Pool.File, Parent.Node).all;
            begin
               if Ancestor = 0 then
                  New_Root
                  (  Container,
                     Get_Key   (Block, Left_Half + 1),
                     Get_Value (Block, Left_Half + 1),
                     New_Node,
                     Parent.Node
                  );
               else
                  Insert
                  (  Container,
                     Ancestor,
                     Get_Key   (Block, Left_Half + 1),
                     Get_Value (Block, Left_Half + 1),
                     New_Node
                  );
               end if;
            end;
            Copy
            (  Container.Pool.File,
               New_Node,
               1,
               Left_Half,
               Parent.Node,
               1
            );
            Index := Index - Left_Half - 1;
            Move
            (  Container.Pool.File,
               Parent.Node,
               1,
               Index - 1,
               Left_Half + 2,
               0
            );
            Move
            (  Container.Pool.File,
               Parent.Node,
               Index + 1,
               Right_Half,
               Index + Left_Half + 1
            );
            Update
            (  Container,   -- Insert key-value pair and the
               Parent.Node, -- child
               Index,
               Key_Index,
               Value_Index,
               Child
            );
         end if;
      end;
   end Insert;

   procedure New_Root
             (  Container : in out B_Tree;
                Key   : Byte_Index;
                Value : Byte_Index;
                Left  : Byte_Index;
                Right : Byte_Index
             )  is
      Node  : constant Byte_Index :=
              Unchecked_Allocate (Container.Pool.all, Node_Size);
      Block : Block_Type renames Update (Container.Pool.File, Node).all;
   begin
      Set_Length (Block, 1);
      Set_Parent_Address (Block, 0);
      Set_Node_Tag       (Block, 0);
      Set_Parent_Index   (Block, 1);
      Set_Key   (Block, 1, Key);
      Set_Value (Block, 1, Value);
      Set_Child (Block, 1, Left);
      Set_Child (Block, 2, Right);
      Container.Root_Bucket := Node;
      Set_Parent (Container.Pool.File, Left,  Node, 1);
      Set_Parent (Container.Pool.File, Right, Node, 2);
   exception
      when others =>
         if Node /= 0 then
            Unchecked_Deallocate (Container.Pool.all, Node);
         end if;
         raise;
   end New_Root;

   procedure New_Root
             (  Container : in out B_Tree;
                Key       : Key_Type;
                Value     : Object_Type;
                Left      : Byte_Index;
                Right     : Byte_Index
             )  is
      Key_Index   : constant Byte_Index :=
                    New_Key (Container.Pool, Key);
      Value_Index : constant Byte_Index :=
                    New_Value (Container.Pool,Value);
   begin
      New_Root (Container, Key_Index, Value_Index, Left, Right);
   exception
      when others =>
         if Key_Index /= 0 then
            Unchecked_Deallocate (Container.Pool.all, Key_Index);
         end if;
         if Value_Index /= 0 then
            Unchecked_Deallocate (Container.Pool.all, Value_Index);
         end if;
         raise;
   end New_Root;

   procedure Remove
             (  Container : in out B_Tree;
                Node      : Byte_Index;
                Index     : Positive
             )  is
      Pool : Persistent_Pool'Class renames Container.Pool.all;
   begin
      if Node = 0 then
         return;
      end if;
      declare
         Block  : Block_Type renames Update (Pool.File, Node).all;
         Key    : Byte_Index;
         Value  : Byte_Index;
         Length : Integer := Get_Length (Block);
      begin
         if Index > Length then
            Raise_Exception (Constraint_Error'Identity, Remove_Error);
         end if;
         Key   := Get_Key   (Block, Index);
         Value := Get_Value (Block, Index);
         if Get_Child (Block, Index) = 0 then
            if Get_Child (Block, Index + 1) = 0 then
               if Length > 1 then
                  Set_Length (Block, Length - 1);
                  Move
                  (  Pool.File,
                     Node,
                     Index,
                     Length - 1,
                     Index + 1
                  );
               else
                  declare
                     Parent : constant Byte_Index :=
                              Get_Parent_Address (Block);
                     Index  : constant Positive :=
                              Get_Parent_Index (Block);
                  begin
                     if Parent = 0 then -- No parent
                        Set_Length (Block, 0);
                     else -- Remove at the parent
                        Set_Child
                        (  Update (Pool.File, Parent).all,
                           Index,
                           0
                        );
                        Unchecked_Deallocate (Pool, Node);
                     end if;
                  end;
               end if;
            else
               if Length > 1 then
                  Set_Length (Block, Length - 1);
                  Move
                  (  Pool.File,
                     Node,
                     Index,
                     Length - 1,
                     Index + 1
                  );
               else
                  declare
                     Parent : constant Byte_Index :=
                              Get_Parent_Address (Block);
                     Right  : constant Byte_Index :=
                              Get_Child (Block, 2);
                  begin
                     if Parent = 0 then -- No parent
                        Length :=
                           Get_Length (Load (Pool.File, Right).all);
                        Set_Length
                        (  Update (Pool.File, Node).all,
                           Length
                        );
                        Copy (Pool.File, Node, 1, Length, Right, 1);
                        Unchecked_Deallocate (Pool, Right);
                     else
                        Set_Parent
                        (  Pool.File,
                           Right,
                           Parent,
                           Get_Parent_Index (Block)
                        );
                        Unchecked_Deallocate (Pool, Node);
                     end if;
                  end;
               end if;
            end if;
         elsif Get_Child (Block, Index + 1) = 0 then
            declare
               Parent : constant Byte_Index :=
                        Get_Parent_Address (Block);
               Left   : constant Byte_Index := Get_Child (Block, 1);
            begin
               if Length > 1 then
                  Set_Length (Block, Length - 1);
                  Move
                  (  Pool.File,
                     Node,
                     Index,
                     Length - 1,
                     Index + 1
                  );
                  Set_Child
                  (  Update (Pool.File, Node).all,
                     Index,
                     Left
                  );
               else
                  if Parent = 0 then -- No parent
                     Length :=
                        Get_Length (Load (Pool.File, Left).all);
                     Set_Length
                     (  Update (Pool.File, Node).all,
                        Length
                     );
                     Copy (Pool.File, Node, 1, Length, Left, 1);
                     Unchecked_Deallocate (Pool, Left);
                  else
                     Set_Parent
                     (  Pool.File,
                        Left,
                        Parent,
                        Get_Parent_Index (Block)
                     );
                     Unchecked_Deallocate (Pool, Node);
                  end if;
               end if;
            end;
         else
            declare
               Left  : Byte_Index := Get_Child (Block, Index);
               Right : Byte_Index := Get_Child (Block, Index + 1);
            begin
               if (  Get_Length (Load (Pool.File, Left).all)
                  >  Get_Length (Load (Pool.File, Right).all)
                  )
               then
                  --
                  -- Rotate left          K-
                  --               [ * | L | R | * ]
                  --                    /     \
                  --     K1  K2  K3    /       \  K6
                  --  [ * | * | * | * ]        [ * | * |   |   ]
                  --                 \
                  --                  \  K4  K5                        |
                  --                  [ * | * | 0 |   ]                |
                  --                                                   V
                  --                      K5
                  --               [ * | L | R |   ]
                  --                    /     \
                  --     K1  K2  K3    /       \  K6
                  --  [ * | * | * | * ]        [ * | * |   |   ]
                  --                 \
                  --                  \  K4
                  --                  [ * | * |   |   ]
                  --
                  loop
                     declare
                        Block : Block_Type renames
                                Load (Pool.File, Left).all;
                     begin
                        Length := Get_Length (Block);
                        Right  := Get_Child (Block, Length + 1);
                     end;
                     exit when Right = 0;
                     Left := Right;
                  end loop;
                  Copy (Pool.File, Node, Index, Left, Length);
                  declare
                     Block : Block_Type renames
                             Update (Pool.File, Left).all;
                  begin
                     if Length = 1 then
                        Set_Parent
                        (  Pool.File,
                           0,
                           Get_Parent_Address (Block),
                           Get_Parent_Index (Block)
                        );
                        Unchecked_Deallocate (Pool, Left);
                     else
                        Set_Length (Block, Length - 1);
                     end if;
                  end;
               else
                  -- Rotate right         K-
                  --               [ * | L | R | * ]
                  --                    /     \
                  --     K1  K2        /       \  K6  K7
                  --  [ * | * | * |   ]        [ * | * | * |   ]
                  --                            /
                  --             K3  K4        /
                  --          [ 0 | * | * |   ]
                  --
                  --                      K3
                  --               [ * | L | R | * ]
                  --                    /     \
                  --     K1  K2        /       \  K6  K7
                  --  [ * | * | * |   ]        [ * | * | * |   ]
                  --                            /
                  --             K4            /
                  --          [ * | * |   |   ]
                  --
                  loop
                     declare
                        Block : Block_Type renames
                                Load (Pool.File, Right).all;
                     begin
                        Length := Get_Length (Block);
                        Left   := Get_Child (Block, Length + 1);
                     end;
                     exit when Left = 0;
                     Right := Left;
                  end loop;
                  Copy (Pool.File, Node, Index, Right, 1);
                  declare
                     Block : Block_Type renames
                             Update (Pool.File, Right).all;
                  begin
                     if Length = 1 then
                        Set_Parent
                        (  Pool.File,
                           0,
                           Get_Parent_Address (Block),
                           Get_Parent_Index (Block)
                        );
                        Unchecked_Deallocate (Pool, Right);
                     else
                        Set_Length (Block, Length - 1);
                        Move (Pool.File, Right, 1, Length - 1, 2);
                     end if;
                  end;
               end if;
            end;
         end if;
         Unchecked_Deallocate (Pool, Key);
         Unchecked_Deallocate (Pool, Value);
      end;
   end Remove;

   procedure Remove (Item : in out Item_Ptr) is
      Container : B_Tree'Class renames Item.Tree.all;
      Lock      : Holder (Container.Pool);
   begin
      Remove (Container, Item.Node, Item.Index);
      Item := No_Item;
   end Remove;

   procedure Remove (Container : in out B_Tree; Key : Key_Type) is
      Lock : Holder (Container.Pool);
      Node  : Byte_Index;
      Index : Integer;
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      if Index > 0 then
         Remove (Container, Node, Index);
      end if;
   end Remove;

   procedure Replace (Item : Item_Ptr; Value : Object_Type) is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, Null_Replace);
      else
         declare
            Container   : B_Tree'Class renames Item.Tree.all;
            Pool        : Persistent_Pool'Class renames
                          Container.Pool.all;
            Lock        : Holder (Container.Pool);
            Replacement : constant Byte_Index :=
                          New_Value (Container.Pool, Value);
            Block       : Block_Type renames
                          Update (Pool.File, Item.Node).all;
         begin
            if Item.Index > Get_Length (Block) then
               Raise_Exception
               (  Constraint_Error'Identity,
                  Replace_Error
               );
            end if;
            declare
               Old : constant Byte_Index :=
                     Get_Value (Block, Item.Index);
            begin
               Set_Value (Block, Item.Index, Replacement);
               Unchecked_Deallocate (Pool, Old);
            end;
         exception
            when others =>
               Unchecked_Deallocate (Pool, Replacement);
               raise;
         end;
      end if;
   end Replace;

   procedure Replace
             (  Item     : Item_Ptr;
                Producer : in out Abstract_Value_Access'Class
             )  is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, Null_Replace);
      end if;
      declare
         Container : B_Tree'Class renames Item.Tree.all;
         Pool      : Persistent_Pool'Class renames Container.Pool.all;
         Lock      : Holder (Container.Pool);
         Stream    : aliased Unchecked_Output_Stream (Container.Pool);
         Block     : Block_Type renames
                     Update (Pool.File, Item.Node).all;
      begin
         if Item.Index > Get_Length (Block) then
            Raise_Exception (Constraint_Error'Identity, Replace_Error);
         end if;
         Open (Stream, Get_Value (Block, Item.Index));
         Put (Producer, Stream);
      end;
   end Replace;

   procedure Replace
             (  Container : in out B_Tree;
                Key       : Key_Type;
                Value     : Object_Type
             )  is
      Pool : Persistent_Pool'Class renames Container.Pool.all;
      Lock : Holder (Container.Pool);
   begin
      if Container.Root_Bucket = 0 then
         New_Root (Container, Key, Value, 0, 0);
      else
         declare
            Value_Index : Byte_Index := 0;
            Node        : Byte_Index;
            Index       : Integer;
         begin
            Search (Container, Container.Root_Bucket, Key, Node, Index);
            Value_Index := New_Value (Container.Pool, Value);
            if Index > 0 then
               declare
                  Block  : Block_Type renames
                           Update (Pool.File, Node).all;
                  Update : constant Byte_Index := Value_Index;
               begin
                  Value_Index := Get_Value (Block, Index);
                  Set_Value (Block, Index, Update);
                  Unchecked_Deallocate (Pool, Value_Index);
               end;
            else
               declare
                  Key_Index : Byte_Index := 0;
               begin
                  Key_Index   := New_Key   (Container.Pool, Key);
                  Value_Index := New_Value (Container.Pool, Value);
                  Insert
                  (  Key         => Key,
                     Key_Index   => Key_Index,
                     Value_Index => Value_Index,
                     Child       => 0,
                     Parent      => (  Container'Unchecked_Access,
                                       Node,
                                      -Index
                  )                 );
               exception
                  when others =>
                     if Key_Index /= 0 then
                        Unchecked_Deallocate (Pool, Key_Index);
                     end if;
                     raise;
               end;
            end if;
         exception
            when others =>
               if Value_Index /= 0 then
                  Unchecked_Deallocate (Pool, Value_Index);
               end if;
               raise;
         end;
      end if;
   end Replace;

   procedure Replace
             (  Container : in out B_Tree;
                Key       : Key_Type;
                Producer  : in out Abstract_Value_Access'Class
             )  is
      Pool   : Persistent_Pool'Class renames Container.Pool.all;
      Lock   : Holder (Container.Pool);
      Stream : aliased Unchecked_Output_Stream (Container.Pool);
   begin
      if Container.Root_Bucket = 0 then
         declare
            Index : constant Byte_Index :=
                    New_Key (Container.Pool, Key);
         begin
            Put (Producer, Stream);
            New_Root
            (  Container,
               Index,
               Get_First (Stream),
               0,
               0
            );
         exception
            when others =>
               if Index /= 0 then
                  Unchecked_Deallocate (Pool, Index);
               end if;
               if Get_First (Stream) /= 0 then
                  Unchecked_Deallocate (Pool, Get_First (Stream));
               end if;
               raise;
         end;
      else
         declare
            Node  : Byte_Index;
            Where : Integer;
         begin
            Search
            (  Container,
               Container.Root_Bucket,
               Key,
               Node,
               Where
            );
            if Where > 0 then
               declare
                  Block : Block_Type renames
                          Update (Pool.File, Node).all;
               begin
                  Open (Stream, Get_Value (Block, Where));
                  Put (Producer, Stream);
               end;
            else
               declare
                  Index : Byte_Index := 0;
               begin
                  Index := New_Key (Container.Pool, Key);
                  Put (Producer, Stream);
                  Insert
                  (  Key         => Key,
                     Key_Index   => Index,
                     Value_Index => Get_First (Stream),
                     Child       => 0,
                     Parent      => (  Container'Unchecked_Access,
                                       Node,
                                      -Where
                  )                 );
               exception
                  when others =>
                     if Index /= 0 then
                        Unchecked_Deallocate (Pool, Index);
                     end if;
                     if Get_First (Stream) /= 0 then
                        Unchecked_Deallocate
                        (  Pool,
                           Get_First (Stream)
                        );
                     end if;
                     raise;
               end;
            end if;
         end;
      end if;
   end Replace;

   procedure Restore
             (  Container : in out B_Tree;
                Reference : Byte_Index
             )  is
   begin
      Set_Root_Address (Container, Reference);
   end Restore;

   procedure Search
             (  Container : B_Tree;
                Root      : Byte_Index;
                Key       : Key_Type;
                Node      : out Byte_Index;
                Index     : out Integer
             )  is
   begin
      Node := Root;
      if Node = 0 then
         Index := 0;
      else
         loop
            Index :=
               Find
               (  Container,
                  Load (Container.Pool.File, Node).all,
                  Key
               );
            exit when Index > 0;
            declare
               Next : constant Byte_Index :=
                      Get_Child
                      (  Load (Container.Pool.File, Node).all,
                        -Index
                      );
            begin
               exit when Next = 0;
               Node := Next;
            end;
         end loop;
      end if;
   end Search;

   procedure Set_Root_Address
             (  Container : in out B_Tree;
                Root      : Byte_Index
             )  is
      Lock : Holder (Container.Pool);
   begin
      Container.Root_Bucket := Root;
   end Set_Root_Address;

   procedure Set_Tag (Item : Item_Ptr; Tag : Byte_Index) is
   begin
      if Item.Node = 0 then
         Raise_Exception (Constraint_Error'Identity, No_Item_Error);
      end if;
      declare
         Pool : Persistent_Pool'Class renames Item.Tree.Pool.all;
         Lock : Holder (Item.Tree.Pool);
      begin
         Set_Node_Tag (Update (Pool.File, Item.Node).all, Tag);
      end;
   end Set_Tag;

   procedure Store
             (  Container : in out B_Tree;
                Reference : out Byte_Index
             )  is
      Lock : Holder (Container.Pool);
   begin
      Reference := Container.Root_Bucket;
      Container.Root_Bucket := 0;
   end Store;

   function Sup (Container : B_Tree; Key : Key_Type) return Item_Ptr is
      Lock  : Holder (Container.Pool);
      Node  : Byte_Index;
      Index : Integer;
   begin
      Search (Container, Container.Root_Bucket, Key, Node, Index);
      if Index = 0 then
         return No_Item;
      elsif Index > 0 then
         return (Container.Self, Node, Index);
      else
         declare
            Block : constant Block_Type_Ref :=
                    Load (Container.Pool.File, Node);
         begin
            if -Index > Get_Length (Block.all) then
               if Index < -1 then
                  return Get_Next (Container, Node, -Index - 1);
               else
                  return No_Item;
               end if;
            else
               return (Container.Self, Node, -Index);
            end if;
         end;
      end if;
   end Sup;

   procedure Traverse
             (  Container : B_Tree;
                Iterator  : in out Abstract_Visitor'Class;
                From      : Item_Ptr;
                To        : Key_Type
             )  is
      function Visit_Item
               (  Container : B_Tree;
                  Key       : Key_Type;
                  Item      : Item_Ptr
               )  return Boolean is
      begin
         return Visit_Item
                (  Iterator'Unchecked_Access,
                   Container,
                   Key,
                   Item
                );
      end Visit_Item;

      function Visit_Range
               (  Container : B_Tree;
                  Item      : Item_Ptr
               )  return Bucket_Traversal is
      begin
         return Visit_Range
                (  Iterator'Unchecked_Access,
                   Container,
                   Item
                );
      end Visit_Range;

      procedure Walker is new Generic_Traverse;
   begin
      Walker (Container, From, To);
   end Traverse;

   procedure Update
             (  Container : in out B_Tree;
                Node      : Byte_Index;
                Index     : Positive;
                Key       : Byte_Index;
                Value     : Byte_Index;
                Child     : Byte_Index;
                Enlarge   : Boolean := False
             )  is
      Block : Block_Type renames Update (Container.Pool.File, Node).all;
   begin
      Set_Key   (Block, Index, Key);
      Set_Value (Block, Index, Value);
      Set_Child (Block, Index, Child);
      if Enlarge then
         Set_Length (Block, Get_Length (Block) + 1);
      end if;
      Set_Parent (Container.Pool.File, Child, Node, Index);
   end Update;

   procedure Update
             (  Container : in out B_Tree;
                Node      : Byte_Index;
                Index     : Positive;
                Key       : Key_Type;
                Value     : Object_Type;
                Child     : Byte_Index;
                Enlarge   : Boolean := False
             )  is
      Key_Index   : constant Byte_Index :=
                    New_Key (Container.Pool, Key);
      Value_Index : constant Byte_Index :=
                    New_Value (Container.Pool, Value);
   begin
      Update
      (  Container,
         Node,
         Index,
         Key_Index,
         Value_Index,
         Child,
         Enlarge
      );
   exception
      when others =>
         if Key_Index /= 0 then
            Unchecked_Deallocate (Container.Pool.all, Key_Index);
         end if;
         if Value_Index /= 0 then
            Unchecked_Deallocate (Container.Pool.all, Value_Index);
         end if;
   end Update;

end Persistent.Memory_Pools.Streams.Generic_External_B_Tree;