/usr/include/bm/bmfunc.h is in bmagic 3.7.0-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 | #ifndef BMFUNC__H__INCLUDED__
#define BMFUNC__H__INCLUDED__
/*
Copyright(c) 2002-2010 Anatoliy Kuznetsov(anatoliy_kuznetsov at yahoo.com)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information please visit: http://bmagic.sourceforge.net
*/
#include <memory.h>
#include "bmdef.h"
#include "bmutil.h"
#ifdef _MSC_VER
# pragma warning( disable: 4146 )
#endif
namespace bm
{
inline
bm::id_t bit_block_calc_count_range(const bm::word_t* block,
bm::word_t left,
bm::word_t right);
inline
bm::id_t bit_block_any_range(const bm::word_t* block,
bm::word_t left,
bm::word_t right);
/*!
@brief Structure with statistical information about bitset's memory
allocation details.
@ingroup bvector
*/
struct bv_statistics
{
/// Number of bit blocks.
unsigned bit_blocks;
/// Number of GAP blocks.
unsigned gap_blocks;
/// Estimated maximum of memory required for serialization.
unsigned max_serialize_mem;
/// Memory used by bitvector including temp and service blocks
unsigned memory_used;
/// Array of all GAP block lengths in the bvector.
gap_word_t gap_length[bm::set_total_blocks];
/// GAP lengths used by bvector
gap_word_t gap_levels[bm::gap_levels];
/// cound bit block
void add_bit_block()
{
++bit_blocks;
unsigned mem_used = sizeof(bm::word_t) * bm::set_block_size;
memory_used += mem_used;
max_serialize_mem += mem_used;
}
/// count gap block
void add_gap_block(unsigned capacity, unsigned length)
{
++gap_blocks;
unsigned mem_used = capacity * sizeof(gap_word_t);
memory_used += mem_used;
max_serialize_mem += length * sizeof(gap_word_t);
}
};
/*! @defgroup gapfunc GAP functions
* GAP functions implement different opereations on GAP compressed blocks
* and serve as a minimal building blocks.
* @ingroup bmagic
*
*/
/*! @defgroup bitfunc BIT functions
* Bit functions implement different opereations on bit blocks
* and serve as a minimal building blocks.
* @ingroup bmagic
*/
/*! @brief Default GAP lengths table.
@ingroup gapfunc
*/
template<bool T> struct gap_len_table
{
static const gap_word_t _len[bm::gap_levels];
};
template<bool T>
const gap_word_t gap_len_table<T>::_len[bm::gap_levels] =
{ 128, 256, 512, bm::gap_max_buff_len };
/*! @brief Alternative GAP lengths table.
Good for for memory saver mode and very sparse bitsets.
@ingroup gapfunc
*/
template<bool T> struct gap_len_table_min
{
static const gap_word_t _len[bm::gap_levels];
};
template<bool T>
const gap_word_t gap_len_table_min<T>::_len[bm::gap_levels] =
{ 32, 96, 128, 512 };
//---------------------------------------------------------------------
/** Structure keeps all-left/right ON bits masks.
@ingroup bitfunc
*/
template<bool T> struct block_set_table
{
static const unsigned _left[32];
static const unsigned _right[32];
};
template<bool T>
const unsigned block_set_table<T>::_left[32] = {
0x1, 0x3, 0x7, 0xf, 0x1f, 0x3f, 0x7f, 0xff, 0x1ff, 0x3ff, 0x7ff,
0xfff, 0x1fff, 0x3fff, 0x7fff, 0xffff, 0x1ffff, 0x3ffff, 0x7ffff,
0xfffff, 0x1fffff, 0x3fffff, 0x7fffff, 0xffffff, 0x1ffffff, 0x3ffffff,
0x7ffffff, 0xfffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff, 0xffffffff
};
template<bool T>
const unsigned block_set_table<T>::_right[32] = {
0xffffffff, 0xfffffffe, 0xfffffffc, 0xfffffff8, 0xfffffff0,
0xffffffe0, 0xffffffc0, 0xffffff80, 0xffffff00, 0xfffffe00,
0xfffffc00, 0xfffff800, 0xfffff000, 0xffffe000, 0xffffc000,
0xffff8000, 0xffff0000, 0xfffe0000, 0xfffc0000, 0xfff80000,
0xfff00000, 0xffe00000, 0xffc00000, 0xff800000, 0xff000000,
0xfe000000, 0xfc000000, 0xf8000000, 0xf0000000, 0xe0000000,
0xc0000000, 0x80000000
};
/*!
Returns bit count
@ingroup bitfunc
*/
BMFORCEINLINE
bm::id_t word_bitcount(bm::id_t w)
{
#ifdef BMSSE4OPT
return _mm_popcnt_u32(w);
#else
return
bm::bit_count_table<true>::_count[(unsigned char)(w)] +
bm::bit_count_table<true>::_count[(unsigned char)((w) >> 8)] +
bm::bit_count_table<true>::_count[(unsigned char)((w) >> 16)] +
bm::bit_count_table<true>::_count[(unsigned char)((w) >> 24)];
#endif
}
inline
int parallel_popcnt_32(unsigned int n)
{
unsigned int tmp;
tmp = n - ((n >> 1) & 033333333333)
- ((n >> 2) & 011111111111);
return ((tmp + (tmp >> 3)) & 030707070707) % 63;
}
#ifdef BM64OPT
/*!
Function calculates number of 1 bits in 64-bit word.
@ingroup bitfunc
*/
inline
int word_bitcount64(bm::id64_t x)
{
x = x - ((x >> 1) & 0x5555555555555555);
x = (x & 0x3333333333333333) + ((x >> 2) & 0x3333333333333333);
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0F;
x = x + (x >> 8);
x = x + (x >> 16);
x = x + (x >> 32);
return x & 0xFF;
}
inline
unsigned bitcount64_4way(bm::id64_t x, bm::id64_t y,
bm::id64_t u, bm::id64_t v)
{
const bm::id64_t m1 = 0x5555555555555555;
const bm::id64_t m2 = 0x3333333333333333;
const bm::id64_t m3 = 0x0F0F0F0F0F0F0F0F;
const bm::id64_t m4 = 0x000000FF000000FF;
x = x - ((x >> 1) & m1);
y = y - ((y >> 1) & m1);
u = u - ((u >> 1) & m1);
v = v - ((v >> 1) & m1);
x = (x & m2) + ((x >> 2) & m2);
y = (y & m2) + ((y >> 2) & m2);
u = (u & m2) + ((u >> 2) & m2);
v = (v & m2) + ((v >> 2) & m2);
x = x + y;
u = u + v;
x = (x & m3) + ((x >> 4) & m3);
u = (u & m3) + ((u >> 4) & m3);
x = x + u;
x = x + (x >> 8);
x = x + (x >> 16);
x = x & m4;
x = x + (x >> 32);
return x & 0x000001FF;
}
#endif
//---------------------------------------------------------------------
/**
Nomenclature of set operations
*/
enum set_operation
{
set_AND = 0,
set_OR = 1,
set_SUB = 2,
set_XOR = 3,
set_ASSIGN = 4,
set_COUNT = 5,
set_COUNT_AND = 6,
set_COUNT_XOR = 7,
set_COUNT_OR = 8,
set_COUNT_SUB_AB= 9,
set_COUNT_SUB_BA= 10,
set_COUNT_A = 11,
set_COUNT_B = 12,
set_END
};
/// Returns true if set operation is constant (bitcount)
inline
bool is_const_set_operation(set_operation op)
{
return (int(op) >= int(set_COUNT));
}
/**
Bit operations enumeration.
*/
enum operation
{
BM_AND = set_AND,
BM_OR = set_OR,
BM_SUB = set_SUB,
BM_XOR = set_XOR
};
/**
Convert set operation to operation
*/
inline
bm::operation setop2op(bm::set_operation op)
{
BM_ASSERT(op == set_AND ||
op == set_OR ||
op == set_SUB ||
op == set_XOR);
return (bm::operation) op;
}
//---------------------------------------------------------------------
/**
Structure carries pointer on bit block with all bits 1
@ingroup bitfunc
*/
template<bool T> struct all_set
{
struct BM_ALIGN16 all_set_block
{
bm::word_t _p[bm::set_block_size] BM_ALIGN16ATTR;
all_set_block()
{
::memset(_p, 0xFF, sizeof(_p));
}
};
static all_set_block _block;
};
template<bool T> typename all_set<T>::all_set_block all_set<T>::_block;
/// XOR swap two scalar variables
template<typename W>
void xor_swap(W& x, W& y)
{
BM_ASSERT(&x != &y);
x ^= y;
y ^= x;
x ^= y;
}
//---------------------------------------------------------------------
/*!
\brief Lexicographical comparison of two words as bit strings.
Auxiliary implementation for testing and reference purposes.
\param buf1 - First word.
\param buf2 - Second word.
\return <0 - less, =0 - equal, >0 - greater.
@ingroup bitfunc
*/
template<typename T> int wordcmp0(T w1, T w2)
{
while (w1 != w2)
{
int res = (w1 & 1) - (w2 & 1);
if (res != 0) return res;
w1 >>= 1;
w2 >>= 1;
}
return 0;
}
/*!
\brief Lexicographical comparison of two words as bit strings.
Auxiliary implementation for testing and reference purposes.
\param buf1 - First word.
\param buf2 - Second word.
\return <0 - less, =0 - equal, >0 - greater.
@ingroup bitfunc
*/
/*
template<typename T> int wordcmp(T w1, T w2)
{
T diff = w1 ^ w2;
return diff ? ((w1 & diff & (diff ^ (diff - 1)))? 1 : -1) : 0;
}
*/
template<typename T> int wordcmp(T a, T b)
{
T diff = a ^ b;
return diff? ( (a & diff & -diff)? 1 : -1 ) : 0;
}
// Low bit extraction
// x & (x ^ (x-1))
/**
Internal structure. Copyright information.
*/
template<bool T> struct _copyright
{
static const char _p[];
};
template<bool T> const char _copyright<T>::_p[] =
"BitMagic C++ Library. v.3.7.0 (c) 2002-2010 Anatoliy Kuznetsov.";
/*!
\brief Byte orders recognized by the library.
*/
enum ByteOrder
{
BigEndian = 0,
LittleEndian = 1
};
/**
Internal structure. Different global settings.
*/
template<bool T> struct globals
{
struct bo
{
ByteOrder _byte_order;
bo()
{
unsigned x;
unsigned char *s = (unsigned char *)&x;
s[0] = 1;
s[1] = 2;
s[2] = 3;
s[3] = 4;
if(x == 0x04030201)
{
_byte_order = LittleEndian;
return;
}
if(x == 0x01020304)
{
_byte_order = BigEndian;
return;
}
BM_ASSERT(0); // "Invalid Byte Order\n"
_byte_order = LittleEndian;
}
};
static bo _bo;
static ByteOrder byte_order() { return _bo._byte_order; }
};
template<bool T> typename globals<T>::bo globals<T>::_bo;
/*
\brief Binary search for the block where bit = pos located.
\param buf - GAP buffer pointer.
\param pos - index of the element.
\param is_set - output. GAP value (0 or 1).
\return GAP index.
@ingroup gapfunc
*/
template<typename T>
unsigned gap_bfind(const T* buf, unsigned pos, unsigned* is_set)
{
BM_ASSERT(pos < bm::gap_max_bits);
*is_set = (*buf) & 1;
register unsigned start = 1;
register unsigned end = 1 + ((*buf) >> 3);
while ( start != end )
{
unsigned curr = (start + end) >> 1;
if ( buf[curr] < pos )
start = curr + 1;
else
end = curr;
}
*is_set ^= ((start-1) & 1);
return start;
}
/*!
\brief Tests if bit = pos is true.
\param buf - GAP buffer pointer.
\param pos - index of the element.
\return true if position is in "1" gap
@ingroup gapfunc
*/
template<typename T> unsigned gap_test(const T* buf, unsigned pos)
{
BM_ASSERT(pos < bm::gap_max_bits);
unsigned start = 1;
unsigned end = 1 + ((*buf) >> 3);
if (end - start < 10)
{
unsigned sv = *buf & 1;
unsigned sv1= sv ^ 1;
if (buf[1] >= pos) return sv;
if (buf[2] >= pos) return sv1;
if (buf[3] >= pos) return sv;
if (buf[4] >= pos) return sv1;
if (buf[5] >= pos) return sv;
if (buf[6] >= pos) return sv1;
if (buf[7] >= pos) return sv;
if (buf[8] >= pos) return sv1;
if (buf[9] >= pos) return sv;
BM_ASSERT(0);
}
else
while ( start != end )
{
unsigned curr = (start + end) >> 1;
if ( buf[curr] < pos )
start = curr + 1;
else
end = curr;
}
return ((*buf) & 1) ^ ((--start) & 1);
}
/*! For each non-zero block executes supplied function.
*/
template<class T, class F>
void for_each_nzblock(T*** root, unsigned size1, //unsigned size2,
F& f)
{
for (unsigned i = 0; i < size1; ++i)
{
T** blk_blk = root[i];
if (!blk_blk)
{
f.on_empty_top(i);
continue;
}
unsigned non_empty_top = 0;
unsigned r = i * bm::set_array_size;
for (unsigned j = 0;j < bm::set_array_size; ++j)
{
if (blk_blk[j])
{
f(blk_blk[j], r + j);
non_empty_top += (blk_blk[j] != 0);// re-check for mutation
}
else
{
f.on_empty_block(r + j);
}
} // for j
if (non_empty_top == 0)
{
f.on_empty_top(i);
}
} // for i
}
/*! For each non-zero block executes supplied function.
*/
template<class T, class F>
void for_each_nzblock2(T*** root, unsigned size1, F& f)
{
for (unsigned i = 0; i < size1; ++i)
{
T** blk_blk;
if ((blk_blk = root[i])!=0)
{
unsigned j = 0;
do
{
if (blk_blk[j])
f(blk_blk[j]);
if (blk_blk[j+1])
f(blk_blk[j+1]);
if (blk_blk[j+2])
f(blk_blk[j+2]);
if (blk_blk[j+3])
f(blk_blk[j+3]);
j += 4;
} while (j < bm::set_array_size);
}
} // for i
}
/*! For each non-zero block executes supplied function-predicate.
Function returns if function-predicate returns true
*/
template<class T, class F>
bool for_each_nzblock_if(T*** root, unsigned size1, F& f)
{
unsigned block_idx = 0;
for (unsigned i = 0; i < size1; ++i)
{
T** blk_blk = root[i];
if (!blk_blk)
{
block_idx += bm::set_array_size;
continue;
}
for (unsigned j = 0;j < bm::set_array_size; ++j, ++block_idx)
{
if (blk_blk[j])
if (f(blk_blk[j], block_idx)) return true;
}
}
return false;
}
/*! For each block executes supplied function.
*/
template<class T, class F>
void for_each_block(T*** root, unsigned size1, F& f)
{
unsigned block_idx = 0;
for (unsigned i = 0; i < size1; ++i)
{
T** blk_blk = root[i];
if (blk_blk)
{
for (unsigned j = 0;j < bm::set_array_size; ++j, ++block_idx)
{
f(blk_blk[j], block_idx);
}
}
else
{
for (unsigned j = 0;j < bm::set_array_size; ++j, ++block_idx)
{
f(0, block_idx);
}
}
}
}
/*! Special BM optimized analog of STL for_each
*/
template<class T, class F> F bmfor_each(T first, T last, F f)
{
do
{
f(*first);
++first;
} while (first < last);
return f;
}
/*! Computes SUM of all elements of the sequence
*/
template<class T> T sum_arr(T* first, T* last)
{
T sum = 0;
while (first < last)
{
sum += *first;
++first;
}
return sum;
}
/*!
\brief Calculates number of bits ON in GAP buffer.
\param buf - GAP buffer pointer.
\param dsize - buffer size
\return Number of non-zero bits.
@ingroup gapfunc
*/
template<typename T> unsigned gap_bit_count(const T* buf, unsigned dsize=0)
{
register const T* pcurr = buf;
if (dsize == 0)
dsize = (*pcurr >> 3);
register const T* pend = pcurr + dsize;
register unsigned bits_counter = 0;
++pcurr;
if (*buf & 1)
{
bits_counter += *pcurr + 1;
++pcurr;
}
++pcurr; // set GAP to 1
while (pcurr <= pend)
{
bits_counter += *pcurr - *(pcurr-1);
pcurr += 2; // jump to the next positive GAP
}
return bits_counter;
}
/*!
\brief Counts 1 bits in GAP buffer in the closed [left, right] diapason.
\param buf - GAP buffer pointer.
\param left - leftmost bit index to start from
\param right- rightmost bit index
\return Number of non-zero bits.
*/
template<typename T>
unsigned gap_bit_count_range(const T* buf, T left, T right)
{
BM_ASSERT(left <= right);
const T* pcurr = buf;
const T* pend = pcurr + (*pcurr >> 3);
unsigned bits_counter = 0;
unsigned is_set;
unsigned start_pos = gap_bfind(buf, left, &is_set);
pcurr = buf + start_pos;
if (right <= *pcurr) // we are in the target block right now
{
if (is_set)
bits_counter = (right - left + 1);
return bits_counter;
}
if (is_set)
bits_counter += *pcurr - left + 1;
unsigned prev_gap = *pcurr++;
is_set ^= 1;
while (right > *pcurr)
{
if (is_set)
bits_counter += *pcurr - prev_gap;
if (pcurr == pend)
return bits_counter;
prev_gap = *pcurr++;
is_set ^= 1;
}
if (is_set)
bits_counter += right - prev_gap;
return bits_counter;
}
/*!
D-GAP block for_each algorithm
D-Gap Functor is called for each element but last one.
\param gap_buf - GAP buffer
*/
template<class T, class Func>
void for_each_dgap(const T* gap_buf, Func& func)
{
const T* pcurr = gap_buf;
const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
T prev = *pcurr;
func(prev + 1); // first element incremented to avoid 0
++pcurr;
do
{
func(*pcurr - prev); // all others are [N] - [N-1]
prev = *pcurr;
} while (++pcurr < pend);
}
/** d-Gap copy functor
@internal
*/
template<typename T> struct d_copy_func
{
d_copy_func(T* dg_buf) : dgap_buf_(dg_buf) {}
void operator()(T dgap) { *dgap_buf_++ = dgap; }
T* dgap_buf_;
};
/*!
\brief Convert GAP buffer into D-GAP buffer
Delta GAP representation is DGAP[N] = GAP[N] - GAP[N-1]
\param gap_buf - GAP buffer
\param dgap_buf - Delta-GAP buffer
\param copy_head - flag to copy GAP header
\internal
@ingroup gapfunc
*/
template<typename T>
T* gap_2_dgap(const T* gap_buf, T* dgap_buf, bool copy_head=true)
{
if (copy_head) // copy GAP header
{
*dgap_buf++ = *gap_buf;
}
d_copy_func<T> copy_func(dgap_buf);
for_each_dgap<T, d_copy_func<T> >(gap_buf, copy_func);
return copy_func.dgap_buf_;
}
/*!
\brief Convert D-GAP buffer into GAP buffer
GAP representation is GAP[N] = DGAP[N] + DGAP[N-1]
\param dgap_buf - Delta-GAP buffer
\param gap_buf - GAP buffer
\internal
@ingroup gapfunc
*/
template<typename T>
void dgap_2_gap(const T* dgap_buf, T* gap_buf, T gap_header=0)
{
const T* pcurr = dgap_buf;
unsigned len;
if (!gap_header) // GAP header is already part of the stream
{
len = *pcurr >> 3;
*gap_buf++ = *pcurr++; // copy GAP header
}
else // GAP header passed as a parameter
{
len = gap_header >> 3;
*gap_buf++ = gap_header; // assign GAP header
}
--len; // last element is actually not encoded
register const T* pend = pcurr + len;
*gap_buf = *pcurr++; // copy first element
if (*gap_buf == 0)
*gap_buf = 65535; // fix +1 overflow
else
*gap_buf = *gap_buf - 1;
for (++gap_buf; pcurr < pend; ++pcurr)
{
T prev = *(gap_buf-1); // don't remove temp(undef expression!)
*gap_buf++ = *pcurr + prev;
}
*gap_buf = 65535; // add missing last element
}
/*!
\brief Lexicographical comparison of GAP buffers.
\param buf1 - First GAP buffer pointer.
\param buf2 - Second GAP buffer pointer.
\return <0 - less, =0 - equal, >0 - greater.
@ingroup gapfunc
*/
template<typename T> int gapcmp(const T* buf1, const T* buf2)
{
const T* pcurr1 = buf1;
const T* pend1 = pcurr1 + (*pcurr1 >> 3);
unsigned bitval1 = *buf1 & 1;
++pcurr1;
const T* pcurr2 = buf2;
unsigned bitval2 = *buf2 & 1;
++pcurr2;
while (pcurr1 <= pend1)
{
if (*pcurr1 == *pcurr2)
{
if (bitval1 != bitval2)
{
return (bitval1) ? 1 : -1;
}
}
else
{
if (bitval1 == bitval2)
{
if (bitval1)
{
return (*pcurr1 < *pcurr2) ? -1 : 1;
}
else
{
return (*pcurr1 < *pcurr2) ? 1 : -1;
}
}
else
{
return (bitval1) ? 1 : -1;
}
}
++pcurr1; ++pcurr2;
bitval1 ^= 1;
bitval2 ^= 1;
}
return 0;
}
/*!
\brief Abstract operation for GAP buffers.
Receives functor F as a template argument
\param dest - destination memory buffer.
\param vect1 - operand 1 GAP encoded buffer.
\param vect1_mask - XOR mask for starting bitflag for vector1
can be 0 or 1 (1 inverts the vector)
\param vect2 - operand 2 GAP encoded buffer.
\param vect2_mask - same as vect1_mask
\param f - operation functor.
\param dlen - destination length after the operation
\note Internal function.
@internal
@ingroup gapfunc
*/
template<typename T, class F>
void gap_buff_op(T* BMRESTRICT dest,
const T* BMRESTRICT vect1,
unsigned vect1_mask,
const T* BMRESTRICT vect2,
unsigned vect2_mask,
F& f,
unsigned& dlen)
{
register const T* cur1 = vect1;
register const T* cur2 = vect2;
T bitval1 = (T)((*cur1++ & 1) ^ vect1_mask);
T bitval2 = (T)((*cur2++ & 1) ^ vect2_mask);
T bitval = (T) f(bitval1, bitval2);
T bitval_prev = bitval;
register T* res = dest;
*res = bitval;
++res;
while (1)
{
bitval = (T) f(bitval1, bitval2);
// Check if GAP value changes and we need to
// start the next one.
if (bitval != bitval_prev)
{
++res;
bitval_prev = bitval;
}
if (*cur1 < *cur2)
{
*res = *cur1;
++cur1;
bitval1 ^= 1;
}
else // >=
{
*res = *cur2;
if (*cur2 < *cur1)
{
bitval2 ^= 1;
}
else // equal
{
if (*cur2 == (bm::gap_max_bits - 1))
{
break;
}
++cur1;
bitval1 ^= 1;
bitval2 ^= 1;
}
++cur2;
}
} // while
dlen = (unsigned)(res - dest);
*dest = (T)((*dest & 7) + (dlen << 3));
}
/*!
\brief Abstract distance test operation for GAP buffers.
Receives functor F as a template argument
\param vect1 - operand 1 GAP encoded buffer.
\param vect1_mask - XOR mask for starting bitflag for vector1
can be 0 or 1 (1 inverts the vector)
\param vect2 - operand 2 GAP encoded buffer.
\param vect2_mask - same as vect1_mask
\param f - operation functor.
\note Internal function.
\return non zero value if operation result returns any 1 bit
@ingroup gapfunc
*/
template<typename T, class F>
unsigned gap_buff_any_op(const T* BMRESTRICT vect1,
unsigned vect1_mask,
const T* BMRESTRICT vect2,
unsigned vect2_mask,
F f)
{
register const T* cur1 = vect1;
register const T* cur2 = vect2;
unsigned bitval1 = (*cur1++ & 1) ^ vect1_mask;
unsigned bitval2 = (*cur2++ & 1) ^ vect2_mask;
unsigned bitval = f(bitval1, bitval2);
if (bitval)
return bitval;
unsigned bitval_prev = bitval;
while (1)
{
bitval = f(bitval1, bitval2);
if (bitval)
return bitval;
if (bitval != bitval_prev)
bitval_prev = bitval;
if (*cur1 < *cur2)
{
++cur1;
bitval1 ^= 1;
}
else // >=
{
if (*cur2 < *cur1)
{
bitval2 ^= 1;
}
else // equal
{
if (*cur2 == (bm::gap_max_bits - 1))
{
break;
}
++cur1;
bitval1 ^= 1;
bitval2 ^= 1;
}
++cur2;
}
} // while
return 0;
}
/*!
\brief Abstract distance(similarity) operation for GAP buffers.
Receives functor F as a template argument
\param vect1 - operand 1 GAP encoded buffer.
\param vect2 - operand 2 GAP encoded buffer.
\param f - operation functor.
\note Internal function.
@ingroup gapfunc
*/
template<typename T, class F>
unsigned gap_buff_count_op(const T* vect1, const T* vect2, F f)
{
unsigned count;// = 0;
const T* cur1 = vect1;
const T* cur2 = vect2;
unsigned bitval1 = (*cur1++ & 1);
unsigned bitval2 = (*cur2++ & 1);
unsigned bitval = count = f(bitval1, bitval2);
unsigned bitval_prev = bitval;
//if (bitval) ++count;
T res, res_prev;
res = res_prev = 0;
while (1)
{
bitval = f(bitval1, bitval2);
// Check if GAP value changes and we need to
// start the next one.
if (bitval != bitval_prev)
{
bitval_prev = bitval;
res_prev = res;
}
if (*cur1 < *cur2)
{
res = *cur1;
if (bitval)
{
count += res - res_prev;
res_prev = res;
}
++cur1;
bitval1 ^= 1;
}
else // >=
{
res = *cur2;
if (bitval)
{
count += res - res_prev;
res_prev = res;
}
if (*cur2 < *cur1)
{
bitval2 ^= 1;
}
else // equal
{
if (*cur2 == (bm::gap_max_bits - 1))
{
break;
}
++cur1;
bitval1 ^= 1;
bitval2 ^= 1;
}
++cur2;
}
} // while
return count;
}
/*!
\brief Sets or clears bit in the GAP buffer.
\param val - new bit value
\param buf - GAP buffer.
\param pos - Index of bit to set.
\param is_set - (OUT) flag if bit was actually set.
\return New GAP buffer length.
@ingroup gapfunc
*/
template<typename T> unsigned gap_set_value(unsigned val,
T* BMRESTRICT buf,
unsigned pos,
unsigned* BMRESTRICT is_set)
{
BM_ASSERT(pos < bm::gap_max_bits);
unsigned curr = gap_bfind(buf, pos, is_set);
register T end = (*buf >> 3);
if (*is_set == val)
{
*is_set = 0;
return end;
}
*is_set = 1;
register T* pcurr = buf + curr;
register T* pprev = pcurr - 1;
register T* pend = buf + end;
// Special case, first bit GAP operation. There is no platform beside it.
// initial flag must be inverted.
if (pos == 0)
{
*buf ^= 1;
if ( buf[1] ) // We need to insert a 1 bit platform here.
{
::memmove(&buf[2], &buf[1], (end - 1) * sizeof(gap_word_t));
buf[1] = 0;
++end;
}
else // Only 1 bit in the GAP. We need to delete the first GAP.
{
pprev = buf + 1;
pcurr = pprev + 1;
do
{
*pprev++ = *pcurr++;
} while (pcurr < pend);
--end;
}
}
else if (curr > 1 && ((unsigned)(*pprev))+1 == pos) // Left border bit
{
++(*pprev);
if (*pprev == *pcurr) // Curr. GAP to be merged with prev.GAP.
{
--end;
if (pcurr != pend) // GAP merge: 2 GAPS to be deleted
{
--end;
++pcurr;
do
{
*pprev++ = *pcurr++;
} while (pcurr < pend);
}
}
}
else if (*pcurr == pos) // Rightmost bit in the GAP. Border goes left.
{
--(*pcurr);
if (pcurr == pend)
{
++end;
}
}
else // Worst case we need to split current block.
{
::memmove(pcurr+2, pcurr,(end - curr + 1)*sizeof(T));
*pcurr++ = (T)(pos - 1);
*pcurr = (T)pos;
end+=2;
}
// Set correct length word.
*buf = (*buf & 7) + (end << 3);
buf[end] = bm::gap_max_bits - 1;
return end;
}
/*!
\brief Add new value to the end of GAP buffer.
\param buf - GAP buffer.
\param pos - Index of bit to set.
\return New GAP buffer length.
@ingroup gapfunc
*/
template<typename T>
unsigned gap_add_value(T* buf, T pos)
{
BM_ASSERT(pos < bm::gap_max_bits);
register T end = (*buf >> 3);
T curr = end;
register T* pcurr = buf + end;
register T* pend = pcurr;
register T* pprev = pcurr - 1;
// Special case, first bit GAP operation. There is no platform beside it.
// initial flag must be inverted.
if (pos == 0)
{
*buf ^= 1;
if ( buf[1] ) // We need to insert a 1 bit platform here.
{
::memmove(&buf[2], &buf[1], (end - 1) * sizeof(gap_word_t));
buf[1] = 0;
++end;
}
else // Only 1 bit in the GAP. We need to delete the first GAP.
{
pprev = buf + 1;
pcurr = pprev + 1;
do
{
*pprev++ = *pcurr++;
} while (pcurr < pend);
--end;
}
}
else if (((unsigned)(*pprev))+1 == pos && (curr > 1) ) // Left border bit
{
++(*pprev);
if (*pprev == *pcurr) // Curr. GAP to be merged with prev.GAP.
{
--end;
if (pcurr != pend) // GAP merge: 2 GAPS to be deleted
{
// TODO: should never get here...
--end;
++pcurr;
do
{
*pprev++ = *pcurr++;
} while (pcurr < pend);
}
}
}
else if (*pcurr == pos) // Rightmost bit in the GAP. Border goes left.
{
--(*pcurr);
if (pcurr == pend)
{
++end;
}
}
else // Worst case we need to split current block.
{
*pcurr++ = pos - 1;
*pcurr = pos;
end+=2;
}
// Set correct length word.
*buf = (*buf & 7) + (end << 3);
buf[end] = bm::gap_max_bits - 1;
return end;
}
/*!
\brief Convert array to GAP buffer.
\param buf - GAP buffer.
\param arr - array of values to set
\param len - length of the array
\return New GAP buffer length.
@ingroup gapfunc
*/
template<typename T>
unsigned gap_set_array(T* buf, const T* arr, unsigned len)
{
*buf = (*buf & 6u) + (1u << 3); // gap header setup
T* pcurr = buf + 1;
unsigned i = 0;
T curr = arr[i];
if (curr != 0) // need to add the first gap: (0 to arr[0]-1)
{
*pcurr = curr - 1;
++pcurr;
}
else
{
*buf += 1; // GAP starts with 1
}
T prev = curr;
T acc = prev;
for (i = 1; i < len; ++i)
{
T curr = arr[i];
if (curr == prev + 1)
{
++acc;
prev = curr;
}
else
{
*pcurr++ = acc;
acc = curr;
*pcurr++ = curr-1;
}
prev = curr;
}
*pcurr = acc;
if (acc != bm::gap_max_bits - 1)
{
++pcurr;
*pcurr = bm::gap_max_bits - 1;
}
unsigned end = pcurr - buf;
*buf = (T)((*buf & 7) + (end << 3));
return end+1;
}
//------------------------------------------------------------------------
/**
\brief Compute number of GAPs in bit-array
\param arr - array of BITs
\param len - array length
@ingroup gapfunc
*/
template<typename T>
unsigned bit_array_compute_gaps(const T* arr,
unsigned len)
{
unsigned gap_count = 1;
T prev = arr[0];
if (prev > 0)
++gap_count;
for (unsigned i = 1; i < len; ++i)
{
T curr = arr[i];
if (curr != prev + 1)
{
gap_count += 2;
}
prev = curr;
}
return gap_count;
}
//------------------------------------------------------------------------
/**
\brief Searches for the next 1 bit in the GAP block
\param buf - GAP buffer
\param nbit - bit index to start checking from.
\param prev - returns previously checked value
@ingroup gapfunc
*/
template<typename T> int gap_find_in_block(const T* buf,
unsigned nbit,
bm::id_t* prev)
{
BM_ASSERT(nbit < bm::gap_max_bits);
unsigned bitval;
unsigned gap_idx = bm::gap_bfind(buf, nbit, &bitval);
if (bitval) // positive block.
{
return 1;
}
register unsigned val = buf[gap_idx] + 1;
*prev += val - nbit;
return (val != bm::gap_max_bits); // no bug here.
}
/*!
\brief Set 1 bit in a block
@ingroup bitfunc
*/
BMFORCEINLINE void set_bit(unsigned* dest, unsigned bitpos)
{
unsigned nbit = unsigned(bitpos & bm::set_block_mask);
unsigned nword = unsigned(nbit >> bm::set_word_shift);
nbit &= bm::set_word_mask;
dest[nword] |= unsigned(1 << nbit);
}
/*!
\brief Test 1 bit in a block
@ingroup bitfunc
*/
BMFORCEINLINE unsigned test_bit(const unsigned* block, unsigned bitpos)
{
unsigned nbit = unsigned(bitpos & bm::set_block_mask);
unsigned nword = unsigned(nbit >> bm::set_word_shift);
nbit &= bm::set_word_mask;
return (block[nword] >> nbit) & 1u;
}
/*!
\brief Sets bits to 1 in the bitblock.
\param dest - Bitset buffer.
\param bitpos - Offset of the start bit.
\param bitcount - number of bits to set.
@ingroup bitfunc
*/
inline void or_bit_block(unsigned* dest,
unsigned bitpos,
unsigned bitcount)
{
unsigned nbit = unsigned(bitpos & bm::set_block_mask);
unsigned nword = unsigned(nbit >> bm::set_word_shift);
nbit &= bm::set_word_mask;
bm::word_t* word = dest + nword;
if (bitcount == 1) // special case (only 1 bit to set)
{
*word |= unsigned(1 << nbit);
return;
}
if (nbit) // starting position is not aligned
{
unsigned right_margin = nbit + bitcount;
// here we checking if we setting bits only in the current
// word. Example: 00111000000000000000000000000000 (32 bits word)
if (right_margin < 32)
{
unsigned mask =
block_set_table<true>::_right[nbit] &
block_set_table<true>::_left[right_margin-1];
*word |= mask;
return; // we are done
}
else
{
*word |= block_set_table<true>::_right[nbit];
bitcount -= 32 - nbit;
}
++word;
}
// now we are word aligned, lets find out how many words we
// can now turn ON using loop
for ( ;bitcount >= 32; bitcount -= 32)
{
*word++ = 0xffffffff;
}
if (bitcount)
{
*word |= block_set_table<true>::_left[bitcount-1];
}
}
/*!
\brief SUB (AND NOT) bit interval to 1 in the bitblock.
\param dest - Bitset buffer.
\param bitpos - Offset of the start bit.
\param bitcount - number of bits to set.
@ingroup bitfunc
*/
inline void sub_bit_block(unsigned* dest,
unsigned bitpos,
unsigned bitcount)
{
unsigned nbit = unsigned(bitpos & bm::set_block_mask);
unsigned nword = unsigned(nbit >> bm::set_word_shift);
nbit &= bm::set_word_mask;
bm::word_t* word = dest + nword;
if (bitcount == 1) // special case (only 1 bit to set)
{
*word &= ~unsigned(1 << nbit);
return;
}
if (nbit) // starting position is not aligned
{
unsigned right_margin = nbit + bitcount;
// here we checking if we setting bits only in the current
// word. Example: 00111000000000000000000000000000 (32 bits word)
if (right_margin < 32)
{
unsigned mask =
block_set_table<true>::_right[nbit] &
block_set_table<true>::_left[right_margin-1];
*word &= ~mask;
return; // we are done
}
else
{
*word &= ~block_set_table<true>::_right[nbit];
bitcount -= 32 - nbit;
}
++word;
}
// now we are word aligned, lets find out how many words we
// can now turn ON using loop
for ( ;bitcount >= 32; bitcount -= 32)
{
*word++ = 0;
}
if (bitcount)
{
*word &= ~block_set_table<true>::_left[bitcount-1];
}
}
/*!
\brief XOR bit interval to 1 in the bitblock.
\param dest - Bitset buffer.
\param bitpos - Offset of the start bit.
\param bitcount - number of bits to set.
@ingroup bitfunc
*/
inline void xor_bit_block(unsigned* dest,
unsigned bitpos,
unsigned bitcount)
{
unsigned nbit = unsigned(bitpos & bm::set_block_mask);
unsigned nword = unsigned(nbit >> bm::set_word_shift);
nbit &= bm::set_word_mask;
bm::word_t* word = dest + nword;
if (bitcount == 1) // special case (only 1 bit to set)
{
*word ^= unsigned(1 << nbit);
return;
}
if (nbit) // starting position is not aligned
{
unsigned right_margin = nbit + bitcount;
// here we checking if we setting bits only in the current
// word. Example: 00111000000000000000000000000000 (32 bits word)
if (right_margin < 32)
{
unsigned mask =
block_set_table<true>::_right[nbit] &
block_set_table<true>::_left[right_margin-1];
*word ^= mask;
return; // we are done
}
else
{
*word ^= block_set_table<true>::_right[nbit];
bitcount -= 32 - nbit;
}
++word;
}
// now we are word aligned, lets find out how many words we
// can now turn ON using loop
for ( ;bitcount >= 32; bitcount -= 32)
{
*word++ ^= 0xffffffff;
}
if (bitcount)
{
*word ^= block_set_table<true>::_left[bitcount-1];
}
}
/*!
\brief SUB (AND NOT) GAP block to bitblock.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc
*/
template<typename T>
void gap_sub_to_bitset(unsigned* BMRESTRICT dest, const T* BMRESTRICT buf)
{
const T* pend = buf + (*buf >> 3);
T b = *buf & 1;
++buf;
if (b) // Starts with 1
{
sub_bit_block(dest, 0, *buf + 1);
++buf;
}
for (++buf; buf <= pend; buf += 2)
{
T prev = *(buf-1);
BM_ASSERT(*buf > prev);
sub_bit_block(dest, prev + 1, *buf - prev);
}
}
/*!
\brief XOR GAP block to bitblock.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc
*/
template<typename T>
void gap_xor_to_bitset(unsigned* BMRESTRICT dest, const T* BMRESTRICT buf)
{
const T* pend = buf + (*buf >> 3);
T b = *buf & 1;
++buf;
if (b) // Starts with 1
{
xor_bit_block(dest, 0, *buf + 1);
++buf;
}
for (++buf; buf <= pend; buf += 2)
{
T prev = *(buf-1);
BM_ASSERT(*buf > prev);
xor_bit_block(dest, prev + 1, *buf - prev);
}
}
/*!
\brief Adds(OR) GAP block to bitblock.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
\param buf_len - GAP buffer length
@ingroup gapfunc
*/
template<typename T>
void gap_add_to_bitset_l(unsigned* dest, const T* buf, unsigned buf_len)
{
BM_ASSERT(buf_len);
register const T* pend = buf + buf_len;
T b = *buf & 1;
++buf;
if (b) // Starts with 1
{
or_bit_block(dest, 0, *buf + 1);
++buf;
}
for (++buf; buf <= pend; buf += 2)
{
T prev = *(buf-1);
BM_ASSERT(*buf > prev);
or_bit_block(dest, prev + 1, *buf - prev);
}
}
/*!
\brief Adds(OR) GAP block to bitblock.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc
*/
template<typename T>
void gap_add_to_bitset(unsigned* dest, const T* buf)
{
gap_add_to_bitset_l(dest, buf, *buf >> 3);
}
/*!
\brief ANDs GAP block to bitblock.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc
*/
template<typename T>
void gap_and_to_bitset(unsigned* dest, const T* buf)
{
register const T* pend = buf + (*buf >> 3);
T b = *buf & 1;
++buf;
if (!b ) // Starts with 0
{
// Instead of AND we can SUB 0 gaps here
sub_bit_block(dest, 0, *buf + 1);
++buf;
}
for (++buf; buf <= pend; buf += 2)
{
T prev = *(buf-1);
BM_ASSERT(*buf > prev);
sub_bit_block(dest, prev + 1, *buf - prev);
}
}
/*!
\brief Compute bitcount of bit block AND masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_and_count(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
const T* pcurr = buf;
const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
bm::id_t count = 0;
if (*buf & 1) // Starts with 1
{
count += bit_block_calc_count_range(block, 0, *pcurr);
++pcurr;
}
++pcurr; // now we are in GAP "1" again
for (;pcurr <= pend; pcurr += 2)
{
count += bit_block_calc_count_range(block, *(pcurr-1)+1, *pcurr);
}
return count;
}
/*!
\brief Bitcount test of bit block AND masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_and_any(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
bm::id_t count = 0;
if (*buf & 1) // Starts with 1
{
count += bit_block_any_range(block, 0, *pcurr);
if (count)
return count;
++pcurr;
}
++pcurr; // now we are in GAP "1" again
while (pcurr <= pend)
{
bm::id_t c = bit_block_any_range(block, *(pcurr-1)+1, *pcurr);
count += c;
if (count)
break;
pcurr += 2;
}
return count;
}
/*!
\brief Compute bitcount of bit block SUB masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_sub_count(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
bm::id_t count = 0;
if (!(*buf & 1)) // Starts with 0
{
count += bit_block_calc_count_range(block, 0, *pcurr);
++pcurr;
}
++pcurr; // now we are in GAP "0" again
for (;pcurr <= pend; pcurr+=2)
{
count += bit_block_calc_count_range(block, *(pcurr-1)+1, *pcurr);
}
return count;
}
/*!
\brief Compute bitcount test of bit block SUB masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_sub_any(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
bm::id_t count = 0;
if (!(*buf & 1)) // Starts with 0
{
count += bit_block_any_range(block, 0, *pcurr);
if (count)
return count;
++pcurr;
}
++pcurr; // now we are in GAP "0" again
for (;pcurr <= pend; pcurr+=2)
{
count += bit_block_any_range(block, *(pcurr-1)+1, *pcurr);
if (count)
return count;
}
return count;
}
/*!
\brief Compute bitcount of bit block XOR masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_xor_count(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
unsigned bitval = *buf & 1;
register bm::id_t count = bit_block_calc_count_range(block, 0, *pcurr);
if (bitval)
{
count = *pcurr + 1 - count;
}
for (bitval^=1, ++pcurr; pcurr <= pend; bitval^=1, ++pcurr)
{
T prev = *(pcurr-1)+1;
bm::id_t c = bit_block_calc_count_range(block, prev, *pcurr);
if (bitval) // 1 gap; means Result = Total_Bits - BitCount;
{
c = (*pcurr - prev + 1) - c;
}
count += c;
}
return count;
}
/*!
\brief Compute bitcount test of bit block XOR masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_xor_any(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
unsigned bitval = *buf & 1;
register bm::id_t count = bit_block_any_range(block, 0, *pcurr);
if (bitval)
{
count = *pcurr + 1 - count;
}
for (bitval^=1, ++pcurr; pcurr <= pend; bitval^=1, ++pcurr)
{
T prev = *(pcurr-1)+1;
bm::id_t c = bit_block_any_range(block, prev, *pcurr);
if (bitval) // 1 gap; means Result = Total_Bits - BitCount;
{
c = (*pcurr - prev + 1) - c;
}
count += c;
if (count)
return count;
}
return count;
}
/*!
\brief Compute bitcount of bit block OR masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_or_count(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
unsigned bitval = *buf & 1;
register bm::id_t count;
if (bitval)
{
count = *pcurr + 1;
}
else
{
count = bit_block_calc_count_range(block, 0, *pcurr);
}
for (bitval^=1, ++pcurr; pcurr <= pend; bitval^=1, ++pcurr)
{
T prev = *(pcurr-1)+1;
bm::id_t c;
if (bitval)
{
c = (*pcurr - prev + 1);
}
else
{
c = bit_block_calc_count_range(block, prev, *pcurr);
}
count += c;
}
return count;
}
/*!
\brief Compute bitcount test of bit block OR masked by GAP block.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc bitfunc
*/
template<typename T>
bm::id_t gap_bitset_or_any(const unsigned* block, const T* buf)
{
BM_ASSERT(block);
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
unsigned bitval = *buf & 1;
register bm::id_t count;
if (bitval)
{
count = *pcurr + 1;
}
else
{
count = bit_block_any_range(block, 0, *pcurr);
}
for (bitval^=1, ++pcurr; pcurr <= pend; bitval^=1, ++pcurr)
{
T prev = *(pcurr-1)+1;
bm::id_t c;
if (bitval)
{
c = (*pcurr - prev + 1);
}
else
{
c = bit_block_any_range(block, prev, *pcurr);
}
count += c;
if (count)
return count;
}
return count;
}
/*!
\brief Bitblock memset operation.
\param dst - destination block.
\param value - value to set.
@ingroup bitfunc
*/
inline
void bit_block_set(bm::word_t* BMRESTRICT dst, bm::word_t value)
{
//#ifdef BMVECTOPT
// VECT_SET_BLOCK(dst, dst + bm::set_block_size, value);
//#else
::memset(dst, value, bm::set_block_size * sizeof(bm::word_t));
//#endif
}
/*!
\brief GAP block to bitblock conversion.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc
*/
template<typename T>
void gap_convert_to_bitset(unsigned* dest, const T* buf)
{
bit_block_set(dest, 0);
gap_add_to_bitset(dest, buf);
}
/*!
\brief GAP block to bitblock conversion.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
@ingroup gapfunc
*/
template<typename T>
void gap_convert_to_bitset_l(unsigned* dest, const T* buf, unsigned buf_len)
{
bit_block_set(dest, 0);
gap_add_to_bitset_l(dest, buf, buf_len ? buf_len : *buf >> 3);
}
/*!
\brief GAP block to bitblock conversion.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
\param dest_size - length of the destination buffer.
@ingroup gapfunc
*/
template<typename T>
void gap_convert_to_bitset(unsigned* dest, const T* buf, unsigned dest_len)
{
::memset(dest, 0, dest_len * sizeof(unsigned));
gap_add_to_bitset(dest, buf);
}
/*!
\brief Smart GAP block to bitblock conversion.
Checks if GAP block is ALL-ZERO or ALL-ON. In those cases returns
pointer on special static bitblocks.
\param dest - bitblock buffer pointer.
\param buf - GAP buffer pointer.
\param set_max - max possible bitset length
@ingroup gapfunc
*/
template<typename T>
unsigned* gap_convert_to_bitset_smart(unsigned* dest,
const T* buf,
id_t set_max)
{
if (buf[1] == set_max - 1)
{
return (buf[0] & 1) ? FULL_BLOCK_ADDR : 0;
}
gap_convert_to_bitset(dest, buf);
return dest;
}
/*!
\brief Calculates sum of all words in GAP block. (For debugging purposes)
\note For debugging and testing ONLY.
\param buf - GAP buffer pointer.
\return Sum of all words.
@ingroup gapfunc
*/
template<typename T> unsigned gap_control_sum(const T* buf)
{
unsigned end = *buf >> 3;
register const T* pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
if (*buf & 1) // Starts with 1
{
++pcurr;
}
++pcurr; // now we are in GAP "1" again
while (pcurr <= pend)
{
BM_ASSERT(*pcurr > *(pcurr-1));
pcurr += 2;
}
return buf[end];
}
/*!
\brief Sets all bits to 0 or 1 (GAP)
\param buf - GAP buffer pointer.
\param set_max - max possible bitset length
@ingroup gapfunc
*/
template<class T> void gap_set_all(T* buf,
unsigned set_max,
unsigned value)
{
BM_ASSERT(value == 0 || value == 1);
*buf = (T)((*buf & 6u) + (1u << 3) + value);
*(++buf) = (T)(set_max - 1);
}
/*!
\brief Init gap block so it has block in it (can be whole block)
\param buf - GAP buffer pointer.
\param from - one block start
\param to - one block end
\param value - (block value)1 or 0
\param set_max - max possible bitset length
@ingroup gapfunc
*/
template<class T>
void gap_init_range_block(T* buf,
T from,
T to,
T value,
unsigned set_max)
{
BM_ASSERT(value == 0 || value == 1);
unsigned gap_len;
if (from == 0)
{
if (to == set_max - 1)
{
gap_set_all(buf, set_max, value);
}
else
{
gap_len = 2;
buf[1] = to;
buf[2] = (T)(set_max - 1);
buf[0] = (T)((*buf & 6u) + (gap_len << 3) + value);
}
return;
}
// from != 0
value = !value;
if (to == set_max - 1)
{
gap_len = 2;
buf[1] = (T)(from - 1);
buf[2] = (T)(set_max - 1);
}
else
{
gap_len = 3;
buf[1] = from - 1;
buf[2] = (T) to;
buf[3] = (T)(set_max - 1);
}
buf[0] = (T)((*buf & 6u) + (gap_len << 3) + value);
}
/*!
\brief Inverts all bits in the GAP buffer.
\param buf - GAP buffer pointer.
@ingroup gapfunc
*/
template<typename T> void gap_invert(T* buf)
{
*buf ^= 1;
}
/*!
\brief Temporary inverts all bits in the GAP buffer.
In this function const-ness of the buffer means nothing.
Calling this function again restores the status of the buffer.
\param buf - GAP buffer pointer. (Buffer IS changed)
@ingroup gapfunc
*/
/*
template<typename T> void gap_temp_invert(const T* buf)
{
T* buftmp = const_cast<T*>(buf);
*buftmp ^= 1;
}
*/
/*!
\brief Checks if GAP block is all-zero.
\param buf - GAP buffer pointer.
\param set_max - max possible bitset length
\returns true if all-zero.
@ingroup gapfunc
*/
template<typename T>
bool gap_is_all_zero(const T* buf, unsigned set_max)
{
return (((*buf & 1)==0) && (*(++buf) == set_max - 1));
}
/*!
\brief Checks if GAP block is all-one.
\param buf - GAP buffer pointer.
\param set_max - max possible bitset length
\returns true if all-one.
@ingroup gapfunc
*/
template<typename T>
bool gap_is_all_one(const T* buf, unsigned set_max)
{
return ((*buf & 1) && (*(++buf) == set_max - 1));
}
/*!
\brief Returs GAP block length.
\param buf - GAP buffer pointer.
\returns GAP block length.
@ingroup gapfunc
*/
template<typename T> T gap_length(const T* buf)
{
return (*buf >> 3) + 1;
}
/*!
\brief Returs GAP block capacity.
\param buf - GAP buffer pointer.
\returns GAP block capacity.
@ingroup gapfunc
*/
template<typename T>
unsigned gap_capacity(const T* buf, const T* glevel_len)
{
return glevel_len[(*buf >> 1) & 3];
}
/*!
\brief Returs GAP block capacity limit.
\param buf - GAP buffer pointer.
\param glevel_len - GAP lengths table (gap_len_table)
\returns GAP block limit.
@ingroup gapfunc
*/
template<typename T>
unsigned gap_limit(const T* buf, const T* glevel_len)
{
return glevel_len[(*buf >> 1) & 3]-4;
}
/*!
\brief Returs GAP blocks capacity level.
\param buf - GAP buffer pointer.
\returns GAP block capacity level.
@ingroup gapfunc
*/
template<typename T> unsigned gap_level(const T* buf)
{
return (*buf >> 1) & 3;
}
/*!
\brief Sets GAP block capacity level.
\param buf - GAP buffer pointer.
\param level new GAP block capacity level.
@ingroup gapfunc
*/
template<typename T>
void set_gap_level(T* buf, unsigned level)
{
BM_ASSERT(level < bm::gap_levels);
*buf = ((level & 3) << 1) | (*buf & 1) | (*buf & ~7);
}
/*!
\brief Calculates GAP block capacity level.
\param len - GAP buffer length.
\param glevel_len - GAP lengths table
\return GAP block capacity level.
-1 if block does not fit any level.
@ingroup gapfunc
*/
template<typename T>
inline int gap_calc_level(int len, const T* glevel_len)
{
if (len <= (glevel_len[0]-4)) return 0;
if (len <= (glevel_len[1]-4)) return 1;
if (len <= (glevel_len[2]-4)) return 2;
if (len <= (glevel_len[3]-4)) return 3;
BM_ASSERT(bm::gap_levels == 4);
return -1;
}
/*! @brief Returns number of free elements in GAP block array.
Difference between GAP block capacity on this level and actual GAP length.
@param buf - GAP buffer pointer
@parma glevel_len - GAP lengths table
@return Number of free GAP elements
@ingroup gapfunc
*/
template<typename T>
inline unsigned gap_free_elements(const T* buf, const T* glevel_len)
{
unsigned len = gap_length(buf);
unsigned capacity = gap_capacity(buf, glevel_len);
return capacity - len;
}
/*!
\brief Lexicographical comparison of BIT buffers.
\param buf1 - First buffer pointer.
\param buf2 - Second buffer pointer.
\param len - Buffer length in elements (T).
\return <0 - less, =0 - equal, >0 - greater.
@ingroup bitfunc
*/
template<typename T>
int bitcmp(const T* buf1, const T* buf2, unsigned len)
{
BM_ASSERT(len);
const T* pend1 = buf1 + len;
do
{
T w1 = *buf1++;
T w2 = *buf2++;
T diff = w1 ^ w2;
if (diff)
{
return (w1 & diff & -diff) ? 1 : -1;
}
} while (buf1 < pend1);
return 0;
}
/*!
\brief Converts bit block to GAP.
\param dest - Destinatio GAP buffer.
\param src - Source bitblock buffer.
\param bits - Number of bits to convert.
\param dest_len - length of the dest. buffer.
\return New length of GAP block or 0 if conversion failed
(insufficicent space).
@ingroup gapfunc
*/
template<typename T>
unsigned bit_convert_to_gap(T* BMRESTRICT dest,
const unsigned* BMRESTRICT src,
bm::id_t bits,
unsigned dest_len)
{
register T* BMRESTRICT pcurr = dest;
T* BMRESTRICT end = dest + dest_len;
register int bitval = (*src) & 1;
// *pcurr |= bitval;
*pcurr = (T)bitval;
++pcurr;
*pcurr = 0;
register unsigned bit_idx = 0;
register int bitval_next;
unsigned val = *src;
do
{
// We can fast pace if *src == 0 or *src = 0xffffffff
while (val == 0 || val == 0xffffffff)
{
bitval_next = val ? 1 : 0;
if (bitval != bitval_next)
{
*pcurr++ = (T)(bit_idx-1);
BM_ASSERT((pcurr-1) == (dest+1) || *(pcurr-1) > *(pcurr-2));
if (pcurr >= end)
{
return 0; // OUT of memory
}
bitval = bitval_next;
}
bit_idx += sizeof(*src) * 8;
if (bit_idx >= bits)
{
goto complete;
}
++src;
val = *src;
}
register unsigned mask = 1;
while (mask)
{
// Now plain bitshifting. TODO: Optimization wanted.
bitval_next = val & mask ? 1 : 0;
if (bitval != bitval_next)
{
*pcurr++ = (T)(bit_idx-1);
BM_ASSERT((pcurr-1) == (dest+1) || *(pcurr-1) > *(pcurr-2));
bitval = bitval_next;
if (pcurr >= end)
{
return 0; // OUT of memory
}
}
mask <<= 1;
++bit_idx;
} // while mask
if (bit_idx >= bits)
{
goto complete;
}
++src;
val = *src;
} while(1);
complete:
*pcurr = (T)(bit_idx-1);
unsigned len = (unsigned)(pcurr - dest);
*dest = (T)((*dest & 7) + (len << 3));
return len;
}
/*!
\brief Iterate gap block as delta-bits with a functor
@ingroup gapfunc
*/
template<class T, class F>
void for_each_gap_dbit(const T* buf, F& func)
{
const T* pcurr = buf;
const T* pend = pcurr + (*pcurr >> 3);
++pcurr;
unsigned prev = 0;
unsigned first_inc;
if (*buf & 1)
{
first_inc = 0;
unsigned to = *pcurr;
for (unsigned i = 0; i <= to; ++i)
{
func(1);
}
prev = to;
++pcurr;
}
else
{
first_inc = 1;
}
++pcurr; // set GAP to 1
while (pcurr <= pend)
{
unsigned from = *(pcurr-1)+1;
unsigned to = *pcurr;
if (first_inc)
{
func(from - prev + first_inc);
first_inc = 0;
}
else
{
func(from - prev);
}
for (unsigned i = from+1; i <= to; ++i)
{
func(1);
}
prev = to;
pcurr += 2; // jump to the next positive GAP
}
}
/*!
\brief Convert gap block into array of ints corresponding to 1 bits
@ingroup gapfunc
*/
template<typename D, typename T>
D gap_convert_to_arr(D* BMRESTRICT dest,
const T* BMRESTRICT buf,
unsigned dest_len,
bool invert = false)
{
register const T* BMRESTRICT pcurr = buf;
register const T* pend = pcurr + (*pcurr >> 3);
D* BMRESTRICT dest_curr = dest;
++pcurr;
int bitval = (*buf) & 1;
if (invert)
bitval = !bitval; // invert the GAP buffer
if (bitval)
{
if (unsigned(*pcurr + 1) >= dest_len)
return 0; // insufficient space
dest_len -= *pcurr;
T to = *pcurr;
for (T i = 0; ;++i)
{
*dest_curr++ = i;
if (i == to) break;
}
++pcurr;
}
++pcurr; // set GAP to 1
while (pcurr <= pend)
{
unsigned pending = *pcurr - *(pcurr-1);
if (pending >= dest_len)
return 0;
dest_len -= pending;
T from = *(pcurr-1)+1;
T to = *pcurr;
for (T i = from; ;++i)
{
*dest_curr++ = i;
if (i == to) break;
}
pcurr += 2; // jump to the next positive GAP
}
return (D) (dest_curr - dest);
}
/*!
@brief Bitcount for bit string
Function calculates number of 1 bits in the given array of words.
Make sure the addresses are aligned.
@ingroup bitfunc
*/
inline
bm::id_t bit_block_calc_count(const bm::word_t* block,
const bm::word_t* block_end)
{
BM_ASSERT(block < block_end);
bm::id_t count = 0;
#ifdef BMVECTOPT
count = VECT_BITCOUNT(block, block_end);
#else
#ifdef BM64OPT
// 64-bit optimized algorithm. No sparse vect opt.
// instead it uses 4-way parallel pipelined version
const bm::id64_t* b1 = (bm::id64_t*) block;
const bm::id64_t* b2 = (bm::id64_t*) block_end;
do
{
count += bitcount64_4way(b1[0], b1[1], b1[2], b1[3]);
b1 += 4;
} while (b1 < b2);
#else
// For 32 bit code the fastest method is
// to use bitcount table for each byte in the block.
// As optimization for sparse bitsets used bits accumulator
// to collect ON bits using bitwise OR.
bm::word_t acc = *block++;
do
{
bm::word_t in = *block++;
bm::word_t acc_prev = acc;
acc |= in;
if (acc_prev &= in) // accumulator miss: counting bits
{
BM_INCWORD_BITCOUNT(count, acc);
acc = acc_prev;
}
} while (block < block_end);
BM_INCWORD_BITCOUNT(count, acc); // count-in remaining accumulator
#endif
#endif
return count;
}
/*!
Function calculates number of times when bit value changed
(1-0 or 0-1).
For 001 result is 2
010 - 3
011 - 2
111 - 1
@ingroup bitfunc
*/
inline
bm::id_t bit_count_change(bm::word_t w)
{
unsigned count = 1;
w ^= (w >> 1);
BM_INCWORD_BITCOUNT(count, w);
count -= (w >> ((sizeof(w) * 8) - 1));
return count;
}
/*!
Function calculates number of times when bit value changed
@internal
*/
inline
void bit_count_change32(const bm::word_t* block,
const bm::word_t* block_end,
unsigned* bit_count,
unsigned* gap_count)
{
BM_ASSERT(block < block_end);
BM_ASSERT(bit_count);
BM_ASSERT(gap_count);
*gap_count = 1;
*bit_count = 0;
bm::word_t w, w0, w_prev, w_l;
w = w0 = *block;
BM_INCWORD_BITCOUNT(*bit_count, w);
const int w_shift = sizeof(w) * 8 - 1;
w ^= (w >> 1);
BM_INCWORD_BITCOUNT(*gap_count, w);
*gap_count -= (w_prev = (w0 >> w_shift)); // negative value correction
for (++block ;block < block_end; ++block)
{
w = w0 = *block;
++(*gap_count);
if (!w)
{
*gap_count -= !w_prev;
w_prev = 0;
}
else
{
BM_INCWORD_BITCOUNT(*bit_count, w);
w ^= (w >> 1);
BM_INCWORD_BITCOUNT(*gap_count, w);
w_l = w0 & 1;
*gap_count -= (w0 >> w_shift); // negative value correction
*gap_count -= !(w_prev ^ w_l); // word border correction
w_prev = (w0 >> w_shift);
}
} // for
}
/*!
Function calculates number of times when bit value changed
(1-0 or 0-1) in the bit block.
Also calulates number of bits ON.
@param bit_count - OUT total number of bits ON
@return number of 1-0, 0-1 transitions
@ingroup bitfunc
*/
inline
bm::id_t bit_block_calc_count_change(const bm::word_t* block,
const bm::word_t* block_end,
unsigned* bit_count)
{
#if defined(BMSSE2OPT) || defined(BMSSE42OPT)
#ifdef BMSSE42OPT
return sse4_bit_block_calc_count_change(
(const __m128i*)block, (const __m128i*)block_end, bit_count);
#else
# ifdef BMSSE2OPT
return sse2_bit_block_calc_count_change(
(const __m128i*)block, (const __m128i*)block_end, bit_count);
# endif
#endif
#else // non-SSE code
BM_ASSERT(block < block_end);
BM_ASSERT(bit_count);
#ifdef BM64OPT
bm::id_t count = 1;
*bit_count = 0;
// 64-bit optimized algorithm.
const bm::id64_t* b1 = (bm::id64_t*) block;
const bm::id64_t* b2 = (bm::id64_t*) block_end;
bm::id64_t w, w0, w_prev, w_l;
w = w0 = *b1;
*bit_count = word_bitcount64(w);
const int w_shift = sizeof(w) * 8 - 1;
w ^= (w >> 1);
count += word_bitcount64(w);
count -= (w_prev = (w0 >> w_shift)); // negative value correction
for (++b1 ;b1 < b2; ++b1)
{
w = w0 = *b1;
++count;
if (!w)
{
count -= !w_prev;
w_prev = 0;
}
else
{
*bit_count += word_bitcount64(w);
w ^= (w >> 1);
count += word_bitcount64(w);
w_l = w0 & 1;
count -= (w0 >> w_shift); // negative value correction
count -= !(w_prev ^ w_l); // word border correction
w_prev = (w0 >> w_shift);
}
} // for
return count;
#else
unsigned gap_count;
bit_count_change32(block, block_end, bit_count, &gap_count);
return gap_count;
#endif
#endif
}
/*!
Function calculates number of 1 bits in the given array of words in
the range between left anf right bits (borders included)
Make sure the addresses are aligned.
@ingroup bitfunc
*/
inline
bm::id_t bit_block_calc_count_range(const bm::word_t* block,
bm::word_t left,
bm::word_t right)
{
BM_ASSERT(left <= right);
unsigned nword, nbit;
nbit = left & bm::set_word_mask;
const bm::word_t* word =
block + (nword = unsigned(left >> bm::set_word_shift));
if (left == right) // special case (only 1 bit to check)
{
return (*word >> nbit) & 1;
}
bm::id_t count = 0;
unsigned acc;
unsigned bitcount = right - left + 1;
if (nbit) // starting position is not aligned
{
unsigned right_margin = nbit + (right - left);
if (right_margin < 32)
{
unsigned mask =
block_set_table<true>::_right[nbit] &
block_set_table<true>::_left[right_margin];
acc = *word & mask;
BM_INCWORD_BITCOUNT(count, acc);
return count;
}
else
{
acc = *word & block_set_table<true>::_right[nbit];
BM_INCWORD_BITCOUNT(count, acc);
bitcount -= 32 - nbit;
}
++word;
}
// now when we are word aligned, we can count bits the usual way
for ( ;bitcount >= 32; bitcount -= 32)
{
acc = *word++;
BM_INCWORD_BITCOUNT(count, acc);
}
if (bitcount) // we have a tail to count
{
acc = (*word) & block_set_table<true>::_left[bitcount-1];
BM_INCWORD_BITCOUNT(count, acc);
}
return count;
}
/*!
Function calculates if there is any number of 1 bits
in the given array of words in the range between left anf right bits
(borders included). Make sure the addresses are aligned.
@ingroup bitfunc
*/
inline
bm::id_t bit_block_any_range(const bm::word_t* block,
bm::word_t left,
bm::word_t right)
{
BM_ASSERT(left <= right);
unsigned nbit = left; // unsigned(left & bm::set_block_mask);
unsigned nword = unsigned(nbit >> bm::set_word_shift);
nbit &= bm::set_word_mask;
const bm::word_t* word = block + nword;
if (left == right) // special case (only 1 bit to check)
{
return (*word >> nbit) & 1;
}
unsigned acc;
unsigned bitcount = right - left + 1;
if (nbit) // starting position is not aligned
{
unsigned right_margin = nbit + (right - left);
if (right_margin < 32)
{
unsigned mask =
block_set_table<true>::_right[nbit] &
block_set_table<true>::_left[right_margin];
acc = *word & mask;
return acc;
}
else
{
acc = *word & block_set_table<true>::_right[nbit];
if (acc)
return acc;
bitcount -= 32 - nbit;
}
++word;
}
// now when we are word aligned, we can check bits the usual way
for ( ;bitcount >= 32; bitcount -= 32)
{
acc = *word++;
if (acc)
return acc;
}
if (bitcount) // we have a tail to count
{
acc = (*word) & block_set_table<true>::_left[bitcount-1];
if (acc)
return acc;
}
return 0;
}
// ----------------------------------------------------------------------
/*! Function inverts block of bits
@ingroup bitfunc
*/
template<typename T> void bit_invert(T* start, T* end)
{
#ifdef BMVECTOPT
VECT_INVERT_ARR(start, end);
#else
do
{
start[0] = ~start[0];
start[1] = ~start[1];
start[2] = ~start[2];
start[3] = ~start[3];
start+=4;
} while (start < end);
#endif
}
// ----------------------------------------------------------------------
/*! @brief Returns "true" if all bits in the block are 1
@ingroup bitfunc
*/
inline bool is_bits_one(const bm::wordop_t* start,
const bm::wordop_t* end)
{
do
{
bm::wordop_t tmp =
start[0] & start[1] & start[2] & start[3];
if (tmp != bm::all_bits_mask)
return false;
start += 4;
} while (start < end);
return true;
}
// ----------------------------------------------------------------------
/*! @brief Returns "true" if all bits in the block are 0
@ingroup bitfunc
*/
inline bool bit_is_all_zero(const bm::wordop_t* start,
const bm::wordop_t* end)
{
do
{
bm::wordop_t tmp =
start[0] | start[1] | start[2] | start[3];
if (tmp)
return false;
start += 4;
} while (start < end);
return true;
}
// ----------------------------------------------------------------------
// GAP blocks manipulation functions:
/*! \brief GAP and functor */
BMFORCEINLINE unsigned and_op(unsigned v1, unsigned v2)
{
return v1 & v2;
}
/*! \brief GAP xor functor */
BMFORCEINLINE unsigned xor_op(unsigned v1, unsigned v2)
{
return v1 ^ v2;
}
/*! \brief GAP or functor */
BMFORCEINLINE unsigned or_op(unsigned v1, unsigned v2)
{
return v1 | v2;
}
/*! \brief GAP or functor */
BMFORCEINLINE unsigned sub_op(unsigned v1, unsigned v2)
{
return v1 & ~v2;
}
/*!
\brief GAP AND operation.
Function performs AND logical operation on gap vectors.
If possible function put the result into vect1 and returns this
pointer. Otherwise result is put into tmp_buf, which should be
twice of the vector size.
\param vect1 - operand 1
\param vect2 - operand 2
\param tmp_buf - pointer on temporary buffer
\param dsize - out size of the destination
\return Result pointer (tmp_buf OR vect1)
@ingroup gapfunc
*/
BMFORCEINLINE
gap_word_t* gap_operation_and(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2,
gap_word_t* BMRESTRICT tmp_buf,
unsigned& dsize)
{
gap_buff_op(tmp_buf, vect1, 0, vect2, 0, and_op, dsize);
return tmp_buf;
}
/*!
\brief GAP AND operation test.
Function performs AND logical operation on gap vectors.
If possible function put the result into vect1 and returns this
pointer. Otherwise result is put into tmp_buf, which should be
twice of the vector size.
\param vect1 - operand 1
\param vect2 - operand 2
\return non zero value if operation returns any 1 bit
@ingroup gapfunc
*/
BMFORCEINLINE
unsigned gap_operation_any_and(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2)
{
return gap_buff_any_op(vect1, 0, vect2, 0, and_op);
}
/*!
\brief GAP bitcount AND operation test.
\param vect1 - operand 1
\param vect2 - operand 2
\return bitcount of vect1 AND vect2
@ingroup gapfunc
*/
BMFORCEINLINE
unsigned gap_count_and(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2)
{
return gap_buff_count_op(vect1, vect2, and_op);
}
/*!
\brief GAP XOR operation.
Function performs XOR logical operation on gap vectors.
If possible function put the result into vect1 and returns this
pointer. Otherwise result is put into tmp_buf, which should be
twice of the vector size.
\param vect1 - operand 1
\param vect2 - operand 2
\param tmp_buf - pointer on temporary buffer
\param dsize - out destination size
\return Result pointer (tmp_buf)
@ingroup gapfunc
*/
BMFORCEINLINE
gap_word_t* gap_operation_xor(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2,
gap_word_t* BMRESTRICT tmp_buf,
unsigned& dsize)
{
gap_buff_op(tmp_buf, vect1, 0, vect2, 0, bm::xor_op, dsize);
return tmp_buf;
}
/*!
\brief GAP XOR operation test.
Function performs AND logical operation on gap vectors.
If possible function put the result into vect1 and returns this
pointer. Otherwise result is put into tmp_buf, which should be
twice of the vector size.
\param vect1 - operand 1
\param vect2 - operand 2
\return non zero value if operation returns any 1 bit
@ingroup gapfunc
*/
BMFORCEINLINE
unsigned gap_operation_any_xor(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2)
{
return gap_buff_any_op(vect1, 0, vect2, 0, bm::xor_op);
}
/*!
\brief GAP bitcount XOR operation test.
\param vect1 - operand 1
\param vect2 - operand 2
\return bitcount of vect1 XOR vect2
@ingroup gapfunc
*/
BMFORCEINLINE
unsigned gap_count_xor(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2)
{
return gap_buff_count_op(vect1, vect2, bm::xor_op);
}
/*!
\brief GAP OR operation.
Function performs OR logical oparation on gap vectors.
If possible function put the result into vect1 and returns this
pointer. Otherwise result is put into tmp_buf, which should be
twice of the vector size.
\param vect1 - operand 1
\param vect2 - operand 2
\param tmp_buf - pointer on temporary buffer
\param dsize - out destination size
\return Result pointer (tmp_buf)
@ingroup gapfunc
*/
inline
gap_word_t* gap_operation_or(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2,
gap_word_t* BMRESTRICT tmp_buf,
unsigned& dsize)
{
gap_buff_op(tmp_buf, vect1, 1, vect2, 1, bm::and_op, dsize);
gap_invert(tmp_buf);
return tmp_buf;
}
/*!
\brief GAP bitcount OR operation test.
\param vect1 - operand 1
\param vect2 - operand 2
\return bitcount of vect1 OR vect2
@ingroup gapfunc
*/
BMFORCEINLINE
unsigned gap_count_or(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2)
{
return gap_buff_count_op(vect1, vect2, bm::or_op);
}
/*!
\brief GAP SUB (AND NOT) operation.
Function performs SUB logical operation on gap vectors.
If possible function put the result into vect1 and returns this
pointer. Otherwise result is put into tmp_buf, which should be
twice of the vector size.
\param vect1 - operand 1
\param vect2 - operand 2
\param tmp_buf - pointer on temporary buffer
\param dsize - out destination size
\return Result pointer (tmp_buf)
@ingroup gapfunc
*/
inline gap_word_t* gap_operation_sub(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2,
gap_word_t* BMRESTRICT tmp_buf,
unsigned& dsize)
{
gap_buff_op(tmp_buf, vect1, 0, vect2, 1, and_op, dsize);
return tmp_buf;
}
/*!
\brief GAP SUB operation test.
Function performs AND logical operation on gap vectors.
If possible function put the result into vect1 and returns this
pointer. Otherwise result is put into tmp_buf, which should be
twice of the vector size.
\param vect1 - operand 1
\param vect2 - operand 2
\return non zero value if operation returns any 1 bit
@ingroup gapfunc
*/
BMFORCEINLINE
unsigned gap_operation_any_sub(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2)
{
return gap_buff_any_op(vect1, 0, vect2, 1, bm::and_op);
}
/*!
\brief GAP bitcount SUB (AND NOT) operation test.
\param vect1 - operand 1
\param vect2 - operand 2
\return bitcount of vect1 SUB (AND NOT) vect2
@ingroup gapfunc
*/
BMFORCEINLINE
unsigned gap_count_sub(const gap_word_t* BMRESTRICT vect1,
const gap_word_t* BMRESTRICT vect2)
{
return gap_buff_count_op(vect1, vect2, bm::sub_op);
}
// ----------------------------------------------------------------------
// BIT blocks manipulation functions:
/*!
\brief Bitblock copy operation.
\param dst - destination block.
\param src - source block.
@ingroup bitfunc
*/
inline
void bit_block_copy(bm::word_t* BMRESTRICT dst, const bm::word_t* BMRESTRICT src)
{
#ifdef BMVECTOPT
VECT_COPY_BLOCK(dst, src, src + bm::set_block_size);
#else
::memcpy(dst, src, bm::set_block_size * sizeof(bm::word_t));
#endif
}
/*!
\brief Plain bitblock AND operation.
Function does not analyse availability of source and destination blocks.
\param dst - destination block.
\param src - source block.
@ingroup bitfunc
*/
inline
void bit_block_and(bm::word_t* BMRESTRICT dst, const bm::word_t* BMRESTRICT src)
{
#ifdef BMVECTOPT
VECT_AND_ARR(dst, src, src + bm::set_block_size);
#else
const bm::wordop_t* BMRESTRICT wrd_ptr = (wordop_t*)src;
const bm::wordop_t* BMRESTRICT wrd_end = (wordop_t*)(src + bm::set_block_size);
bm::wordop_t* BMRESTRICT dst_ptr = (wordop_t*)dst;
do
{
dst_ptr[0] &= wrd_ptr[0];
dst_ptr[1] &= wrd_ptr[1];
dst_ptr[2] &= wrd_ptr[2];
dst_ptr[3] &= wrd_ptr[3];
dst_ptr+=4;
wrd_ptr+=4;
} while (wrd_ptr < wrd_end);
#endif
}
/*!
\brief Function ANDs two bitblocks and computes the bitcount.
Function does not analyse availability of source blocks.
\param src1 - first bit block
\param src1_end - first bit block end
\param src2 - second bit block
@ingroup bitfunc
*/
inline
unsigned bit_block_and_count(const bm::word_t* src1,
const bm::word_t* src1_end,
const bm::word_t* src2)
{
unsigned count;
#ifdef BMVECTOPT
count = VECT_BITCOUNT_AND(src1, src1_end, src2);
#else
count = 0;
# ifdef BM64OPT
const bm::id64_t* b1 = (bm::id64_t*) src1;
const bm::id64_t* b1_end = (bm::id64_t*) src1_end;
const bm::id64_t* b2 = (bm::id64_t*) src2;
do
{
count += bitcount64_4way(b1[0] & b2[0],
b1[1] & b2[1],
b1[2] & b2[2],
b1[3] & b2[3]);
b1 += 4;
b2 += 4;
} while (b1 < b1_end);
# else
do
{
BM_INCWORD_BITCOUNT(count, src1[0] & src2[0]);
BM_INCWORD_BITCOUNT(count, src1[1] & src2[1]);
BM_INCWORD_BITCOUNT(count, src1[2] & src2[2]);
BM_INCWORD_BITCOUNT(count, src1[3] & src2[3]);
src1+=4;
src2+=4;
} while (src1 < src1_end);
# endif
#endif
return count;
}
/*!
\brief Function ANDs two bitblocks and tests for any bit.
Function does not analyse availability of source blocks.
\param src1 - first bit block
\param src1_end - first bit block end
\param src2 - second bit block
@ingroup bitfunc
*/
inline
unsigned bit_block_and_any(const bm::word_t* src1,
const bm::word_t* src1_end,
const bm::word_t* src2)
{
unsigned count = 0;
do
{
count = (src1[0] & src2[0]) |
(src1[1] & src2[1]) |
(src1[2] & src2[2]) |
(src1[3] & src2[3]);
src1+=4; src2+=4;
} while ((src1 < src1_end) && (count == 0));
return count;
}
/*!
\brief Function XORs two bitblocks and computes the bitcount.
Function does not analyse availability of source blocks.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
@ingroup bitfunc
*/
inline
unsigned bit_block_xor_count(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
unsigned count;
#ifdef BMVECTOPT
count = VECT_BITCOUNT_XOR(src1, src1_end, src2);
#else
count = 0;
# ifdef BM64OPT
const bm::id64_t* b1 = (bm::id64_t*) src1;
const bm::id64_t* b1_end = (bm::id64_t*) src1_end;
const bm::id64_t* b2 = (bm::id64_t*) src2;
do
{
count += bitcount64_4way(b1[0] ^ b2[0],
b1[1] ^ b2[1],
b1[2] ^ b2[2],
b1[3] ^ b2[3]);
b1 += 4;
b2 += 4;
} while (b1 < b1_end);
# else
do
{
BM_INCWORD_BITCOUNT(count, src1[0] ^ src2[0]);
BM_INCWORD_BITCOUNT(count, src1[1] ^ src2[1]);
BM_INCWORD_BITCOUNT(count, src1[2] ^ src2[2]);
BM_INCWORD_BITCOUNT(count, src1[3] ^ src2[3]);
src1+=4;
src2+=4;
} while (src1 < src1_end);
# endif
#endif
return count;
}
/*!
\brief Function XORs two bitblocks and and tests for any bit.
Function does not analyse availability of source blocks.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
@ingroup bitfunc
*/
inline
unsigned bit_block_xor_any(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
unsigned count = 0;
do
{
count = (src1[0] ^ src2[0]) |
(src1[1] ^ src2[1]) |
(src1[2] ^ src2[2]) |
(src1[3] ^ src2[3]);
src1+=4; src2+=4;
} while ((src1 < src1_end) && (count == 0));
return count;
}
/*!
\brief Function SUBs two bitblocks and computes the bitcount.
Function does not analyse availability of source blocks.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
@ingroup bitfunc
*/
inline
unsigned bit_block_sub_count(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
unsigned count;
#ifdef BMVECTOPT
count = VECT_BITCOUNT_SUB(src1, src1_end, src2);
#else
count = 0;
# ifdef BM64OPT
const bm::id64_t* b1 = (bm::id64_t*) src1;
const bm::id64_t* b1_end = (bm::id64_t*) src1_end;
const bm::id64_t* b2 = (bm::id64_t*) src2;
do
{
count += bitcount64_4way(b1[0] & ~b2[0],
b1[1] & ~b2[1],
b1[2] & ~b2[2],
b1[3] & ~b2[3]);
b1 += 4;
b2 += 4;
} while (b1 < b1_end);
# else
do
{
BM_INCWORD_BITCOUNT(count, src1[0] & ~src2[0]);
BM_INCWORD_BITCOUNT(count, src1[1] & ~src2[1]);
BM_INCWORD_BITCOUNT(count, src1[2] & ~src2[2]);
BM_INCWORD_BITCOUNT(count, src1[3] & ~src2[3]);
src1+=4;
src2+=4;
} while (src1 < src1_end);
# endif
#endif
return count;
}
/*!
\brief Function SUBs two bitblocks and and tests for any bit.
Function does not analyse availability of source blocks.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
@ingroup bitfunc
*/
inline
unsigned bit_block_sub_any(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
unsigned count = 0;
do
{
count = (src1[0] & ~src2[0]) |
(src1[1] & ~src2[1]) |
(src1[2] & ~src2[2]) |
(src1[3] & ~src2[3]);
src1+=4; src2+=4;
} while ((src1 < src1_end) && (count == 0));
return count;
}
/*!
\brief Function ORs two bitblocks and computes the bitcount.
Function does not analyse availability of source blocks.
\param src1 - first bit block
\param src1_end - first block end
\param src2 - second bit block.
@ingroup bitfunc
*/
inline
unsigned bit_block_or_count(const bm::word_t* src1,
const bm::word_t* src1_end,
const bm::word_t* src2)
{
unsigned count;
#ifdef BMVECTOPT
count = VECT_BITCOUNT_OR(src1, src1_end, src2);
#else
count = 0;
# ifdef BM64OPT
const bm::id64_t* b1 = (bm::id64_t*) src1;
const bm::id64_t* b1_end = (bm::id64_t*) src1_end;
const bm::id64_t* b2 = (bm::id64_t*) src2;
do
{
count += bitcount64_4way(b1[0] | b2[0],
b1[1] | b2[1],
b1[2] | b2[2],
b1[3] | b2[3]);
b1 += 4;
b2 += 4;
} while (b1 < b1_end);
# else
do
{
BM_INCWORD_BITCOUNT(count, src1[0] | src2[0]);
BM_INCWORD_BITCOUNT(count, src1[1] | src2[1]);
BM_INCWORD_BITCOUNT(count, src1[2] | src2[2]);
BM_INCWORD_BITCOUNT(count, src1[3] | src2[3]);
src1+=4;
src2+=4;
} while (src1 < src1_end);
# endif
#endif
return count;
}
/*!
\brief Function ORs two bitblocks and and tests for any bit.
Function does not analyse availability of source blocks.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
@ingroup bitfunc
*/
inline
unsigned bit_block_or_any(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
unsigned count = 0;
do
{
count = (src1[0] | src2[0]) |
(src1[1] | src2[1]) |
(src1[2] | src2[2]) |
(src1[3] | src2[3]);
src1+=4; src2+=4;
} while ((src1 < src1_end) && (count == 0));
return count;
}
/*!
\brief bitblock AND operation.
\param dst - destination block.
\param src - source block.
\returns pointer on destination block.
If returned value equal to src means that block mutation requested.
NULL is valid return value.
@ingroup bitfunc
*/
inline bm::word_t* bit_operation_and(bm::word_t* BMRESTRICT dst,
const bm::word_t* BMRESTRICT src)
{
BM_ASSERT(dst || src);
bm::word_t* ret = dst;
if (IS_VALID_ADDR(dst)) // The destination block already exists
{
if (!IS_VALID_ADDR(src))
{
if (IS_EMPTY_BLOCK(src))
{
//If the source block is zero
//just clean the destination block
return 0;
}
}
else
{
// Regular operation AND on the whole block.
bit_block_and(dst, src);
}
}
else // The destination block does not exist yet
{
if(!IS_VALID_ADDR(src))
{
if(IS_EMPTY_BLOCK(src))
{
// The source block is empty.
// One argument empty - all result is empty.
return 0;
}
// Here we have nothing to do.
// Src block is all ON, dst block remains as it is
}
else // destination block does not exists, src - valid block
{
if (IS_FULL_BLOCK(dst))
{
return const_cast<bm::word_t*>(src);
}
// Nothng to do.
// Dst block is all ZERO no combination required.
}
}
return ret;
}
/*!
\brief Performs bitblock AND operation and calculates bitcount of the result.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
\returns bitcount value
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_and_count(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1) || IS_EMPTY_BLOCK(src2))
{
return 0;
}
return bit_block_and_count(src1, src1_end, src2);
}
/*!
\brief Performs bitblock AND operation test.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
\returns non zero if there is any value
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_and_any(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1) || IS_EMPTY_BLOCK(src2))
{
return 0;
}
return bit_block_and_any(src1, src1_end, src2);
}
/*!
\brief Performs bitblock SUB operation and calculates bitcount of the result.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block
\returns bitcount value
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_sub_count(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1))
{
return 0;
}
if (IS_EMPTY_BLOCK(src2)) // nothing to diff
{
return bit_block_calc_count(src1, src1_end);
}
return bit_block_sub_count(src1, src1_end, src2);
}
/*!
\brief Performs inverted bitblock SUB operation and calculates
bitcount of the result.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block
\returns bitcount value
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_sub_count_inv(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
unsigned arr_size = unsigned(src1_end - src1);
return bit_operation_sub_count(src2, src2+arr_size, src1);
}
/*!
\brief Performs bitblock test of SUB operation.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block
\returns non zero value if there are any bits
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_sub_any(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1))
{
return 0;
}
if (IS_EMPTY_BLOCK(src2)) // nothing to diff
{
return !bit_is_all_zero((bm::wordop_t*)src1, (bm::wordop_t*)src1_end);
}
return bit_block_sub_any(src1, src1_end, src2);
}
/*!
\brief Performs bitblock OR operation and calculates bitcount of the result.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
\returns bitcount value
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_or_count(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1))
{
if (!IS_EMPTY_BLOCK(src2))
return bit_block_calc_count(src2, src2 + (src1_end - src1));
else
return 0; // both blocks are empty
}
else
{
if (IS_EMPTY_BLOCK(src2))
return bit_block_calc_count(src1, src1_end);
}
return bit_block_or_count(src1, src1_end, src2);
}
/*!
\brief Performs bitblock OR operation test.
\param src1 - first bit block.
\param src1_end - first bit block end
\param src2 - second bit block.
\returns non zero value if there are any bits
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_or_any(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1))
{
if (!IS_EMPTY_BLOCK(src2))
return !bit_is_all_zero((bm::wordop_t*)src2,
(bm::wordop_t*)(src2 + (src1_end - src1)));
else
return 0; // both blocks are empty
}
else
{
if (IS_EMPTY_BLOCK(src2))
return !bit_is_all_zero((bm::wordop_t*)src1, (bm::wordop_t*)src1_end);
}
return bit_block_or_any(src1, src1_end, src2);
}
/*!
\brief Plain bitblock OR operation.
Function does not analyse availability of source and destination blocks.
\param dst - destination block.
\param src - source block.
@ingroup bitfunc
*/
inline void bit_block_or(bm::word_t* BMRESTRICT dst,
const bm::word_t* BMRESTRICT src)
{
#ifdef BMVECTOPT
VECT_OR_ARR(dst, src, src + bm::set_block_size);
#else
const bm::wordop_t* BMRESTRICT wrd_ptr = (wordop_t*)src;
const bm::wordop_t* BMRESTRICT wrd_end = (wordop_t*)(src + set_block_size);
bm::wordop_t* BMRESTRICT dst_ptr = (wordop_t*)dst;
do
{
dst_ptr[0] |= wrd_ptr[0];
dst_ptr[1] |= wrd_ptr[1];
dst_ptr[2] |= wrd_ptr[2];
dst_ptr[3] |= wrd_ptr[3];
dst_ptr+=4;
wrd_ptr+=4;
} while (wrd_ptr < wrd_end);
#endif
}
/*!
\brief Block OR operation. Makes analysis if block is 0 or FULL.
\param dst - destination block.
\param src - source block.
\returns pointer on destination block.
If returned value equal to src means that block mutation requested.
NULL is valid return value.
@ingroup bitfunc
*/
inline
bm::word_t* bit_operation_or(bm::word_t* BMRESTRICT dst,
const bm::word_t* BMRESTRICT src)
{
BM_ASSERT(dst || src);
bm::word_t* ret = dst;
if (IS_VALID_ADDR(dst)) // The destination block already exists
{
if (!IS_VALID_ADDR(src))
{
if (IS_FULL_BLOCK(src))
{
// if the source block is all set
// just set the destination block
::memset(dst, 0xFF, bm::set_block_size * sizeof(bm::word_t));
}
}
else
{
// Regular operation OR on the whole block
bit_block_or(dst, src);
}
}
else // The destination block does not exist yet
{
if (!IS_VALID_ADDR(src))
{
if (IS_FULL_BLOCK(src))
{
// The source block is all set, because dst does not exist
// we can simply replace it.
return const_cast<bm::word_t*>(FULL_BLOCK_ADDR);
}
}
else
{
if (dst == 0)
{
// The only case when we have to allocate the new block:
// Src is all zero and Dst does not exist
return const_cast<bm::word_t*>(src);
}
}
}
return ret;
}
/*!
\brief Plain bitblock SUB (AND NOT) operation.
Function does not analyse availability of source and destination blocks.
\param dst - destination block.
\param src - source block.
@ingroup bitfunc
*/
inline
void bit_block_sub(bm::word_t* BMRESTRICT dst,
const bm::word_t* BMRESTRICT src)
{
#ifdef BMVECTOPT
VECT_SUB_ARR(dst, src, src + bm::set_block_size);
#else
const bm::wordop_t* BMRESTRICT wrd_ptr = (wordop_t*) src;
const bm::wordop_t* BMRESTRICT wrd_end =
(wordop_t*) (src + bm::set_block_size);
bm::wordop_t* dst_ptr = (wordop_t*)dst;
// Regular operation AND-NOT on the whole block.
do
{
dst_ptr[0] &= ~wrd_ptr[0];
dst_ptr[1] &= ~wrd_ptr[1];
dst_ptr[2] &= ~wrd_ptr[2];
dst_ptr[3] &= ~wrd_ptr[3];
dst_ptr+=4;
wrd_ptr+=4;
} while (wrd_ptr < wrd_end);
#endif
}
/*!
\brief bitblock SUB operation.
\param dst - destination block.
\param src - source block.
\returns pointer on destination block.
If returned value equal to src means that block mutation requested.
NULL is valid return value.
@ingroup bitfunc
*/
inline
bm::word_t* bit_operation_sub(bm::word_t* BMRESTRICT dst,
const bm::word_t* BMRESTRICT src)
{
BM_ASSERT(dst || src);
bm::word_t* ret = dst;
if (IS_VALID_ADDR(dst)) // The destination block already exists
{
if (!IS_VALID_ADDR(src))
{
if (IS_FULL_BLOCK(src))
{
// If the source block is all set
// just clean the destination block
return 0;
}
}
else
{
bit_block_sub(dst, src);
}
}
else // The destination block does not exist yet
{
if (!IS_VALID_ADDR(src))
{
if (IS_FULL_BLOCK(src))
{
// The source block is full
return 0;
}
}
else
{
if (IS_FULL_BLOCK(dst))
{
// The only case when we have to allocate the new block:
// dst is all set and src exists
return const_cast<bm::word_t*>(src);
}
}
}
return ret;
}
/*!
\brief Plain bitblock XOR operation.
Function does not analyse availability of source and destination blocks.
\param dst - destination block.
\param src - source block.
@ingroup bitfunc
*/
inline
void bit_block_xor(bm::word_t* BMRESTRICT dst,
const bm::word_t* BMRESTRICT src)
{
#ifdef BMVECTOPT
VECT_XOR_ARR(dst, src, src + bm::set_block_size);
#else
const bm::wordop_t* BMRESTRICT wrd_ptr = (wordop_t*) src;
const bm::wordop_t* BMRESTRICT wrd_end =
(wordop_t*) (src + bm::set_block_size);
bm::wordop_t* BMRESTRICT dst_ptr = (wordop_t*)dst;
// Regular XOR operation on the whole block.
do
{
dst_ptr[0] ^= wrd_ptr[0];
dst_ptr[1] ^= wrd_ptr[1];
dst_ptr[2] ^= wrd_ptr[2];
dst_ptr[3] ^= wrd_ptr[3];
dst_ptr+=4;
wrd_ptr+=4;
} while (wrd_ptr < wrd_end);
#endif
}
/*!
\brief bitblock XOR operation.
\param dst - destination block.
\param src - source block.
\returns pointer on destination block.
If returned value equal to src means that block mutation requested.
NULL is valid return value.
@ingroup bitfunc
*/
inline
bm::word_t* bit_operation_xor(bm::word_t* BMRESTRICT dst,
const bm::word_t* BMRESTRICT src)
{
BM_ASSERT(dst || src);
if (src == dst) return 0; // XOR rule
bm::word_t* ret = dst;
if (IS_VALID_ADDR(dst)) // The destination block already exists
{
if (!src) return dst;
bit_block_xor(dst, src);
}
else // The destination block does not exist yet
{
if (!src) return dst; // 1 xor 0 = 1
// Here we have two cases:
// if dest block is full or zero if zero we need to copy the source
// otherwise XOR loop against 0xFF...
//BM_ASSERT(dst == 0);
return const_cast<bm::word_t*>(src); // src is the final result
}
return ret;
}
/*!
\brief Performs bitblock XOR operation and calculates bitcount of the result.
\param src1 - first bit block.
\param src2 - second bit block.
\returns bitcount value
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_xor_count(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1) || IS_EMPTY_BLOCK(src2))
{
if (IS_EMPTY_BLOCK(src1) && IS_EMPTY_BLOCK(src2))
return 0;
const bm::word_t* block = IS_EMPTY_BLOCK(src1) ? src2 : src1;
return bit_block_calc_count(block, block + (src1_end - src1));
}
return bit_block_xor_count(src1, src1_end, src2);
}
/*!
\brief Performs bitblock XOR operation test.
\param src1 - first bit block.
\param src2 - second bit block.
\returns non zero value if there are bits
@ingroup bitfunc
*/
inline
bm::id_t bit_operation_xor_any(const bm::word_t* BMRESTRICT src1,
const bm::word_t* BMRESTRICT src1_end,
const bm::word_t* BMRESTRICT src2)
{
if (IS_EMPTY_BLOCK(src1) || IS_EMPTY_BLOCK(src2))
{
if (IS_EMPTY_BLOCK(src1) && IS_EMPTY_BLOCK(src2))
return 0;
const bm::word_t* block = IS_EMPTY_BLOCK(src1) ? src2 : src1;
return !bit_is_all_zero((bm::wordop_t*)block,
(bm::wordop_t*)(block + (src1_end - src1)));
}
return bit_block_xor_any(src1, src1_end, src2);
}
/**
\brief Inspects block for full zero words
\param data - bit block pointer
\param data_size - data size
\return size of all non-zero words
@ingroup bitfunc
*/
template<class T>
unsigned bit_count_nonzero_size(const T* blk,
unsigned data_size)
{
BM_ASSERT(blk && data_size);
unsigned count = 0;
const T* blk_end = blk + data_size - 2;
do
{
if (*blk == 0)
{
// scan fwd to find 0 island length
const T* blk_j = blk + 1;
for (; blk_j < blk_end; ++blk_j)
{
if (*blk_j != 0)
break;
}
blk = blk_j-1;
count += sizeof(gap_word_t);
}
else
{
// scan fwd to find non-0 island length
const T* blk_j = blk + 1;
for ( ; blk_j < blk_end; ++blk_j)
{
if (*blk_j == 0)
{
// look ahead to identify and ignore short 0-run
if (blk_j[1] | blk_j[2])
{
// skip zero word
++blk_j;
continue;
}
break;
}
}
count += sizeof(gap_word_t);
// count all bit-words now
count += (blk_j - blk) * sizeof(T);
blk = blk_j;
}
++blk;
}
while(blk < blk_end);
return count + (2 * sizeof(T));
}
/**
\brief Searches for the next 1 bit in the BIT block
\param data - BIT buffer
\param nbit - bit index to start checking from
\param prev - returns previously checked value
@ingroup bitfunc
*/
inline
int bit_find_in_block(const bm::word_t* data,
unsigned nbit,
bm::id_t* prev)
{
register bm::id_t p = *prev;
int found = 0;
for(;;)
{
unsigned nword = nbit >> bm::set_word_shift;
if (nword >= bm::set_block_size) break;
register bm::word_t val = data[nword] >> (p & bm::set_word_mask);
// TODO: consider BSF and de bruijn sequences here:
// http://www.0xe3.com/text/ntz/ComputingTrailingZerosHOWTO.html#debruijn
if (val)
{
while((val & 1) == 0)
{
val >>= 1;
++nbit;
++p;
}
++found;
break;
}
else
{
p += (bm::set_word_mask + 1) - (nbit & bm::set_word_mask);
nbit += (bm::set_word_mask + 1) - (nbit & bm::set_word_mask);
}
}
*prev = p;
return found;
}
/*!
\brief Templated algorithm to unpacks octet based word into list of ON bit indexes
\param w - value
\param func - bit functor
@ingroup bitfunc
*/
template<typename T, typename F>
void bit_for_each_4(T w, F& func)
{
for (unsigned sub_octet = 0; w != 0; w >>= 4, sub_octet += 4)
{
switch (w & 15)
{
case 0: // 0000
break;
case 1: // 0001
func(sub_octet);
break;
case 2: // 0010
func(sub_octet + 1);
break;
case 3: // 0011
func(sub_octet, sub_octet + 1);
break;
case 4: // 0100
func(sub_octet + 2);
break;
case 5: // 0101
func(sub_octet, sub_octet + 2);
break;
case 6: // 0110
func(sub_octet + 1, sub_octet + 2);
break;
case 7: // 0111
func(sub_octet, sub_octet + 1, sub_octet + 2);
break;
case 8: // 1000
func(sub_octet + 3);
break;
case 9: // 1001
func(sub_octet, sub_octet + 3);
break;
case 10: // 1010
func(sub_octet + 1, sub_octet + 3);
break;
case 11: // 1011
func(sub_octet, sub_octet + 1, sub_octet + 3);
break;
case 12: // 1100
func(sub_octet + 2, sub_octet + 3);
break;
case 13: // 1101
func(sub_octet, sub_octet + 2, sub_octet + 3);
break;
case 14: // 1110
func(sub_octet + 1, sub_octet + 2, sub_octet + 3);
break;
case 15: // 1111
func(sub_octet, sub_octet + 1, sub_octet + 2, sub_octet + 3);
break;
default:
BM_ASSERT(0);
break;
}
} // for
}
/*!
\brief Templated algorithm to unpacks word into list of ON bit indexes
\param w - value
\param func - bit functor
@ingroup bitfunc
*/
template<typename T, typename F>
void bit_for_each(T w, F& func)
{
// Note: 4-bit table method works slower than plain check approach
for (unsigned octet = 0; w != 0; w >>= 8, octet += 8)
{
if (w & 1) func(octet + 0);
if (w & 2) func(octet + 1);
if (w & 4) func(octet + 2);
if (w & 8) func(octet + 3);
if (w & 16) func(octet + 4);
if (w & 32) func(octet + 5);
if (w & 64) func(octet + 6);
if (w & 128) func(octet + 7);
} // for
}
/*! @brief Adaptor to copy 1 bits to array
@internal
*/
template<typename B> class copy_to_array_functor
{
public:
copy_to_array_functor(B* bits): bp_(bits)
{}
B* ptr() { return bp_; }
void operator()(unsigned bit_idx) { *bp_++ = (B)bit_idx; }
void operator()(unsigned bit_idx0,
unsigned bit_idx1)
{
bp_[0] = (B)bit_idx0; bp_[1] = (B)bit_idx1;
bp_+=2;
}
void operator()(unsigned bit_idx0,
unsigned bit_idx1,
unsigned bit_idx2)
{
bp_[0] = (B)bit_idx0; bp_[1] = (B)bit_idx1; bp_[2] = (B)bit_idx2;
bp_+=3;
}
void operator()(unsigned bit_idx0,
unsigned bit_idx1,
unsigned bit_idx2,
unsigned bit_idx3)
{
bp_[0] = (B)bit_idx0; bp_[1] = (B)bit_idx1;
bp_[2] = (B)bit_idx2; bp_[3] = (B)bit_idx3;
bp_+=4;
}
private:
copy_to_array_functor(const copy_to_array_functor&);
copy_to_array_functor& operator=(const copy_to_array_functor&);
private:
B* bp_;
};
/*! @brief Adaptor to copy 1 bits to array with base increment
@internal
*/
template<typename B> class copy_to_array_functor_inc
{
public:
copy_to_array_functor_inc(B* bits, unsigned base_idx)
: bp_(bits), base_idx_(base_idx)
{}
B* ptr() { return bp_; }
void operator()(unsigned bit_idx)
{
*bp_++ = (B)(bit_idx + base_idx_);
}
void operator()(unsigned bit_idx0,
unsigned bit_idx1)
{
bp_[0]=(B)(bit_idx0+base_idx_);bp_[1]=(B)(bit_idx1+base_idx_);
bp_+=2;
}
void operator()(unsigned bit_idx0,
unsigned bit_idx1,
unsigned bit_idx2)
{
bp_[0]=(B)(bit_idx0+base_idx_);bp_[1]=(B)(bit_idx1+base_idx_);
bp_[2]=(B)(bit_idx2+base_idx_);
bp_+=3;
}
void operator()(unsigned bit_idx0,
unsigned bit_idx1,
unsigned bit_idx2,
unsigned bit_idx3)
{
bp_[0]=(B)(bit_idx0+base_idx_);bp_[1]=(B)(bit_idx1+base_idx_);
bp_[2]=(B)(bit_idx2+base_idx_);bp_[3]=(B)(bit_idx3+base_idx_);
bp_+=4;
}
private:
copy_to_array_functor_inc(const copy_to_array_functor_inc&);
copy_to_array_functor_inc& operator=(const copy_to_array_functor_inc&);
private:
B* bp_;
unsigned base_idx_; ///< Base increment factor
};
/*!
\brief Unpacks word into list of ON bit indexes (quad-bit based)
\param w - value
\param bits - pointer on the result array
\return number of bits in the list
@ingroup bitfunc
*/
template<typename T,typename B> unsigned bit_list_4(T w, B* bits)
{
copy_to_array_functor<B> func(bits);
bit_for_each_4(w, func);
return (unsigned)(func.ptr() - bits);
}
/*!
\brief Unpacks word into list of ON bit indexes
\param w - value
\param bits - pointer on the result array
\return number of bits in the list
@ingroup bitfunc
*/
template<typename T,typename B> unsigned bit_list(T w, B* bits)
{
copy_to_array_functor<B> func(bits);
bit_for_each(w, func);
return (unsigned)(func.ptr() - bits);
}
/*!
@brief Choose best representation for a bit-block
@ingroup bitfunc
*/
inline
bm::set_representation best_representation(unsigned bit_count,
unsigned total_possible_bitcount,
unsigned gap_count,
unsigned block_size)
{
unsigned arr_size = sizeof(bm::gap_word_t) * bit_count + sizeof(bm::gap_word_t);
unsigned gap_size = sizeof(bm::gap_word_t) * gap_count + sizeof(bm::gap_word_t);
unsigned inv_arr_size = sizeof(bm::gap_word_t) * (total_possible_bitcount - bit_count) + sizeof(bm::gap_word_t);
if ((gap_size < block_size) && (gap_size < arr_size) && (gap_size < inv_arr_size))
{
return bm::set_gap;
}
if (arr_size < inv_arr_size)
{
if ((arr_size < block_size) && (arr_size < gap_size))
{
return bm::set_array1;
}
}
else
{
if ((inv_arr_size < block_size) && (inv_arr_size < gap_size))
{
return bm::set_array0;
}
}
return bm::set_bitset;
}
/*!
@brief Convert bit block into an array of ints corresponding to 1 bits
@ingroup bitfunc
*/
template<typename T> T bit_convert_to_arr(T* BMRESTRICT dest,
const unsigned* BMRESTRICT src,
bm::id_t bits,
unsigned dest_len,
unsigned mask = 0)
{
T* BMRESTRICT pcurr = dest;
for(unsigned bit_idx=0; bit_idx < bits; ++src,bit_idx += sizeof(*src) * 8)
{
unsigned val = *src ^ mask; // possible to invert value by XOR 0xFF..
if (val == 0)
{
continue;
}
if (pcurr + sizeof(val)*8 >= dest + dest_len) // insufficient space
{
return 0;
}
copy_to_array_functor_inc<T> func(pcurr, bit_idx);
bit_for_each_4(val, func);
unsigned word_bit_cnt = func.ptr() - pcurr;
pcurr += word_bit_cnt;
} // for
return (T)(pcurr - dest);
}
/*!
OBSOLETE function
\brief Convert bit block into an array of ints corresponding to 1 bits
\internal
@ingroup bitfunc
*/
/*
template<typename T> T bit_convert_to_arr2(T* BMRESTRICT dest,
const unsigned* BMRESTRICT src,
bm::id_t bits,
unsigned dest_len)
{
register T* BMRESTRICT pcurr = dest;
T* BMRESTRICT end = dest + dest_len;
unsigned bit_idx = 0;
do
{
register unsigned val = *src;
// We can skip if *src == 0
while (val == 0)
{
bit_idx += sizeof(*src) * 8;
if (bit_idx >= bits)
{
return (T)(pcurr - dest);
}
val = *(++src);
}
if (pcurr + sizeof(val)*8 > end) // insufficient space
{
return 0;
}
for (int i = 0; i < 32; i+=4)
{
if (val & 1)
*pcurr++ = bit_idx;
val >>= 1; ++bit_idx;
if (val & 1)
*pcurr++ = bit_idx;
val >>= 1; ++bit_idx;
if (val & 1)
*pcurr++ = bit_idx;
val >>= 1; ++bit_idx;
if (val & 1)
*pcurr++ = bit_idx;
val >>= 1; ++bit_idx;
}
if (bits <= bit_idx)
break;
val = *(++src);
} while (1);
return (T)(pcurr - dest);
}
*/
/*! @brief Calculates memory overhead for number of gap blocks sharing
the same memory allocation table (level lengths table).
@ingroup gapfunc
*/
template<typename T>
unsigned gap_overhead(const T* length,
const T* length_end,
const T* glevel_len)
{
BM_ASSERT(length && length_end && glevel_len);
unsigned overhead = 0;
for (;length < length_end; ++length)
{
unsigned len = *length;
int level = gap_calc_level(len, glevel_len);
BM_ASSERT(level >= 0 && level < (int)bm::gap_levels);
unsigned capacity = glevel_len[level];
BM_ASSERT(capacity >= len);
overhead += capacity - len;
}
return overhead;
}
/*! @brief Finds optimal gap blocks lengths.
@param length - first element of GAP lengths array
@param length_end - end of the GAP lengths array
@param glevel_len - destination GAP lengths array
@ingroup gapfunc
*/
template<typename T>
bool improve_gap_levels(const T* length,
const T* length_end,
T* glevel_len)
{
BM_ASSERT(length && length_end && glevel_len);
size_t lsize = length_end - length;
BM_ASSERT(lsize);
gap_word_t max_len = 0;
unsigned i;
for (i = 0; i < lsize; ++i)
{
if (length[i] > max_len)
max_len = length[i];
}
if (max_len < 5 || lsize <= bm::gap_levels)
{
glevel_len[0] = max_len + 4;
for (i = 1; i < bm::gap_levels; ++i)
{
glevel_len[i] = bm::gap_max_buff_len;
}
return true;
}
glevel_len[bm::gap_levels-1] = max_len + 5;
unsigned min_overhead = gap_overhead(length, length_end, glevel_len);
bool is_improved = false;
gap_word_t prev_value = glevel_len[bm::gap_levels-1];
// main problem solving loop
//
for (i = bm::gap_levels-2; ; --i)
{
unsigned opt_len = 0;
unsigned j;
bool imp_flag = false;
gap_word_t gap_saved_value = glevel_len[i];
for (j = 0; j < lsize; ++j)
{
glevel_len[i] = length[j]+4;
unsigned ov = gap_overhead(length, length_end, glevel_len);
if (ov <= min_overhead)
{
min_overhead = ov;
opt_len = length[j]+4;
imp_flag = true;
}
}
if (imp_flag)
{
glevel_len[i] = (T)opt_len; // length[opt_idx]+4;
is_improved = true;
}
else
{
glevel_len[i] = gap_saved_value;
}
if (i == 0)
break;
prev_value = glevel_len[i];
}
//
// Remove duplicates
//
T val = *glevel_len;
T* gp = glevel_len;
T* res = glevel_len;
for (i = 0; i < bm::gap_levels; ++i)
{
if (val != *gp)
{
val = *gp;
*++res = val;
}
++gp;
}
// Filling the "unused" part with max. possible value
while (++res < (glevel_len + bm::gap_levels))
{
*res = bm::gap_max_buff_len;
}
return is_improved;
}
/**
Bit-block get adapter, takes bitblock and represents it as a
get_32() accessor function
/internal
*/
class bitblock_get_adapter
{
public:
bitblock_get_adapter(const bm::word_t* bit_block) : b_(bit_block) {}
BMFORCEINLINE
bm::word_t get_32() { return *b_++; }
private:
const bm::word_t* b_;
};
/**
Bit-block store adapter, takes bitblock and saves results into it
/internal
*/
class bitblock_store_adapter
{
public:
bitblock_store_adapter(bm::word_t* bit_block) : b_(bit_block) {}
BMFORCEINLINE
void push_back(bm::word_t w) { *b_++ = w; }
private:
bm::word_t* b_;
};
/**
Bit-block sum adapter, takes values and sums it
/internal
*/
class bitblock_sum_adapter
{
public:
bitblock_sum_adapter() : sum_(0) {}
BMFORCEINLINE
void push_back(bm::word_t w) { this->sum_+= w; }
/// Get accumulated sum
bm::word_t sum() const { return this->sum_; }
private:
bm::word_t sum_;
};
/**
Adapter to get words from a range stream
(see range serialized bit-block)
\internal
*/
template<class DEC> class decoder_range_adapter
{
public:
decoder_range_adapter(DEC& dec, unsigned from_idx, unsigned to_idx)
: decoder_(dec),
from_(from_idx),
to_(to_idx),
cnt_(0)
{}
bm::word_t get_32()
{
if (cnt_ < from_ || cnt_ > to_)
{
++cnt_; return 0;
}
++cnt_;
return decoder_.get_32();
}
private:
DEC& decoder_;
unsigned from_;
unsigned to_;
unsigned cnt_;
};
/*!
Abstract recombination algorithm for two bit-blocks
Bit blocks can come as dserialization decoders or bit-streams
*/
template<class It1, class It2, class BinaryOp, class Encoder>
void bit_recomb(It1& it1, It2& it2,
BinaryOp& op,
Encoder& enc,
unsigned block_size = bm::set_block_size)
{
for (unsigned i = 0; i < block_size; ++i)
{
bm::word_t w1 = it1.get_32();
bm::word_t w2 = it2.get_32();
bm::word_t w = op(w1, w2);
enc.push_back( w );
} // for
}
/// Bit AND functor
template<typename W> struct bit_AND
{
W operator()(W w1, W w2) { return w1 & w2; }
};
/// Bit OR functor
template<typename W> struct bit_OR
{
W operator()(W w1, W w2) { return w1 | w2; }
};
/// Bit SUB functor
template<typename W> struct bit_SUB
{
W operator()(W w1, W w2) { return w1 & ~w2; }
};
/// Bit XOR functor
template<typename W> struct bit_XOR
{
W operator()(W w1, W w2) { return w1 ^ w2; }
};
/// Bit ASSIGN functor
template<typename W> struct bit_ASSIGN
{
W operator()(W w1, W w2) { return w2; }
};
/// Bit COUNT functor
template<typename W> struct bit_COUNT
{
W operator()(W w1, W w2)
{
w1 = 0;
BM_INCWORD_BITCOUNT(w1, w2);
return w1;
}
};
/// Bit COUNT AND functor
template<typename W> struct bit_COUNT_AND
{
W operator()(W w1, W w2)
{
W r = 0;
BM_INCWORD_BITCOUNT(r, w1 & w2);
return r;
}
};
/// Bit COUNT XOR functor
template<typename W> struct bit_COUNT_XOR
{
W operator()(W w1, W w2)
{
W r = 0;
BM_INCWORD_BITCOUNT(r, w1 ^ w2);
return r;
}
};
/// Bit COUNT OR functor
template<typename W> struct bit_COUNT_OR
{
W operator()(W w1, W w2)
{
W r = 0;
BM_INCWORD_BITCOUNT(r, w1 | w2);
return r;
}
};
/// Bit COUNT SUB AB functor
template<typename W> struct bit_COUNT_SUB_AB
{
W operator()(W w1, W w2)
{
W r = 0;
BM_INCWORD_BITCOUNT(r, w1 & (~w2));
return r;
}
};
/// Bit SUB BA functor
template<typename W> struct bit_COUNT_SUB_BA
{
W operator()(W w1, W w2)
{
W r = 0;
BM_INCWORD_BITCOUNT(r, w2 & (~w1));
return r;
}
};
/// Bit COUNT A functor
template<typename W> struct bit_COUNT_A
{
W operator()(W w1, W w2)
{
W r = 0;
BM_INCWORD_BITCOUNT(r, w1);
return r;
}
};
/// Bit COUNT B functor
template<typename W> struct bit_COUNT_B
{
W operator()(W w1, W w2)
{
W r = 0;
BM_INCWORD_BITCOUNT(r, w2);
return r;
}
};
typedef
void (*gap_operation_to_bitset_func_type)(unsigned*,
const gap_word_t*);
typedef
gap_word_t* (*gap_operation_func_type)(const gap_word_t* BMRESTRICT,
const gap_word_t* BMRESTRICT,
gap_word_t* BMRESTRICT,
unsigned& );
typedef
bm::id_t (*bit_operation_count_func_type)(const bm::word_t* BMRESTRICT,
const bm::word_t* BMRESTRICT,
const bm::word_t* BMRESTRICT);
template<bool T>
struct operation_functions
{
static
gap_operation_to_bitset_func_type gap2bit_table_[bm::set_END];
static
gap_operation_func_type gapop_table_[bm::set_END];
static
bit_operation_count_func_type bit_op_count_table_[bm::set_END];
static
gap_operation_to_bitset_func_type gap_op_to_bit(unsigned i)
{
return gap2bit_table_[i];
}
static
gap_operation_func_type gap_operation(unsigned i)
{
return gapop_table_[i];
}
static
bit_operation_count_func_type bit_operation_count(unsigned i)
{
return bit_op_count_table_[i];
}
};
template<bool T>
gap_operation_to_bitset_func_type
operation_functions<T>::gap2bit_table_[bm::set_END] = {
&gap_and_to_bitset<bm::gap_word_t>, // set_AND
&gap_add_to_bitset<bm::gap_word_t>, // set_OR
&gap_sub_to_bitset<bm::gap_word_t>, // set_SUB
&gap_xor_to_bitset<bm::gap_word_t>, // set_XOR
0
};
template<bool T>
gap_operation_func_type
operation_functions<T>::gapop_table_[bm::set_END] = {
&gap_operation_and, // set_AND
&gap_operation_or, // set_OR
&gap_operation_sub, // set_SUB
&gap_operation_xor, // set_XOR
0
};
template<bool T>
bit_operation_count_func_type
operation_functions<T>::bit_op_count_table_[bm::set_END] = {
0, // set_AND
0, // set_OR
0, // set_SUB
0, // set_XOR
0, // set_ASSIGN
0, // set_COUNT
&bit_operation_and_count, // set_COUNT_AND
&bit_operation_xor_count, // set_COUNT_XOR
&bit_operation_or_count, // set_COUNT_OR
&bit_operation_sub_count, // set_COUNT_SUB_AB
&bit_operation_sub_count_inv, // set_COUNT_SUB_BA
0, // set_COUNT_A
0, // set_COUNT_B
};
} // namespace bm
#endif
|