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 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366
|
------------------------------------------------------------------------------
-- --
-- GNAT LIBRARY COMPONENTS --
-- --
-- ADA.CONTAINERS.BOUNDED_MULTIWAY_TREES --
-- --
-- B o d y --
-- --
-- Copyright (C) 2011-2024, Free Software Foundation, Inc. --
-- --
-- GNAT is free software; you can redistribute it and/or modify it under --
-- terms of the GNU General Public License as published by the Free Soft- --
-- ware Foundation; either version 3, or (at your option) any later ver- --
-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. --
-- --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception, --
-- version 3.1, as published by the Free Software Foundation. --
-- --
-- You should have received a copy of the GNU General Public License and --
-- a copy of the GCC Runtime Library Exception along with this program; --
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
-- <http://www.gnu.org/licenses/>. --
-- --
-- This unit was originally developed by Matthew J Heaney. --
------------------------------------------------------------------------------
with Ada.Finalization;
with System; use type System.Address;
with System.Put_Images;
package body Ada.Containers.Bounded_Multiway_Trees with
SPARK_Mode => Off
is
pragma Warnings (Off, "variable ""Busy*"" is not referenced");
pragma Warnings (Off, "variable ""Lock*"" is not referenced");
-- See comment in Ada.Containers.Helpers
use Finalization;
--------------------
-- Root_Iterator --
--------------------
type Root_Iterator is abstract new Limited_Controlled and
Tree_Iterator_Interfaces.Forward_Iterator with
record
Container : Tree_Access;
Subtree : Count_Type;
end record;
overriding procedure Finalize (Object : in out Root_Iterator);
-----------------------
-- Subtree_Iterator --
-----------------------
type Subtree_Iterator is new Root_Iterator with null record;
overriding function First (Object : Subtree_Iterator) return Cursor;
overriding function Next
(Object : Subtree_Iterator;
Position : Cursor) return Cursor;
---------------------
-- Child_Iterator --
---------------------
type Child_Iterator is new Root_Iterator and
Tree_Iterator_Interfaces.Reversible_Iterator with null record;
overriding function First (Object : Child_Iterator) return Cursor;
overriding function Next
(Object : Child_Iterator;
Position : Cursor) return Cursor;
overriding function Last (Object : Child_Iterator) return Cursor;
overriding function Previous
(Object : Child_Iterator;
Position : Cursor) return Cursor;
-----------------------
-- Local Subprograms --
-----------------------
procedure Initialize_Node (Container : in out Tree; Index : Count_Type);
procedure Initialize_Root (Container : in out Tree);
procedure Allocate_Node
(Container : in out Tree;
Initialize_Element : not null access procedure (Index : Count_Type);
New_Node : out Count_Type);
procedure Allocate_Node
(Container : in out Tree;
New_Item : Element_Type;
New_Node : out Count_Type);
procedure Allocate_Node
(Container : in out Tree;
Stream : not null access Root_Stream_Type'Class;
New_Node : out Count_Type);
procedure Deallocate_Node
(Container : in out Tree;
X : Count_Type);
procedure Deallocate_Children
(Container : in out Tree;
Subtree : Count_Type;
Count : in out Count_Type);
procedure Deallocate_Subtree
(Container : in out Tree;
Subtree : Count_Type;
Count : in out Count_Type);
function Equal_Children
(Left_Tree : Tree;
Left_Subtree : Count_Type;
Right_Tree : Tree;
Right_Subtree : Count_Type) return Boolean;
function Equal_Subtree
(Left_Tree : Tree;
Left_Subtree : Count_Type;
Right_Tree : Tree;
Right_Subtree : Count_Type) return Boolean;
procedure Iterate_Children
(Container : Tree;
Subtree : Count_Type;
Process : not null access procedure (Position : Cursor));
procedure Iterate_Subtree
(Container : Tree;
Subtree : Count_Type;
Process : not null access procedure (Position : Cursor));
procedure Copy_Children
(Source : Tree;
Source_Parent : Count_Type;
Target : in out Tree;
Target_Parent : Count_Type;
Count : in out Count_Type);
procedure Copy_Subtree
(Source : Tree;
Source_Subtree : Count_Type;
Target : in out Tree;
Target_Parent : Count_Type;
Target_Subtree : out Count_Type;
Count : in out Count_Type);
function Find_In_Children
(Container : Tree;
Subtree : Count_Type;
Item : Element_Type) return Count_Type;
function Find_In_Subtree
(Container : Tree;
Subtree : Count_Type;
Item : Element_Type) return Count_Type;
function Child_Count
(Container : Tree;
Parent : Count_Type) return Count_Type;
function Subtree_Node_Count
(Container : Tree;
Subtree : Count_Type) return Count_Type;
function Is_Reachable
(Container : Tree;
From, To : Count_Type) return Boolean;
function Root_Node (Container : Tree) return Count_Type;
procedure Remove_Subtree
(Container : in out Tree;
Subtree : Count_Type);
procedure Insert_Subtree_Node
(Container : in out Tree;
Subtree : Count_Type'Base;
Parent : Count_Type;
Before : Count_Type'Base);
procedure Insert_Subtree_List
(Container : in out Tree;
First : Count_Type'Base;
Last : Count_Type'Base;
Parent : Count_Type;
Before : Count_Type'Base);
procedure Splice_Children
(Container : in out Tree;
Target_Parent : Count_Type;
Before : Count_Type'Base;
Source_Parent : Count_Type);
procedure Splice_Children
(Target : in out Tree;
Target_Parent : Count_Type;
Before : Count_Type'Base;
Source : in out Tree;
Source_Parent : Count_Type);
procedure Splice_Subtree
(Target : in out Tree;
Parent : Count_Type;
Before : Count_Type'Base;
Source : in out Tree;
Position : in out Count_Type); -- source on input, target on output
---------
-- "=" --
---------
function "=" (Left, Right : Tree) return Boolean is
begin
if Left.Count /= Right.Count then
return False;
end if;
if Left.Count = 0 then
return True;
end if;
return Equal_Children
(Left_Tree => Left,
Left_Subtree => Root_Node (Left),
Right_Tree => Right,
Right_Subtree => Root_Node (Right));
end "=";
-------------------
-- Allocate_Node --
-------------------
procedure Allocate_Node
(Container : in out Tree;
Initialize_Element : not null access procedure (Index : Count_Type);
New_Node : out Count_Type)
is
begin
if Container.Free >= 0 then
New_Node := Container.Free;
pragma Assert (New_Node in Container.Elements'Range);
-- We always perform the assignment first, before we change container
-- state, in order to defend against exceptions duration assignment.
Initialize_Element (New_Node);
Container.Free := Container.Nodes (New_Node).Next;
else
-- A negative free store value means that the links of the nodes in
-- the free store have not been initialized. In this case, the nodes
-- are physically contiguous in the array, starting at the index that
-- is the absolute value of the Container.Free, and continuing until
-- the end of the array (Nodes'Last).
New_Node := abs Container.Free;
pragma Assert (New_Node in Container.Elements'Range);
-- As above, we perform this assignment first, before modifying any
-- container state.
Initialize_Element (New_Node);
Container.Free := Container.Free - 1;
if abs Container.Free > Container.Capacity then
Container.Free := 0;
end if;
end if;
Initialize_Node (Container, New_Node);
end Allocate_Node;
procedure Allocate_Node
(Container : in out Tree;
New_Item : Element_Type;
New_Node : out Count_Type)
is
procedure Initialize_Element (Index : Count_Type);
procedure Initialize_Element (Index : Count_Type) is
begin
Container.Elements (Index) := New_Item;
end Initialize_Element;
begin
Allocate_Node (Container, Initialize_Element'Access, New_Node);
end Allocate_Node;
procedure Allocate_Node
(Container : in out Tree;
Stream : not null access Root_Stream_Type'Class;
New_Node : out Count_Type)
is
procedure Initialize_Element (Index : Count_Type);
procedure Initialize_Element (Index : Count_Type) is
begin
Element_Type'Read (Stream, Container.Elements (Index));
end Initialize_Element;
begin
Allocate_Node (Container, Initialize_Element'Access, New_Node);
end Allocate_Node;
-------------------
-- Ancestor_Find --
-------------------
function Ancestor_Find
(Position : Cursor;
Item : Element_Type) return Cursor
is
R, N : Count_Type;
begin
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
-- AI-0136 says to raise PE if Position equals the root node. This does
-- not seem correct, as this value is just the limiting condition of the
-- search. For now we omit this check, pending a ruling from the ARG.
-- ???
--
-- if Checks and then Is_Root (Position) then
-- raise Program_Error with "Position cursor designates root";
-- end if;
R := Root_Node (Position.Container.all);
N := Position.Node;
while N /= R loop
if Position.Container.Elements (N) = Item then
return Cursor'(Position.Container, N);
end if;
N := Position.Container.Nodes (N).Parent;
end loop;
return No_Element;
end Ancestor_Find;
------------------
-- Append_Child --
------------------
procedure Append_Child
(Container : in out Tree;
Parent : Cursor;
New_Item : Element_Type;
Count : Count_Type := 1)
is
Nodes : Tree_Node_Array renames Container.Nodes;
First, Last : Count_Type;
begin
TC_Check (Container.TC);
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Container'Unrestricted_Access then
raise Program_Error with "Parent cursor not in container";
end if;
if Count = 0 then
return;
end if;
if Checks and then Container.Count > Container.Capacity - Count then
raise Capacity_Error
with "requested count exceeds available storage";
end if;
if Container.Count = 0 then
Initialize_Root (Container);
end if;
Allocate_Node (Container, New_Item, First);
Nodes (First).Parent := Parent.Node;
Last := First;
for J in Count_Type'(2) .. Count loop
Allocate_Node (Container, New_Item, Nodes (Last).Next);
Nodes (Nodes (Last).Next).Parent := Parent.Node;
Nodes (Nodes (Last).Next).Prev := Last;
Last := Nodes (Last).Next;
end loop;
Insert_Subtree_List
(Container => Container,
First => First,
Last => Last,
Parent => Parent.Node,
Before => No_Node); -- means "insert at end of list"
Container.Count := Container.Count + Count;
end Append_Child;
------------
-- Assign --
------------
procedure Assign (Target : in out Tree; Source : Tree) is
Target_Count : Count_Type;
begin
if Target'Address = Source'Address then
return;
end if;
if Checks and then Target.Capacity < Source.Count then
raise Capacity_Error -- ???
with "Target capacity is less than Source count";
end if;
Target.Clear; -- Checks busy bit
if Source.Count = 0 then
return;
end if;
Initialize_Root (Target);
-- Copy_Children returns the number of nodes that it allocates, but it
-- does this by incrementing the count value passed in, so we must
-- initialize the count before calling Copy_Children.
Target_Count := 0;
Copy_Children
(Source => Source,
Source_Parent => Root_Node (Source),
Target => Target,
Target_Parent => Root_Node (Target),
Count => Target_Count);
pragma Assert (Target_Count = Source.Count);
Target.Count := Source.Count;
end Assign;
-----------------
-- Child_Count --
-----------------
function Child_Count (Parent : Cursor) return Count_Type is
begin
if Parent = No_Element then
return 0;
elsif Parent.Container.Count = 0 then
pragma Assert (Is_Root (Parent));
return 0;
else
return Child_Count (Parent.Container.all, Parent.Node);
end if;
end Child_Count;
function Child_Count
(Container : Tree;
Parent : Count_Type) return Count_Type
is
NN : Tree_Node_Array renames Container.Nodes;
CC : Children_Type renames NN (Parent).Children;
Result : Count_Type;
Node : Count_Type'Base;
begin
Result := 0;
Node := CC.First;
while Node > 0 loop
Result := Result + 1;
Node := NN (Node).Next;
end loop;
return Result;
end Child_Count;
-----------------
-- Child_Depth --
-----------------
function Child_Depth (Parent, Child : Cursor) return Count_Type is
Result : Count_Type;
N : Count_Type'Base;
begin
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Child = No_Element then
raise Constraint_Error with "Child cursor has no element";
end if;
if Checks and then Parent.Container /= Child.Container then
raise Program_Error with "Parent and Child in different containers";
end if;
if Parent.Container.Count = 0 then
pragma Assert (Is_Root (Parent));
pragma Assert (Child = Parent);
return 0;
end if;
Result := 0;
N := Child.Node;
while N /= Parent.Node loop
Result := Result + 1;
N := Parent.Container.Nodes (N).Parent;
if Checks and then N < 0 then
raise Program_Error with "Parent is not ancestor of Child";
end if;
end loop;
return Result;
end Child_Depth;
-----------
-- Clear --
-----------
procedure Clear (Container : in out Tree) is
Container_Count : constant Count_Type := Container.Count;
Count : Count_Type;
begin
TC_Check (Container.TC);
if Container_Count = 0 then
return;
end if;
Container.Count := 0;
-- Deallocate_Children returns the number of nodes that it deallocates,
-- but it does this by incrementing the count value that is passed in,
-- so we must first initialize the count return value before calling it.
Count := 0;
Deallocate_Children
(Container => Container,
Subtree => Root_Node (Container),
Count => Count);
pragma Assert (Count = Container_Count);
end Clear;
------------------------
-- Constant_Reference --
------------------------
function Constant_Reference
(Container : aliased Tree;
Position : Cursor) return Constant_Reference_Type
is
begin
if Checks and then Position.Container = null then
raise Constraint_Error with
"Position cursor has no element";
end if;
if Checks and then Position.Container /= Container'Unrestricted_Access
then
raise Program_Error with
"Position cursor designates wrong container";
end if;
if Checks and then Position.Node = Root_Node (Container) then
raise Program_Error with "Position cursor designates root";
end if;
-- Implement Vet for multiway tree???
-- pragma Assert (Vet (Position),
-- "Position cursor in Constant_Reference is bad");
declare
TC : constant Tamper_Counts_Access :=
Container.TC'Unrestricted_Access;
begin
return R : constant Constant_Reference_Type :=
(Element => Container.Elements (Position.Node)'Unchecked_Access,
Control => (Controlled with TC))
do
Busy (TC.all);
end return;
end;
end Constant_Reference;
--------------
-- Contains --
--------------
function Contains
(Container : Tree;
Item : Element_Type) return Boolean
is
begin
return Find (Container, Item) /= No_Element;
end Contains;
----------
-- Copy --
----------
function Copy
(Source : Tree;
Capacity : Count_Type := 0) return Tree
is
C : constant Count_Type :=
(if Capacity = 0 then Source.Count
else Capacity);
begin
if Checks and then C < Source.Count then
raise Capacity_Error with "Capacity too small";
end if;
return Target : Tree (Capacity => C) do
Initialize_Root (Target);
if Source.Count = 0 then
return;
end if;
Copy_Children
(Source => Source,
Source_Parent => Root_Node (Source),
Target => Target,
Target_Parent => Root_Node (Target),
Count => Target.Count);
pragma Assert (Target.Count = Source.Count);
end return;
end Copy;
-------------------
-- Copy_Children --
-------------------
procedure Copy_Children
(Source : Tree;
Source_Parent : Count_Type;
Target : in out Tree;
Target_Parent : Count_Type;
Count : in out Count_Type)
is
S_Nodes : Tree_Node_Array renames Source.Nodes;
S_Node : Tree_Node_Type renames S_Nodes (Source_Parent);
T_Nodes : Tree_Node_Array renames Target.Nodes;
T_Node : Tree_Node_Type renames T_Nodes (Target_Parent);
pragma Assert (T_Node.Children.First <= 0);
pragma Assert (T_Node.Children.Last <= 0);
T_CC : Children_Type;
C : Count_Type'Base;
begin
-- We special-case the first allocation, in order to establish the
-- representation invariants for type Children_Type.
C := S_Node.Children.First;
if C <= 0 then -- source parent has no children
return;
end if;
Copy_Subtree
(Source => Source,
Source_Subtree => C,
Target => Target,
Target_Parent => Target_Parent,
Target_Subtree => T_CC.First,
Count => Count);
T_CC.Last := T_CC.First;
-- The representation invariants for the Children_Type list have been
-- established, so we can now copy the remaining children of Source.
C := S_Nodes (C).Next;
while C > 0 loop
Copy_Subtree
(Source => Source,
Source_Subtree => C,
Target => Target,
Target_Parent => Target_Parent,
Target_Subtree => T_Nodes (T_CC.Last).Next,
Count => Count);
T_Nodes (T_Nodes (T_CC.Last).Next).Prev := T_CC.Last;
T_CC.Last := T_Nodes (T_CC.Last).Next;
C := S_Nodes (C).Next;
end loop;
-- We add the newly-allocated children to their parent list only after
-- the allocation has succeeded, in order to preserve invariants of the
-- parent.
T_Node.Children := T_CC;
end Copy_Children;
------------------
-- Copy_Subtree --
------------------
procedure Copy_Subtree
(Target : in out Tree;
Parent : Cursor;
Before : Cursor;
Source : Cursor)
is
Target_Subtree : Count_Type;
Target_Count : Count_Type;
begin
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Target'Unrestricted_Access then
raise Program_Error with "Parent cursor not in container";
end if;
if Before /= No_Element then
if Checks and then Before.Container /= Target'Unrestricted_Access then
raise Program_Error with "Before cursor not in container";
end if;
if Checks and then
Before.Container.Nodes (Before.Node).Parent /= Parent.Node
then
raise Constraint_Error with "Before cursor not child of Parent";
end if;
end if;
if Source = No_Element then
return;
end if;
if Checks and then Is_Root (Source) then
raise Constraint_Error with "Source cursor designates root";
end if;
if Target.Count = 0 then
Initialize_Root (Target);
end if;
-- Copy_Subtree returns a count of the number of nodes that it
-- allocates, but it works by incrementing the value that is passed
-- in. We must therefore initialize the count value before calling
-- Copy_Subtree.
Target_Count := 0;
Copy_Subtree
(Source => Source.Container.all,
Source_Subtree => Source.Node,
Target => Target,
Target_Parent => Parent.Node,
Target_Subtree => Target_Subtree,
Count => Target_Count);
Insert_Subtree_Node
(Container => Target,
Subtree => Target_Subtree,
Parent => Parent.Node,
Before => Before.Node);
Target.Count := Target.Count + Target_Count;
end Copy_Subtree;
procedure Copy_Subtree
(Source : Tree;
Source_Subtree : Count_Type;
Target : in out Tree;
Target_Parent : Count_Type;
Target_Subtree : out Count_Type;
Count : in out Count_Type)
is
T_Nodes : Tree_Node_Array renames Target.Nodes;
begin
-- First we allocate the root of the target subtree.
Allocate_Node
(Container => Target,
New_Item => Source.Elements (Source_Subtree),
New_Node => Target_Subtree);
T_Nodes (Target_Subtree).Parent := Target_Parent;
Count := Count + 1;
-- We now have a new subtree (for the Target tree), containing only a
-- copy of the corresponding element in the Source subtree. Next we copy
-- the children of the Source subtree as children of the new Target
-- subtree.
Copy_Children
(Source => Source,
Source_Parent => Source_Subtree,
Target => Target,
Target_Parent => Target_Subtree,
Count => Count);
end Copy_Subtree;
-------------------------
-- Deallocate_Children --
-------------------------
procedure Deallocate_Children
(Container : in out Tree;
Subtree : Count_Type;
Count : in out Count_Type)
is
Nodes : Tree_Node_Array renames Container.Nodes;
Node : Tree_Node_Type renames Nodes (Subtree); -- parent
CC : Children_Type renames Node.Children;
C : Count_Type'Base;
begin
while CC.First > 0 loop
C := CC.First;
CC.First := Nodes (C).Next;
Deallocate_Subtree (Container, C, Count);
end loop;
CC.Last := 0;
end Deallocate_Children;
---------------------
-- Deallocate_Node --
---------------------
procedure Deallocate_Node
(Container : in out Tree;
X : Count_Type)
is
NN : Tree_Node_Array renames Container.Nodes;
pragma Assert (X > 0);
pragma Assert (X <= NN'Last);
N : Tree_Node_Type renames NN (X);
pragma Assert (N.Parent /= X); -- node is active
begin
-- The tree container actually contains two lists: one for the "active"
-- nodes that contain elements that have been inserted onto the tree,
-- and another for the "inactive" nodes of the free store, from which
-- nodes are allocated when a new child is inserted in the tree.
-- We desire that merely declaring a tree object should have only
-- minimal cost; specially, we want to avoid having to initialize the
-- free store (to fill in the links), especially if the capacity of the
-- tree object is large.
-- The head of the free list is indicated by Container.Free. If its
-- value is non-negative, then the free store has been initialized in
-- the "normal" way: Container.Free points to the head of the list of
-- free (inactive) nodes, and the value 0 means the free list is
-- empty. Each node on the free list has been initialized to point to
-- the next free node (via its Next component), and the value 0 means
-- that this is the last node of the free list.
-- If Container.Free is negative, then the links on the free store have
-- not been initialized. In this case the link values are implied: the
-- free store comprises the components of the node array started with
-- the absolute value of Container.Free, and continuing until the end of
-- the array (Nodes'Last).
-- We prefer to lazy-init the free store (in fact, we would prefer to
-- not initialize it at all, because such initialization is an O(n)
-- operation). The time when we need to actually initialize the nodes in
-- the free store is when the node that becomes inactive is not at the
-- end of the active list. The free store would then be discontigous and
-- so its nodes would need to be linked in the traditional way.
-- It might be possible to perform an optimization here. Suppose that
-- the free store can be represented as having two parts: one comprising
-- the non-contiguous inactive nodes linked together in the normal way,
-- and the other comprising the contiguous inactive nodes (that are not
-- linked together, at the end of the nodes array). This would allow us
-- to never have to initialize the free store, except in a lazy way as
-- nodes become inactive. ???
-- When an element is deleted from the list container, its node becomes
-- inactive, and so we set its Parent and Prev components to an
-- impossible value (the index of the node itself), to indicate that it
-- is now inactive. This provides a useful way to detect a dangling
-- cursor reference.
N.Parent := X; -- Node is deallocated (not on active list)
N.Prev := X;
if Container.Free >= 0 then
-- The free store has previously been initialized. All we need to do
-- here is link the newly-free'd node onto the free list.
N.Next := Container.Free;
Container.Free := X;
elsif X + 1 = abs Container.Free then
-- The free store has not been initialized, and the node becoming
-- inactive immediately precedes the start of the free store. All
-- we need to do is move the start of the free store back by one.
N.Next := X; -- Not strictly necessary, but marginally safer
Container.Free := Container.Free + 1;
else
-- The free store has not been initialized, and the node becoming
-- inactive does not immediately precede the free store. Here we
-- first initialize the free store (meaning the links are given
-- values in the traditional way), and then link the newly-free'd
-- node onto the head of the free store.
-- See the comments above for an optimization opportunity. If the
-- next link for a node on the free store is negative, then this
-- means the remaining nodes on the free store are physically
-- contiguous, starting at the absolute value of that index value.
-- ???
Container.Free := abs Container.Free;
if Container.Free > Container.Capacity then
Container.Free := 0;
else
for J in Container.Free .. Container.Capacity - 1 loop
NN (J).Next := J + 1;
end loop;
NN (Container.Capacity).Next := 0;
end if;
NN (X).Next := Container.Free;
Container.Free := X;
end if;
end Deallocate_Node;
------------------------
-- Deallocate_Subtree --
------------------------
procedure Deallocate_Subtree
(Container : in out Tree;
Subtree : Count_Type;
Count : in out Count_Type)
is
begin
Deallocate_Children (Container, Subtree, Count);
Deallocate_Node (Container, Subtree);
Count := Count + 1;
end Deallocate_Subtree;
---------------------
-- Delete_Children --
---------------------
procedure Delete_Children
(Container : in out Tree;
Parent : Cursor)
is
Count : Count_Type;
begin
TC_Check (Container.TC);
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Container'Unrestricted_Access then
raise Program_Error with "Parent cursor not in container";
end if;
if Container.Count = 0 then
pragma Assert (Is_Root (Parent));
return;
end if;
-- Deallocate_Children returns a count of the number of nodes that it
-- deallocates, but it works by incrementing the value that is passed
-- in. We must therefore initialize the count value before calling
-- Deallocate_Children.
Count := 0;
Deallocate_Children (Container, Parent.Node, Count);
pragma Assert (Count <= Container.Count);
Container.Count := Container.Count - Count;
end Delete_Children;
-----------------
-- Delete_Leaf --
-----------------
procedure Delete_Leaf
(Container : in out Tree;
Position : in out Cursor)
is
X : Count_Type;
begin
TC_Check (Container.TC);
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Position.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Position cursor not in container";
end if;
if Checks and then Is_Root (Position) then
raise Program_Error with "Position cursor designates root";
end if;
if Checks and then not Is_Leaf (Position) then
raise Constraint_Error with "Position cursor does not designate leaf";
end if;
X := Position.Node;
Position := No_Element;
Remove_Subtree (Container, X);
Container.Count := Container.Count - 1;
Deallocate_Node (Container, X);
end Delete_Leaf;
--------------------
-- Delete_Subtree --
--------------------
procedure Delete_Subtree
(Container : in out Tree;
Position : in out Cursor)
is
X : Count_Type;
Count : Count_Type;
begin
TC_Check (Container.TC);
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Position.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Position cursor not in container";
end if;
if Checks and then Is_Root (Position) then
raise Program_Error with "Position cursor designates root";
end if;
X := Position.Node;
Position := No_Element;
Remove_Subtree (Container, X);
-- Deallocate_Subtree returns a count of the number of nodes that it
-- deallocates, but it works by incrementing the value that is passed
-- in. We must therefore initialize the count value before calling
-- Deallocate_Subtree.
Count := 0;
Deallocate_Subtree (Container, X, Count);
pragma Assert (Count <= Container.Count);
Container.Count := Container.Count - Count;
end Delete_Subtree;
-----------
-- Depth --
-----------
function Depth (Position : Cursor) return Count_Type is
Result : Count_Type;
N : Count_Type'Base;
begin
if Position = No_Element then
return 0;
end if;
if Is_Root (Position) then
return 1;
end if;
Result := 0;
N := Position.Node;
while N >= 0 loop
N := Position.Container.Nodes (N).Parent;
Result := Result + 1;
end loop;
return Result;
end Depth;
-------------
-- Element --
-------------
function Element (Position : Cursor) return Element_Type is
begin
if Checks and then Position.Container = null then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Position.Node = Root_Node (Position.Container.all)
then
raise Program_Error with "Position cursor designates root";
end if;
return Position.Container.Elements (Position.Node);
end Element;
--------------------
-- Equal_Children --
--------------------
function Equal_Children
(Left_Tree : Tree;
Left_Subtree : Count_Type;
Right_Tree : Tree;
Right_Subtree : Count_Type) return Boolean
is
L_NN : Tree_Node_Array renames Left_Tree.Nodes;
R_NN : Tree_Node_Array renames Right_Tree.Nodes;
Left_Children : Children_Type renames L_NN (Left_Subtree).Children;
Right_Children : Children_Type renames R_NN (Right_Subtree).Children;
L, R : Count_Type'Base;
begin
if Child_Count (Left_Tree, Left_Subtree)
/= Child_Count (Right_Tree, Right_Subtree)
then
return False;
end if;
L := Left_Children.First;
R := Right_Children.First;
while L > 0 loop
if not Equal_Subtree (Left_Tree, L, Right_Tree, R) then
return False;
end if;
L := L_NN (L).Next;
R := R_NN (R).Next;
end loop;
return True;
end Equal_Children;
-------------------
-- Equal_Subtree --
-------------------
function Equal_Subtree
(Left_Position : Cursor;
Right_Position : Cursor) return Boolean
is
begin
if Checks and then Left_Position = No_Element then
raise Constraint_Error with "Left cursor has no element";
end if;
if Checks and then Right_Position = No_Element then
raise Constraint_Error with "Right cursor has no element";
end if;
if Left_Position = Right_Position then
return True;
end if;
if Is_Root (Left_Position) then
if not Is_Root (Right_Position) then
return False;
end if;
if Left_Position.Container.Count = 0 then
return Right_Position.Container.Count = 0;
end if;
if Right_Position.Container.Count = 0 then
return False;
end if;
return Equal_Children
(Left_Tree => Left_Position.Container.all,
Left_Subtree => Left_Position.Node,
Right_Tree => Right_Position.Container.all,
Right_Subtree => Right_Position.Node);
end if;
if Is_Root (Right_Position) then
return False;
end if;
return Equal_Subtree
(Left_Tree => Left_Position.Container.all,
Left_Subtree => Left_Position.Node,
Right_Tree => Right_Position.Container.all,
Right_Subtree => Right_Position.Node);
end Equal_Subtree;
function Equal_Subtree
(Left_Tree : Tree;
Left_Subtree : Count_Type;
Right_Tree : Tree;
Right_Subtree : Count_Type) return Boolean
is
begin
if Left_Tree.Elements (Left_Subtree) /=
Right_Tree.Elements (Right_Subtree)
then
return False;
end if;
return Equal_Children
(Left_Tree => Left_Tree,
Left_Subtree => Left_Subtree,
Right_Tree => Right_Tree,
Right_Subtree => Right_Subtree);
end Equal_Subtree;
--------------
-- Finalize --
--------------
procedure Finalize (Object : in out Root_Iterator) is
begin
Unbusy (Object.Container.TC);
end Finalize;
----------
-- Find --
----------
function Find
(Container : Tree;
Item : Element_Type) return Cursor
is
Node : Count_Type;
begin
if Container.Count = 0 then
return No_Element;
end if;
Node := Find_In_Children (Container, Root_Node (Container), Item);
if Node = 0 then
return No_Element;
end if;
return Cursor'(Container'Unrestricted_Access, Node);
end Find;
-----------
-- First --
-----------
overriding function First (Object : Subtree_Iterator) return Cursor is
begin
if Object.Subtree = Root_Node (Object.Container.all) then
return First_Child (Root (Object.Container.all));
else
return Cursor'(Object.Container, Object.Subtree);
end if;
end First;
overriding function First (Object : Child_Iterator) return Cursor is
begin
return First_Child (Cursor'(Object.Container, Object.Subtree));
end First;
-----------------
-- First_Child --
-----------------
function First_Child (Parent : Cursor) return Cursor is
Node : Count_Type'Base;
begin
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Parent.Container.Count = 0 then
pragma Assert (Is_Root (Parent));
return No_Element;
end if;
Node := Parent.Container.Nodes (Parent.Node).Children.First;
if Node <= 0 then
return No_Element;
end if;
return Cursor'(Parent.Container, Node);
end First_Child;
-------------------------
-- First_Child_Element --
-------------------------
function First_Child_Element (Parent : Cursor) return Element_Type is
begin
return Element (First_Child (Parent));
end First_Child_Element;
----------------------
-- Find_In_Children --
----------------------
function Find_In_Children
(Container : Tree;
Subtree : Count_Type;
Item : Element_Type) return Count_Type
is
N : Count_Type'Base;
Result : Count_Type;
begin
N := Container.Nodes (Subtree).Children.First;
while N > 0 loop
Result := Find_In_Subtree (Container, N, Item);
if Result > 0 then
return Result;
end if;
N := Container.Nodes (N).Next;
end loop;
return 0;
end Find_In_Children;
---------------------
-- Find_In_Subtree --
---------------------
function Find_In_Subtree
(Position : Cursor;
Item : Element_Type) return Cursor
is
Result : Count_Type;
begin
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
-- Commented-out pending ruling by ARG. ???
-- if Checks and then
-- Position.Container /= Container'Unrestricted_Access
-- then
-- raise Program_Error with "Position cursor not in container";
-- end if;
if Position.Container.Count = 0 then
pragma Assert (Is_Root (Position));
return No_Element;
end if;
if Is_Root (Position) then
Result := Find_In_Children
(Container => Position.Container.all,
Subtree => Position.Node,
Item => Item);
else
Result := Find_In_Subtree
(Container => Position.Container.all,
Subtree => Position.Node,
Item => Item);
end if;
if Result = 0 then
return No_Element;
end if;
return Cursor'(Position.Container, Result);
end Find_In_Subtree;
function Find_In_Subtree
(Container : Tree;
Subtree : Count_Type;
Item : Element_Type) return Count_Type
is
begin
if Container.Elements (Subtree) = Item then
return Subtree;
end if;
return Find_In_Children (Container, Subtree, Item);
end Find_In_Subtree;
------------------------
-- Get_Element_Access --
------------------------
function Get_Element_Access
(Position : Cursor) return not null Element_Access is
begin
return Position.Container.Elements (Position.Node)'Access;
end Get_Element_Access;
-----------------
-- Has_Element --
-----------------
function Has_Element (Position : Cursor) return Boolean is
begin
if Position = No_Element then
return False;
end if;
return Position.Node /= Root_Node (Position.Container.all);
end Has_Element;
---------------------
-- Initialize_Node --
---------------------
procedure Initialize_Node
(Container : in out Tree;
Index : Count_Type)
is
begin
Container.Nodes (Index) :=
(Parent => No_Node,
Prev => 0,
Next => 0,
Children => (others => 0));
end Initialize_Node;
---------------------
-- Initialize_Root --
---------------------
procedure Initialize_Root (Container : in out Tree) is
begin
Initialize_Node (Container, Root_Node (Container));
end Initialize_Root;
------------------
-- Insert_Child --
------------------
procedure Insert_Child
(Container : in out Tree;
Parent : Cursor;
Before : Cursor;
New_Item : Element_Type;
Count : Count_Type := 1)
is
Position : Cursor;
begin
Insert_Child (Container, Parent, Before, New_Item, Position, Count);
end Insert_Child;
procedure Insert_Child
(Container : in out Tree;
Parent : Cursor;
Before : Cursor;
New_Item : Element_Type;
Position : out Cursor;
Count : Count_Type := 1)
is
Nodes : Tree_Node_Array renames Container.Nodes;
First : Count_Type;
Last : Count_Type;
begin
TC_Check (Container.TC);
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Container'Unrestricted_Access then
raise Program_Error with "Parent cursor not in container";
end if;
if Before /= No_Element then
if Checks and then Before.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Before cursor not in container";
end if;
if Checks and then
Before.Container.Nodes (Before.Node).Parent /= Parent.Node
then
raise Constraint_Error with "Parent cursor not parent of Before";
end if;
end if;
if Count = 0 then
Position := No_Element; -- Need ruling from ARG ???
return;
end if;
if Checks and then Container.Count > Container.Capacity - Count then
raise Capacity_Error
with "requested count exceeds available storage";
end if;
if Container.Count = 0 then
Initialize_Root (Container);
end if;
Allocate_Node (Container, New_Item, First);
Nodes (First).Parent := Parent.Node;
Last := First;
for J in Count_Type'(2) .. Count loop
Allocate_Node (Container, New_Item, Nodes (Last).Next);
Nodes (Nodes (Last).Next).Parent := Parent.Node;
Nodes (Nodes (Last).Next).Prev := Last;
Last := Nodes (Last).Next;
end loop;
Insert_Subtree_List
(Container => Container,
First => First,
Last => Last,
Parent => Parent.Node,
Before => Before.Node);
Container.Count := Container.Count + Count;
Position := Cursor'(Parent.Container, First);
end Insert_Child;
procedure Insert_Child
(Container : in out Tree;
Parent : Cursor;
Before : Cursor;
Position : out Cursor;
Count : Count_Type := 1)
is
Nodes : Tree_Node_Array renames Container.Nodes;
First : Count_Type;
Last : Count_Type;
pragma Warnings (Off);
Default_Initialized_Item : Element_Type;
pragma Unmodified (Default_Initialized_Item);
-- OK to reference, see below
begin
TC_Check (Container.TC);
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Container'Unrestricted_Access then
raise Program_Error with "Parent cursor not in container";
end if;
if Before /= No_Element then
if Checks and then Before.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Before cursor not in container";
end if;
if Checks and then
Before.Container.Nodes (Before.Node).Parent /= Parent.Node
then
raise Constraint_Error with "Parent cursor not parent of Before";
end if;
end if;
if Count = 0 then
Position := No_Element; -- Need ruling from ARG ???
return;
end if;
if Checks and then Container.Count > Container.Capacity - Count then
raise Capacity_Error
with "requested count exceeds available storage";
end if;
if Container.Count = 0 then
Initialize_Root (Container);
end if;
-- There is no explicit element provided, but in an instance the element
-- type may be a scalar with a Default_Value aspect, or a composite
-- type with such a scalar component, or components with default
-- initialization, so insert the specified number of possibly
-- initialized elements at the given position.
Allocate_Node (Container, Default_Initialized_Item, First);
Nodes (First).Parent := Parent.Node;
Last := First;
for J in Count_Type'(2) .. Count loop
Allocate_Node
(Container, Default_Initialized_Item, Nodes (Last).Next);
Nodes (Nodes (Last).Next).Parent := Parent.Node;
Nodes (Nodes (Last).Next).Prev := Last;
Last := Nodes (Last).Next;
end loop;
Insert_Subtree_List
(Container => Container,
First => First,
Last => Last,
Parent => Parent.Node,
Before => Before.Node);
Container.Count := Container.Count + Count;
Position := Cursor'(Parent.Container, First);
pragma Warnings (On);
end Insert_Child;
-------------------------
-- Insert_Subtree_List --
-------------------------
procedure Insert_Subtree_List
(Container : in out Tree;
First : Count_Type'Base;
Last : Count_Type'Base;
Parent : Count_Type;
Before : Count_Type'Base)
is
NN : Tree_Node_Array renames Container.Nodes;
N : Tree_Node_Type renames NN (Parent);
CC : Children_Type renames N.Children;
begin
-- This is a simple utility operation to insert a list of nodes
-- (First..Last) as children of Parent. The Before node specifies where
-- the new children should be inserted relative to existing children.
if First <= 0 then
pragma Assert (Last <= 0);
return;
end if;
pragma Assert (Last > 0);
pragma Assert (Before <= 0 or else NN (Before).Parent = Parent);
if CC.First <= 0 then -- no existing children
CC.First := First;
NN (CC.First).Prev := 0;
CC.Last := Last;
NN (CC.Last).Next := 0;
elsif Before <= 0 then -- means "insert after existing nodes"
NN (CC.Last).Next := First;
NN (First).Prev := CC.Last;
CC.Last := Last;
NN (CC.Last).Next := 0;
elsif Before = CC.First then
NN (Last).Next := CC.First;
NN (CC.First).Prev := Last;
CC.First := First;
NN (CC.First).Prev := 0;
else
NN (NN (Before).Prev).Next := First;
NN (First).Prev := NN (Before).Prev;
NN (Last).Next := Before;
NN (Before).Prev := Last;
end if;
end Insert_Subtree_List;
-------------------------
-- Insert_Subtree_Node --
-------------------------
procedure Insert_Subtree_Node
(Container : in out Tree;
Subtree : Count_Type'Base;
Parent : Count_Type;
Before : Count_Type'Base)
is
begin
-- This is a simple wrapper operation to insert a single child into the
-- Parent's children list.
Insert_Subtree_List
(Container => Container,
First => Subtree,
Last => Subtree,
Parent => Parent,
Before => Before);
end Insert_Subtree_Node;
--------------
-- Is_Empty --
--------------
function Is_Empty (Container : Tree) return Boolean is
begin
return Container.Count = 0;
end Is_Empty;
-------------
-- Is_Leaf --
-------------
function Is_Leaf (Position : Cursor) return Boolean is
begin
if Position = No_Element then
return False;
end if;
if Position.Container.Count = 0 then
pragma Assert (Is_Root (Position));
return True;
end if;
return Position.Container.Nodes (Position.Node).Children.First <= 0;
end Is_Leaf;
------------------
-- Is_Reachable --
------------------
function Is_Reachable
(Container : Tree;
From, To : Count_Type) return Boolean
is
Idx : Count_Type'Base := From;
begin
while Idx >= 0 loop
if Idx = To then
return True;
end if;
Idx := Container.Nodes (Idx).Parent;
end loop;
return False;
end Is_Reachable;
-------------
-- Is_Root --
-------------
function Is_Root (Position : Cursor) return Boolean is
begin
return
(if Position.Container = null then False
else Position.Node = Root_Node (Position.Container.all));
end Is_Root;
-------------
-- Iterate --
-------------
procedure Iterate
(Container : Tree;
Process : not null access procedure (Position : Cursor))
is
Busy : With_Busy (Container.TC'Unrestricted_Access);
begin
if Container.Count = 0 then
return;
end if;
Iterate_Children
(Container => Container,
Subtree => Root_Node (Container),
Process => Process);
end Iterate;
function Iterate (Container : Tree)
return Tree_Iterator_Interfaces.Forward_Iterator'Class
is
begin
return Iterate_Subtree (Root (Container));
end Iterate;
----------------------
-- Iterate_Children --
----------------------
procedure Iterate_Children
(Parent : Cursor;
Process : not null access procedure (Position : Cursor))
is
begin
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Parent.Container.Count = 0 then
pragma Assert (Is_Root (Parent));
return;
end if;
declare
C : Count_Type;
NN : Tree_Node_Array renames Parent.Container.Nodes;
Busy : With_Busy (Parent.Container.TC'Unrestricted_Access);
begin
C := NN (Parent.Node).Children.First;
while C > 0 loop
Process (Cursor'(Parent.Container, Node => C));
C := NN (C).Next;
end loop;
end;
end Iterate_Children;
procedure Iterate_Children
(Container : Tree;
Subtree : Count_Type;
Process : not null access procedure (Position : Cursor))
is
NN : Tree_Node_Array renames Container.Nodes;
N : Tree_Node_Type renames NN (Subtree);
C : Count_Type;
begin
-- This is a helper function to recursively iterate over all the nodes
-- in a subtree, in depth-first fashion. This particular helper just
-- visits the children of this subtree, not the root of the subtree
-- itself. This is useful when starting from the ultimate root of the
-- entire tree (see Iterate), as that root does not have an element.
C := N.Children.First;
while C > 0 loop
Iterate_Subtree (Container, C, Process);
C := NN (C).Next;
end loop;
end Iterate_Children;
function Iterate_Children
(Container : Tree;
Parent : Cursor)
return Tree_Iterator_Interfaces.Reversible_Iterator'Class
is
C : constant Tree_Access := Container'Unrestricted_Access;
begin
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= C then
raise Program_Error with "Parent cursor not in container";
end if;
return It : constant Child_Iterator :=
Child_Iterator'(Limited_Controlled with
Container => C,
Subtree => Parent.Node)
do
Busy (C.TC);
end return;
end Iterate_Children;
---------------------
-- Iterate_Subtree --
---------------------
function Iterate_Subtree
(Position : Cursor)
return Tree_Iterator_Interfaces.Forward_Iterator'Class
is
C : constant Tree_Access := Position.Container;
begin
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
-- Implement Vet for multiway trees???
-- pragma Assert (Vet (Position), "bad subtree cursor");
return It : constant Subtree_Iterator :=
(Limited_Controlled with
Container => C,
Subtree => Position.Node)
do
Busy (C.TC);
end return;
end Iterate_Subtree;
procedure Iterate_Subtree
(Position : Cursor;
Process : not null access procedure (Position : Cursor))
is
begin
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Position.Container.Count = 0 then
pragma Assert (Is_Root (Position));
return;
end if;
declare
T : Tree renames Position.Container.all;
Busy : With_Busy (T.TC'Unrestricted_Access);
begin
if Is_Root (Position) then
Iterate_Children (T, Position.Node, Process);
else
Iterate_Subtree (T, Position.Node, Process);
end if;
end;
end Iterate_Subtree;
procedure Iterate_Subtree
(Container : Tree;
Subtree : Count_Type;
Process : not null access procedure (Position : Cursor))
is
begin
-- This is a helper function to recursively iterate over all the nodes
-- in a subtree, in depth-first fashion. It first visits the root of the
-- subtree, then visits its children.
Process (Cursor'(Container'Unrestricted_Access, Subtree));
Iterate_Children (Container, Subtree, Process);
end Iterate_Subtree;
----------
-- Last --
----------
overriding function Last (Object : Child_Iterator) return Cursor is
begin
return Last_Child (Cursor'(Object.Container, Object.Subtree));
end Last;
----------------
-- Last_Child --
----------------
function Last_Child (Parent : Cursor) return Cursor is
Node : Count_Type'Base;
begin
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Parent.Container.Count = 0 then
pragma Assert (Is_Root (Parent));
return No_Element;
end if;
Node := Parent.Container.Nodes (Parent.Node).Children.Last;
if Node <= 0 then
return No_Element;
end if;
return Cursor'(Parent.Container, Node);
end Last_Child;
------------------------
-- Last_Child_Element --
------------------------
function Last_Child_Element (Parent : Cursor) return Element_Type is
begin
return Element (Last_Child (Parent));
end Last_Child_Element;
----------
-- Move --
----------
procedure Move (Target : in out Tree; Source : in out Tree) is
begin
if Target'Address = Source'Address then
return;
end if;
TC_Check (Source.TC);
Target.Assign (Source);
Source.Clear;
end Move;
----------
-- Next --
----------
overriding function Next
(Object : Subtree_Iterator;
Position : Cursor) return Cursor
is
begin
if Position.Container = null then
return No_Element;
end if;
if Checks and then Position.Container /= Object.Container then
raise Program_Error with
"Position cursor of Next designates wrong tree";
end if;
pragma Assert (Object.Container.Count > 0);
pragma Assert (Position.Node /= Root_Node (Object.Container.all));
declare
Nodes : Tree_Node_Array renames Object.Container.Nodes;
Node : Count_Type;
begin
Node := Position.Node;
if Nodes (Node).Children.First > 0 then
return Cursor'(Object.Container, Nodes (Node).Children.First);
end if;
while Node /= Object.Subtree loop
if Nodes (Node).Next > 0 then
return Cursor'(Object.Container, Nodes (Node).Next);
end if;
Node := Nodes (Node).Parent;
end loop;
return No_Element;
end;
end Next;
overriding function Next
(Object : Child_Iterator;
Position : Cursor) return Cursor
is
begin
if Position.Container = null then
return No_Element;
end if;
if Checks and then Position.Container /= Object.Container then
raise Program_Error with
"Position cursor of Next designates wrong tree";
end if;
pragma Assert (Object.Container.Count > 0);
pragma Assert (Position.Node /= Root_Node (Object.Container.all));
return Next_Sibling (Position);
end Next;
------------------
-- Next_Sibling --
------------------
function Next_Sibling (Position : Cursor) return Cursor is
begin
if Position = No_Element then
return No_Element;
end if;
if Position.Container.Count = 0 then
pragma Assert (Is_Root (Position));
return No_Element;
end if;
declare
T : Tree renames Position.Container.all;
NN : Tree_Node_Array renames T.Nodes;
N : Tree_Node_Type renames NN (Position.Node);
begin
if N.Next <= 0 then
return No_Element;
end if;
return Cursor'(Position.Container, N.Next);
end;
end Next_Sibling;
procedure Next_Sibling (Position : in out Cursor) is
begin
Position := Next_Sibling (Position);
end Next_Sibling;
----------------
-- Node_Count --
----------------
function Node_Count (Container : Tree) return Count_Type is
begin
-- Container.Count is the number of nodes we have actually allocated. We
-- cache the value specifically so this Node_Count operation can execute
-- in O(1) time, which makes it behave similarly to how the Length
-- selector function behaves for other containers.
--
-- The cached node count value only describes the nodes we have
-- allocated; the root node itself is not included in that count. The
-- Node_Count operation returns a value that includes the root node
-- (because the RM says so), so we must add 1 to our cached value.
return 1 + Container.Count;
end Node_Count;
------------
-- Parent --
------------
function Parent (Position : Cursor) return Cursor is
begin
if Position = No_Element then
return No_Element;
end if;
if Position.Container.Count = 0 then
pragma Assert (Is_Root (Position));
return No_Element;
end if;
declare
T : Tree renames Position.Container.all;
NN : Tree_Node_Array renames T.Nodes;
N : Tree_Node_Type renames NN (Position.Node);
begin
if N.Parent < 0 then
pragma Assert (Position.Node = Root_Node (T));
return No_Element;
end if;
return Cursor'(Position.Container, N.Parent);
end;
end Parent;
-------------------
-- Prepend_Child --
-------------------
procedure Prepend_Child
(Container : in out Tree;
Parent : Cursor;
New_Item : Element_Type;
Count : Count_Type := 1)
is
Nodes : Tree_Node_Array renames Container.Nodes;
First, Last : Count_Type;
begin
TC_Check (Container.TC);
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Container'Unrestricted_Access then
raise Program_Error with "Parent cursor not in container";
end if;
if Count = 0 then
return;
end if;
if Checks and then Container.Count > Container.Capacity - Count then
raise Capacity_Error
with "requested count exceeds available storage";
end if;
if Container.Count = 0 then
Initialize_Root (Container);
end if;
Allocate_Node (Container, New_Item, First);
Nodes (First).Parent := Parent.Node;
Last := First;
for J in Count_Type'(2) .. Count loop
Allocate_Node (Container, New_Item, Nodes (Last).Next);
Nodes (Nodes (Last).Next).Parent := Parent.Node;
Nodes (Nodes (Last).Next).Prev := Last;
Last := Nodes (Last).Next;
end loop;
Insert_Subtree_List
(Container => Container,
First => First,
Last => Last,
Parent => Parent.Node,
Before => Nodes (Parent.Node).Children.First);
Container.Count := Container.Count + Count;
end Prepend_Child;
--------------
-- Previous --
--------------
overriding function Previous
(Object : Child_Iterator;
Position : Cursor) return Cursor
is
begin
if Position.Container = null then
return No_Element;
end if;
if Checks and then Position.Container /= Object.Container then
raise Program_Error with
"Position cursor of Previous designates wrong tree";
end if;
return Previous_Sibling (Position);
end Previous;
----------------------
-- Previous_Sibling --
----------------------
function Previous_Sibling (Position : Cursor) return Cursor is
begin
if Position = No_Element then
return No_Element;
end if;
if Position.Container.Count = 0 then
pragma Assert (Is_Root (Position));
return No_Element;
end if;
declare
T : Tree renames Position.Container.all;
NN : Tree_Node_Array renames T.Nodes;
N : Tree_Node_Type renames NN (Position.Node);
begin
if N.Prev <= 0 then
return No_Element;
end if;
return Cursor'(Position.Container, N.Prev);
end;
end Previous_Sibling;
procedure Previous_Sibling (Position : in out Cursor) is
begin
Position := Previous_Sibling (Position);
end Previous_Sibling;
----------------------
-- Pseudo_Reference --
----------------------
function Pseudo_Reference
(Container : aliased Tree'Class) return Reference_Control_Type
is
TC : constant Tamper_Counts_Access := Container.TC'Unrestricted_Access;
begin
return R : constant Reference_Control_Type := (Controlled with TC) do
Busy (TC.all);
end return;
end Pseudo_Reference;
-------------------
-- Query_Element --
-------------------
procedure Query_Element
(Position : Cursor;
Process : not null access procedure (Element : Element_Type))
is
begin
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Is_Root (Position) then
raise Program_Error with "Position cursor designates root";
end if;
declare
T : Tree renames Position.Container.all'Unrestricted_Access.all;
Lock : With_Lock (T.TC'Unrestricted_Access);
begin
Process (Element => T.Elements (Position.Node));
end;
end Query_Element;
---------------
-- Put_Image --
---------------
procedure Put_Image
(S : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; V : Tree)
is
use System.Put_Images;
procedure Rec (Position : Cursor);
-- Recursive routine operating on cursors
procedure Rec (Position : Cursor) is
First_Time : Boolean := True;
begin
Array_Before (S);
for X in Iterate_Children (V, Position) loop
if First_Time then
First_Time := False;
else
Array_Between (S);
end if;
Element_Type'Put_Image (S, Element (X));
if Child_Count (X) > 0 then
Simple_Array_Between (S);
Rec (X);
end if;
end loop;
Array_After (S);
end Rec;
begin
if First_Child (Root (V)) = No_Element then
Array_Before (S);
Array_After (S);
else
Rec (First_Child (Root (V)));
end if;
end Put_Image;
----------
-- Read --
----------
procedure Read
(Stream : not null access Root_Stream_Type'Class;
Container : out Tree)
is
procedure Read_Children (Subtree : Count_Type);
function Read_Subtree
(Parent : Count_Type) return Count_Type;
NN : Tree_Node_Array renames Container.Nodes;
Total_Count : Count_Type'Base;
-- Value read from the stream that says how many elements follow
Read_Count : Count_Type'Base;
-- Actual number of elements read from the stream
-------------------
-- Read_Children --
-------------------
procedure Read_Children (Subtree : Count_Type) is
Count : Count_Type'Base;
-- number of child subtrees
CC : Children_Type;
begin
Count_Type'Read (Stream, Count);
if Checks and then Count < 0 then
raise Program_Error with "attempt to read from corrupt stream";
end if;
if Count = 0 then
return;
end if;
CC.First := Read_Subtree (Parent => Subtree);
CC.Last := CC.First;
for J in Count_Type'(2) .. Count loop
NN (CC.Last).Next := Read_Subtree (Parent => Subtree);
NN (NN (CC.Last).Next).Prev := CC.Last;
CC.Last := NN (CC.Last).Next;
end loop;
-- Now that the allocation and reads have completed successfully, it
-- is safe to link the children to their parent.
NN (Subtree).Children := CC;
end Read_Children;
------------------
-- Read_Subtree --
------------------
function Read_Subtree
(Parent : Count_Type) return Count_Type
is
Subtree : Count_Type;
begin
Allocate_Node (Container, Stream, Subtree);
Container.Nodes (Subtree).Parent := Parent;
Read_Count := Read_Count + 1;
Read_Children (Subtree);
return Subtree;
end Read_Subtree;
-- Start of processing for Read
begin
Container.Clear; -- checks busy bit
Count_Type'Read (Stream, Total_Count);
if Checks and then Total_Count < 0 then
raise Program_Error with "attempt to read from corrupt stream";
end if;
if Total_Count = 0 then
return;
end if;
if Checks and then Total_Count > Container.Capacity then
raise Capacity_Error -- ???
with "node count in stream exceeds container capacity";
end if;
Initialize_Root (Container);
Read_Count := 0;
Read_Children (Root_Node (Container));
if Checks and then Read_Count /= Total_Count then
raise Program_Error with "attempt to read from corrupt stream";
end if;
Container.Count := Total_Count;
end Read;
procedure Read
(Stream : not null access Root_Stream_Type'Class;
Position : out Cursor)
is
begin
raise Program_Error with "attempt to read tree cursor from stream";
end Read;
procedure Read
(Stream : not null access Root_Stream_Type'Class;
Item : out Reference_Type)
is
begin
raise Program_Error with "attempt to stream reference";
end Read;
procedure Read
(Stream : not null access Root_Stream_Type'Class;
Item : out Constant_Reference_Type)
is
begin
raise Program_Error with "attempt to stream reference";
end Read;
---------------
-- Reference --
---------------
function Reference
(Container : aliased in out Tree;
Position : Cursor) return Reference_Type
is
begin
if Checks and then Position.Container = null then
raise Constraint_Error with
"Position cursor has no element";
end if;
if Checks and then Position.Container /= Container'Unrestricted_Access
then
raise Program_Error with
"Position cursor designates wrong container";
end if;
if Checks and then Position.Node = Root_Node (Container) then
raise Program_Error with "Position cursor designates root";
end if;
-- Implement Vet for multiway tree???
-- pragma Assert (Vet (Position),
-- "Position cursor in Constant_Reference is bad");
declare
TC : constant Tamper_Counts_Access :=
Container.TC'Unrestricted_Access;
begin
return R : constant Reference_Type :=
(Element => Container.Elements (Position.Node)'Unchecked_Access,
Control => (Controlled with TC))
do
Busy (TC.all);
end return;
end;
end Reference;
--------------------
-- Remove_Subtree --
--------------------
procedure Remove_Subtree
(Container : in out Tree;
Subtree : Count_Type)
is
NN : Tree_Node_Array renames Container.Nodes;
N : Tree_Node_Type renames NN (Subtree);
CC : Children_Type renames NN (N.Parent).Children;
begin
-- This is a utility operation to remove a subtree node from its
-- parent's list of children.
if CC.First = Subtree then
pragma Assert (N.Prev <= 0);
if CC.Last = Subtree then
pragma Assert (N.Next <= 0);
CC.First := 0;
CC.Last := 0;
else
CC.First := N.Next;
NN (CC.First).Prev := 0;
end if;
elsif CC.Last = Subtree then
pragma Assert (N.Next <= 0);
CC.Last := N.Prev;
NN (CC.Last).Next := 0;
else
NN (N.Prev).Next := N.Next;
NN (N.Next).Prev := N.Prev;
end if;
end Remove_Subtree;
----------------------
-- Replace_Element --
----------------------
procedure Replace_Element
(Container : in out Tree;
Position : Cursor;
New_Item : Element_Type)
is
begin
TE_Check (Container.TC);
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Position.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Position cursor not in container";
end if;
if Checks and then Is_Root (Position) then
raise Program_Error with "Position cursor designates root";
end if;
Container.Elements (Position.Node) := New_Item;
end Replace_Element;
------------------------------
-- Reverse_Iterate_Children --
------------------------------
procedure Reverse_Iterate_Children
(Parent : Cursor;
Process : not null access procedure (Position : Cursor))
is
begin
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Parent.Container.Count = 0 then
pragma Assert (Is_Root (Parent));
return;
end if;
declare
NN : Tree_Node_Array renames Parent.Container.Nodes;
Busy : With_Busy (Parent.Container.TC'Unrestricted_Access);
C : Count_Type;
begin
C := NN (Parent.Node).Children.Last;
while C > 0 loop
Process (Cursor'(Parent.Container, Node => C));
C := NN (C).Prev;
end loop;
end;
end Reverse_Iterate_Children;
----------
-- Root --
----------
function Root (Container : Tree) return Cursor is
begin
return (Container'Unrestricted_Access, Root_Node (Container));
end Root;
---------------
-- Root_Node --
---------------
function Root_Node (Container : Tree) return Count_Type is
pragma Unreferenced (Container);
begin
return 0;
end Root_Node;
---------------------
-- Splice_Children --
---------------------
procedure Splice_Children
(Target : in out Tree;
Target_Parent : Cursor;
Before : Cursor;
Source : in out Tree;
Source_Parent : Cursor)
is
begin
TC_Check (Target.TC);
TC_Check (Source.TC);
if Checks and then Target_Parent = No_Element then
raise Constraint_Error with "Target_Parent cursor has no element";
end if;
if Checks and then Target_Parent.Container /= Target'Unrestricted_Access
then
raise Program_Error
with "Target_Parent cursor not in Target container";
end if;
if Before /= No_Element then
if Checks and then Before.Container /= Target'Unrestricted_Access then
raise Program_Error
with "Before cursor not in Target container";
end if;
if Checks and then
Target.Nodes (Before.Node).Parent /= Target_Parent.Node
then
raise Constraint_Error
with "Before cursor not child of Target_Parent";
end if;
end if;
if Checks and then Source_Parent = No_Element then
raise Constraint_Error with "Source_Parent cursor has no element";
end if;
if Checks and then Source_Parent.Container /= Source'Unrestricted_Access
then
raise Program_Error
with "Source_Parent cursor not in Source container";
end if;
if Source.Count = 0 then
pragma Assert (Is_Root (Source_Parent));
return;
end if;
if Target'Address = Source'Address then
if Target_Parent = Source_Parent then
return;
end if;
if Checks and then Is_Reachable (Container => Target,
From => Target_Parent.Node,
To => Source_Parent.Node)
then
raise Constraint_Error
with "Source_Parent is ancestor of Target_Parent";
end if;
Splice_Children
(Container => Target,
Target_Parent => Target_Parent.Node,
Before => Before.Node,
Source_Parent => Source_Parent.Node);
return;
end if;
if Target.Count = 0 then
Initialize_Root (Target);
end if;
Splice_Children
(Target => Target,
Target_Parent => Target_Parent.Node,
Before => Before.Node,
Source => Source,
Source_Parent => Source_Parent.Node);
end Splice_Children;
procedure Splice_Children
(Container : in out Tree;
Target_Parent : Cursor;
Before : Cursor;
Source_Parent : Cursor)
is
begin
TC_Check (Container.TC);
if Checks and then Target_Parent = No_Element then
raise Constraint_Error with "Target_Parent cursor has no element";
end if;
if Checks and then
Target_Parent.Container /= Container'Unrestricted_Access
then
raise Program_Error
with "Target_Parent cursor not in container";
end if;
if Before /= No_Element then
if Checks and then Before.Container /= Container'Unrestricted_Access
then
raise Program_Error
with "Before cursor not in container";
end if;
if Checks and then
Container.Nodes (Before.Node).Parent /= Target_Parent.Node
then
raise Constraint_Error
with "Before cursor not child of Target_Parent";
end if;
end if;
if Checks and then Source_Parent = No_Element then
raise Constraint_Error with "Source_Parent cursor has no element";
end if;
if Checks and then
Source_Parent.Container /= Container'Unrestricted_Access
then
raise Program_Error
with "Source_Parent cursor not in container";
end if;
if Target_Parent = Source_Parent then
return;
end if;
pragma Assert (Container.Count > 0);
if Checks and then Is_Reachable (Container => Container,
From => Target_Parent.Node,
To => Source_Parent.Node)
then
raise Constraint_Error
with "Source_Parent is ancestor of Target_Parent";
end if;
Splice_Children
(Container => Container,
Target_Parent => Target_Parent.Node,
Before => Before.Node,
Source_Parent => Source_Parent.Node);
end Splice_Children;
procedure Splice_Children
(Container : in out Tree;
Target_Parent : Count_Type;
Before : Count_Type'Base;
Source_Parent : Count_Type)
is
NN : Tree_Node_Array renames Container.Nodes;
CC : constant Children_Type := NN (Source_Parent).Children;
C : Count_Type'Base;
begin
-- This is a utility operation to remove the children from Source parent
-- and insert them into Target parent.
NN (Source_Parent).Children := Children_Type'(others => 0);
-- Fix up the Parent pointers of each child to designate its new Target
-- parent.
C := CC.First;
while C > 0 loop
NN (C).Parent := Target_Parent;
C := NN (C).Next;
end loop;
Insert_Subtree_List
(Container => Container,
First => CC.First,
Last => CC.Last,
Parent => Target_Parent,
Before => Before);
end Splice_Children;
procedure Splice_Children
(Target : in out Tree;
Target_Parent : Count_Type;
Before : Count_Type'Base;
Source : in out Tree;
Source_Parent : Count_Type)
is
S_NN : Tree_Node_Array renames Source.Nodes;
S_CC : Children_Type renames S_NN (Source_Parent).Children;
Target_Count, Source_Count : Count_Type;
T, S : Count_Type'Base;
begin
-- This is a utility operation to copy the children from the Source
-- parent and insert them as children of the Target parent, and then
-- delete them from the Source. (This is not a true splice operation,
-- but it is the best we can do in a bounded form.) The Before position
-- specifies where among the Target parent's exising children the new
-- children are inserted.
-- Before we attempt the insertion, we must count the sources nodes in
-- order to determine whether the target have enough storage
-- available. Note that calculating this value is an O(n) operation.
-- Here is an optimization opportunity: iterate of each children the
-- source explicitly, and keep a running count of the total number of
-- nodes. Compare the running total to the capacity of the target each
-- pass through the loop. This is more efficient than summing the counts
-- of child subtree (which is what Subtree_Node_Count does) and then
-- comparing that total sum to the target's capacity. ???
-- Here is another possibility. We currently treat the splice as an
-- all-or-nothing proposition: either we can insert all of children of
-- the source, or we raise exception with modifying the target. The
-- price for not causing side-effect is an O(n) determination of the
-- source count. If we are willing to tolerate side-effect, then we
-- could loop over the children of the source, counting that subtree and
-- then immediately inserting it in the target. The issue here is that
-- the test for available storage could fail during some later pass,
-- after children have already been inserted into target. ???
Source_Count := Subtree_Node_Count (Source, Source_Parent) - 1;
if Source_Count = 0 then
return;
end if;
if Checks and then Target.Count > Target.Capacity - Source_Count then
raise Capacity_Error -- ???
with "Source count exceeds available storage on Target";
end if;
-- Copy_Subtree returns a count of the number of nodes it inserts, but
-- it does this by incrementing the value passed in. Therefore we must
-- initialize the count before calling Copy_Subtree.
Target_Count := 0;
S := S_CC.First;
while S > 0 loop
Copy_Subtree
(Source => Source,
Source_Subtree => S,
Target => Target,
Target_Parent => Target_Parent,
Target_Subtree => T,
Count => Target_Count);
Insert_Subtree_Node
(Container => Target,
Subtree => T,
Parent => Target_Parent,
Before => Before);
S := S_NN (S).Next;
end loop;
pragma Assert (Target_Count = Source_Count);
Target.Count := Target.Count + Target_Count;
-- As with Copy_Subtree, operation Deallocate_Children returns a count
-- of the number of nodes it deallocates, but it works by incrementing
-- the value passed in. We must therefore initialize the count before
-- calling it.
Source_Count := 0;
Deallocate_Children (Source, Source_Parent, Source_Count);
pragma Assert (Source_Count = Target_Count);
Source.Count := Source.Count - Source_Count;
end Splice_Children;
--------------------
-- Splice_Subtree --
--------------------
procedure Splice_Subtree
(Target : in out Tree;
Parent : Cursor;
Before : Cursor;
Source : in out Tree;
Position : in out Cursor)
is
begin
TC_Check (Target.TC);
TC_Check (Source.TC);
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Target'Unrestricted_Access then
raise Program_Error with "Parent cursor not in Target container";
end if;
if Before /= No_Element then
if Checks and then Before.Container /= Target'Unrestricted_Access then
raise Program_Error with "Before cursor not in Target container";
end if;
if Checks and then Target.Nodes (Before.Node).Parent /= Parent.Node
then
raise Constraint_Error with "Before cursor not child of Parent";
end if;
end if;
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Position.Container /= Source'Unrestricted_Access then
raise Program_Error with "Position cursor not in Source container";
end if;
if Checks and then Is_Root (Position) then
raise Program_Error with "Position cursor designates root";
end if;
if Target'Address = Source'Address then
if Target.Nodes (Position.Node).Parent = Parent.Node then
if Before = No_Element then
if Target.Nodes (Position.Node).Next <= 0 then -- last child
return;
end if;
elsif Position.Node = Before.Node then
return;
elsif Target.Nodes (Position.Node).Next = Before.Node then
return;
end if;
end if;
if Checks and then Is_Reachable (Container => Target,
From => Parent.Node,
To => Position.Node)
then
raise Constraint_Error with "Position is ancestor of Parent";
end if;
Remove_Subtree (Target, Position.Node);
Target.Nodes (Position.Node).Parent := Parent.Node;
Insert_Subtree_Node (Target, Position.Node, Parent.Node, Before.Node);
return;
end if;
if Target.Count = 0 then
Initialize_Root (Target);
end if;
Splice_Subtree
(Target => Target,
Parent => Parent.Node,
Before => Before.Node,
Source => Source,
Position => Position.Node); -- modified during call
Position.Container := Target'Unrestricted_Access;
end Splice_Subtree;
procedure Splice_Subtree
(Container : in out Tree;
Parent : Cursor;
Before : Cursor;
Position : Cursor)
is
begin
TC_Check (Container.TC);
if Checks and then Parent = No_Element then
raise Constraint_Error with "Parent cursor has no element";
end if;
if Checks and then Parent.Container /= Container'Unrestricted_Access then
raise Program_Error with "Parent cursor not in container";
end if;
if Before /= No_Element then
if Checks and then Before.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Before cursor not in container";
end if;
if Checks and then Container.Nodes (Before.Node).Parent /= Parent.Node
then
raise Constraint_Error with "Before cursor not child of Parent";
end if;
end if;
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Position.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Position cursor not in container";
end if;
if Checks and then Is_Root (Position) then
-- Should this be PE instead? Need ARG confirmation. ???
raise Constraint_Error with "Position cursor designates root";
end if;
if Container.Nodes (Position.Node).Parent = Parent.Node then
if Before = No_Element then
if Container.Nodes (Position.Node).Next <= 0 then -- last child
return;
end if;
elsif Position.Node = Before.Node then
return;
elsif Container.Nodes (Position.Node).Next = Before.Node then
return;
end if;
end if;
if Checks and then Is_Reachable (Container => Container,
From => Parent.Node,
To => Position.Node)
then
raise Constraint_Error with "Position is ancestor of Parent";
end if;
Remove_Subtree (Container, Position.Node);
Container.Nodes (Position.Node).Parent := Parent.Node;
Insert_Subtree_Node (Container, Position.Node, Parent.Node, Before.Node);
end Splice_Subtree;
procedure Splice_Subtree
(Target : in out Tree;
Parent : Count_Type;
Before : Count_Type'Base;
Source : in out Tree;
Position : in out Count_Type) -- Source on input, Target on output
is
Source_Count : Count_Type := Subtree_Node_Count (Source, Position);
pragma Assert (Source_Count >= 1);
Target_Subtree : Count_Type;
Target_Count : Count_Type;
begin
-- This is a utility operation to do the heavy lifting associated with
-- splicing a subtree from one tree to another. Note that "splicing"
-- is a bit of a misnomer here in the case of a bounded tree, because
-- the elements must be copied from the source to the target.
if Checks and then Target.Count > Target.Capacity - Source_Count then
raise Capacity_Error -- ???
with "Source count exceeds available storage on Target";
end if;
-- Copy_Subtree returns a count of the number of nodes it inserts, but
-- it does this by incrementing the value passed in. Therefore we must
-- initialize the count before calling Copy_Subtree.
Target_Count := 0;
Copy_Subtree
(Source => Source,
Source_Subtree => Position,
Target => Target,
Target_Parent => Parent,
Target_Subtree => Target_Subtree,
Count => Target_Count);
pragma Assert (Target_Count = Source_Count);
-- Now link the newly-allocated subtree into the target.
Insert_Subtree_Node
(Container => Target,
Subtree => Target_Subtree,
Parent => Parent,
Before => Before);
Target.Count := Target.Count + Target_Count;
-- The manipulation of the Target container is complete. Now we remove
-- the subtree from the Source container.
Remove_Subtree (Source, Position); -- unlink the subtree
-- As with Copy_Subtree, operation Deallocate_Subtree returns a count of
-- the number of nodes it deallocates, but it works by incrementing the
-- value passed in. We must therefore initialize the count before
-- calling it.
Source_Count := 0;
Deallocate_Subtree (Source, Position, Source_Count);
pragma Assert (Source_Count = Target_Count);
Source.Count := Source.Count - Source_Count;
Position := Target_Subtree;
end Splice_Subtree;
------------------------
-- Subtree_Node_Count --
------------------------
function Subtree_Node_Count (Position : Cursor) return Count_Type is
begin
if Position = No_Element then
return 0;
end if;
if Position.Container.Count = 0 then
pragma Assert (Is_Root (Position));
return 1;
end if;
return Subtree_Node_Count (Position.Container.all, Position.Node);
end Subtree_Node_Count;
function Subtree_Node_Count
(Container : Tree;
Subtree : Count_Type) return Count_Type
is
Result : Count_Type;
Node : Count_Type'Base;
begin
Result := 1;
Node := Container.Nodes (Subtree).Children.First;
while Node > 0 loop
Result := Result + Subtree_Node_Count (Container, Node);
Node := Container.Nodes (Node).Next;
end loop;
return Result;
end Subtree_Node_Count;
----------
-- Swap --
----------
procedure Swap
(Container : in out Tree;
I, J : Cursor)
is
begin
TE_Check (Container.TC);
if Checks and then I = No_Element then
raise Constraint_Error with "I cursor has no element";
end if;
if Checks and then I.Container /= Container'Unrestricted_Access then
raise Program_Error with "I cursor not in container";
end if;
if Checks and then Is_Root (I) then
raise Program_Error with "I cursor designates root";
end if;
if I = J then -- make this test sooner???
return;
end if;
if Checks and then J = No_Element then
raise Constraint_Error with "J cursor has no element";
end if;
if Checks and then J.Container /= Container'Unrestricted_Access then
raise Program_Error with "J cursor not in container";
end if;
if Checks and then Is_Root (J) then
raise Program_Error with "J cursor designates root";
end if;
declare
EE : Element_Array renames Container.Elements;
EI : constant Element_Type := EE (I.Node);
begin
EE (I.Node) := EE (J.Node);
EE (J.Node) := EI;
end;
end Swap;
--------------------
-- Update_Element --
--------------------
procedure Update_Element
(Container : in out Tree;
Position : Cursor;
Process : not null access procedure (Element : in out Element_Type))
is
begin
if Checks and then Position = No_Element then
raise Constraint_Error with "Position cursor has no element";
end if;
if Checks and then Position.Container /= Container'Unrestricted_Access
then
raise Program_Error with "Position cursor not in container";
end if;
if Checks and then Is_Root (Position) then
raise Program_Error with "Position cursor designates root";
end if;
declare
T : Tree renames Position.Container.all'Unrestricted_Access.all;
Lock : With_Lock (T.TC'Unrestricted_Access);
begin
Process (Element => T.Elements (Position.Node));
end;
end Update_Element;
-----------
-- Write --
-----------
procedure Write
(Stream : not null access Root_Stream_Type'Class;
Container : Tree)
is
procedure Write_Children (Subtree : Count_Type);
procedure Write_Subtree (Subtree : Count_Type);
--------------------
-- Write_Children --
--------------------
procedure Write_Children (Subtree : Count_Type) is
CC : Children_Type renames Container.Nodes (Subtree).Children;
C : Count_Type'Base;
begin
Count_Type'Write (Stream, Child_Count (Container, Subtree));
C := CC.First;
while C > 0 loop
Write_Subtree (C);
C := Container.Nodes (C).Next;
end loop;
end Write_Children;
-------------------
-- Write_Subtree --
-------------------
procedure Write_Subtree (Subtree : Count_Type) is
begin
Element_Type'Write (Stream, Container.Elements (Subtree));
Write_Children (Subtree);
end Write_Subtree;
-- Start of processing for Write
begin
Count_Type'Write (Stream, Container.Count);
if Container.Count = 0 then
return;
end if;
Write_Children (Root_Node (Container));
end Write;
procedure Write
(Stream : not null access Root_Stream_Type'Class;
Position : Cursor)
is
begin
raise Program_Error with "attempt to write tree cursor to stream";
end Write;
procedure Write
(Stream : not null access Root_Stream_Type'Class;
Item : Reference_Type)
is
begin
raise Program_Error with "attempt to stream reference";
end Write;
procedure Write
(Stream : not null access Root_Stream_Type'Class;
Item : Constant_Reference_Type)
is
begin
raise Program_Error with "attempt to stream reference";
end Write;
end Ada.Containers.Bounded_Multiway_Trees;
|