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
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
|
/*
* Amalgamated copy of CRoaring 0.2.66, modified for GTK to reduce compiler
* warnings.
*
* Copyright 2016-2020 The CRoaring authors
* Copyright 2020 Benjamin Otte
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
/* begin file include/roaring/roaring_version.h */
// /include/roaring/roaring_version.h automatically generated by release.py, do not change by hand
#ifndef ROARING_INCLUDE_ROARING_VERSION
#define ROARING_INCLUDE_ROARING_VERSION
#define ROARING_VERSION = 0.2.66,
enum {
ROARING_VERSION_MAJOR = 0,
ROARING_VERSION_MINOR = 2,
ROARING_VERSION_REVISION = 66
};
#endif // ROARING_INCLUDE_ROARING_VERSION
/* end file include/roaring/roaring_version.h */
/* begin file include/roaring/portability.h */
/*
* portability.h
*
*/
#ifndef INCLUDE_PORTABILITY_H_
#define INCLUDE_PORTABILITY_H_
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS 1
#endif
#if !(defined(_POSIX_C_SOURCE)) || (_POSIX_C_SOURCE < 200809L)
#define _POSIX_C_SOURCE 200809L
#endif
#if !(defined(_XOPEN_SOURCE)) || (_XOPEN_SOURCE < 700)
#define _XOPEN_SOURCE 700
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h> // will provide posix_memalign with _POSIX_C_SOURCE as defined above
#if !(defined(__APPLE__)) && !(defined(__FreeBSD__)) && !(defined(__OpenBSD__))
#include <malloc.h> // this should never be needed but there are some reports that it is needed.
#endif
#if defined(_MSC_VER) && !defined(__clang__) && !defined(_WIN64) && !defined(ROARING_ACK_32BIT)
#pragma message( \
"You appear to be attempting a 32-bit build under Visual Studio. We recommend a 64-bit build instead.")
#endif
#if defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ != 8
#error This code assumes 64-bit long longs (by use of the GCC intrinsics). Your system is not currently supported.
#endif
#if defined(_MSC_VER)
#define __restrict__ __restrict
#endif
#ifndef DISABLE_X64 // some users may want to compile as if they did not have
// an x64 processor
///////////////////////
/// We support X64 hardware in the following manner:
///
/// if IS_X64 is defined then we have at least SSE and SSE2
/// (All Intel processors sold in the recent past have at least SSE and SSE2 support,
/// going back to the Pentium 4.)
///
/// if USESSE4 is defined then we assume at least SSE4.2, SSE4.1,
/// SSSE3, SSE3... + IS_X64
/// if USEAVX is defined, then we assume AVX2, AVX + USESSE4
///
/// So if you have hardware that supports AVX but not AVX2, then "USEAVX"
/// won't be enabled.
/// If you have hardware that supports SSE4.1, but not SSE4.2, then USESSE4
/// won't be defined.
//////////////////////
// unless DISABLEAVX was defined, if we have __AVX2__, we enable AVX
#if (!defined(USEAVX)) && (!defined(DISABLEAVX)) && (defined(__AVX2__))
#define USEAVX
#endif
// if we have __SSE4_2__, we enable SSE4
#if (defined(__POPCNT__)) && (defined(__SSE4_2__))
#define USESSE4
#endif
#if defined(USEAVX) || defined(__x86_64__) || defined(_M_X64)
// we have an x64 processor
#define IS_X64
// we include the intrinsic header
#ifndef _MSC_VER
/* Non-Microsoft C/C++-compatible compiler */
#include <x86intrin.h> // on some recent GCC, this will declare posix_memalign
#endif
#endif
#if !defined(USENEON) && !defined(DISABLENEON) && defined(__ARM_NEON)
# define USENEON
#endif
#if defined(USENEON)
# include <arm_neon.h>
#endif
#ifndef _MSC_VER
/* Non-Microsoft C/C++-compatible compiler, assumes that it supports inline
* assembly */
#define ROARING_INLINE_ASM
#endif
#ifdef USEAVX
#define USESSE4 // if we have AVX, then we have SSE4
#define USE_BMI // we assume that AVX2 and BMI go hand and hand
#define USEAVX2FORDECODING // optimization
// vector operations should work on not just AVX
#define ROARING_VECTOR_OPERATIONS_ENABLED // vector unions (optimization)
#endif
#endif // DISABLE_X64
#ifdef _MSC_VER
/* Microsoft C/C++-compatible compiler */
#include <intrin.h>
#ifndef __clang__ // if one compiles with MSVC *with* clang, then these
// intrinsics are defined!!!
// sadly there is no way to check whether we are missing these intrinsics
// specifically.
/* wrappers for Visual Studio built-ins that look like gcc built-ins */
/* result might be undefined when input_num is zero */
static inline int __builtin_ctzll(unsigned long long input_num) {
unsigned long index;
#ifdef _WIN64 // highly recommended!!!
_BitScanForward64(&index, input_num);
#else // if we must support 32-bit Windows
if ((uint32_t)input_num != 0) {
_BitScanForward(&index, (uint32_t)input_num);
} else {
_BitScanForward(&index, (uint32_t)(input_num >> 32));
index += 32;
}
#endif
return index;
}
/* result might be undefined when input_num is zero */
static inline int __builtin_clzll(unsigned long long input_num) {
unsigned long index;
#ifdef _WIN64 // highly recommended!!!
_BitScanReverse64(&index, input_num);
#else // if we must support 32-bit Windows
if (input_num > 0xFFFFFFFF) {
_BitScanReverse(&index, (uint32_t)(input_num >> 32));
index += 32;
} else {
_BitScanReverse(&index, (uint32_t)(input_num));
}
#endif
return 63 - index;
}
/* result might be undefined when input_num is zero */
#ifdef USESSE4
/* POPCNT support was added to processors around the release of SSE4.2 */
/* USESSE4 flag guarantees POPCNT support */
static inline int __builtin_popcountll(unsigned long long input_num) {
#ifdef _WIN64 // highly recommended!!!
return (int)__popcnt64(input_num);
#else // if we must support 32-bit Windows
return (int)(__popcnt((uint32_t)input_num) +
__popcnt((uint32_t)(input_num >> 32)));
#endif
}
#else
/* software implementation avoids POPCNT */
static inline int __builtin_popcountll(unsigned long long input_num) {
const uint64_t m1 = 0x5555555555555555; //binary: 0101...
const uint64_t m2 = 0x3333333333333333; //binary: 00110011..
const uint64_t m4 = 0x0f0f0f0f0f0f0f0f; //binary: 4 zeros, 4 ones ...
const uint64_t h01 = 0x0101010101010101; //the sum of 256 to the power of 0,1,2,3...
input_num -= (input_num >> 1) & m1;
input_num = (input_num & m2) + ((input_num >> 2) & m2);
input_num = (input_num + (input_num >> 4)) & m4;
return (input_num * h01) >> 56;
}
#endif
/* Use #define so this is effective even under /Ob0 (no inline) */
#define __builtin_unreachable() __assume(0)
#endif
#endif
// portable version of posix_memalign
static inline void *roaring_bitmap_aligned_malloc(size_t alignment, size_t size) {
void *p;
#ifdef _MSC_VER
p = _aligned_malloc(size, alignment);
#elif defined(__MINGW32__) || defined(__MINGW64__)
p = __mingw_aligned_malloc(size, alignment);
#else
// somehow, if this is used before including "x86intrin.h", it creates an
// implicit defined warning.
if (posix_memalign(&p, alignment, size) != 0) return NULL;
#endif
return p;
}
static inline void roaring_bitmap_aligned_free(void *memblock) {
#ifdef _MSC_VER
_aligned_free(memblock);
#elif defined(__MINGW32__) || defined(__MINGW64__)
__mingw_aligned_free(memblock);
#else
free(memblock);
#endif
}
#if defined(_MSC_VER)
#define ALIGNED(x) __declspec(align(x))
#else
#if defined(__GNUC__)
#define ALIGNED(x) __attribute__((aligned(x)))
#endif
#endif
#ifdef __GNUC__
#define WARN_UNUSED __attribute__((warn_unused_result))
#else
#define WARN_UNUSED
#endif
#define IS_BIG_ENDIAN (*(uint16_t *)"\0\xff" < 0x100)
static inline int hamming(uint64_t x) {
#ifdef USESSE4
return (int) _mm_popcnt_u64(x);
#else
// won't work under visual studio, but hopeful we have _mm_popcnt_u64 in
// many cases
return __builtin_popcountll(x);
#endif
}
#ifndef UINT64_C
#define UINT64_C(c) (c##ULL)
#endif
#ifndef UINT32_C
#define UINT32_C(c) (c##UL)
#endif
#endif /* INCLUDE_PORTABILITY_H_ */
/* end file include/roaring/portability.h */
/* begin file include/roaring/containers/perfparameters.h */
#ifndef PERFPARAMETERS_H_
#define PERFPARAMETERS_H_
#include <stdbool.h>
/**
During lazy computations, we can transform array containers into bitset
containers as
long as we can expect them to have ARRAY_LAZY_LOWERBOUND values.
*/
enum { ARRAY_LAZY_LOWERBOUND = 1024 };
/* default initial size of a run container
setting it to zero delays the malloc.*/
enum { RUN_DEFAULT_INIT_SIZE = 0 };
/* default initial size of an array container
setting it to zero delays the malloc */
enum { ARRAY_DEFAULT_INIT_SIZE = 0 };
/* automatic bitset conversion during lazy or */
#ifndef LAZY_OR_BITSET_CONVERSION
#define LAZY_OR_BITSET_CONVERSION true
#endif
/* automatically attempt to convert a bitset to a full run during lazy
* evaluation */
#ifndef LAZY_OR_BITSET_CONVERSION_TO_FULL
#define LAZY_OR_BITSET_CONVERSION_TO_FULL true
#endif
/* automatically attempt to convert a bitset to a full run */
#ifndef OR_BITSET_CONVERSION_TO_FULL
#define OR_BITSET_CONVERSION_TO_FULL true
#endif
#endif
/* end file include/roaring/containers/perfparameters.h */
/* begin file include/roaring/array_util.h */
#ifndef ARRAY_UTIL_H
#define ARRAY_UTIL_H
#include <stddef.h> // for size_t
#include <stdint.h>
/*
* Good old binary search.
* Assumes that array is sorted, has logarithmic complexity.
* if the result is x, then:
* if ( x>0 ) you have array[x] = ikey
* if ( x<0 ) then inserting ikey at position -x-1 in array (insuring that array[-x-1]=ikey)
* keys the array sorted.
*/
static inline int32_t binarySearch(const uint16_t *array, int32_t lenarray,
uint16_t ikey) {
int32_t low = 0;
int32_t high = lenarray - 1;
while (low <= high) {
int32_t middleIndex = (low + high) >> 1;
uint16_t middleValue = array[middleIndex];
if (middleValue < ikey) {
low = middleIndex + 1;
} else if (middleValue > ikey) {
high = middleIndex - 1;
} else {
return middleIndex;
}
}
return -(low + 1);
}
/**
* Galloping search
* Assumes that array is sorted, has logarithmic complexity.
* if the result is x, then if x = length, you have that all values in array between pos and length
* are smaller than min.
* otherwise returns the first index x such that array[x] >= min.
*/
static inline int32_t advanceUntil(const uint16_t *array, int32_t pos,
int32_t length, uint16_t min) {
int32_t lower = pos + 1;
if ((lower >= length) || (array[lower] >= min)) {
return lower;
}
int32_t spansize = 1;
while ((lower + spansize < length) && (array[lower + spansize] < min)) {
spansize <<= 1;
}
int32_t upper = (lower + spansize < length) ? lower + spansize : length - 1;
if (array[upper] == min) {
return upper;
}
if (array[upper] < min) {
// means
// array
// has no
// item
// >= min
// pos = array.length;
return length;
}
// we know that the next-smallest span was too small
lower += (spansize >> 1);
int32_t mid = 0;
while (lower + 1 != upper) {
mid = (lower + upper) >> 1;
if (array[mid] == min) {
return mid;
} else if (array[mid] < min) {
lower = mid;
} else {
upper = mid;
}
}
return upper;
}
/**
* Returns number of elements which are less then $ikey.
* Array elements must be unique and sorted.
*/
static inline int32_t count_less(const uint16_t *array, int32_t lenarray,
uint16_t ikey) {
if (lenarray == 0) return 0;
int32_t pos = binarySearch(array, lenarray, ikey);
return pos >= 0 ? pos : -(pos+1);
}
/**
* Returns number of elements which are greater then $ikey.
* Array elements must be unique and sorted.
*/
static inline int32_t count_greater(const uint16_t *array, int32_t lenarray,
uint16_t ikey) {
if (lenarray == 0) return 0;
int32_t pos = binarySearch(array, lenarray, ikey);
if (pos >= 0) {
return lenarray - (pos+1);
} else {
return lenarray - (-pos-1);
}
}
/**
* From Schlegel et al., Fast Sorted-Set Intersection using SIMD Instructions
* Optimized by D. Lemire on May 3rd 2013
*
* C should have capacity greater than the minimum of s_1 and s_b + 8
* where 8 is sizeof(__m128i)/sizeof(uint16_t).
*/
int32_t intersect_vector16(const uint16_t *__restrict__ A, size_t s_a,
const uint16_t *__restrict__ B, size_t s_b,
uint16_t *C);
/**
* Compute the cardinality of the intersection using SSE4 instructions
*/
int32_t intersect_vector16_cardinality(const uint16_t *__restrict__ A,
size_t s_a,
const uint16_t *__restrict__ B,
size_t s_b);
/* Computes the intersection between one small and one large set of uint16_t.
* Stores the result into buffer and return the number of elements. */
int32_t intersect_skewed_uint16(const uint16_t *smallarray, size_t size_s,
const uint16_t *largearray, size_t size_l,
uint16_t *buffer);
/* Computes the size of the intersection between one small and one large set of
* uint16_t. */
int32_t intersect_skewed_uint16_cardinality(const uint16_t *smallarray,
size_t size_s,
const uint16_t *largearray,
size_t size_l);
/* Check whether the size of the intersection between one small and one large set of uint16_t is non-zero. */
bool intersect_skewed_uint16_nonempty(const uint16_t *smallarray, size_t size_s,
const uint16_t *largearray, size_t size_l);
/**
* Generic intersection function.
*/
int32_t intersect_uint16(const uint16_t *A, const size_t lenA,
const uint16_t *B, const size_t lenB, uint16_t *out);
/**
* Compute the size of the intersection (generic).
*/
int32_t intersect_uint16_cardinality(const uint16_t *A, const size_t lenA,
const uint16_t *B, const size_t lenB);
/**
* Checking whether the size of the intersection is non-zero.
*/
bool intersect_uint16_nonempty(const uint16_t *A, const size_t lenA,
const uint16_t *B, const size_t lenB);
/**
* Generic union function.
*/
size_t union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
size_t size_2, uint16_t *buffer);
/**
* Generic XOR function.
*/
int32_t xor_uint16(const uint16_t *array_1, int32_t card_1,
const uint16_t *array_2, int32_t card_2, uint16_t *out);
/**
* Generic difference function (ANDNOT).
*/
int difference_uint16(const uint16_t *a1, int length1, const uint16_t *a2,
int length2, uint16_t *a_out);
/**
* Generic intersection function.
*/
size_t intersection_uint32(const uint32_t *A, const size_t lenA,
const uint32_t *B, const size_t lenB, uint32_t *out);
/**
* Generic intersection function, returns just the cardinality.
*/
size_t intersection_uint32_card(const uint32_t *A, const size_t lenA,
const uint32_t *B, const size_t lenB);
/**
* Generic union function.
*/
size_t union_uint32(const uint32_t *set_1, size_t size_1, const uint32_t *set_2,
size_t size_2, uint32_t *buffer);
/**
* A fast SSE-based union function.
*/
uint32_t union_vector16(const uint16_t *__restrict__ set_1, uint32_t size_1,
const uint16_t *__restrict__ set_2, uint32_t size_2,
uint16_t *__restrict__ buffer);
/**
* A fast SSE-based XOR function.
*/
uint32_t xor_vector16(const uint16_t *__restrict__ array1, uint32_t length1,
const uint16_t *__restrict__ array2, uint32_t length2,
uint16_t *__restrict__ output);
/**
* A fast SSE-based difference function.
*/
int32_t difference_vector16(const uint16_t *__restrict__ A, size_t s_a,
const uint16_t *__restrict__ B, size_t s_b,
uint16_t *C);
/**
* Generic union function, returns just the cardinality.
*/
size_t union_uint32_card(const uint32_t *set_1, size_t size_1,
const uint32_t *set_2, size_t size_2);
/**
* combines union_uint16 and union_vector16 optimally
*/
size_t fast_union_uint16(const uint16_t *set_1, size_t size_1, const uint16_t *set_2,
size_t size_2, uint16_t *buffer);
bool memequals(const void *s1, const void *s2, size_t n);
#endif
/* end file include/roaring/array_util.h */
/* begin file include/roaring/roaring_types.h */
/*
Typedefs used by various components
*/
#ifndef ROARING_TYPES_H
#define ROARING_TYPES_H
typedef bool (*roaring_iterator)(uint32_t value, void *param);
typedef bool (*roaring_iterator64)(uint64_t value, void *param);
/**
* (For advanced users.)
* The roaring_statistics_t can be used to collect detailed statistics about
* the composition of a roaring bitmap.
*/
typedef struct roaring_statistics_s {
uint32_t n_containers; /* number of containers */
uint32_t n_array_containers; /* number of array containers */
uint32_t n_run_containers; /* number of run containers */
uint32_t n_bitset_containers; /* number of bitmap containers */
uint32_t
n_values_array_containers; /* number of values in array containers */
uint32_t n_values_run_containers; /* number of values in run containers */
uint32_t
n_values_bitset_containers; /* number of values in bitmap containers */
uint32_t n_bytes_array_containers; /* number of allocated bytes in array
containers */
uint32_t n_bytes_run_containers; /* number of allocated bytes in run
containers */
uint32_t n_bytes_bitset_containers; /* number of allocated bytes in bitmap
containers */
uint32_t
max_value; /* the maximal value, undefined if cardinality is zero */
uint32_t
min_value; /* the minimal value, undefined if cardinality is zero */
uint64_t sum_value; /* the sum of all values (could be used to compute
average) */
uint64_t cardinality; /* total number of values stored in the bitmap */
// and n_values_arrays, n_values_rle, n_values_bitmap
} roaring_statistics_t;
#endif /* ROARING_TYPES_H */
/* end file include/roaring/roaring_types.h */
/* begin file include/roaring/utilasm.h */
/*
* utilasm.h
*
*/
#ifndef INCLUDE_UTILASM_H_
#define INCLUDE_UTILASM_H_
#if defined(USE_BMI) & defined(ROARING_INLINE_ASM)
#define ASMBITMANIPOPTIMIZATION // optimization flag
#define ASM_SHIFT_RIGHT(srcReg, bitsReg, destReg) \
__asm volatile("shrx %1, %2, %0" \
: "=r"(destReg) \
: /* write */ \
"r"(bitsReg), /* read only */ \
"r"(srcReg) /* read only */ \
)
#define ASM_INPLACESHIFT_RIGHT(srcReg, bitsReg) \
__asm volatile("shrx %1, %0, %0" \
: "+r"(srcReg) \
: /* read/write */ \
"r"(bitsReg) /* read only */ \
)
#define ASM_SHIFT_LEFT(srcReg, bitsReg, destReg) \
__asm volatile("shlx %1, %2, %0" \
: "=r"(destReg) \
: /* write */ \
"r"(bitsReg), /* read only */ \
"r"(srcReg) /* read only */ \
)
// set bit at position testBit within testByte to 1 and
// copy cmovDst to cmovSrc if that bit was previously clear
#define ASM_SET_BIT_INC_WAS_CLEAR(testByte, testBit, count) \
__asm volatile( \
"bts %2, %0\n" \
"sbb $-1, %1\n" \
: "+r"(testByte), /* read/write */ \
"+r"(count) \
: /* read/write */ \
"r"(testBit) /* read only */ \
)
#define ASM_CLEAR_BIT_DEC_WAS_SET(testByte, testBit, count) \
__asm volatile( \
"btr %2, %0\n" \
"sbb $0, %1\n" \
: "+r"(testByte), /* read/write */ \
"+r"(count) \
: /* read/write */ \
"r"(testBit) /* read only */ \
)
#define ASM_BT64(testByte, testBit, count) \
__asm volatile( \
"bt %2,%1\n" \
"sbb %0,%0" /*could use setb */ \
: "=r"(count) \
: /* write */ \
"r"(testByte), /* read only */ \
"r"(testBit) /* read only */ \
)
#endif // USE_BMI
#endif /* INCLUDE_UTILASM_H_ */
/* end file include/roaring/utilasm.h */
/* begin file include/roaring/bitset_util.h */
#ifndef BITSET_UTIL_H
#define BITSET_UTIL_H
#include <stdint.h>
/*
* Set all bits in indexes [begin,end) to true.
*/
static inline void bitset_set_range(uint64_t *bitmap, uint32_t start,
uint32_t end) {
if (start == end) return;
uint32_t firstword = start / 64;
uint32_t endword = (end - 1) / 64;
if (firstword == endword) {
bitmap[firstword] |= ((~UINT64_C(0)) << (start % 64)) &
((~UINT64_C(0)) >> ((~end + 1) % 64));
return;
}
bitmap[firstword] |= (~UINT64_C(0)) << (start % 64);
for (uint32_t i = firstword + 1; i < endword; i++) bitmap[i] = ~UINT64_C(0);
bitmap[endword] |= (~UINT64_C(0)) >> ((~end + 1) % 64);
}
/*
* Find the cardinality of the bitset in [begin,begin+lenminusone]
*/
static inline int bitset_lenrange_cardinality(uint64_t *bitmap, uint32_t start,
uint32_t lenminusone) {
uint32_t firstword = start / 64;
uint32_t endword = (start + lenminusone) / 64;
if (firstword == endword) {
return hamming(bitmap[firstword] &
((~UINT64_C(0)) >> ((63 - lenminusone) % 64))
<< (start % 64));
}
int answer = hamming(bitmap[firstword] & ((~UINT64_C(0)) << (start % 64)));
for (uint32_t i = firstword + 1; i < endword; i++) {
answer += hamming(bitmap[i]);
}
answer +=
hamming(bitmap[endword] &
(~UINT64_C(0)) >> (((~start + 1) - lenminusone - 1) % 64));
return answer;
}
/*
* Check whether the cardinality of the bitset in [begin,begin+lenminusone] is 0
*/
static inline bool bitset_lenrange_empty(uint64_t *bitmap, uint32_t start,
uint32_t lenminusone) {
uint32_t firstword = start / 64;
uint32_t endword = (start + lenminusone) / 64;
if (firstword == endword) {
return (bitmap[firstword] & ((~UINT64_C(0)) >> ((63 - lenminusone) % 64))
<< (start % 64)) == 0;
}
if(((bitmap[firstword] & ((~UINT64_C(0)) << (start%64)))) != 0) return false;
for (uint32_t i = firstword + 1; i < endword; i++) {
if(bitmap[i] != 0) return false;
}
if((bitmap[endword] & (~UINT64_C(0)) >> (((~start + 1) - lenminusone - 1) % 64)) != 0) return false;
return true;
}
/*
* Set all bits in indexes [begin,begin+lenminusone] to true.
*/
static inline void bitset_set_lenrange(uint64_t *bitmap, uint32_t start,
uint32_t lenminusone) {
uint32_t firstword = start / 64;
uint32_t endword = (start + lenminusone) / 64;
if (firstword == endword) {
bitmap[firstword] |= ((~UINT64_C(0)) >> ((63 - lenminusone) % 64))
<< (start % 64);
return;
}
uint64_t temp = bitmap[endword];
bitmap[firstword] |= (~UINT64_C(0)) << (start % 64);
for (uint32_t i = firstword + 1; i < endword; i += 2)
bitmap[i] = bitmap[i + 1] = ~UINT64_C(0);
bitmap[endword] =
temp | (~UINT64_C(0)) >> (((~start + 1) - lenminusone - 1) % 64);
}
/*
* Flip all the bits in indexes [begin,end).
*/
static inline void bitset_flip_range(uint64_t *bitmap, uint32_t start,
uint32_t end) {
if (start == end) return;
uint32_t firstword = start / 64;
uint32_t endword = (end - 1) / 64;
bitmap[firstword] ^= ~((~UINT64_C(0)) << (start % 64));
for (uint32_t i = firstword; i < endword; i++) bitmap[i] = ~bitmap[i];
bitmap[endword] ^= ((~UINT64_C(0)) >> ((~end + 1) % 64));
}
/*
* Set all bits in indexes [begin,end) to false.
*/
static inline void bitset_reset_range(uint64_t *bitmap, uint32_t start,
uint32_t end) {
if (start == end) return;
uint32_t firstword = start / 64;
uint32_t endword = (end - 1) / 64;
if (firstword == endword) {
bitmap[firstword] &= ~(((~UINT64_C(0)) << (start % 64)) &
((~UINT64_C(0)) >> ((~end + 1) % 64)));
return;
}
bitmap[firstword] &= ~((~UINT64_C(0)) << (start % 64));
for (uint32_t i = firstword + 1; i < endword; i++) bitmap[i] = UINT64_C(0);
bitmap[endword] &= ~((~UINT64_C(0)) >> ((~end + 1) % 64));
}
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out", values start at "base".
*
* The "out" pointer should be sufficient to store the actual number of bits
* set.
*
* Returns how many values were actually decoded.
*
* This function should only be expected to be faster than
* bitset_extract_setbits
* when the density of the bitset is high.
*
* This function uses AVX2 decoding.
*/
size_t bitset_extract_setbits_avx2(uint64_t *bitset, size_t length, void *vout,
size_t outcapacity, uint32_t base);
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out", values start at "base".
*
* The "out" pointer should be sufficient to store the actual number of bits
*set.
*
* Returns how many values were actually decoded.
*/
size_t bitset_extract_setbits(uint64_t *bitset, size_t length, void *vout,
uint32_t base);
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out" as 16-bit integers, values start at "base" (can
*be set to zero)
*
* The "out" pointer should be sufficient to store the actual number of bits
*set.
*
* Returns how many values were actually decoded.
*
* This function should only be expected to be faster than
*bitset_extract_setbits_uint16
* when the density of the bitset is high.
*
* This function uses SSE decoding.
*/
size_t bitset_extract_setbits_sse_uint16(const uint64_t *bitset, size_t length,
uint16_t *out, size_t outcapacity,
uint16_t base);
/*
* Given a bitset containing "length" 64-bit words, write out the position
* of all the set bits to "out", values start at "base"
* (can be set to zero)
*
* The "out" pointer should be sufficient to store the actual number of bits
*set.
*
* Returns how many values were actually decoded.
*/
size_t bitset_extract_setbits_uint16(const uint64_t *bitset, size_t length,
uint16_t *out, uint16_t base);
/*
* Given two bitsets containing "length" 64-bit words, write out the position
* of all the common set bits to "out", values start at "base"
* (can be set to zero)
*
* The "out" pointer should be sufficient to store the actual number of bits
* set.
*
* Returns how many values were actually decoded.
*/
size_t bitset_extract_intersection_setbits_uint16(const uint64_t * __restrict__ bitset1,
const uint64_t * __restrict__ bitset2,
size_t length, uint16_t *out,
uint16_t base);
/*
* Given a bitset having cardinality card, set all bit values in the list (there
* are length of them)
* and return the updated cardinality. This evidently assumes that the bitset
* already contained data.
*/
uint64_t bitset_set_list_withcard(void *bitset, uint64_t card,
const uint16_t *list, uint64_t length);
/*
* Given a bitset, set all bit values in the list (there
* are length of them).
*/
void bitset_set_list(void *bitset, const uint16_t *list, uint64_t length);
/*
* Given a bitset having cardinality card, unset all bit values in the list
* (there are length of them)
* and return the updated cardinality. This evidently assumes that the bitset
* already contained data.
*/
uint64_t bitset_clear_list(void *bitset, uint64_t card, const uint16_t *list,
uint64_t length);
/*
* Given a bitset having cardinality card, toggle all bit values in the list
* (there are length of them)
* and return the updated cardinality. This evidently assumes that the bitset
* already contained data.
*/
uint64_t bitset_flip_list_withcard(void *bitset, uint64_t card,
const uint16_t *list, uint64_t length);
void bitset_flip_list(void *bitset, const uint16_t *list, uint64_t length);
#ifdef USEAVX
/***
* BEGIN Harley-Seal popcount functions.
*/
/**
* Compute the population count of a 256-bit word
* This is not especially fast, but it is convenient as part of other functions.
*/
static inline __m256i popcount256(__m256i v) {
const __m256i lookuppos = _mm256_setr_epi8(
/* 0 */ 4 + 0, /* 1 */ 4 + 1, /* 2 */ 4 + 1, /* 3 */ 4 + 2,
/* 4 */ 4 + 1, /* 5 */ 4 + 2, /* 6 */ 4 + 2, /* 7 */ 4 + 3,
/* 8 */ 4 + 1, /* 9 */ 4 + 2, /* a */ 4 + 2, /* b */ 4 + 3,
/* c */ 4 + 2, /* d */ 4 + 3, /* e */ 4 + 3, /* f */ 4 + 4,
/* 0 */ 4 + 0, /* 1 */ 4 + 1, /* 2 */ 4 + 1, /* 3 */ 4 + 2,
/* 4 */ 4 + 1, /* 5 */ 4 + 2, /* 6 */ 4 + 2, /* 7 */ 4 + 3,
/* 8 */ 4 + 1, /* 9 */ 4 + 2, /* a */ 4 + 2, /* b */ 4 + 3,
/* c */ 4 + 2, /* d */ 4 + 3, /* e */ 4 + 3, /* f */ 4 + 4);
const __m256i lookupneg = _mm256_setr_epi8(
/* 0 */ 4 - 0, /* 1 */ 4 - 1, /* 2 */ 4 - 1, /* 3 */ 4 - 2,
/* 4 */ 4 - 1, /* 5 */ 4 - 2, /* 6 */ 4 - 2, /* 7 */ 4 - 3,
/* 8 */ 4 - 1, /* 9 */ 4 - 2, /* a */ 4 - 2, /* b */ 4 - 3,
/* c */ 4 - 2, /* d */ 4 - 3, /* e */ 4 - 3, /* f */ 4 - 4,
/* 0 */ 4 - 0, /* 1 */ 4 - 1, /* 2 */ 4 - 1, /* 3 */ 4 - 2,
/* 4 */ 4 - 1, /* 5 */ 4 - 2, /* 6 */ 4 - 2, /* 7 */ 4 - 3,
/* 8 */ 4 - 1, /* 9 */ 4 - 2, /* a */ 4 - 2, /* b */ 4 - 3,
/* c */ 4 - 2, /* d */ 4 - 3, /* e */ 4 - 3, /* f */ 4 - 4);
const __m256i low_mask = _mm256_set1_epi8(0x0f);
const __m256i lo = _mm256_and_si256(v, low_mask);
const __m256i hi = _mm256_and_si256(_mm256_srli_epi16(v, 4), low_mask);
const __m256i popcnt1 = _mm256_shuffle_epi8(lookuppos, lo);
const __m256i popcnt2 = _mm256_shuffle_epi8(lookupneg, hi);
return _mm256_sad_epu8(popcnt1, popcnt2);
}
/**
* Simple CSA over 256 bits
*/
static inline void CSA(__m256i *h, __m256i *l, __m256i a, __m256i b,
__m256i c) {
const __m256i u = _mm256_xor_si256(a, b);
*h = _mm256_or_si256(_mm256_and_si256(a, b), _mm256_and_si256(u, c));
*l = _mm256_xor_si256(u, c);
}
/**
* Fast Harley-Seal AVX population count function
*/
inline static uint64_t avx2_harley_seal_popcount256(const __m256i *data,
const uint64_t size) {
__m256i total = _mm256_setzero_si256();
__m256i ones = _mm256_setzero_si256();
__m256i twos = _mm256_setzero_si256();
__m256i fours = _mm256_setzero_si256();
__m256i eights = _mm256_setzero_si256();
__m256i sixteens = _mm256_setzero_si256();
__m256i twosA, twosB, foursA, foursB, eightsA, eightsB;
const uint64_t limit = size - size % 16;
uint64_t i = 0;
for (; i < limit; i += 16) {
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i),
_mm256_lddqu_si256(data + i + 1));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 2),
_mm256_lddqu_si256(data + i + 3));
CSA(&foursA, &twos, twos, twosA, twosB);
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i + 4),
_mm256_lddqu_si256(data + i + 5));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 6),
_mm256_lddqu_si256(data + i + 7));
CSA(&foursB, &twos, twos, twosA, twosB);
CSA(&eightsA, &fours, fours, foursA, foursB);
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i + 8),
_mm256_lddqu_si256(data + i + 9));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 10),
_mm256_lddqu_si256(data + i + 11));
CSA(&foursA, &twos, twos, twosA, twosB);
CSA(&twosA, &ones, ones, _mm256_lddqu_si256(data + i + 12),
_mm256_lddqu_si256(data + i + 13));
CSA(&twosB, &ones, ones, _mm256_lddqu_si256(data + i + 14),
_mm256_lddqu_si256(data + i + 15));
CSA(&foursB, &twos, twos, twosA, twosB);
CSA(&eightsB, &fours, fours, foursA, foursB);
CSA(&sixteens, &eights, eights, eightsA, eightsB);
total = _mm256_add_epi64(total, popcount256(sixteens));
}
total = _mm256_slli_epi64(total, 4); // * 16
total = _mm256_add_epi64(
total, _mm256_slli_epi64(popcount256(eights), 3)); // += 8 * ...
total = _mm256_add_epi64(
total, _mm256_slli_epi64(popcount256(fours), 2)); // += 4 * ...
total = _mm256_add_epi64(
total, _mm256_slli_epi64(popcount256(twos), 1)); // += 2 * ...
total = _mm256_add_epi64(total, popcount256(ones));
for (; i < size; i++)
total =
_mm256_add_epi64(total, popcount256(_mm256_lddqu_si256(data + i)));
return (uint64_t)(_mm256_extract_epi64(total, 0)) +
(uint64_t)(_mm256_extract_epi64(total, 1)) +
(uint64_t)(_mm256_extract_epi64(total, 2)) +
(uint64_t)(_mm256_extract_epi64(total, 3));
}
#define AVXPOPCNTFNC(opname, avx_intrinsic) \
static inline uint64_t avx2_harley_seal_popcount256_##opname( \
const __m256i *data1, const __m256i *data2, const uint64_t size) { \
__m256i total = _mm256_setzero_si256(); \
__m256i ones = _mm256_setzero_si256(); \
__m256i twos = _mm256_setzero_si256(); \
__m256i fours = _mm256_setzero_si256(); \
__m256i eights = _mm256_setzero_si256(); \
__m256i sixteens = _mm256_setzero_si256(); \
__m256i twosA, twosB, foursA, foursB, eightsA, eightsB; \
__m256i A1, A2; \
const uint64_t limit = size - size % 16; \
uint64_t i = 0; \
for (; i < limit; i += 16) { \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i), \
_mm256_lddqu_si256(data2 + i)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 1), \
_mm256_lddqu_si256(data2 + i + 1)); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 2), \
_mm256_lddqu_si256(data2 + i + 2)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 3), \
_mm256_lddqu_si256(data2 + i + 3)); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursA, &twos, twos, twosA, twosB); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 4), \
_mm256_lddqu_si256(data2 + i + 4)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 5), \
_mm256_lddqu_si256(data2 + i + 5)); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 6), \
_mm256_lddqu_si256(data2 + i + 6)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 7), \
_mm256_lddqu_si256(data2 + i + 7)); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursB, &twos, twos, twosA, twosB); \
CSA(&eightsA, &fours, fours, foursA, foursB); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 8), \
_mm256_lddqu_si256(data2 + i + 8)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 9), \
_mm256_lddqu_si256(data2 + i + 9)); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 10), \
_mm256_lddqu_si256(data2 + i + 10)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 11), \
_mm256_lddqu_si256(data2 + i + 11)); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursA, &twos, twos, twosA, twosB); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 12), \
_mm256_lddqu_si256(data2 + i + 12)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 13), \
_mm256_lddqu_si256(data2 + i + 13)); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 14), \
_mm256_lddqu_si256(data2 + i + 14)); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 15), \
_mm256_lddqu_si256(data2 + i + 15)); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursB, &twos, twos, twosA, twosB); \
CSA(&eightsB, &fours, fours, foursA, foursB); \
CSA(&sixteens, &eights, eights, eightsA, eightsB); \
total = _mm256_add_epi64(total, popcount256(sixteens)); \
} \
total = _mm256_slli_epi64(total, 4); \
total = _mm256_add_epi64(total, \
_mm256_slli_epi64(popcount256(eights), 3)); \
total = \
_mm256_add_epi64(total, _mm256_slli_epi64(popcount256(fours), 2)); \
total = \
_mm256_add_epi64(total, _mm256_slli_epi64(popcount256(twos), 1)); \
total = _mm256_add_epi64(total, popcount256(ones)); \
for (; i < size; i++) { \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i), \
_mm256_lddqu_si256(data2 + i)); \
total = _mm256_add_epi64(total, popcount256(A1)); \
} \
return (uint64_t)(_mm256_extract_epi64(total, 0)) + \
(uint64_t)(_mm256_extract_epi64(total, 1)) + \
(uint64_t)(_mm256_extract_epi64(total, 2)) + \
(uint64_t)(_mm256_extract_epi64(total, 3)); \
} \
static inline uint64_t avx2_harley_seal_popcount256andstore_##opname( \
const __m256i *__restrict__ data1, const __m256i *__restrict__ data2, \
__m256i *__restrict__ out, const uint64_t size) { \
__m256i total = _mm256_setzero_si256(); \
__m256i ones = _mm256_setzero_si256(); \
__m256i twos = _mm256_setzero_si256(); \
__m256i fours = _mm256_setzero_si256(); \
__m256i eights = _mm256_setzero_si256(); \
__m256i sixteens = _mm256_setzero_si256(); \
__m256i twosA, twosB, foursA, foursB, eightsA, eightsB; \
__m256i A1, A2; \
const uint64_t limit = size - size % 16; \
uint64_t i = 0; \
for (; i < limit; i += 16) { \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i), \
_mm256_lddqu_si256(data2 + i)); \
_mm256_storeu_si256(out + i, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 1), \
_mm256_lddqu_si256(data2 + i + 1)); \
_mm256_storeu_si256(out + i + 1, A2); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 2), \
_mm256_lddqu_si256(data2 + i + 2)); \
_mm256_storeu_si256(out + i + 2, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 3), \
_mm256_lddqu_si256(data2 + i + 3)); \
_mm256_storeu_si256(out + i + 3, A2); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursA, &twos, twos, twosA, twosB); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 4), \
_mm256_lddqu_si256(data2 + i + 4)); \
_mm256_storeu_si256(out + i + 4, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 5), \
_mm256_lddqu_si256(data2 + i + 5)); \
_mm256_storeu_si256(out + i + 5, A2); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 6), \
_mm256_lddqu_si256(data2 + i + 6)); \
_mm256_storeu_si256(out + i + 6, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 7), \
_mm256_lddqu_si256(data2 + i + 7)); \
_mm256_storeu_si256(out + i + 7, A2); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursB, &twos, twos, twosA, twosB); \
CSA(&eightsA, &fours, fours, foursA, foursB); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 8), \
_mm256_lddqu_si256(data2 + i + 8)); \
_mm256_storeu_si256(out + i + 8, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 9), \
_mm256_lddqu_si256(data2 + i + 9)); \
_mm256_storeu_si256(out + i + 9, A2); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 10), \
_mm256_lddqu_si256(data2 + i + 10)); \
_mm256_storeu_si256(out + i + 10, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 11), \
_mm256_lddqu_si256(data2 + i + 11)); \
_mm256_storeu_si256(out + i + 11, A2); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursA, &twos, twos, twosA, twosB); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 12), \
_mm256_lddqu_si256(data2 + i + 12)); \
_mm256_storeu_si256(out + i + 12, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 13), \
_mm256_lddqu_si256(data2 + i + 13)); \
_mm256_storeu_si256(out + i + 13, A2); \
CSA(&twosA, &ones, ones, A1, A2); \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 14), \
_mm256_lddqu_si256(data2 + i + 14)); \
_mm256_storeu_si256(out + i + 14, A1); \
A2 = avx_intrinsic(_mm256_lddqu_si256(data1 + i + 15), \
_mm256_lddqu_si256(data2 + i + 15)); \
_mm256_storeu_si256(out + i + 15, A2); \
CSA(&twosB, &ones, ones, A1, A2); \
CSA(&foursB, &twos, twos, twosA, twosB); \
CSA(&eightsB, &fours, fours, foursA, foursB); \
CSA(&sixteens, &eights, eights, eightsA, eightsB); \
total = _mm256_add_epi64(total, popcount256(sixteens)); \
} \
total = _mm256_slli_epi64(total, 4); \
total = _mm256_add_epi64(total, \
_mm256_slli_epi64(popcount256(eights), 3)); \
total = \
_mm256_add_epi64(total, _mm256_slli_epi64(popcount256(fours), 2)); \
total = \
_mm256_add_epi64(total, _mm256_slli_epi64(popcount256(twos), 1)); \
total = _mm256_add_epi64(total, popcount256(ones)); \
for (; i < size; i++) { \
A1 = avx_intrinsic(_mm256_lddqu_si256(data1 + i), \
_mm256_lddqu_si256(data2 + i)); \
_mm256_storeu_si256(out + i, A1); \
total = _mm256_add_epi64(total, popcount256(A1)); \
} \
return (uint64_t)(_mm256_extract_epi64(total, 0)) + \
(uint64_t)(_mm256_extract_epi64(total, 1)) + \
(uint64_t)(_mm256_extract_epi64(total, 2)) + \
(uint64_t)(_mm256_extract_epi64(total, 3)); \
}
AVXPOPCNTFNC(or, _mm256_or_si256)
AVXPOPCNTFNC(union, _mm256_or_si256)
AVXPOPCNTFNC(and, _mm256_and_si256)
AVXPOPCNTFNC(intersection, _mm256_and_si256)
AVXPOPCNTFNC (xor, _mm256_xor_si256)
AVXPOPCNTFNC(andnot, _mm256_andnot_si256)
/***
* END Harley-Seal popcount functions.
*/
#endif // USEAVX
#endif
/* end file include/roaring/bitset_util.h */
/* begin file include/roaring/containers/array.h */
/*
* array.h
*
*/
#ifndef INCLUDE_CONTAINERS_ARRAY_H_
#define INCLUDE_CONTAINERS_ARRAY_H_
#include <string.h>
/* Containers with DEFAULT_MAX_SIZE or less integers should be arrays */
enum { DEFAULT_MAX_SIZE = 4096 };
/* struct array_container - sparse representation of a bitmap
*
* @cardinality: number of indices in `array` (and the bitmap)
* @capacity: allocated size of `array`
* @array: sorted list of integers
*/
struct array_container_s {
int32_t cardinality;
int32_t capacity;
uint16_t *array;
};
typedef struct array_container_s array_container_t;
/* Create a new array with default. Return NULL in case of failure. See also
* array_container_create_given_capacity. */
array_container_t *array_container_create(void);
/* Create a new array with a specified capacity size. Return NULL in case of
* failure. */
array_container_t *array_container_create_given_capacity(int32_t size);
/* Create a new array containing all values in [min,max). */
array_container_t * array_container_create_range(uint32_t min, uint32_t max);
/*
* Shrink the capacity to the actual size, return the number of bytes saved.
*/
int array_container_shrink_to_fit(array_container_t *src);
/* Free memory owned by `array'. */
void array_container_free(array_container_t *array);
/* Duplicate container */
array_container_t *array_container_clone(const array_container_t *src);
int32_t array_container_serialize(const array_container_t *container,
char *buf) WARN_UNUSED;
uint32_t array_container_serialization_len(const array_container_t *container);
void *array_container_deserialize(const char *buf, size_t buf_len);
/* Get the cardinality of `array'. */
static inline int array_container_cardinality(const array_container_t *array) {
return array->cardinality;
}
static inline bool array_container_nonzero_cardinality(
const array_container_t *array) {
return array->cardinality > 0;
}
/* Copy one container into another. We assume that they are distinct. */
void array_container_copy(const array_container_t *src, array_container_t *dst);
/* Add all the values in [min,max) (included) at a distance k*step from min.
The container must have a size less or equal to DEFAULT_MAX_SIZE after this
addition. */
void array_container_add_from_range(array_container_t *arr, uint32_t min,
uint32_t max, uint16_t step);
/* Set the cardinality to zero (does not release memory). */
static inline void array_container_clear(array_container_t *array) {
array->cardinality = 0;
}
static inline bool array_container_empty(const array_container_t *array) {
return array->cardinality == 0;
}
/* check whether the cardinality is equal to the capacity (this does not mean
* that it contains 1<<16 elements) */
static inline bool array_container_full(const array_container_t *array) {
return array->cardinality == array->capacity;
}
/* Compute the union of `src_1' and `src_2' and write the result to `dst'
* It is assumed that `dst' is distinct from both `src_1' and `src_2'. */
void array_container_union(const array_container_t *src_1,
const array_container_t *src_2,
array_container_t *dst);
/* symmetric difference, see array_container_union */
void array_container_xor(const array_container_t *array_1,
const array_container_t *array_2,
array_container_t *out);
/* Computes the intersection of src_1 and src_2 and write the result to
* dst. It is assumed that dst is distinct from both src_1 and src_2. */
void array_container_intersection(const array_container_t *src_1,
const array_container_t *src_2,
array_container_t *dst);
/* Check whether src_1 and src_2 intersect. */
bool array_container_intersect(const array_container_t *src_1,
const array_container_t *src_2);
/* computers the size of the intersection between two arrays.
*/
int array_container_intersection_cardinality(const array_container_t *src_1,
const array_container_t *src_2);
/* computes the intersection of array1 and array2 and write the result to
* array1.
* */
void array_container_intersection_inplace(array_container_t *src_1,
const array_container_t *src_2);
/*
* Write out the 16-bit integers contained in this container as a list of 32-bit
* integers using base
* as the starting value (it might be expected that base has zeros in its 16
* least significant bits).
* The function returns the number of values written.
* The caller is responsible for allocating enough memory in out.
*/
int array_container_to_uint32_array(void *vout, const array_container_t *cont,
uint32_t base);
/* Compute the number of runs */
int32_t array_container_number_of_runs(const array_container_t *a);
/*
* Print this container using printf (useful for debugging).
*/
void array_container_printf(const array_container_t *v);
/*
* Print this container using printf as a comma-separated list of 32-bit
* integers starting at base.
*/
void array_container_printf_as_uint32_array(const array_container_t *v,
uint32_t base);
/**
* Return the serialized size in bytes of a container having cardinality "card".
*/
static inline int32_t array_container_serialized_size_in_bytes(int32_t card) {
return card * 2 + 2;
}
/**
* Increase capacity to at least min.
* Whether the existing data needs to be copied over depends on the "preserve"
* parameter. If preserve is false, then the new content will be uninitialized,
* otherwise the old content is copied.
*/
void array_container_grow(array_container_t *container, int32_t min,
bool preserve);
bool array_container_iterate(const array_container_t *cont, uint32_t base,
roaring_iterator iterator, void *ptr);
bool array_container_iterate64(const array_container_t *cont, uint32_t base,
roaring_iterator64 iterator, uint64_t high_bits,
void *ptr);
/**
* Writes the underlying array to buf, outputs how many bytes were written.
* This is meant to be byte-by-byte compatible with the Java and Go versions of
* Roaring.
* The number of bytes written should be
* array_container_size_in_bytes(container).
*
*/
int32_t array_container_write(const array_container_t *container, char *buf);
/**
* Reads the instance from buf, outputs how many bytes were read.
* This is meant to be byte-by-byte compatible with the Java and Go versions of
* Roaring.
* The number of bytes read should be array_container_size_in_bytes(container).
* You need to provide the (known) cardinality.
*/
int32_t array_container_read(int32_t cardinality, array_container_t *container,
const char *buf);
/**
* Return the serialized size in bytes of a container (see
* bitset_container_write)
* This is meant to be compatible with the Java and Go versions of Roaring and
* assumes
* that the cardinality of the container is already known.
*
*/
static inline int32_t array_container_size_in_bytes(
const array_container_t *container) {
return container->cardinality * sizeof(uint16_t);
}
/**
* Return true if the two arrays have the same content.
*/
static inline bool array_container_equals(
const array_container_t *container1,
const array_container_t *container2) {
if (container1->cardinality != container2->cardinality) {
return false;
}
return memequals(container1->array, container2->array, container1->cardinality*2);
}
/**
* Return true if container1 is a subset of container2.
*/
bool array_container_is_subset(const array_container_t *container1,
const array_container_t *container2);
/**
* If the element of given rank is in this container, supposing that the first
* element has rank start_rank, then the function returns true and sets element
* accordingly.
* Otherwise, it returns false and update start_rank.
*/
static inline bool array_container_select(const array_container_t *container,
uint32_t *start_rank, uint32_t rank,
uint32_t *element) {
int card = array_container_cardinality(container);
if (*start_rank + card <= rank) {
*start_rank += card;
return false;
} else {
*element = container->array[rank - *start_rank];
return true;
}
}
/* Computes the difference of array1 and array2 and write the result
* to array out.
* Array out does not need to be distinct from array_1
*/
void array_container_andnot(const array_container_t *array_1,
const array_container_t *array_2,
array_container_t *out);
/* Append x to the set. Assumes that the value is larger than any preceding
* values. */
static inline void array_container_append(array_container_t *arr,
uint16_t pos) {
const int32_t capacity = arr->capacity;
if (array_container_full(arr)) {
array_container_grow(arr, capacity + 1, true);
}
arr->array[arr->cardinality++] = pos;
}
/**
* Add value to the set if final cardinality doesn't exceed max_cardinality.
* Return code:
* 1 -- value was added
* 0 -- value was already present
* -1 -- value was not added because cardinality would exceed max_cardinality
*/
static inline int array_container_try_add(array_container_t *arr, uint16_t value,
int32_t max_cardinality) {
const int32_t cardinality = arr->cardinality;
// best case, we can append.
if ((array_container_empty(arr) || arr->array[cardinality - 1] < value) &&
cardinality < max_cardinality) {
array_container_append(arr, value);
return 1;
}
const int32_t loc = binarySearch(arr->array, cardinality, value);
if (loc >= 0) {
return 0;
} else if (cardinality < max_cardinality) {
if (array_container_full(arr)) {
array_container_grow(arr, arr->capacity + 1, true);
}
const int32_t insert_idx = -loc - 1;
memmove(arr->array + insert_idx + 1, arr->array + insert_idx,
(cardinality - insert_idx) * sizeof(uint16_t));
arr->array[insert_idx] = value;
arr->cardinality++;
return 1;
} else {
return -1;
}
}
/* Add value to the set. Returns true if x was not already present. */
static inline bool array_container_add(array_container_t *arr, uint16_t value) {
return array_container_try_add(arr, value, INT32_MAX) == 1;
}
/* Remove x from the set. Returns true if x was present. */
static inline bool array_container_remove(array_container_t *arr,
uint16_t pos) {
const int32_t idx = binarySearch(arr->array, arr->cardinality, pos);
const bool is_present = idx >= 0;
if (is_present) {
memmove(arr->array + idx, arr->array + idx + 1,
(arr->cardinality - idx - 1) * sizeof(uint16_t));
arr->cardinality--;
}
return is_present;
}
/* Check whether x is present. */
static inline bool array_container_contains(const array_container_t *arr,
uint16_t pos) {
// return binarySearch(arr->array, arr->cardinality, pos) >= 0;
// binary search with fallback to linear search for short ranges
int32_t low = 0;
const uint16_t * carr = (const uint16_t *) arr->array;
int32_t high = arr->cardinality - 1;
// while (high - low >= 0) {
while(high >= low + 16) {
int32_t middleIndex = (low + high)>>1;
uint16_t middleValue = carr[middleIndex];
if (middleValue < pos) {
low = middleIndex + 1;
} else if (middleValue > pos) {
high = middleIndex - 1;
} else {
return true;
}
}
for (int i=low; i <= high; i++) {
uint16_t v = carr[i];
if (v == pos) {
return true;
}
if ( v > pos ) return false;
}
return false;
}
//* Check whether a range of values from range_start (included) to range_end (excluded) is present. */
static inline bool array_container_contains_range(const array_container_t *arr,
uint32_t range_start, uint32_t range_end) {
const uint16_t rs_included = range_start;
const uint16_t re_included = range_end - 1;
const uint16_t *carr = (const uint16_t *) arr->array;
const int32_t start = advanceUntil(carr, -1, arr->cardinality, rs_included);
const int32_t end = advanceUntil(carr, start - 1, arr->cardinality, re_included);
return (start < arr->cardinality) && (end < arr->cardinality)
&& (((uint16_t)(end - start)) == re_included - rs_included)
&& (carr[start] == rs_included) && (carr[end] == re_included);
}
/* Returns the smallest value (assumes not empty) */
static inline uint16_t array_container_minimum(const array_container_t *arr) {
if (arr->cardinality == 0) return 0;
return arr->array[0];
}
/* Returns the largest value (assumes not empty) */
static inline uint16_t array_container_maximum(const array_container_t *arr) {
if (arr->cardinality == 0) return 0;
return arr->array[arr->cardinality - 1];
}
/* Returns the number of values equal or smaller than x */
static inline int array_container_rank(const array_container_t *arr, uint16_t x) {
const int32_t idx = binarySearch(arr->array, arr->cardinality, x);
const bool is_present = idx >= 0;
if (is_present) {
return idx + 1;
} else {
return -idx - 1;
}
}
/* Returns the index of the first value equal or smaller than x, or -1 */
static inline int array_container_index_equalorlarger(const array_container_t *arr, uint16_t x) {
const int32_t idx = binarySearch(arr->array, arr->cardinality, x);
const bool is_present = idx >= 0;
if (is_present) {
return idx;
} else {
int32_t candidate = - idx - 1;
if(candidate < arr->cardinality) return candidate;
return -1;
}
}
/*
* Adds all values in range [min,max] using hint:
* nvals_less is the number of array values less than $min
* nvals_greater is the number of array values greater than $max
*/
static inline void array_container_add_range_nvals(array_container_t *array,
uint32_t min, uint32_t max,
int32_t nvals_less,
int32_t nvals_greater) {
int32_t union_cardinality = nvals_less + (max - min + 1) + nvals_greater;
if (union_cardinality > array->capacity) {
array_container_grow(array, union_cardinality, true);
}
memmove(&(array->array[union_cardinality - nvals_greater]),
&(array->array[array->cardinality - nvals_greater]),
nvals_greater * sizeof(uint16_t));
for (uint32_t i = 0; i <= max - min; i++) {
array->array[nvals_less + i] = min + i;
}
array->cardinality = union_cardinality;
}
/**
* Adds all values in range [min,max].
*/
static inline void array_container_add_range(array_container_t *array,
uint32_t min, uint32_t max) {
int32_t nvals_greater = count_greater(array->array, array->cardinality, max);
int32_t nvals_less = count_less(array->array, array->cardinality - nvals_greater, min);
array_container_add_range_nvals(array, min, max, nvals_less, nvals_greater);
}
/*
* Removes all elements array[pos] .. array[pos+count-1]
*/
static inline void array_container_remove_range(array_container_t *array,
uint32_t pos, uint32_t count) {
if (count != 0) {
memmove(&(array->array[pos]), &(array->array[pos+count]),
(array->cardinality - pos - count) * sizeof(uint16_t));
array->cardinality -= count;
}
}
#endif /* INCLUDE_CONTAINERS_ARRAY_H_ */
/* end file include/roaring/containers/array.h */
/* begin file include/roaring/containers/bitset.h */
/*
* bitset.h
*
*/
#ifndef INCLUDE_CONTAINERS_BITSET_H_
#define INCLUDE_CONTAINERS_BITSET_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef USEAVX
#define ALIGN_AVX __attribute__((aligned(sizeof(__m256i))))
#else
#define ALIGN_AVX
#endif
enum {
BITSET_CONTAINER_SIZE_IN_WORDS = (1 << 16) / 64,
BITSET_UNKNOWN_CARDINALITY = -1
};
struct bitset_container_s {
int32_t cardinality;
uint64_t *array;
};
typedef struct bitset_container_s bitset_container_t;
/* Create a new bitset. Return NULL in case of failure. */
bitset_container_t *bitset_container_create(void);
/* Free memory. */
void bitset_container_free(bitset_container_t *bitset);
/* Clear bitset (sets bits to 0). */
void bitset_container_clear(bitset_container_t *bitset);
/* Set all bits to 1. */
void bitset_container_set_all(bitset_container_t *bitset);
/* Duplicate bitset */
bitset_container_t *bitset_container_clone(const bitset_container_t *src);
int32_t bitset_container_serialize(const bitset_container_t *container,
char *buf) WARN_UNUSED;
uint32_t bitset_container_serialization_len(void);
void *bitset_container_deserialize(const char *buf, size_t buf_len);
/* Set the bit in [begin,end). WARNING: as of April 2016, this method is slow
* and
* should not be used in performance-sensitive code. Ever. */
void bitset_container_set_range(bitset_container_t *bitset, uint32_t begin,
uint32_t end);
#ifdef ASMBITMANIPOPTIMIZATION
/* Set the ith bit. */
static inline void bitset_container_set(bitset_container_t *bitset,
uint16_t pos) {
uint64_t shift = 6;
uint64_t offset;
uint64_t p = pos;
ASM_SHIFT_RIGHT(p, shift, offset);
uint64_t load = bitset->array[offset];
ASM_SET_BIT_INC_WAS_CLEAR(load, p, bitset->cardinality);
bitset->array[offset] = load;
}
/* Unset the ith bit. */
static inline void bitset_container_unset(bitset_container_t *bitset,
uint16_t pos) {
uint64_t shift = 6;
uint64_t offset;
uint64_t p = pos;
ASM_SHIFT_RIGHT(p, shift, offset);
uint64_t load = bitset->array[offset];
ASM_CLEAR_BIT_DEC_WAS_SET(load, p, bitset->cardinality);
bitset->array[offset] = load;
}
/* Add `pos' to `bitset'. Returns true if `pos' was not present. Might be slower
* than bitset_container_set. */
static inline bool bitset_container_add(bitset_container_t *bitset,
uint16_t pos) {
uint64_t shift = 6;
uint64_t offset;
uint64_t p = pos;
ASM_SHIFT_RIGHT(p, shift, offset);
uint64_t load = bitset->array[offset];
// could be possibly slightly further optimized
const int32_t oldcard = bitset->cardinality;
ASM_SET_BIT_INC_WAS_CLEAR(load, p, bitset->cardinality);
bitset->array[offset] = load;
return bitset->cardinality - oldcard;
}
/* Remove `pos' from `bitset'. Returns true if `pos' was present. Might be
* slower than bitset_container_unset. */
static inline bool bitset_container_remove(bitset_container_t *bitset,
uint16_t pos) {
uint64_t shift = 6;
uint64_t offset;
uint64_t p = pos;
ASM_SHIFT_RIGHT(p, shift, offset);
uint64_t load = bitset->array[offset];
// could be possibly slightly further optimized
const int32_t oldcard = bitset->cardinality;
ASM_CLEAR_BIT_DEC_WAS_SET(load, p, bitset->cardinality);
bitset->array[offset] = load;
return oldcard - bitset->cardinality;
}
/* Get the value of the ith bit. */
static inline bool bitset_container_get(const bitset_container_t *bitset,
uint16_t pos) {
uint64_t word = bitset->array[pos >> 6];
const uint64_t p = pos;
ASM_INPLACESHIFT_RIGHT(word, p);
return word & 1;
}
#else
/* Set the ith bit. */
static inline void bitset_container_set(bitset_container_t *bitset,
uint16_t pos) {
const uint64_t old_word = bitset->array[pos >> 6];
const int index = pos & 63;
const uint64_t new_word = old_word | (UINT64_C(1) << index);
bitset->cardinality += (uint32_t)((old_word ^ new_word) >> index);
bitset->array[pos >> 6] = new_word;
}
/* Unset the ith bit. */
static inline void bitset_container_unset(bitset_container_t *bitset,
uint16_t pos) {
const uint64_t old_word = bitset->array[pos >> 6];
const int index = pos & 63;
const uint64_t new_word = old_word & (~(UINT64_C(1) << index));
bitset->cardinality -= (uint32_t)((old_word ^ new_word) >> index);
bitset->array[pos >> 6] = new_word;
}
/* Add `pos' to `bitset'. Returns true if `pos' was not present. Might be slower
* than bitset_container_set. */
static inline bool bitset_container_add(bitset_container_t *bitset,
uint16_t pos) {
const uint64_t old_word = bitset->array[pos >> 6];
const int index = pos & 63;
const uint64_t new_word = old_word | (UINT64_C(1) << index);
const uint64_t increment = (old_word ^ new_word) >> index;
bitset->cardinality += (uint32_t)increment;
bitset->array[pos >> 6] = new_word;
return increment > 0;
}
/* Remove `pos' from `bitset'. Returns true if `pos' was present. Might be
* slower than bitset_container_unset. */
static inline bool bitset_container_remove(bitset_container_t *bitset,
uint16_t pos) {
const uint64_t old_word = bitset->array[pos >> 6];
const int index = pos & 63;
const uint64_t new_word = old_word & (~(UINT64_C(1) << index));
const uint64_t increment = (old_word ^ new_word) >> index;
bitset->cardinality -= (uint32_t)increment;
bitset->array[pos >> 6] = new_word;
return increment > 0;
}
/* Get the value of the ith bit. */
static inline bool bitset_container_get(const bitset_container_t *bitset,
uint16_t pos) {
const uint64_t word = bitset->array[pos >> 6];
return (word >> (pos & 63)) & 1;
}
#endif
/*
* Check if all bits are set in a range of positions from pos_start (included) to
* pos_end (excluded).
*/
static inline bool bitset_container_get_range(const bitset_container_t *bitset,
uint32_t pos_start, uint32_t pos_end) {
const uint32_t start = pos_start >> 6;
const uint32_t end = pos_end >> 6;
const uint64_t first = ~((1ULL << (pos_start & 0x3F)) - 1);
const uint64_t last = (1ULL << (pos_end & 0x3F)) - 1;
if (start == end) return ((bitset->array[end] & first & last) == (first & last));
if ((bitset->array[start] & first) != first) return false;
if ((end < BITSET_CONTAINER_SIZE_IN_WORDS) && ((bitset->array[end] & last) != last)){
return false;
}
for (uint16_t i = start + 1; (i < BITSET_CONTAINER_SIZE_IN_WORDS) && (i < end); ++i){
if (bitset->array[i] != UINT64_C(0xFFFFFFFFFFFFFFFF)) return false;
}
return true;
}
/* Check whether `bitset' is present in `array'. Calls bitset_container_get. */
static inline bool bitset_container_contains(const bitset_container_t *bitset,
uint16_t pos) {
return bitset_container_get(bitset, pos);
}
/*
* Check whether a range of bits from position `pos_start' (included) to `pos_end' (excluded)
* is present in `bitset'. Calls bitset_container_get_all.
*/
static inline bool bitset_container_contains_range(const bitset_container_t *bitset,
uint32_t pos_start, uint32_t pos_end) {
return bitset_container_get_range(bitset, pos_start, pos_end);
}
/* Get the number of bits set */
static inline int bitset_container_cardinality(
const bitset_container_t *bitset) {
return bitset->cardinality;
}
/* Copy one container into another. We assume that they are distinct. */
void bitset_container_copy(const bitset_container_t *source,
bitset_container_t *dest);
/* Add all the values [min,max) at a distance k*step from min: min,
* min+step,.... */
void bitset_container_add_from_range(bitset_container_t *bitset, uint32_t min,
uint32_t max, uint16_t step);
/* Get the number of bits set (force computation). This does not modify bitset.
* To update the cardinality, you should do
* bitset->cardinality = bitset_container_compute_cardinality(bitset).*/
int bitset_container_compute_cardinality(const bitset_container_t *bitset);
/* Get whether there is at least one bit set (see bitset_container_empty for the reverse),
when the cardinality is unknown, it is computed and stored in the struct */
static inline bool bitset_container_nonzero_cardinality(
bitset_container_t *bitset) {
// account for laziness
if (bitset->cardinality == BITSET_UNKNOWN_CARDINALITY) {
// could bail early instead with a nonzero result
bitset->cardinality = bitset_container_compute_cardinality(bitset);
}
return bitset->cardinality > 0;
}
/* Check whether this bitset is empty (see bitset_container_nonzero_cardinality for the reverse),
* it never modifies the bitset struct. */
static inline bool bitset_container_empty(
const bitset_container_t *bitset) {
if (bitset->cardinality == BITSET_UNKNOWN_CARDINALITY) {
for (int i = 0; i < BITSET_CONTAINER_SIZE_IN_WORDS; i ++) {
if((bitset->array[i]) != 0) return false;
}
return true;
}
return bitset->cardinality == 0;
}
/* Get whether there is at least one bit set (see bitset_container_empty for the reverse),
the bitset is never modified */
static inline bool bitset_container_const_nonzero_cardinality(
const bitset_container_t *bitset) {
return !bitset_container_empty(bitset);
}
/*
* Check whether the two bitsets intersect
*/
bool bitset_container_intersect(const bitset_container_t *src_1,
const bitset_container_t *src_2);
/* Computes the union of bitsets `src_1' and `src_2' into `dst' and return the
* cardinality. */
int bitset_container_or(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the union of bitsets `src_1' and `src_2' and return the cardinality.
*/
int bitset_container_or_justcard(const bitset_container_t *src_1,
const bitset_container_t *src_2);
/* Computes the union of bitsets `src_1' and `src_2' into `dst' and return the
* cardinality. Same as bitset_container_or. */
int bitset_container_union(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the union of bitsets `src_1' and `src_2' and return the
* cardinality. Same as bitset_container_or_justcard. */
int bitset_container_union_justcard(const bitset_container_t *src_1,
const bitset_container_t *src_2);
/* Computes the union of bitsets `src_1' and `src_2' into `dst', but does not
* update the cardinality. Provided to optimize chained operations. */
int bitset_container_or_nocard(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the union of bitsets `src_1' and `src_2' into `dst', but does not
* update the cardinality. Same as bitset_container_or_nocard */
int bitset_container_union_nocard(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the intersection of bitsets `src_1' and `src_2' into `dst' and
* return the cardinality. */
int bitset_container_and(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the intersection of bitsets `src_1' and `src_2' and return the
* cardinality. */
int bitset_container_and_justcard(const bitset_container_t *src_1,
const bitset_container_t *src_2);
/* Computes the intersection of bitsets `src_1' and `src_2' into `dst' and
* return the cardinality. Same as bitset_container_and. */
int bitset_container_intersection(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the intersection of bitsets `src_1' and `src_2' and return the
* cardinality. Same as bitset_container_and_justcard. */
int bitset_container_intersection_justcard(const bitset_container_t *src_1,
const bitset_container_t *src_2);
/* Computes the intersection of bitsets `src_1' and `src_2' into `dst', but does
* not update the cardinality. Provided to optimize chained operations. */
int bitset_container_and_nocard(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the intersection of bitsets `src_1' and `src_2' into `dst', but does
* not update the cardinality. Same as bitset_container_and_nocard */
int bitset_container_intersection_nocard(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the exclusive or of bitsets `src_1' and `src_2' into `dst' and
* return the cardinality. */
int bitset_container_xor(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the exclusive or of bitsets `src_1' and `src_2' and return the
* cardinality. */
int bitset_container_xor_justcard(const bitset_container_t *src_1,
const bitset_container_t *src_2);
/* Computes the exclusive or of bitsets `src_1' and `src_2' into `dst', but does
* not update the cardinality. Provided to optimize chained operations. */
int bitset_container_xor_nocard(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the and not of bitsets `src_1' and `src_2' into `dst' and return the
* cardinality. */
int bitset_container_andnot(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Computes the and not of bitsets `src_1' and `src_2' and return the
* cardinality. */
int bitset_container_andnot_justcard(const bitset_container_t *src_1,
const bitset_container_t *src_2);
/* Computes the and not or of bitsets `src_1' and `src_2' into `dst', but does
* not update the cardinality. Provided to optimize chained operations. */
int bitset_container_andnot_nocard(const bitset_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/*
* Write out the 16-bit integers contained in this container as a list of 32-bit
* integers using base
* as the starting value (it might be expected that base has zeros in its 16
* least significant bits).
* The function returns the number of values written.
* The caller is responsible for allocating enough memory in out.
* The out pointer should point to enough memory (the cardinality times 32
* bits).
*/
int bitset_container_to_uint32_array(void *out, const bitset_container_t *cont,
uint32_t base);
/*
* Print this container using printf (useful for debugging).
*/
void bitset_container_printf(const bitset_container_t *v);
/*
* Print this container using printf as a comma-separated list of 32-bit
* integers starting at base.
*/
void bitset_container_printf_as_uint32_array(const bitset_container_t *v,
uint32_t base);
/**
* Return the serialized size in bytes of a container.
*/
static inline int32_t bitset_container_serialized_size_in_bytes(void) {
return BITSET_CONTAINER_SIZE_IN_WORDS * 8;
}
/**
* Return the the number of runs.
*/
int bitset_container_number_of_runs(bitset_container_t *b);
bool bitset_container_iterate(const bitset_container_t *cont, uint32_t base,
roaring_iterator iterator, void *ptr);
bool bitset_container_iterate64(const bitset_container_t *cont, uint32_t base,
roaring_iterator64 iterator, uint64_t high_bits,
void *ptr);
/**
* Writes the underlying array to buf, outputs how many bytes were written.
* This is meant to be byte-by-byte compatible with the Java and Go versions of
* Roaring.
* The number of bytes written should be
* bitset_container_size_in_bytes(container).
*/
int32_t bitset_container_write(const bitset_container_t *container, char *buf);
/**
* Reads the instance from buf, outputs how many bytes were read.
* This is meant to be byte-by-byte compatible with the Java and Go versions of
* Roaring.
* The number of bytes read should be bitset_container_size_in_bytes(container).
* You need to provide the (known) cardinality.
*/
int32_t bitset_container_read(int32_t cardinality,
bitset_container_t *container, const char *buf);
/**
* Return the serialized size in bytes of a container (see
* bitset_container_write).
* This is meant to be compatible with the Java and Go versions of Roaring and
* assumes
* that the cardinality of the container is already known or can be computed.
*/
static inline int32_t bitset_container_size_in_bytes(
const bitset_container_t *container) {
(void)container;
return BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
}
/**
* Return true if the two containers have the same content.
*/
bool bitset_container_equals(const bitset_container_t *container1,
const bitset_container_t *container2);
/**
* Return true if container1 is a subset of container2.
*/
bool bitset_container_is_subset(const bitset_container_t *container1,
const bitset_container_t *container2);
/**
* If the element of given rank is in this container, supposing that the first
* element has rank start_rank, then the function returns true and sets element
* accordingly.
* Otherwise, it returns false and update start_rank.
*/
bool bitset_container_select(const bitset_container_t *container,
uint32_t *start_rank, uint32_t rank,
uint32_t *element);
/* Returns the smallest value (assumes not empty) */
uint16_t bitset_container_minimum(const bitset_container_t *container);
/* Returns the largest value (assumes not empty) */
uint16_t bitset_container_maximum(const bitset_container_t *container);
/* Returns the number of values equal or smaller than x */
int bitset_container_rank(const bitset_container_t *container, uint16_t x);
/* Returns the index of the first value equal or larger than x, or -1 */
int bitset_container_index_equalorlarger(const bitset_container_t *container, uint16_t x);
#endif /* INCLUDE_CONTAINERS_BITSET_H_ */
/* end file include/roaring/containers/bitset.h */
/* begin file include/roaring/containers/run.h */
/*
* run.h
*
*/
#ifndef INCLUDE_CONTAINERS_RUN_H_
#define INCLUDE_CONTAINERS_RUN_H_
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
/* struct rle16_s - run length pair
*
* @value: start position of the run
* @length: length of the run is `length + 1`
*
* An RLE pair {v, l} would represent the integers between the interval
* [v, v+l+1], e.g. {3, 2} = [3, 4, 5].
*/
struct rle16_s {
uint16_t value;
uint16_t length;
};
typedef struct rle16_s rle16_t;
/* struct run_container_s - run container bitmap
*
* @n_runs: number of rle_t pairs in `runs`.
* @capacity: capacity in rle_t pairs `runs` can hold.
* @runs: pairs of rle_t.
*
*/
struct run_container_s {
int32_t n_runs;
int32_t capacity;
rle16_t *runs;
};
typedef struct run_container_s run_container_t;
/* Create a new run container. Return NULL in case of failure. */
run_container_t *run_container_create(void);
/* Create a new run container with given capacity. Return NULL in case of
* failure. */
run_container_t *run_container_create_given_capacity(int32_t size);
/*
* Shrink the capacity to the actual size, return the number of bytes saved.
*/
int run_container_shrink_to_fit(run_container_t *src);
/* Free memory owned by `run'. */
void run_container_free(run_container_t *run);
/* Duplicate container */
run_container_t *run_container_clone(const run_container_t *src);
int32_t run_container_serialize(const run_container_t *container,
char *buf) WARN_UNUSED;
uint32_t run_container_serialization_len(const run_container_t *container);
void *run_container_deserialize(const char *buf, size_t buf_len);
/*
* Effectively deletes the value at index index, repacking data.
*/
static inline void recoverRoomAtIndex(run_container_t *run, uint16_t index) {
memmove(run->runs + index, run->runs + (1 + index),
(run->n_runs - index - 1) * sizeof(rle16_t));
run->n_runs--;
}
/**
* Good old binary search through rle data
*/
static inline int32_t interleavedBinarySearch(const rle16_t *array, int32_t lenarray,
uint16_t ikey) {
int32_t low = 0;
int32_t high = lenarray - 1;
while (low <= high) {
int32_t middleIndex = (low + high) >> 1;
uint16_t middleValue = array[middleIndex].value;
if (middleValue < ikey) {
low = middleIndex + 1;
} else if (middleValue > ikey) {
high = middleIndex - 1;
} else {
return middleIndex;
}
}
return -(low + 1);
}
/*
* Returns index of the run which contains $ikey
*/
static inline int32_t rle16_find_run(const rle16_t *array, int32_t lenarray,
uint16_t ikey) {
int32_t low = 0;
int32_t high = lenarray - 1;
while (low <= high) {
int32_t middleIndex = (low + high) >> 1;
uint16_t min = array[middleIndex].value;
uint16_t max = array[middleIndex].value + array[middleIndex].length;
if (ikey > max) {
low = middleIndex + 1;
} else if (ikey < min) {
high = middleIndex - 1;
} else {
return middleIndex;
}
}
return -(low + 1);
}
/**
* Returns number of runs which can'be be merged with the key because they
* are less than the key.
* Note that [5,6,7,8] can be merged with the key 9 and won't be counted.
*/
static inline int32_t rle16_count_less(const rle16_t* array, int32_t lenarray,
uint16_t key) {
if (lenarray == 0) return 0;
int32_t low = 0;
int32_t high = lenarray - 1;
while (low <= high) {
int32_t middleIndex = (low + high) >> 1;
uint16_t min_value = array[middleIndex].value;
uint16_t max_value = array[middleIndex].value + array[middleIndex].length;
if (max_value + UINT32_C(1) < key) { // uint32 arithmetic
low = middleIndex + 1;
} else if (key < min_value) {
high = middleIndex - 1;
} else {
return middleIndex;
}
}
return low;
}
static inline int32_t rle16_count_greater(const rle16_t* array, int32_t lenarray,
uint16_t key) {
if (lenarray == 0) return 0;
int32_t low = 0;
int32_t high = lenarray - 1;
while (low <= high) {
int32_t middleIndex = (low + high) >> 1;
uint16_t min_value = array[middleIndex].value;
uint16_t max_value = array[middleIndex].value + array[middleIndex].length;
if (max_value < key) {
low = middleIndex + 1;
} else if (key + UINT32_C(1) < min_value) { // uint32 arithmetic
high = middleIndex - 1;
} else {
return lenarray - (middleIndex + 1);
}
}
return lenarray - low;
}
/**
* increase capacity to at least min. Whether the
* existing data needs to be copied over depends on copy. If "copy" is false,
* then the new content will be uninitialized, otherwise a copy is made.
*/
void run_container_grow(run_container_t *run, int32_t min, bool copy);
/**
* Moves the data so that we can write data at index
*/
static inline void makeRoomAtIndex(run_container_t *run, uint16_t index) {
/* This function calls realloc + memmove sequentially to move by one index.
* Potentially copying twice the array.
*/
if (run->n_runs + 1 > run->capacity)
run_container_grow(run, run->n_runs + 1, true);
memmove(run->runs + 1 + index, run->runs + index,
(run->n_runs - index) * sizeof(rle16_t));
run->n_runs++;
}
/* Add `pos' to `run'. Returns true if `pos' was not present. */
bool run_container_add(run_container_t *run, uint16_t pos);
/* Remove `pos' from `run'. Returns true if `pos' was present. */
static inline bool run_container_remove(run_container_t *run, uint16_t pos) {
int32_t index = interleavedBinarySearch(run->runs, run->n_runs, pos);
if (index >= 0) {
int32_t le = run->runs[index].length;
if (le == 0) {
recoverRoomAtIndex(run, (uint16_t)index);
} else {
run->runs[index].value++;
run->runs[index].length--;
}
return true;
}
index = -index - 2; // points to preceding value, possibly -1
if (index >= 0) { // possible match
int32_t offset = pos - run->runs[index].value;
int32_t le = run->runs[index].length;
if (offset < le) {
// need to break in two
run->runs[index].length = (uint16_t)(offset - 1);
// need to insert
uint16_t newvalue = pos + 1;
int32_t newlength = le - offset - 1;
makeRoomAtIndex(run, (uint16_t)(index + 1));
run->runs[index + 1].value = newvalue;
run->runs[index + 1].length = (uint16_t)newlength;
return true;
} else if (offset == le) {
run->runs[index].length--;
return true;
}
}
// no match
return false;
}
/* Check whether `pos' is present in `run'. */
static inline bool run_container_contains(const run_container_t *run, uint16_t pos) {
int32_t index = interleavedBinarySearch(run->runs, run->n_runs, pos);
if (index >= 0) return true;
index = -index - 2; // points to preceding value, possibly -1
if (index != -1) { // possible match
int32_t offset = pos - run->runs[index].value;
int32_t le = run->runs[index].length;
if (offset <= le) return true;
}
return false;
}
/*
* Check whether all positions in a range of positions from pos_start (included)
* to pos_end (excluded) is present in `run'.
*/
static inline bool run_container_contains_range(const run_container_t *run,
uint32_t pos_start, uint32_t pos_end) {
uint32_t count = 0;
int32_t index = interleavedBinarySearch(run->runs, run->n_runs, pos_start);
if (index < 0) {
index = -index - 2;
if ((index == -1) || ((pos_start - run->runs[index].value) > run->runs[index].length)){
return false;
}
}
for (int32_t i = index; i < run->n_runs; ++i) {
const uint32_t stop = run->runs[i].value + run->runs[i].length;
if (run->runs[i].value >= pos_end) break;
if (stop >= pos_end) {
count += (((pos_end - run->runs[i].value) > 0) ? (pos_end - run->runs[i].value) : 0);
break;
}
const uint32_t min = (stop - pos_start) > 0 ? (stop - pos_start) : 0;
count += (min < run->runs[i].length) ? min : run->runs[i].length;
}
return count >= (pos_end - pos_start - 1);
}
#ifdef USEAVX
/* Get the cardinality of `run'. Requires an actual computation. */
static inline int run_container_cardinality(const run_container_t *run) {
const int32_t n_runs = run->n_runs;
const rle16_t *runs = run->runs;
/* by initializing with n_runs, we omit counting the +1 for each pair. */
int sum = n_runs;
int32_t k = 0;
const int32_t step = sizeof(__m256i) / sizeof(rle16_t);
if (n_runs > step) {
__m256i total = _mm256_setzero_si256();
for (; k + step <= n_runs; k += step) {
__m256i ymm1 = _mm256_lddqu_si256((const __m256i *)(runs + k));
__m256i justlengths = _mm256_srli_epi32(ymm1, 16);
total = _mm256_add_epi32(total, justlengths);
}
// a store might be faster than extract?
uint32_t buffer[sizeof(__m256i) / sizeof(rle16_t)];
_mm256_storeu_si256((__m256i *)buffer, total);
sum += (buffer[0] + buffer[1]) + (buffer[2] + buffer[3]) +
(buffer[4] + buffer[5]) + (buffer[6] + buffer[7]);
}
for (; k < n_runs; ++k) {
sum += runs[k].length;
}
return sum;
}
#else
/* Get the cardinality of `run'. Requires an actual computation. */
static inline int run_container_cardinality(const run_container_t *run) {
const int32_t n_runs = run->n_runs;
const rle16_t *runs = run->runs;
/* by initializing with n_runs, we omit counting the +1 for each pair. */
int sum = n_runs;
for (int k = 0; k < n_runs; ++k) {
sum += runs[k].length;
}
return sum;
}
#endif
/* Card > 0?, see run_container_empty for the reverse */
static inline bool run_container_nonzero_cardinality(
const run_container_t *run) {
return run->n_runs > 0; // runs never empty
}
/* Card == 0?, see run_container_nonzero_cardinality for the reverse */
static inline bool run_container_empty(
const run_container_t *run) {
return run->n_runs == 0; // runs never empty
}
/* Copy one container into another. We assume that they are distinct. */
void run_container_copy(const run_container_t *src, run_container_t *dst);
/* Set the cardinality to zero (does not release memory). */
static inline void run_container_clear(run_container_t *run) {
run->n_runs = 0;
}
/**
* Append run described by vl to the run container, possibly merging.
* It is assumed that the run would be inserted at the end of the container, no
* check is made.
* It is assumed that the run container has the necessary capacity: caller is
* responsible for checking memory capacity.
*
*
* This is not a safe function, it is meant for performance: use with care.
*/
static inline void run_container_append(run_container_t *run, rle16_t vl,
rle16_t *previousrl) {
const uint32_t previousend = previousrl->value + previousrl->length;
if (vl.value > previousend + 1) { // we add a new one
run->runs[run->n_runs] = vl;
run->n_runs++;
*previousrl = vl;
} else {
uint32_t newend = vl.value + vl.length + UINT32_C(1);
if (newend > previousend) { // we merge
previousrl->length = (uint16_t)(newend - 1 - previousrl->value);
run->runs[run->n_runs - 1] = *previousrl;
}
}
}
/**
* Like run_container_append but it is assumed that the content of run is empty.
*/
static inline rle16_t run_container_append_first(run_container_t *run,
rle16_t vl) {
run->runs[run->n_runs] = vl;
run->n_runs++;
return vl;
}
/**
* append a single value given by val to the run container, possibly merging.
* It is assumed that the value would be inserted at the end of the container,
* no check is made.
* It is assumed that the run container has the necessary capacity: caller is
* responsible for checking memory capacity.
*
* This is not a safe function, it is meant for performance: use with care.
*/
static inline void run_container_append_value(run_container_t *run,
uint16_t val,
rle16_t *previousrl) {
const uint32_t previousend = previousrl->value + previousrl->length;
if (val > previousend + 1) { // we add a new one
//*previousrl = (rle16_t){.value = val, .length = 0};// requires C99
previousrl->value = val;
previousrl->length = 0;
run->runs[run->n_runs] = *previousrl;
run->n_runs++;
} else if (val == previousend + 1) { // we merge
previousrl->length++;
run->runs[run->n_runs - 1] = *previousrl;
}
}
/**
* Like run_container_append_value but it is assumed that the content of run is
* empty.
*/
static inline rle16_t run_container_append_value_first(run_container_t *run,
uint16_t val) {
// rle16_t newrle = (rle16_t){.value = val, .length = 0};// requires C99
rle16_t newrle;
newrle.value = val;
newrle.length = 0;
run->runs[run->n_runs] = newrle;
run->n_runs++;
return newrle;
}
/* Check whether the container spans the whole chunk (cardinality = 1<<16).
* This check can be done in constant time (inexpensive). */
static inline bool run_container_is_full(const run_container_t *run) {
rle16_t vl = run->runs[0];
return (run->n_runs == 1) && (vl.value == 0) && (vl.length == 0xFFFF);
}
/* Compute the union of `src_1' and `src_2' and write the result to `dst'
* It is assumed that `dst' is distinct from both `src_1' and `src_2'. */
void run_container_union(const run_container_t *src_1,
const run_container_t *src_2, run_container_t *dst);
/* Compute the union of `src_1' and `src_2' and write the result to `src_1' */
void run_container_union_inplace(run_container_t *src_1,
const run_container_t *src_2);
/* Compute the intersection of src_1 and src_2 and write the result to
* dst. It is assumed that dst is distinct from both src_1 and src_2. */
void run_container_intersection(const run_container_t *src_1,
const run_container_t *src_2,
run_container_t *dst);
/* Compute the size of the intersection of src_1 and src_2 . */
int run_container_intersection_cardinality(const run_container_t *src_1,
const run_container_t *src_2);
/* Check whether src_1 and src_2 intersect. */
bool run_container_intersect(const run_container_t *src_1,
const run_container_t *src_2);
/* Compute the symmetric difference of `src_1' and `src_2' and write the result
* to `dst'
* It is assumed that `dst' is distinct from both `src_1' and `src_2'. */
void run_container_xor(const run_container_t *src_1,
const run_container_t *src_2, run_container_t *dst);
/*
* Write out the 16-bit integers contained in this container as a list of 32-bit
* integers using base
* as the starting value (it might be expected that base has zeros in its 16
* least significant bits).
* The function returns the number of values written.
* The caller is responsible for allocating enough memory in out.
*/
int run_container_to_uint32_array(void *vout, const run_container_t *cont,
uint32_t base);
/*
* Print this container using printf (useful for debugging).
*/
void run_container_printf(const run_container_t *v);
/*
* Print this container using printf as a comma-separated list of 32-bit
* integers starting at base.
*/
void run_container_printf_as_uint32_array(const run_container_t *v,
uint32_t base);
/**
* Return the serialized size in bytes of a container having "num_runs" runs.
*/
static inline int32_t run_container_serialized_size_in_bytes(int32_t num_runs) {
return sizeof(uint16_t) +
sizeof(rle16_t) * num_runs; // each run requires 2 2-byte entries.
}
bool run_container_iterate(const run_container_t *cont, uint32_t base,
roaring_iterator iterator, void *ptr);
bool run_container_iterate64(const run_container_t *cont, uint32_t base,
roaring_iterator64 iterator, uint64_t high_bits,
void *ptr);
/**
* Writes the underlying array to buf, outputs how many bytes were written.
* This is meant to be byte-by-byte compatible with the Java and Go versions of
* Roaring.
* The number of bytes written should be run_container_size_in_bytes(container).
*/
int32_t run_container_write(const run_container_t *container, char *buf);
/**
* Reads the instance from buf, outputs how many bytes were read.
* This is meant to be byte-by-byte compatible with the Java and Go versions of
* Roaring.
* The number of bytes read should be bitset_container_size_in_bytes(container).
* The cardinality parameter is provided for consistency with other containers,
* but
* it might be effectively ignored..
*/
int32_t run_container_read(int32_t cardinality, run_container_t *container,
const char *buf);
/**
* Return the serialized size in bytes of a container (see run_container_write).
* This is meant to be compatible with the Java and Go versions of Roaring.
*/
static inline int32_t run_container_size_in_bytes(
const run_container_t *container) {
return run_container_serialized_size_in_bytes(container->n_runs);
}
/**
* Return true if the two containers have the same content.
*/
static inline bool run_container_equals(const run_container_t *container1,
const run_container_t *container2) {
if (container1->n_runs != container2->n_runs) {
return false;
}
return memequals(container1->runs, container2->runs,
container1->n_runs * sizeof(rle16_t));
}
/**
* Return true if container1 is a subset of container2.
*/
bool run_container_is_subset(const run_container_t *container1,
const run_container_t *container2);
/**
* Used in a start-finish scan that appends segments, for XOR and NOT
*/
void run_container_smart_append_exclusive(run_container_t *src,
const uint16_t start,
const uint16_t length);
/**
* The new container consists of a single run [start,stop).
* It is required that stop>start, the caller is responsibility for this check.
* It is required that stop <= (1<<16), the caller is responsibility for this check.
* The cardinality of the created container is stop - start.
* Returns NULL on failure
*/
static inline run_container_t *run_container_create_range(uint32_t start,
uint32_t stop) {
run_container_t *rc = run_container_create_given_capacity(1);
if (rc) {
rle16_t r;
r.value = (uint16_t)start;
r.length = (uint16_t)(stop - start - 1);
run_container_append_first(rc, r);
}
return rc;
}
/**
* If the element of given rank is in this container, supposing that the first
* element has rank start_rank, then the function returns true and sets element
* accordingly.
* Otherwise, it returns false and update start_rank.
*/
bool run_container_select(const run_container_t *container,
uint32_t *start_rank, uint32_t rank,
uint32_t *element);
/* Compute the difference of src_1 and src_2 and write the result to
* dst. It is assumed that dst is distinct from both src_1 and src_2. */
void run_container_andnot(const run_container_t *src_1,
const run_container_t *src_2, run_container_t *dst);
/* Returns the smallest value (assumes not empty) */
static inline uint16_t run_container_minimum(const run_container_t *run) {
if (run->n_runs == 0) return 0;
return run->runs[0].value;
}
/* Returns the largest value (assumes not empty) */
static inline uint16_t run_container_maximum(const run_container_t *run) {
if (run->n_runs == 0) return 0;
return run->runs[run->n_runs - 1].value + run->runs[run->n_runs - 1].length;
}
/* Returns the number of values equal or smaller than x */
int run_container_rank(const run_container_t *arr, uint16_t x);
/* Returns the index of the first run containing a value at least as large as x, or -1 */
static inline int run_container_index_equalorlarger(const run_container_t *arr, uint16_t x) {
int32_t index = interleavedBinarySearch(arr->runs, arr->n_runs, x);
if (index >= 0) return index;
index = -index - 2; // points to preceding run, possibly -1
if (index != -1) { // possible match
int32_t offset = x - arr->runs[index].value;
int32_t le = arr->runs[index].length;
if (offset <= le) return index;
}
index += 1;
if(index < arr->n_runs) {
return index;
}
return -1;
}
/*
* Add all values in range [min, max] using hint.
*/
static inline void run_container_add_range_nruns(run_container_t* run,
uint32_t min, uint32_t max,
int32_t nruns_less,
int32_t nruns_greater) {
int32_t nruns_common = run->n_runs - nruns_less - nruns_greater;
if (nruns_common == 0) {
makeRoomAtIndex(run, nruns_less);
run->runs[nruns_less].value = min;
run->runs[nruns_less].length = max - min;
} else {
uint32_t common_min = run->runs[nruns_less].value;
uint32_t common_max = run->runs[nruns_less + nruns_common - 1].value +
run->runs[nruns_less + nruns_common - 1].length;
uint32_t result_min = (common_min < min) ? common_min : min;
uint32_t result_max = (common_max > max) ? common_max : max;
run->runs[nruns_less].value = result_min;
run->runs[nruns_less].length = result_max - result_min;
memmove(&(run->runs[nruns_less + 1]),
&(run->runs[run->n_runs - nruns_greater]),
nruns_greater*sizeof(rle16_t));
run->n_runs = nruns_less + 1 + nruns_greater;
}
}
/**
* Add all values in range [min, max]
*/
static inline void run_container_add_range(run_container_t* run,
uint32_t min, uint32_t max) {
int32_t nruns_greater = rle16_count_greater(run->runs, run->n_runs, max);
int32_t nruns_less = rle16_count_less(run->runs, run->n_runs - nruns_greater, min);
run_container_add_range_nruns(run, min, max, nruns_less, nruns_greater);
}
/**
* Shifts last $count elements either left (distance < 0) or right (distance > 0)
*/
static inline void run_container_shift_tail(run_container_t* run,
int32_t count, int32_t distance) {
if (distance > 0) {
if (run->capacity < count+distance) {
run_container_grow(run, count+distance, true);
}
}
int32_t srcpos = run->n_runs - count;
int32_t dstpos = srcpos + distance;
memmove(&(run->runs[dstpos]), &(run->runs[srcpos]), sizeof(rle16_t) * count);
run->n_runs += distance;
}
/**
* Remove all elements in range [min, max]
*/
static inline void run_container_remove_range(run_container_t *run, uint32_t min, uint32_t max) {
int32_t first = rle16_find_run(run->runs, run->n_runs, min);
int32_t last = rle16_find_run(run->runs, run->n_runs, max);
if (first >= 0 && min > run->runs[first].value &&
max < ((uint32_t)run->runs[first].value + (uint32_t)run->runs[first].length)) {
// split this run into two adjacent runs
// right subinterval
makeRoomAtIndex(run, first+1);
run->runs[first+1].value = max + 1;
run->runs[first+1].length = (run->runs[first].value + run->runs[first].length) - (max + 1);
// left subinterval
run->runs[first].length = (min - 1) - run->runs[first].value;
return;
}
// update left-most partial run
if (first >= 0) {
if (min > run->runs[first].value) {
run->runs[first].length = (min - 1) - run->runs[first].value;
first++;
}
} else {
first = -first-1;
}
// update right-most run
if (last >= 0) {
uint16_t run_max = run->runs[last].value + run->runs[last].length;
if (run_max > max) {
run->runs[last].value = max + 1;
run->runs[last].length = run_max - (max + 1);
last--;
}
} else {
last = (-last-1) - 1;
}
// remove intermediate runs
if (first <= last) {
run_container_shift_tail(run, run->n_runs - (last+1), -(last-first+1));
}
}
#endif /* INCLUDE_CONTAINERS_RUN_H_ */
/* end file include/roaring/containers/run.h */
/* begin file include/roaring/containers/convert.h */
/*
* convert.h
*
*/
#ifndef INCLUDE_CONTAINERS_CONVERT_H_
#define INCLUDE_CONTAINERS_CONVERT_H_
/* Convert an array into a bitset. The input container is not freed or modified.
*/
bitset_container_t *bitset_container_from_array(const array_container_t *arr);
/* Convert a run into a bitset. The input container is not freed or modified. */
bitset_container_t *bitset_container_from_run(const run_container_t *arr);
/* Convert a run into an array. The input container is not freed or modified. */
array_container_t *array_container_from_run(const run_container_t *arr);
/* Convert a bitset into an array. The input container is not freed or modified.
*/
array_container_t *array_container_from_bitset(const bitset_container_t *bits);
/* Convert an array into a run. The input container is not freed or modified.
*/
run_container_t *run_container_from_array(const array_container_t *c);
/* convert a run into either an array or a bitset
* might free the container. This does not free the input run container. */
void *convert_to_bitset_or_array_container(run_container_t *r, int32_t card,
uint8_t *resulttype);
/* convert containers to and from runcontainers, as is most space efficient.
* The container might be freed. */
void *convert_run_optimize(void *c, uint8_t typecode_original,
uint8_t *typecode_after);
/* converts a run container to either an array or a bitset, IF it saves space.
*/
/* If a conversion occurs, the caller is responsible to free the original
* container and
* he becomes responsible to free the new one. */
void *convert_run_to_efficient_container(run_container_t *c,
uint8_t *typecode_after);
// like convert_run_to_efficient_container but frees the old result if needed
void *convert_run_to_efficient_container_and_free(run_container_t *c,
uint8_t *typecode_after);
/**
* Create new bitset container which is a union of run container and
* range [min, max]. Caller is responsible for freeing run container.
*/
bitset_container_t *bitset_container_from_run_range(const run_container_t *run,
uint32_t min, uint32_t max);
#endif /* INCLUDE_CONTAINERS_CONVERT_H_ */
/* end file include/roaring/containers/convert.h */
/* begin file include/roaring/containers/mixed_equal.h */
/*
* mixed_equal.h
*
*/
#ifndef CONTAINERS_MIXED_EQUAL_H_
#define CONTAINERS_MIXED_EQUAL_H_
/**
* Return true if the two containers have the same content.
*/
bool array_container_equal_bitset(const array_container_t* container1,
const bitset_container_t* container2);
/**
* Return true if the two containers have the same content.
*/
bool run_container_equals_array(const run_container_t* container1,
const array_container_t* container2);
/**
* Return true if the two containers have the same content.
*/
bool run_container_equals_bitset(const run_container_t* container1,
const bitset_container_t* container2);
#endif /* CONTAINERS_MIXED_EQUAL_H_ */
/* end file include/roaring/containers/mixed_equal.h */
/* begin file include/roaring/containers/mixed_subset.h */
/*
* mixed_subset.h
*
*/
#ifndef CONTAINERS_MIXED_SUBSET_H_
#define CONTAINERS_MIXED_SUBSET_H_
/**
* Return true if container1 is a subset of container2.
*/
bool array_container_is_subset_bitset(const array_container_t* container1,
const bitset_container_t* container2);
/**
* Return true if container1 is a subset of container2.
*/
bool run_container_is_subset_array(const run_container_t* container1,
const array_container_t* container2);
/**
* Return true if container1 is a subset of container2.
*/
bool array_container_is_subset_run(const array_container_t* container1,
const run_container_t* container2);
/**
* Return true if container1 is a subset of container2.
*/
bool run_container_is_subset_bitset(const run_container_t* container1,
const bitset_container_t* container2);
/**
* Return true if container1 is a subset of container2.
*/
bool bitset_container_is_subset_run(const bitset_container_t* container1,
const run_container_t* container2);
#endif /* CONTAINERS_MIXED_SUBSET_H_ */
/* end file include/roaring/containers/mixed_subset.h */
/* begin file include/roaring/containers/mixed_andnot.h */
/*
* mixed_andnot.h
*/
#ifndef INCLUDE_CONTAINERS_MIXED_ANDNOT_H_
#define INCLUDE_CONTAINERS_MIXED_ANDNOT_H_
/* Compute the andnot of src_1 and src_2 and write the result to
* dst, a valid array container that could be the same as dst.*/
void array_bitset_container_andnot(const array_container_t *src_1,
const bitset_container_t *src_2,
array_container_t *dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* src_1 */
void array_bitset_container_iandnot(array_container_t *src_1,
const bitset_container_t *src_2);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst, which does not initially have a valid container.
* Return true for a bitset result; false for array
*/
bool bitset_array_container_andnot(const bitset_container_t *src_1,
const array_container_t *src_2, void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst (which has no container initially). It will modify src_1
* to be dst if the result is a bitset. Otherwise, it will
* free src_1 and dst will be a new array container. In both
* cases, the caller is responsible for deallocating dst.
* Returns true iff dst is a bitset */
bool bitset_array_container_iandnot(bitset_container_t *src_1,
const array_container_t *src_2, void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst. Result may be either a bitset or an array container
* (returns "result is bitset"). dst does not initially have
* any container, but becomes either a bitset container (return
* result true) or an array container.
*/
bool run_bitset_container_andnot(const run_container_t *src_1,
const bitset_container_t *src_2, void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst. Result may be either a bitset or an array container
* (returns "result is bitset"). dst does not initially have
* any container, but becomes either a bitset container (return
* result true) or an array container.
*/
bool run_bitset_container_iandnot(run_container_t *src_1,
const bitset_container_t *src_2, void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst. Result may be either a bitset or an array container
* (returns "result is bitset"). dst does not initially have
* any container, but becomes either a bitset container (return
* result true) or an array container.
*/
bool bitset_run_container_andnot(const bitset_container_t *src_1,
const run_container_t *src_2, void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst (which has no container initially). It will modify src_1
* to be dst if the result is a bitset. Otherwise, it will
* free src_1 and dst will be a new array container. In both
* cases, the caller is responsible for deallocating dst.
* Returns true iff dst is a bitset */
bool bitset_run_container_iandnot(bitset_container_t *src_1,
const run_container_t *src_2, void **dst);
/* dst does not indicate a valid container initially. Eventually it
* can become any type of container.
*/
int run_array_container_andnot(const run_container_t *src_1,
const array_container_t *src_2, void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst (which has no container initially). It will modify src_1
* to be dst if the result is a bitset. Otherwise, it will
* free src_1 and dst will be a new array container. In both
* cases, the caller is responsible for deallocating dst.
* Returns true iff dst is a bitset */
int run_array_container_iandnot(run_container_t *src_1,
const array_container_t *src_2, void **dst);
/* dst must be a valid array container, allowed to be src_1 */
void array_run_container_andnot(const array_container_t *src_1,
const run_container_t *src_2,
array_container_t *dst);
/* dst does not indicate a valid container initially. Eventually it
* can become any kind of container.
*/
void array_run_container_iandnot(array_container_t *src_1,
const run_container_t *src_2);
/* dst does not indicate a valid container initially. Eventually it
* can become any kind of container.
*/
int run_run_container_andnot(const run_container_t *src_1,
const run_container_t *src_2, void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst (which has no container initially). It will modify src_1
* to be dst if the result is a bitset. Otherwise, it will
* free src_1 and dst will be a new array container. In both
* cases, the caller is responsible for deallocating dst.
* Returns true iff dst is a bitset */
int run_run_container_iandnot(run_container_t *src_1,
const run_container_t *src_2, void **dst);
/*
* dst is a valid array container and may be the same as src_1
*/
void array_array_container_andnot(const array_container_t *src_1,
const array_container_t *src_2,
array_container_t *dst);
/* inplace array-array andnot will always be able to reuse the space of
* src_1 */
void array_array_container_iandnot(array_container_t *src_1,
const array_container_t *src_2);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst (which has no container initially). Return value is
* "dst is a bitset"
*/
bool bitset_bitset_container_andnot(const bitset_container_t *src_1,
const bitset_container_t *src_2,
void **dst);
/* Compute the andnot of src_1 and src_2 and write the result to
* dst (which has no container initially). It will modify src_1
* to be dst if the result is a bitset. Otherwise, it will
* free src_1 and dst will be a new array container. In both
* cases, the caller is responsible for deallocating dst.
* Returns true iff dst is a bitset */
bool bitset_bitset_container_iandnot(bitset_container_t *src_1,
const bitset_container_t *src_2,
void **dst);
#endif
/* end file include/roaring/containers/mixed_andnot.h */
/* begin file include/roaring/containers/mixed_intersection.h */
/*
* mixed_intersection.h
*
*/
#ifndef INCLUDE_CONTAINERS_MIXED_INTERSECTION_H_
#define INCLUDE_CONTAINERS_MIXED_INTERSECTION_H_
/* These functions appear to exclude cases where the
* inputs have the same type and the output is guaranteed
* to have the same type as the inputs. Eg, array intersection
*/
/* Compute the intersection of src_1 and src_2 and write the result to
* dst. It is allowed for dst to be equal to src_1. We assume that dst is a
* valid container. */
void array_bitset_container_intersection(const array_container_t *src_1,
const bitset_container_t *src_2,
array_container_t *dst);
/* Compute the size of the intersection of src_1 and src_2. */
int array_bitset_container_intersection_cardinality(
const array_container_t *src_1, const bitset_container_t *src_2);
/* Checking whether src_1 and src_2 intersect. */
bool array_bitset_container_intersect(const array_container_t *src_1,
const bitset_container_t *src_2);
/*
* Compute the intersection between src_1 and src_2 and write the result
* to *dst. If the return function is true, the result is a bitset_container_t
* otherwise is a array_container_t. We assume that dst is not pre-allocated. In
* case of failure, *dst will be NULL.
*/
bool bitset_bitset_container_intersection(const bitset_container_t *src_1,
const bitset_container_t *src_2,
void **dst);
/* Compute the intersection between src_1 and src_2 and write the result to
* dst. It is allowed for dst to be equal to src_1. We assume that dst is a
* valid container. */
void array_run_container_intersection(const array_container_t *src_1,
const run_container_t *src_2,
array_container_t *dst);
/* Compute the intersection between src_1 and src_2 and write the result to
* *dst. If the result is true then the result is a bitset_container_t
* otherwise is a array_container_t.
* If *dst == src_2, then an in-place intersection is attempted
**/
bool run_bitset_container_intersection(const run_container_t *src_1,
const bitset_container_t *src_2,
void **dst);
/* Compute the size of the intersection between src_1 and src_2 . */
int array_run_container_intersection_cardinality(const array_container_t *src_1,
const run_container_t *src_2);
/* Compute the size of the intersection between src_1 and src_2
**/
int run_bitset_container_intersection_cardinality(const run_container_t *src_1,
const bitset_container_t *src_2);
/* Check that src_1 and src_2 intersect. */
bool array_run_container_intersect(const array_container_t *src_1,
const run_container_t *src_2);
/* Check that src_1 and src_2 intersect.
**/
bool run_bitset_container_intersect(const run_container_t *src_1,
const bitset_container_t *src_2);
/*
* Same as bitset_bitset_container_intersection except that if the output is to
* be a
* bitset_container_t, then src_1 is modified and no allocation is made.
* If the output is to be an array_container_t, then caller is responsible
* to free the container.
* In all cases, the result is in *dst.
*/
bool bitset_bitset_container_intersection_inplace(
bitset_container_t *src_1, const bitset_container_t *src_2, void **dst);
#endif /* INCLUDE_CONTAINERS_MIXED_INTERSECTION_H_ */
/* end file include/roaring/containers/mixed_intersection.h */
/* begin file include/roaring/containers/mixed_negation.h */
/*
* mixed_negation.h
*
*/
#ifndef INCLUDE_CONTAINERS_MIXED_NEGATION_H_
#define INCLUDE_CONTAINERS_MIXED_NEGATION_H_
/* Negation across the entire range of the container.
* Compute the negation of src and write the result
* to *dst. The complement of a
* sufficiently sparse set will always be dense and a hence a bitmap
* We assume that dst is pre-allocated and a valid bitset container
* There can be no in-place version.
*/
void array_container_negation(const array_container_t *src,
bitset_container_t *dst);
/* Negation across the entire range of the container
* Compute the negation of src and write the result
* to *dst. A true return value indicates a bitset result,
* otherwise the result is an array container.
* We assume that dst is not pre-allocated. In
* case of failure, *dst will be NULL.
*/
bool bitset_container_negation(const bitset_container_t *src, void **dst);
/* inplace version */
/*
* Same as bitset_container_negation except that if the output is to
* be a
* bitset_container_t, then src is modified and no allocation is made.
* If the output is to be an array_container_t, then caller is responsible
* to free the container.
* In all cases, the result is in *dst.
*/
bool bitset_container_negation_inplace(bitset_container_t *src, void **dst);
/* Negation across the entire range of container
* Compute the negation of src and write the result
* to *dst.
* Return values are the *_TYPECODES as defined * in containers.h
* We assume that dst is not pre-allocated. In
* case of failure, *dst will be NULL.
*/
int run_container_negation(const run_container_t *src, void **dst);
/*
* Same as run_container_negation except that if the output is to
* be a
* run_container_t, and has the capacity to hold the result,
* then src is modified and no allocation is made.
* In all cases, the result is in *dst.
*/
int run_container_negation_inplace(run_container_t *src, void **dst);
/* Negation across a range of the container.
* Compute the negation of src and write the result
* to *dst. Returns true if the result is a bitset container
* and false for an array container. *dst is not preallocated.
*/
bool array_container_negation_range(const array_container_t *src,
const int range_start, const int range_end,
void **dst);
/* Even when the result would fit, it is unclear how to make an
* inplace version without inefficient copying. Thus this routine
* may be a wrapper for the non-in-place version
*/
bool array_container_negation_range_inplace(array_container_t *src,
const int range_start,
const int range_end, void **dst);
/* Negation across a range of the container
* Compute the negation of src and write the result
* to *dst. A true return value indicates a bitset result,
* otherwise the result is an array container.
* We assume that dst is not pre-allocated. In
* case of failure, *dst will be NULL.
*/
bool bitset_container_negation_range(const bitset_container_t *src,
const int range_start, const int range_end,
void **dst);
/* inplace version */
/*
* Same as bitset_container_negation except that if the output is to
* be a
* bitset_container_t, then src is modified and no allocation is made.
* If the output is to be an array_container_t, then caller is responsible
* to free the container.
* In all cases, the result is in *dst.
*/
bool bitset_container_negation_range_inplace(bitset_container_t *src,
const int range_start,
const int range_end, void **dst);
/* Negation across a range of container
* Compute the negation of src and write the result
* to *dst. Return values are the *_TYPECODES as defined * in containers.h
* We assume that dst is not pre-allocated. In
* case of failure, *dst will be NULL.
*/
int run_container_negation_range(const run_container_t *src,
const int range_start, const int range_end,
void **dst);
/*
* Same as run_container_negation except that if the output is to
* be a
* run_container_t, and has the capacity to hold the result,
* then src is modified and no allocation is made.
* In all cases, the result is in *dst.
*/
int run_container_negation_range_inplace(run_container_t *src,
const int range_start,
const int range_end, void **dst);
#endif /* INCLUDE_CONTAINERS_MIXED_NEGATION_H_ */
/* end file include/roaring/containers/mixed_negation.h */
/* begin file include/roaring/containers/mixed_union.h */
/*
* mixed_intersection.h
*
*/
#ifndef INCLUDE_CONTAINERS_MIXED_UNION_H_
#define INCLUDE_CONTAINERS_MIXED_UNION_H_
/* These functions appear to exclude cases where the
* inputs have the same type and the output is guaranteed
* to have the same type as the inputs. Eg, bitset unions
*/
/* Compute the union of src_1 and src_2 and write the result to
* dst. It is allowed for src_2 to be dst. */
void array_bitset_container_union(const array_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Compute the union of src_1 and src_2 and write the result to
* dst. It is allowed for src_2 to be dst. This version does not
* update the cardinality of dst (it is set to BITSET_UNKNOWN_CARDINALITY). */
void array_bitset_container_lazy_union(const array_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/*
* Compute the union between src_1 and src_2 and write the result
* to *dst. If the return function is true, the result is a bitset_container_t
* otherwise is a array_container_t. We assume that dst is not pre-allocated. In
* case of failure, *dst will be NULL.
*/
bool array_array_container_union(const array_container_t *src_1,
const array_container_t *src_2, void **dst);
/*
* Compute the union between src_1 and src_2 and write the result
* to *dst if it cannot be written to src_1. If the return function is true,
* the result is a bitset_container_t
* otherwise is a array_container_t. When the result is an array_container_t, it
* it either written to src_1 (if *dst is null) or to *dst.
* If the result is a bitset_container_t and *dst is null, then there was a failure.
*/
bool array_array_container_inplace_union(array_container_t *src_1,
const array_container_t *src_2, void **dst);
/*
* Same as array_array_container_union except that it will more eagerly produce
* a bitset.
*/
bool array_array_container_lazy_union(const array_container_t *src_1,
const array_container_t *src_2,
void **dst);
/*
* Same as array_array_container_inplace_union except that it will more eagerly produce
* a bitset.
*/
bool array_array_container_lazy_inplace_union(array_container_t *src_1,
const array_container_t *src_2,
void **dst);
/* Compute the union of src_1 and src_2 and write the result to
* dst. We assume that dst is a
* valid container. The result might need to be further converted to array or
* bitset container,
* the caller is responsible for the eventual conversion. */
void array_run_container_union(const array_container_t *src_1,
const run_container_t *src_2,
run_container_t *dst);
/* Compute the union of src_1 and src_2 and write the result to
* src2. The result might need to be further converted to array or
* bitset container,
* the caller is responsible for the eventual conversion. */
void array_run_container_inplace_union(const array_container_t *src_1,
run_container_t *src_2);
/* Compute the union of src_1 and src_2 and write the result to
* dst. It is allowed for dst to be src_2.
* If run_container_is_full(src_1) is true, you must not be calling this
*function.
**/
void run_bitset_container_union(const run_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Compute the union of src_1 and src_2 and write the result to
* dst. It is allowed for dst to be src_2. This version does not
* update the cardinality of dst (it is set to BITSET_UNKNOWN_CARDINALITY).
* If run_container_is_full(src_1) is true, you must not be calling this
* function.
* */
void run_bitset_container_lazy_union(const run_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
#endif /* INCLUDE_CONTAINERS_MIXED_UNION_H_ */
/* end file include/roaring/containers/mixed_union.h */
/* begin file include/roaring/containers/mixed_xor.h */
/*
* mixed_xor.h
*
*/
#ifndef INCLUDE_CONTAINERS_MIXED_XOR_H_
#define INCLUDE_CONTAINERS_MIXED_XOR_H_
/* These functions appear to exclude cases where the
* inputs have the same type and the output is guaranteed
* to have the same type as the inputs. Eg, bitset unions
*/
/*
* Java implementation (as of May 2016) for array_run, run_run
* and bitset_run don't do anything different for inplace.
* (They are not truly in place.)
*/
/* Compute the xor of src_1 and src_2 and write the result to
* dst (which has no container initially).
* Result is true iff dst is a bitset */
bool array_bitset_container_xor(const array_container_t *src_1,
const bitset_container_t *src_2, void **dst);
/* Compute the xor of src_1 and src_2 and write the result to
* dst. It is allowed for src_2 to be dst. This version does not
* update the cardinality of dst (it is set to BITSET_UNKNOWN_CARDINALITY).
*/
void array_bitset_container_lazy_xor(const array_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* Compute the xor of src_1 and src_2 and write the result to
* dst (which has no container initially). Return value is
* "dst is a bitset"
*/
bool bitset_bitset_container_xor(const bitset_container_t *src_1,
const bitset_container_t *src_2, void **dst);
/* Compute the xor of src_1 and src_2 and write the result to
* dst. Result may be either a bitset or an array container
* (returns "result is bitset"). dst does not initially have
* any container, but becomes either a bitset container (return
* result true) or an array container.
*/
bool run_bitset_container_xor(const run_container_t *src_1,
const bitset_container_t *src_2, void **dst);
/* lazy xor. Dst is initialized and may be equal to src_2.
* Result is left as a bitset container, even if actual
* cardinality would dictate an array container.
*/
void run_bitset_container_lazy_xor(const run_container_t *src_1,
const bitset_container_t *src_2,
bitset_container_t *dst);
/* dst does not indicate a valid container initially. Eventually it
* can become any kind of container.
*/
int array_run_container_xor(const array_container_t *src_1,
const run_container_t *src_2, void **dst);
/* dst does not initially have a valid container. Creates either
* an array or a bitset container, indicated by return code
*/
bool array_array_container_xor(const array_container_t *src_1,
const array_container_t *src_2, void **dst);
/* dst does not initially have a valid container. Creates either
* an array or a bitset container, indicated by return code.
* A bitset container will not have a valid cardinality and the
* container type might not be correct for the actual cardinality
*/
bool array_array_container_lazy_xor(const array_container_t *src_1,
const array_container_t *src_2, void **dst);
/* Dst is a valid run container. (Can it be src_2? Let's say not.)
* Leaves result as run container, even if other options are
* smaller.
*/
void array_run_container_lazy_xor(const array_container_t *src_1,
const run_container_t *src_2,
run_container_t *dst);
/* dst does not indicate a valid container initially. Eventually it
* can become any kind of container.
*/
int run_run_container_xor(const run_container_t *src_1,
const run_container_t *src_2, void **dst);
/* INPLACE versions (initial implementation may not exploit all inplace
* opportunities (if any...)
*/
/* Compute the xor of src_1 and src_2 and write the result to
* dst (which has no container initially). It will modify src_1
* to be dst if the result is a bitset. Otherwise, it will
* free src_1 and dst will be a new array container. In both
* cases, the caller is responsible for deallocating dst.
* Returns true iff dst is a bitset */
bool bitset_array_container_ixor(bitset_container_t *src_1,
const array_container_t *src_2, void **dst);
bool bitset_bitset_container_ixor(bitset_container_t *src_1,
const bitset_container_t *src_2, void **dst);
bool array_bitset_container_ixor(array_container_t *src_1,
const bitset_container_t *src_2, void **dst);
/* Compute the xor of src_1 and src_2 and write the result to
* dst. Result may be either a bitset or an array container
* (returns "result is bitset"). dst does not initially have
* any container, but becomes either a bitset container (return
* result true) or an array container.
*/
bool run_bitset_container_ixor(run_container_t *src_1,
const bitset_container_t *src_2, void **dst);
bool bitset_run_container_ixor(bitset_container_t *src_1,
const run_container_t *src_2, void **dst);
/* dst does not indicate a valid container initially. Eventually it
* can become any kind of container.
*/
int array_run_container_ixor(array_container_t *src_1,
const run_container_t *src_2, void **dst);
int run_array_container_ixor(run_container_t *src_1,
const array_container_t *src_2, void **dst);
bool array_array_container_ixor(array_container_t *src_1,
const array_container_t *src_2, void **dst);
int run_run_container_ixor(run_container_t *src_1, const run_container_t *src_2,
void **dst);
#endif
/* end file include/roaring/containers/mixed_xor.h */
/* begin file include/roaring/containers/containers.h */
#ifndef CONTAINERS_CONTAINERS_H
#define CONTAINERS_CONTAINERS_H
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
// would enum be possible or better?
/**
* The switch case statements follow
* BITSET_CONTAINER_TYPE_CODE -- ARRAY_CONTAINER_TYPE_CODE --
* RUN_CONTAINER_TYPE_CODE
* so it makes more sense to number them 1, 2, 3 (in the vague hope that the
* compiler might exploit this ordering).
*/
#define BITSET_CONTAINER_TYPE_CODE 1
#define ARRAY_CONTAINER_TYPE_CODE 2
#define RUN_CONTAINER_TYPE_CODE 3
#define SHARED_CONTAINER_TYPE_CODE 4
// macro for pairing container type codes
#define CONTAINER_PAIR(c1, c2) (4 * (c1) + (c2))
/**
* A shared container is a wrapper around a container
* with reference counting.
*/
struct shared_container_s {
void *container;
uint8_t typecode;
uint32_t counter; // to be managed atomically
};
typedef struct shared_container_s shared_container_t;
/*
* With copy_on_write = true
* Create a new shared container if the typecode is not SHARED_CONTAINER_TYPE,
* otherwise, increase the count
* If copy_on_write = false, then clone.
* Return NULL in case of failure.
**/
void *get_copy_of_container(void *container, uint8_t *typecode,
bool copy_on_write);
/* Frees a shared container (actually decrement its counter and only frees when
* the counter falls to zero). */
void shared_container_free(shared_container_t *container);
/* extract a copy from the shared container, freeing the shared container if
there is just one instance left,
clone instances when the counter is higher than one
*/
void *shared_container_extract_copy(shared_container_t *container,
uint8_t *typecode);
/* access to container underneath */
static inline const void *container_unwrap_shared(
const void *candidate_shared_container, uint8_t *type) {
if (*type == SHARED_CONTAINER_TYPE_CODE) {
*type =
((const shared_container_t *)candidate_shared_container)->typecode;
assert(*type != SHARED_CONTAINER_TYPE_CODE);
return ((const shared_container_t *)candidate_shared_container)->container;
} else {
return candidate_shared_container;
}
}
/* access to container underneath */
static inline void *container_mutable_unwrap_shared(
void *candidate_shared_container, uint8_t *type) {
if (*type == SHARED_CONTAINER_TYPE_CODE) {
*type =
((shared_container_t *)candidate_shared_container)->typecode;
assert(*type != SHARED_CONTAINER_TYPE_CODE);
return ((shared_container_t *)candidate_shared_container)->container;
} else {
return candidate_shared_container;
}
}
/* access to container underneath and queries its type */
static inline uint8_t get_container_type(const void *container, uint8_t type) {
if (type == SHARED_CONTAINER_TYPE_CODE) {
return ((const shared_container_t *)container)->typecode;
} else {
return type;
}
}
/**
* Copies a container, requires a typecode. This allocates new memory, caller
* is responsible for deallocation. If the container is not shared, then it is
* physically cloned. Shareable containers are not clonable.
*/
void *container_clone(const void *container, uint8_t typecode);
/* access to container underneath, cloning it if needed */
static inline void *get_writable_copy_if_shared(
void *candidate_shared_container, uint8_t *type) {
if (*type == SHARED_CONTAINER_TYPE_CODE) {
return shared_container_extract_copy(
(shared_container_t *)candidate_shared_container, type);
} else {
return candidate_shared_container;
}
}
/**
* End of shared container code
*/
static const char *container_names[] = {"bitset", "array", "run", "shared"};
static const char *shared_container_names[] = {
"bitset (shared)", "array (shared)", "run (shared)"};
// no matter what the initial container was, convert it to a bitset
// if a new container is produced, caller responsible for freeing the previous
// one
// container should not be a shared container
static inline void *container_to_bitset(void *container, uint8_t typecode) {
bitset_container_t *result = NULL;
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return container; // nothing to do
case ARRAY_CONTAINER_TYPE_CODE:
result =
bitset_container_from_array((array_container_t *)container);
return result;
case RUN_CONTAINER_TYPE_CODE:
result = bitset_container_from_run((run_container_t *)container);
return result;
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
/**
* Get the container name from the typecode
*/
static inline const char *get_container_name(uint8_t typecode) {
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return container_names[0];
case ARRAY_CONTAINER_TYPE_CODE:
return container_names[1];
case RUN_CONTAINER_TYPE_CODE:
return container_names[2];
case SHARED_CONTAINER_TYPE_CODE:
return container_names[3];
default:
assert(false);
__builtin_unreachable();
return "unknown";
}
}
static inline const char *get_full_container_name(const void *container,
uint8_t typecode) {
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return container_names[0];
case ARRAY_CONTAINER_TYPE_CODE:
return container_names[1];
case RUN_CONTAINER_TYPE_CODE:
return container_names[2];
case SHARED_CONTAINER_TYPE_CODE:
switch (((const shared_container_t *)container)->typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return shared_container_names[0];
case ARRAY_CONTAINER_TYPE_CODE:
return shared_container_names[1];
case RUN_CONTAINER_TYPE_CODE:
return shared_container_names[2];
default:
assert(false);
__builtin_unreachable();
return "unknown";
}
break;
default:
assert(false);
__builtin_unreachable();
return "unknown";
}
__builtin_unreachable();
return NULL;
}
/**
* Get the container cardinality (number of elements), requires a typecode
*/
static inline int container_get_cardinality(const void *container,
uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_cardinality(
(const bitset_container_t *)container);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_cardinality(
(const array_container_t *)container);
case RUN_CONTAINER_TYPE_CODE:
return run_container_cardinality(
(const run_container_t *)container);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
// returns true if a container is known to be full. Note that a lazy bitset
// container
// might be full without us knowing
static inline bool container_is_full(const void *container, uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_cardinality(
(const bitset_container_t *)container) == (1 << 16);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_cardinality(
(const array_container_t *)container) == (1 << 16);
case RUN_CONTAINER_TYPE_CODE:
return run_container_is_full((const run_container_t *)container);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
static inline int container_shrink_to_fit(void *container, uint8_t typecode) {
container = container_mutable_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return 0; // no shrinking possible
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_shrink_to_fit(
(array_container_t *)container);
case RUN_CONTAINER_TYPE_CODE:
return run_container_shrink_to_fit((run_container_t *)container);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
/**
* make a container with a run of ones
*/
/* initially always use a run container, even if an array might be
* marginally
* smaller */
static inline void *container_range_of_ones(uint32_t range_start,
uint32_t range_end,
uint8_t *result_type) {
assert(range_end >= range_start);
uint64_t cardinality = range_end - range_start + 1;
if(cardinality <= 2) {
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return array_container_create_range(range_start, range_end);
} else {
*result_type = RUN_CONTAINER_TYPE_CODE;
return run_container_create_range(range_start, range_end);
}
}
/* Create a container with all the values between in [min,max) at a
distance k*step from min. */
static inline void *container_from_range(uint8_t *type, uint32_t min,
uint32_t max, uint16_t step) {
if (step == 0) return NULL; // being paranoid
if (step == 1) {
return container_range_of_ones(min,max,type);
// Note: the result is not always a run (need to check the cardinality)
//*type = RUN_CONTAINER_TYPE_CODE;
//return run_container_create_range(min, max);
}
int size = (max - min + step - 1) / step;
if (size <= DEFAULT_MAX_SIZE) { // array container
*type = ARRAY_CONTAINER_TYPE_CODE;
array_container_t *array = array_container_create_given_capacity(size);
array_container_add_from_range(array, min, max, step);
assert(array->cardinality == size);
return array;
} else { // bitset container
*type = BITSET_CONTAINER_TYPE_CODE;
bitset_container_t *bitset = bitset_container_create();
bitset_container_add_from_range(bitset, min, max, step);
assert(bitset->cardinality == size);
return bitset;
}
}
/**
* "repair" the container after lazy operations.
*/
static inline void *container_repair_after_lazy(void *container,
uint8_t *typecode) {
container = get_writable_copy_if_shared(
container, typecode); // TODO: this introduces unnecessary cloning
void *result = NULL;
switch (*typecode) {
case BITSET_CONTAINER_TYPE_CODE:
((bitset_container_t *)container)->cardinality =
bitset_container_compute_cardinality(
(bitset_container_t *)container);
if (((bitset_container_t *)container)->cardinality <=
DEFAULT_MAX_SIZE) {
result = array_container_from_bitset(
(const bitset_container_t *)container);
bitset_container_free((bitset_container_t *)container);
*typecode = ARRAY_CONTAINER_TYPE_CODE;
return result;
}
return container;
case ARRAY_CONTAINER_TYPE_CODE:
return container; // nothing to do
case RUN_CONTAINER_TYPE_CODE:
return convert_run_to_efficient_container_and_free(
(run_container_t *)container, typecode);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
/**
* Writes the underlying array to buf, outputs how many bytes were written.
* This is meant to be byte-by-byte compatible with the Java and Go versions of
* Roaring.
* The number of bytes written should be
* container_write(container, buf).
*
*/
static inline int32_t container_write(const void *container, uint8_t typecode,
char *buf) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_write((const bitset_container_t *)container, buf);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_write((const array_container_t *)container, buf);
case RUN_CONTAINER_TYPE_CODE:
return run_container_write((const run_container_t *)container, buf);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
/**
* Get the container size in bytes under portable serialization (see
* container_write), requires a
* typecode
*/
static inline int32_t container_size_in_bytes(const void *container,
uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_size_in_bytes(
(const bitset_container_t *)container);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_size_in_bytes(
(const array_container_t *)container);
case RUN_CONTAINER_TYPE_CODE:
return run_container_size_in_bytes((const run_container_t *)container);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
/**
* print the container (useful for debugging), requires a typecode
*/
void container_printf(const void *container, uint8_t typecode);
/**
* print the content of the container as a comma-separated list of 32-bit values
* starting at base, requires a typecode
*/
void container_printf_as_uint32_array(const void *container, uint8_t typecode,
uint32_t base);
/**
* Checks whether a container is not empty, requires a typecode
*/
static inline bool container_nonzero_cardinality(const void *container,
uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_const_nonzero_cardinality(
(const bitset_container_t *)container);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_nonzero_cardinality(
(const array_container_t *)container);
case RUN_CONTAINER_TYPE_CODE:
return run_container_nonzero_cardinality(
(const run_container_t *)container);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
/**
* Recover memory from a container, requires a typecode
*/
void container_free(void *container, uint8_t typecode);
/**
* Convert a container to an array of values, requires a typecode as well as a
* "base" (most significant values)
* Returns number of ints added.
*/
static inline int container_to_uint32_array(uint32_t *output,
const void *container,
uint8_t typecode, uint32_t base) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_to_uint32_array(
output, (const bitset_container_t *)container, base);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_to_uint32_array(
output, (const array_container_t *)container, base);
case RUN_CONTAINER_TYPE_CODE:
return run_container_to_uint32_array(
output, (const run_container_t *)container, base);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return 0; // unreached
}
}
/**
* Add a value to a container, requires a typecode, fills in new_typecode and
* return (possibly different) container.
* This function may allocate a new container, and caller is responsible for
* memory deallocation
*/
static inline void *container_add(void *container, uint16_t val,
uint8_t typecode, uint8_t *new_typecode) {
container = get_writable_copy_if_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
bitset_container_set((bitset_container_t *)container, val);
*new_typecode = BITSET_CONTAINER_TYPE_CODE;
return container;
case ARRAY_CONTAINER_TYPE_CODE: {
array_container_t *ac = (array_container_t *)container;
if (array_container_try_add(ac, val, DEFAULT_MAX_SIZE) != -1) {
*new_typecode = ARRAY_CONTAINER_TYPE_CODE;
return ac;
} else {
bitset_container_t* bitset = bitset_container_from_array(ac);
bitset_container_add(bitset, val);
*new_typecode = BITSET_CONTAINER_TYPE_CODE;
return bitset;
}
} break;
case RUN_CONTAINER_TYPE_CODE:
// per Java, no container type adjustments are done (revisit?)
run_container_add((run_container_t *)container, val);
*new_typecode = RUN_CONTAINER_TYPE_CODE;
return container;
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Remove a value from a container, requires a typecode, fills in new_typecode
* and
* return (possibly different) container.
* This function may allocate a new container, and caller is responsible for
* memory deallocation
*/
static inline void *container_remove(void *container, uint16_t val,
uint8_t typecode, uint8_t *new_typecode) {
container = get_writable_copy_if_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
if (bitset_container_remove((bitset_container_t *)container, val)) {
if (bitset_container_cardinality(
(bitset_container_t *)container) <= DEFAULT_MAX_SIZE) {
*new_typecode = ARRAY_CONTAINER_TYPE_CODE;
return array_container_from_bitset(
(bitset_container_t *)container);
}
}
*new_typecode = typecode;
return container;
case ARRAY_CONTAINER_TYPE_CODE:
*new_typecode = typecode;
array_container_remove((array_container_t *)container, val);
return container;
case RUN_CONTAINER_TYPE_CODE:
// per Java, no container type adjustments are done (revisit?)
run_container_remove((run_container_t *)container, val);
*new_typecode = RUN_CONTAINER_TYPE_CODE;
return container;
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Check whether a value is in a container, requires a typecode
*/
static inline bool container_contains(const void *container, uint16_t val,
uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_get((const bitset_container_t *)container,
val);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_contains(
(const array_container_t *)container, val);
case RUN_CONTAINER_TYPE_CODE:
return run_container_contains((const run_container_t *)container,
val);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
/**
* Check whether a range of values from range_start (included) to range_end (excluded)
* is in a container, requires a typecode
*/
static inline bool container_contains_range(const void *container, uint32_t range_start,
uint32_t range_end, uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_get_range((const bitset_container_t *)container,
range_start, range_end);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_contains_range((const array_container_t *)container,
range_start, range_end);
case RUN_CONTAINER_TYPE_CODE:
return run_container_contains_range((const run_container_t *)container,
range_start, range_end);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
int32_t container_serialize(const void *container, uint8_t typecode,
char *buf) WARN_UNUSED;
uint32_t container_serialization_len(const void *container, uint8_t typecode);
void *container_deserialize(uint8_t typecode, const char *buf, size_t buf_len);
/**
* Returns true if the two containers have the same content. Note that
* two containers having different types can be "equal" in this sense.
*/
static inline bool container_equals(const void *c1, uint8_t type1,
const void *c2, uint8_t type2) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return bitset_container_equals((const bitset_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
return run_container_equals_bitset((const run_container_t *)c2,
(const bitset_container_t *)c1);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return run_container_equals_bitset((const run_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
// java would always return false?
return array_container_equal_bitset((const array_container_t *)c2,
(const bitset_container_t *)c1);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
// java would always return false?
return array_container_equal_bitset((const array_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return run_container_equals_array((const run_container_t *)c2,
(const array_container_t *)c1);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
return run_container_equals_array((const run_container_t *)c1,
(const array_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
return array_container_equals((const array_container_t *)c1,
(const array_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return run_container_equals((const run_container_t *)c1,
(const run_container_t *)c2);
default:
assert(false);
__builtin_unreachable();
return false;
}
}
/**
* Returns true if the container c1 is a subset of the container c2. Note that
* c1 can be a subset of c2 even if they have a different type.
*/
static inline bool container_is_subset(const void *c1, uint8_t type1,
const void *c2, uint8_t type2) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return bitset_container_is_subset((const bitset_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
return bitset_container_is_subset_run((const bitset_container_t *)c1,
(const run_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return run_container_is_subset_bitset((const run_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
return false; // by construction, size(c1) > size(c2)
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return array_container_is_subset_bitset((const array_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return array_container_is_subset_run((const array_container_t *)c1,
(const run_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
return run_container_is_subset_array((const run_container_t *)c1,
(const array_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
return array_container_is_subset((const array_container_t *)c1,
(const array_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return run_container_is_subset((const run_container_t *)c1,
(const run_container_t *)c2);
default:
assert(false);
__builtin_unreachable();
return false;
}
}
// macro-izations possibilities for generic non-inplace binary-op dispatch
/**
* Compute intersection between two containers, generate a new container (having
* type result_type), requires a typecode. This allocates new memory, caller
* is responsible for deallocation.
*/
static inline void *container_and(const void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = bitset_bitset_container_intersection(
(const bitset_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
result = array_container_create();
array_container_intersection((const array_container_t *)c1,
(const array_container_t *)c2,
(array_container_t *)result);
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
run_container_intersection((const run_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
return convert_run_to_efficient_container_and_free(
(run_container_t *)result, result_type);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
result = array_container_create();
array_bitset_container_intersection((const array_container_t *)c2,
(const bitset_container_t *)c1,
(array_container_t *)result);
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
array_bitset_container_intersection((const array_container_t *)c1,
(const bitset_container_t *)c2,
(array_container_t *)result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_intersection(
(const run_container_t *)c2,
(const bitset_container_t *)c1, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_intersection(
(const run_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
array_run_container_intersection((const array_container_t *)c1,
(const run_container_t *)c2,
(array_container_t *)result);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
array_run_container_intersection((const array_container_t *)c2,
(const run_container_t *)c1,
(array_container_t *)result);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Compute the size of the intersection between two containers.
*/
static inline int container_and_cardinality(const void *c1, uint8_t type1,
const void *c2, uint8_t type2) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return bitset_container_and_justcard(
(const bitset_container_t *)c1, (const bitset_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
return array_container_intersection_cardinality(
(const array_container_t *)c1, (const array_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return run_container_intersection_cardinality(
(const run_container_t *)c1, (const run_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
return array_bitset_container_intersection_cardinality(
(const array_container_t *)c2, (const bitset_container_t *)c1);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return array_bitset_container_intersection_cardinality(
(const array_container_t *)c1, (const bitset_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
return run_bitset_container_intersection_cardinality(
(const run_container_t *)c2, (const bitset_container_t *)c1);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return run_bitset_container_intersection_cardinality(
(const run_container_t *)c1, (const bitset_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return array_run_container_intersection_cardinality(
(const array_container_t *)c1, (const run_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
return array_run_container_intersection_cardinality(
(const array_container_t *)c2, (const run_container_t *)c1);
default:
assert(false);
__builtin_unreachable();
return 0;
}
}
/**
* Check whether two containers intersect.
*/
static inline bool container_intersect(const void *c1, uint8_t type1, const void *c2,
uint8_t type2) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return bitset_container_intersect(
(const bitset_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
return array_container_intersect((const array_container_t *)c1,
(const array_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return run_container_intersect((const run_container_t *)c1,
(const run_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
return array_bitset_container_intersect((const array_container_t *)c2,
(const bitset_container_t *)c1);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return array_bitset_container_intersect((const array_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
return run_bitset_container_intersect(
(const run_container_t *)c2,
(const bitset_container_t *)c1);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
return run_bitset_container_intersect(
(const run_container_t *)c1,
(const bitset_container_t *)c2);
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
return array_run_container_intersect((const array_container_t *)c1,
(const run_container_t *)c2);
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
return array_run_container_intersect((const array_container_t *)c2,
(const run_container_t *)c1);
default:
assert(false);
__builtin_unreachable();
return 0;
}
}
/**
* Compute intersection between two containers, with result in the first
container if possible. If the returned pointer is identical to c1,
then the container has been modified. If the returned pointer is different
from c1, then a new container has been created and the caller is responsible
for freeing it.
The type of the first container may change. Returns the modified
(and possibly new) container.
*/
static inline void *container_iand(void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
c1 = get_writable_copy_if_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type =
bitset_bitset_container_intersection_inplace(
(bitset_container_t *)c1, (const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
array_container_intersection_inplace((array_container_t *)c1,
(const array_container_t *)c2);
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return c1;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
run_container_intersection((const run_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
// as of January 2016, Java code used non-in-place intersection for
// two runcontainers
return convert_run_to_efficient_container_and_free(
(run_container_t *)result, result_type);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
// c1 is a bitmap so no inplace possible
result = array_container_create();
array_bitset_container_intersection((const array_container_t *)c2,
(const bitset_container_t *)c1,
(array_container_t *)result);
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
array_bitset_container_intersection(
(const array_container_t *)c1, (const bitset_container_t *)c2,
(array_container_t *)c1); // allowed
return c1;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
// will attempt in-place computation
*result_type = run_bitset_container_intersection(
(const run_container_t *)c2,
(const bitset_container_t *)c1, &c1)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return c1;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_intersection(
(const run_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
array_run_container_intersection((const array_container_t *)c1,
(const run_container_t *)c2,
(array_container_t *)result);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE; // never bitset
array_run_container_intersection((const array_container_t *)c2,
(const run_container_t *)c1,
(array_container_t *)result);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Compute union between two containers, generate a new container (having type
* result_type), requires a typecode. This allocates new memory, caller
* is responsible for deallocation.
*/
static inline void *container_or(const void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = bitset_container_create();
bitset_container_or((const bitset_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_array_container_union(
(const array_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
run_container_union((const run_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
*result_type = RUN_CONTAINER_TYPE_CODE;
// todo: could be optimized since will never convert to array
result = convert_run_to_efficient_container_and_free(
(run_container_t *)result, (uint8_t *)result_type);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
result = bitset_container_create();
array_bitset_container_union((const array_container_t *)c2,
(const bitset_container_t *)c1,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = bitset_container_create();
array_bitset_container_union((const array_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c2)) {
result = run_container_create();
*result_type = RUN_CONTAINER_TYPE_CODE;
run_container_copy((const run_container_t *)c2,
(run_container_t *)result);
return result;
}
result = bitset_container_create();
run_bitset_container_union((const run_container_t *)c2,
(const bitset_container_t *)c1,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c1)) {
result = run_container_create();
*result_type = RUN_CONTAINER_TYPE_CODE;
run_container_copy((const run_container_t *)c1,
(run_container_t *)result);
return result;
}
result = bitset_container_create();
run_bitset_container_union((const run_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_union((const array_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
result = convert_run_to_efficient_container_and_free(
(run_container_t *)result, (uint8_t *)result_type);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_union((const array_container_t *)c2,
(const run_container_t *)c1,
(run_container_t *)result);
result = convert_run_to_efficient_container_and_free(
(run_container_t *)result, (uint8_t *)result_type);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL; // unreached
}
}
/**
* Compute union between two containers, generate a new container (having type
* result_type), requires a typecode. This allocates new memory, caller
* is responsible for deallocation.
*
* This lazy version delays some operations such as the maintenance of the
* cardinality. It requires repair later on the generated containers.
*/
static inline void *container_lazy_or(const void *c1, uint8_t type1,
const void *c2, uint8_t type2,
uint8_t *result_type) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = bitset_container_create();
bitset_container_or_nocard(
(const bitset_container_t *)c1, (const bitset_container_t *)c2,
(bitset_container_t *)result); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_array_container_lazy_union(
(const array_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
run_container_union((const run_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
*result_type = RUN_CONTAINER_TYPE_CODE;
// we are being lazy
result = convert_run_to_efficient_container(
(run_container_t *)result, result_type);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
result = bitset_container_create();
array_bitset_container_lazy_union(
(const array_container_t *)c2, (const bitset_container_t *)c1,
(bitset_container_t *)result); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = bitset_container_create();
array_bitset_container_lazy_union(
(const array_container_t *)c1, (const bitset_container_t *)c2,
(bitset_container_t *)result); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c2)) {
result = run_container_create();
*result_type = RUN_CONTAINER_TYPE_CODE;
run_container_copy((const run_container_t *)c2,
(run_container_t *)result);
return result;
}
result = bitset_container_create();
run_bitset_container_lazy_union(
(const run_container_t *)c2, (const bitset_container_t *)c1,
(bitset_container_t *)result); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c1)) {
result = run_container_create();
*result_type = RUN_CONTAINER_TYPE_CODE;
run_container_copy((const run_container_t *)c1,
(run_container_t *)result);
return result;
}
result = bitset_container_create();
run_bitset_container_lazy_union(
(const run_container_t *)c1, (const bitset_container_t *)c2,
(bitset_container_t *)result); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_union((const array_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
*result_type = RUN_CONTAINER_TYPE_CODE;
// next line skipped since we are lazy
// result = convert_run_to_efficient_container(result, result_type);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_union(
(const array_container_t *)c2, (const run_container_t *)c1,
(run_container_t *)result); // TODO make lazy
*result_type = RUN_CONTAINER_TYPE_CODE;
// next line skipped since we are lazy
// result = convert_run_to_efficient_container(result, result_type);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL; // unreached
}
}
/**
* Compute the union between two containers, with result in the first container.
* If the returned pointer is identical to c1, then the container has been
* modified.
* If the returned pointer is different from c1, then a new container has been
* created and the caller is responsible for freeing it.
* The type of the first container may change. Returns the modified
* (and possibly new) container
*/
static inline void *container_ior(void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
c1 = get_writable_copy_if_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
bitset_container_or((const bitset_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)c1);
#ifdef OR_BITSET_CONVERSION_TO_FULL
if (((bitset_container_t *)c1)->cardinality ==
(1 << 16)) { // we convert
result = run_container_create_range(0, (1 << 16));
*result_type = RUN_CONTAINER_TYPE_CODE;
return result;
}
#endif
*result_type = BITSET_CONTAINER_TYPE_CODE;
return c1;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_array_container_inplace_union(
(array_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
if((result == NULL)
&& (*result_type == ARRAY_CONTAINER_TYPE_CODE)) {
return c1; // the computation was done in-place!
}
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
run_container_union_inplace((run_container_t *)c1,
(const run_container_t *)c2);
return convert_run_to_efficient_container((run_container_t *)c1,
result_type);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
array_bitset_container_union((const array_container_t *)c2,
(const bitset_container_t *)c1,
(bitset_container_t *)c1);
*result_type = BITSET_CONTAINER_TYPE_CODE; // never array
return c1;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
// c1 is an array, so no in-place possible
result = bitset_container_create();
*result_type = BITSET_CONTAINER_TYPE_CODE;
array_bitset_container_union((const array_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c2)) {
result = run_container_create();
*result_type = RUN_CONTAINER_TYPE_CODE;
run_container_copy((const run_container_t *)c2,
(run_container_t *)result);
return result;
}
run_bitset_container_union((const run_container_t *)c2,
(const bitset_container_t *)c1,
(bitset_container_t *)c1); // allowed
*result_type = BITSET_CONTAINER_TYPE_CODE;
return c1;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c1)) {
*result_type = RUN_CONTAINER_TYPE_CODE;
return c1;
}
result = bitset_container_create();
run_bitset_container_union((const run_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_union((const array_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
result = convert_run_to_efficient_container_and_free(
(run_container_t *)result, result_type);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
array_run_container_inplace_union((const array_container_t *)c2,
(run_container_t *)c1);
c1 = convert_run_to_efficient_container((run_container_t *)c1,
result_type);
return c1;
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Compute the union between two containers, with result in the first container.
* If the returned pointer is identical to c1, then the container has been
* modified.
* If the returned pointer is different from c1, then a new container has been
* created and the caller is responsible for freeing it.
* The type of the first container may change. Returns the modified
* (and possibly new) container
*
* This lazy version delays some operations such as the maintenance of the
* cardinality. It requires repair later on the generated containers.
*/
static inline void *container_lazy_ior(void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
assert(type1 != SHARED_CONTAINER_TYPE_CODE);
// c1 = get_writable_copy_if_shared(c1,&type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
#ifdef LAZY_OR_BITSET_CONVERSION_TO_FULL
// if we have two bitsets, we might as well compute the cardinality
bitset_container_or((const bitset_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)c1);
// it is possible that two bitsets can lead to a full container
if (((bitset_container_t *)c1)->cardinality ==
(1 << 16)) { // we convert
result = run_container_create_range(0, (1 << 16));
*result_type = RUN_CONTAINER_TYPE_CODE;
return result;
}
#else
bitset_container_or_nocard((const bitset_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)c1);
#endif
*result_type = BITSET_CONTAINER_TYPE_CODE;
return c1;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_array_container_lazy_inplace_union(
(array_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
if((result == NULL)
&& (*result_type == ARRAY_CONTAINER_TYPE_CODE)) {
return c1; // the computation was done in-place!
}
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
run_container_union_inplace((run_container_t *)c1,
(const run_container_t *)c2);
*result_type = RUN_CONTAINER_TYPE_CODE;
return convert_run_to_efficient_container((run_container_t *)c1,
result_type);
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
array_bitset_container_lazy_union(
(const array_container_t *)c2, (const bitset_container_t *)c1,
(bitset_container_t *)c1); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE; // never array
return c1;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
// c1 is an array, so no in-place possible
result = bitset_container_create();
*result_type = BITSET_CONTAINER_TYPE_CODE;
array_bitset_container_lazy_union(
(const array_container_t *)c1, (const bitset_container_t *)c2,
(bitset_container_t *)result); // is lazy
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c2)) {
result = run_container_create();
*result_type = RUN_CONTAINER_TYPE_CODE;
run_container_copy((const run_container_t *)c2,
(run_container_t *)result);
return result;
}
run_bitset_container_lazy_union(
(const run_container_t *)c2, (const bitset_container_t *)c1,
(bitset_container_t *)c1); // allowed // lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return c1;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c1)) {
*result_type = RUN_CONTAINER_TYPE_CODE;
return c1;
}
result = bitset_container_create();
run_bitset_container_lazy_union(
(const run_container_t *)c1, (const bitset_container_t *)c2,
(bitset_container_t *)result); // lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_union((const array_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
*result_type = RUN_CONTAINER_TYPE_CODE;
// next line skipped since we are lazy
// result = convert_run_to_efficient_container_and_free(result,
// result_type);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
array_run_container_inplace_union((const array_container_t *)c2,
(run_container_t *)c1);
*result_type = RUN_CONTAINER_TYPE_CODE;
// next line skipped since we are lazy
// result = convert_run_to_efficient_container_and_free(result,
// result_type);
return c1;
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Compute symmetric difference (xor) between two containers, generate a new
* container (having type result_type), requires a typecode. This allocates new
* memory, caller is responsible for deallocation.
*/
static inline void *container_xor(const void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = bitset_bitset_container_xor(
(const bitset_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_array_container_xor(
(const array_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
*result_type =
run_run_container_xor((const run_container_t *)c1,
(const run_container_t *)c2, &result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_bitset_container_xor(
(const array_container_t *)c2,
(const bitset_container_t *)c1, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = array_bitset_container_xor(
(const array_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_xor(
(const run_container_t *)c2,
(const bitset_container_t *)c1, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_xor(
(const run_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
*result_type =
array_run_container_xor((const array_container_t *)c1,
(const run_container_t *)c2, &result);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
*result_type =
array_run_container_xor((const array_container_t *)c2,
(const run_container_t *)c1, &result);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL; // unreached
}
}
/**
* Compute xor between two containers, generate a new container (having type
* result_type), requires a typecode. This allocates new memory, caller
* is responsible for deallocation.
*
* This lazy version delays some operations such as the maintenance of the
* cardinality. It requires repair later on the generated containers.
*/
static inline void *container_lazy_xor(const void *c1, uint8_t type1,
const void *c2, uint8_t type2,
uint8_t *result_type) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = bitset_container_create();
bitset_container_xor_nocard(
(const bitset_container_t *)c1, (const bitset_container_t *)c2,
(bitset_container_t *)result); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_array_container_lazy_xor(
(const array_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
// nothing special done yet.
*result_type =
run_run_container_xor((const run_container_t *)c1,
(const run_container_t *)c2, &result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
result = bitset_container_create();
*result_type = BITSET_CONTAINER_TYPE_CODE;
array_bitset_container_lazy_xor((const array_container_t *)c2,
(const bitset_container_t *)c1,
(bitset_container_t *)result);
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = bitset_container_create();
*result_type = BITSET_CONTAINER_TYPE_CODE;
array_bitset_container_lazy_xor((const array_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
result = bitset_container_create();
run_bitset_container_lazy_xor((const run_container_t *)c2,
(const bitset_container_t *)c1,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = bitset_container_create();
run_bitset_container_lazy_xor((const run_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)result);
*result_type = BITSET_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_lazy_xor((const array_container_t *)c1,
(const run_container_t *)c2,
(run_container_t *)result);
*result_type = RUN_CONTAINER_TYPE_CODE;
// next line skipped since we are lazy
// result = convert_run_to_efficient_container(result, result_type);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
result = run_container_create();
array_run_container_lazy_xor((const array_container_t *)c2,
(const run_container_t *)c1,
(run_container_t *)result);
*result_type = RUN_CONTAINER_TYPE_CODE;
// next line skipped since we are lazy
// result = convert_run_to_efficient_container(result, result_type);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL; // unreached
}
}
/**
* Compute the xor between two containers, with result in the first container.
* If the returned pointer is identical to c1, then the container has been
* modified.
* If the returned pointer is different from c1, then a new container has been
* created and the caller is responsible for freeing it.
* The type of the first container may change. Returns the modified
* (and possibly new) container
*/
static inline void *container_ixor(void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
c1 = get_writable_copy_if_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = bitset_bitset_container_ixor(
(bitset_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = array_array_container_ixor(
(array_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
*result_type = run_run_container_ixor(
(run_container_t *)c1, (const run_container_t *)c2, &result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = bitset_array_container_ixor(
(bitset_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = array_bitset_container_ixor(
(array_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
*result_type =
bitset_run_container_ixor((bitset_container_t *)c1,
(const run_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_ixor(
(run_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
*result_type = array_run_container_ixor(
(array_container_t *)c1, (const run_container_t *)c2, &result);
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
*result_type = run_array_container_ixor(
(run_container_t *)c1, (const array_container_t *)c2, &result);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Compute the xor between two containers, with result in the first container.
* If the returned pointer is identical to c1, then the container has been
* modified.
* If the returned pointer is different from c1, then a new container has been
* created and the caller is responsible for freeing it.
* The type of the first container may change. Returns the modified
* (and possibly new) container
*
* This lazy version delays some operations such as the maintenance of the
* cardinality. It requires repair later on the generated containers.
*/
static inline void *container_lazy_ixor(void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
assert(type1 != SHARED_CONTAINER_TYPE_CODE);
// c1 = get_writable_copy_if_shared(c1,&type1);
c2 = container_unwrap_shared(c2, &type2);
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
bitset_container_xor_nocard((bitset_container_t *)c1,
(const bitset_container_t *)c2,
(bitset_container_t *)c1); // is lazy
*result_type = BITSET_CONTAINER_TYPE_CODE;
return c1;
// TODO: other cases being lazy, esp. when we know inplace not likely
// could see the corresponding code for union
default:
// we may have a dirty bitset (without a precomputed cardinality) and
// calling container_ixor on it might be unsafe.
if( (type1 == BITSET_CONTAINER_TYPE_CODE)
&& (((const bitset_container_t *)c1)->cardinality == BITSET_UNKNOWN_CARDINALITY)) {
((bitset_container_t *)c1)->cardinality = bitset_container_compute_cardinality((bitset_container_t *)c1);
}
return container_ixor(c1, type1, c2, type2, result_type);
}
}
/**
* Compute difference (andnot) between two containers, generate a new
* container (having type result_type), requires a typecode. This allocates new
* memory, caller is responsible for deallocation.
*/
static inline void *container_andnot(const void *c1, uint8_t type1,
const void *c2, uint8_t type2,
uint8_t *result_type) {
c1 = container_unwrap_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = bitset_bitset_container_andnot(
(const bitset_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
result = array_container_create();
array_array_container_andnot((const array_container_t *)c1,
(const array_container_t *)c2,
(array_container_t *)result);
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c2)) {
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return result;
}
*result_type =
run_run_container_andnot((const run_container_t *)c1,
(const run_container_t *)c2, &result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = bitset_array_container_andnot(
(const bitset_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
result = array_container_create();
array_bitset_container_andnot((const array_container_t *)c1,
(const bitset_container_t *)c2,
(array_container_t *)result);
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c2)) {
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return result;
}
*result_type = bitset_run_container_andnot(
(const bitset_container_t *)c1,
(const run_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_andnot(
(const run_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
if (run_container_is_full((const run_container_t *)c2)) {
result = array_container_create();
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return result;
}
result = array_container_create();
array_run_container_andnot((const array_container_t *)c1,
(const run_container_t *)c2,
(array_container_t *)result);
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
*result_type = run_array_container_andnot(
(const run_container_t *)c1, (const array_container_t *)c2,
&result);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL; // unreached
}
}
/**
* Compute the andnot between two containers, with result in the first
* container.
* If the returned pointer is identical to c1, then the container has been
* modified.
* If the returned pointer is different from c1, then a new container has been
* created and the caller is responsible for freeing it.
* The type of the first container may change. Returns the modified
* (and possibly new) container
*/
static inline void *container_iandnot(void *c1, uint8_t type1, const void *c2,
uint8_t type2, uint8_t *result_type) {
c1 = get_writable_copy_if_shared(c1, &type1);
c2 = container_unwrap_shared(c2, &type2);
void *result = NULL;
switch (CONTAINER_PAIR(type1, type2)) {
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = bitset_bitset_container_iandnot(
(bitset_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
array_array_container_iandnot((array_container_t *)c1,
(const array_container_t *)c2);
*result_type = ARRAY_CONTAINER_TYPE_CODE;
return c1;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
*result_type = run_run_container_iandnot(
(run_container_t *)c1, (const run_container_t *)c2, &result);
return result;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
ARRAY_CONTAINER_TYPE_CODE):
*result_type = bitset_array_container_iandnot(
(bitset_container_t *)c1,
(const array_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = ARRAY_CONTAINER_TYPE_CODE;
array_bitset_container_iandnot((array_container_t *)c1,
(const bitset_container_t *)c2);
return c1;
case CONTAINER_PAIR(BITSET_CONTAINER_TYPE_CODE,
RUN_CONTAINER_TYPE_CODE):
*result_type = bitset_run_container_iandnot(
(bitset_container_t *)c1,
(const run_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE,
BITSET_CONTAINER_TYPE_CODE):
*result_type = run_bitset_container_iandnot(
(run_container_t *)c1,
(const bitset_container_t *)c2, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case CONTAINER_PAIR(ARRAY_CONTAINER_TYPE_CODE, RUN_CONTAINER_TYPE_CODE):
*result_type = ARRAY_CONTAINER_TYPE_CODE;
array_run_container_iandnot((array_container_t *)c1,
(const run_container_t *)c2);
return c1;
case CONTAINER_PAIR(RUN_CONTAINER_TYPE_CODE, ARRAY_CONTAINER_TYPE_CODE):
*result_type = run_array_container_iandnot(
(run_container_t *)c1, (const array_container_t *)c2, &result);
return result;
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* Visit all values x of the container once, passing (base+x,ptr)
* to iterator. You need to specify a container and its type.
* Returns true if the iteration should continue.
*/
static inline bool container_iterate(const void *container, uint8_t typecode,
uint32_t base, roaring_iterator iterator,
void *ptr) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_iterate(
(const bitset_container_t *)container, base, iterator, ptr);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_iterate((const array_container_t *)container,
base, iterator, ptr);
case RUN_CONTAINER_TYPE_CODE:
return run_container_iterate((const run_container_t *)container,
base, iterator, ptr);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
static inline bool container_iterate64(const void *container, uint8_t typecode,
uint32_t base,
roaring_iterator64 iterator,
uint64_t high_bits, void *ptr) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_iterate64(
(const bitset_container_t *)container, base, iterator,
high_bits, ptr);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_iterate64(
(const array_container_t *)container, base, iterator, high_bits,
ptr);
case RUN_CONTAINER_TYPE_CODE:
return run_container_iterate64((const run_container_t *)container,
base, iterator, high_bits, ptr);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
static inline void *container_not(const void *c, uint8_t typ,
uint8_t *result_type) {
c = container_unwrap_shared(c, &typ);
void *result = NULL;
switch (typ) {
case BITSET_CONTAINER_TYPE_CODE:
*result_type = bitset_container_negation(
(const bitset_container_t *)c, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case ARRAY_CONTAINER_TYPE_CODE:
result = bitset_container_create();
*result_type = BITSET_CONTAINER_TYPE_CODE;
array_container_negation((const array_container_t *)c,
(bitset_container_t *)result);
return result;
case RUN_CONTAINER_TYPE_CODE:
*result_type =
run_container_negation((const run_container_t *)c, &result);
return result;
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
static inline void *container_not_range(const void *c, uint8_t typ,
uint32_t range_start,
uint32_t range_end,
uint8_t *result_type) {
c = container_unwrap_shared(c, &typ);
void *result = NULL;
switch (typ) {
case BITSET_CONTAINER_TYPE_CODE:
*result_type =
bitset_container_negation_range((const bitset_container_t *)c,
range_start, range_end, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case ARRAY_CONTAINER_TYPE_CODE:
*result_type =
array_container_negation_range((const array_container_t *)c,
range_start, range_end, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case RUN_CONTAINER_TYPE_CODE:
*result_type = run_container_negation_range(
(const run_container_t *)c, range_start, range_end, &result);
return result;
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
static inline void *container_inot(void *c, uint8_t typ, uint8_t *result_type) {
c = get_writable_copy_if_shared(c, &typ);
void *result = NULL;
switch (typ) {
case BITSET_CONTAINER_TYPE_CODE:
*result_type = bitset_container_negation_inplace(
(bitset_container_t *)c, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case ARRAY_CONTAINER_TYPE_CODE:
// will never be inplace
result = bitset_container_create();
*result_type = BITSET_CONTAINER_TYPE_CODE;
array_container_negation((array_container_t *)c,
(bitset_container_t *)result);
array_container_free((array_container_t *)c);
return result;
case RUN_CONTAINER_TYPE_CODE:
*result_type =
run_container_negation_inplace((run_container_t *)c, &result);
return result;
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
static inline void *container_inot_range(void *c, uint8_t typ,
uint32_t range_start,
uint32_t range_end,
uint8_t *result_type) {
c = get_writable_copy_if_shared(c, &typ);
void *result = NULL;
switch (typ) {
case BITSET_CONTAINER_TYPE_CODE:
*result_type =
bitset_container_negation_range_inplace(
(bitset_container_t *)c, range_start, range_end, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case ARRAY_CONTAINER_TYPE_CODE:
*result_type =
array_container_negation_range_inplace(
(array_container_t *)c, range_start, range_end, &result)
? BITSET_CONTAINER_TYPE_CODE
: ARRAY_CONTAINER_TYPE_CODE;
return result;
case RUN_CONTAINER_TYPE_CODE:
*result_type = run_container_negation_range_inplace(
(run_container_t *)c, range_start, range_end, &result);
return result;
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return NULL;
}
}
/**
* If the element of given rank is in this container, supposing that
* the first
* element has rank start_rank, then the function returns true and
* sets element
* accordingly.
* Otherwise, it returns false and update start_rank.
*/
static inline bool container_select(const void *container, uint8_t typecode,
uint32_t *start_rank, uint32_t rank,
uint32_t *element) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_select((const bitset_container_t *)container,
start_rank, rank, element);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_select((const array_container_t *)container,
start_rank, rank, element);
case RUN_CONTAINER_TYPE_CODE:
return run_container_select((const run_container_t *)container,
start_rank, rank, element);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
static inline uint16_t container_maximum(const void *container,
uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_maximum((const bitset_container_t *)container);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_maximum((const array_container_t *)container);
case RUN_CONTAINER_TYPE_CODE:
return run_container_maximum((const run_container_t *)container);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
static inline uint16_t container_minimum(const void *container,
uint8_t typecode) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_minimum((const bitset_container_t *)container);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_minimum((const array_container_t *)container);
case RUN_CONTAINER_TYPE_CODE:
return run_container_minimum((const run_container_t *)container);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
// number of values smaller or equal to x
static inline int container_rank(const void *container, uint8_t typecode,
uint16_t x) {
container = container_unwrap_shared(container, &typecode);
switch (typecode) {
case BITSET_CONTAINER_TYPE_CODE:
return bitset_container_rank((const bitset_container_t *)container, x);
case ARRAY_CONTAINER_TYPE_CODE:
return array_container_rank((const array_container_t *)container, x);
case RUN_CONTAINER_TYPE_CODE:
return run_container_rank((const run_container_t *)container, x);
case SHARED_CONTAINER_TYPE_CODE:
default:
assert(false);
__builtin_unreachable();
return false;
}
}
/**
* Add all values in range [min, max] to a given container.
*
* If the returned pointer is different from $container, then a new container
* has been created and the caller is responsible for freeing it.
* The type of the first container may change. Returns the modified
* (and possibly new) container.
*/
static inline void *container_add_range(void *container, uint8_t type,
uint32_t min, uint32_t max,
uint8_t *result_type) {
// NB: when selecting new container type, we perform only inexpensive checks
switch (type) {
case BITSET_CONTAINER_TYPE_CODE: {
bitset_container_t *bitset = (bitset_container_t *) container;
int32_t union_cardinality = 0;
union_cardinality += bitset->cardinality;
union_cardinality += max - min + 1;
union_cardinality -= bitset_lenrange_cardinality(bitset->array, min, max-min);
if (union_cardinality == INT32_C(0x10000)) {
*result_type = RUN_CONTAINER_TYPE_CODE;
return run_container_create_range(0, INT32_C(0x10000));
} else {
*result_type = BITSET_CONTAINER_TYPE_CODE;
bitset_set_lenrange(bitset->array, min, max - min);
bitset->cardinality = union_cardinality;
return bitset;
}
}
case ARRAY_CONTAINER_TYPE_CODE: {
array_container_t *array = (array_container_t *) container;
int32_t nvals_greater = count_greater(array->array, array->cardinality, max);
int32_t nvals_less = count_less(array->array, array->cardinality - nvals_greater, min);
int32_t union_cardinality = nvals_less + (max - min + 1) + nvals_greater;
if (union_cardinality == INT32_C(0x10000)) {
*result_type = RUN_CONTAINER_TYPE_CODE;
return run_container_create_range(0, INT32_C(0x10000));
} else if (union_cardinality <= DEFAULT_MAX_SIZE) {
*result_type = ARRAY_CONTAINER_TYPE_CODE;
array_container_add_range_nvals(array, min, max, nvals_less, nvals_greater);
return array;
} else {
*result_type = BITSET_CONTAINER_TYPE_CODE;
bitset_container_t *bitset = bitset_container_from_array(array);
bitset_set_lenrange(bitset->array, min, max - min);
bitset->cardinality = union_cardinality;
return bitset;
}
}
case RUN_CONTAINER_TYPE_CODE: {
run_container_t *run = (run_container_t *) container;
int32_t nruns_greater = rle16_count_greater(run->runs, run->n_runs, max);
int32_t nruns_less = rle16_count_less(run->runs, run->n_runs - nruns_greater, min);
int32_t run_size_bytes = (nruns_less + 1 + nruns_greater) * sizeof(rle16_t);
int32_t bitset_size_bytes = BITSET_CONTAINER_SIZE_IN_WORDS * sizeof(uint64_t);
if (run_size_bytes <= bitset_size_bytes) {
run_container_add_range_nruns(run, min, max, nruns_less, nruns_greater);
*result_type = RUN_CONTAINER_TYPE_CODE;
return run;
} else {
*result_type = BITSET_CONTAINER_TYPE_CODE;
return bitset_container_from_run_range(run, min, max);
}
}
case SHARED_CONTAINER_TYPE_CODE:
default:
__builtin_unreachable();
}
}
/*
* Removes all elements in range [min, max].
* Returns one of:
* - NULL if no elements left
* - pointer to the original container
* - pointer to a newly-allocated container (if it is more efficient)
*
* If the returned pointer is different from $container, then a new container
* has been created and the caller is responsible for freeing the original container.
*/
static inline void *container_remove_range(void *container, uint8_t type,
uint32_t min, uint32_t max,
uint8_t *result_type) {
switch (type) {
case BITSET_CONTAINER_TYPE_CODE: {
bitset_container_t *bitset = (bitset_container_t *) container;
int32_t result_cardinality = bitset->cardinality -
bitset_lenrange_cardinality(bitset->array, min, max-min);
if (result_cardinality == 0) {
return NULL;
} else if (result_cardinality < DEFAULT_MAX_SIZE) {
*result_type = ARRAY_CONTAINER_TYPE_CODE;
bitset_reset_range(bitset->array, min, max+1);
bitset->cardinality = result_cardinality;
return array_container_from_bitset(bitset);
} else {
*result_type = BITSET_CONTAINER_TYPE_CODE;
bitset_reset_range(bitset->array, min, max+1);
bitset->cardinality = result_cardinality;
return bitset;
}
}
case ARRAY_CONTAINER_TYPE_CODE: {
array_container_t *array = (array_container_t *) container;
int32_t nvals_greater = count_greater(array->array, array->cardinality, max);
int32_t nvals_less = count_less(array->array, array->cardinality - nvals_greater, min);
int32_t result_cardinality = nvals_less + nvals_greater;
if (result_cardinality == 0) {
return NULL;
} else {
*result_type = ARRAY_CONTAINER_TYPE_CODE;
array_container_remove_range(array, nvals_less,
array->cardinality - result_cardinality);
return array;
}
}
case RUN_CONTAINER_TYPE_CODE: {
run_container_t *run = (run_container_t *) container;
if (run->n_runs == 0) {
return NULL;
}
if (min <= run_container_minimum(run) && max >= run_container_maximum(run)) {
return NULL;
}
run_container_remove_range(run, min, max);
if (run_container_serialized_size_in_bytes(run->n_runs) <=
bitset_container_serialized_size_in_bytes()) {
*result_type = RUN_CONTAINER_TYPE_CODE;
return run;
} else {
*result_type = BITSET_CONTAINER_TYPE_CODE;
return bitset_container_from_run(run);
}
}
case SHARED_CONTAINER_TYPE_CODE:
default:
__builtin_unreachable();
}
}
#endif
/* end file include/roaring/containers/containers.h */
/* begin file include/roaring/roaring_array.h */
#ifndef INCLUDE_ROARING_ARRAY_H
#define INCLUDE_ROARING_ARRAY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#define MAX_CONTAINERS 65536
#define SERIALIZATION_ARRAY_UINT32 1
#define SERIALIZATION_CONTAINER 2
#define ROARING_FLAG_COW UINT8_C(0x1)
#define ROARING_FLAG_FROZEN UINT8_C(0x2)
enum {
SERIAL_COOKIE_NO_RUNCONTAINER = 12346,
SERIAL_COOKIE = 12347,
FROZEN_COOKIE = 13766,
NO_OFFSET_THRESHOLD = 4
};
/**
* Roaring arrays are array-based key-value pairs having containers as values
* and 16-bit integer keys. A roaring bitmap might be implemented as such.
*/
// parallel arrays. Element sizes quite different.
// Alternative is array
// of structs. Which would have better
// cache performance through binary searches?
typedef struct roaring_array_s {
int32_t size;
int32_t allocation_size;
void **containers;
uint16_t *keys;
uint8_t *typecodes;
uint8_t flags;
} roaring_array_t;
/**
* Create a new roaring array
*/
roaring_array_t *ra_create(void);
/**
* Initialize an existing roaring array with the specified capacity (in number
* of containers)
*/
bool ra_init_with_capacity(roaring_array_t *new_ra, uint32_t cap);
/**
* Initialize with zero capacity
*/
void ra_init(roaring_array_t *t);
/**
* Copies this roaring array, we assume that dest is not initialized
*/
bool ra_copy(const roaring_array_t *source, roaring_array_t *dest,
bool copy_on_write);
/*
* Shrinks the capacity, returns the number of bytes saved.
*/
int ra_shrink_to_fit(roaring_array_t *ra);
/**
* Copies this roaring array, we assume that dest is initialized
*/
bool ra_overwrite(const roaring_array_t *source, roaring_array_t *dest,
bool copy_on_write);
/**
* Frees the memory used by a roaring array
*/
void ra_clear(roaring_array_t *r);
/**
* Frees the memory used by a roaring array, but does not free the containers
*/
void ra_clear_without_containers(roaring_array_t *r);
/**
* Frees just the containers
*/
void ra_clear_containers(roaring_array_t *ra);
/**
* Get the index corresponding to a 16-bit key
*/
static inline int32_t ra_get_index(const roaring_array_t *ra, uint16_t x) {
if ((ra->size == 0) || ra->keys[ra->size - 1] == x) return ra->size - 1;
return binarySearch(ra->keys, (int32_t)ra->size, x);
}
/**
* Retrieves the container at index i, filling in the typecode
*/
static inline void *ra_get_container_at_index(const roaring_array_t *ra, uint16_t i,
uint8_t *typecode) {
*typecode = ra->typecodes[i];
return ra->containers[i];
}
/**
* Retrieves the key at index i
*/
uint16_t ra_get_key_at_index(const roaring_array_t *ra, uint16_t i);
/**
* Add a new key-value pair at index i
*/
void ra_insert_new_key_value_at(roaring_array_t *ra, int32_t i, uint16_t key,
void *container, uint8_t typecode);
/**
* Append a new key-value pair
*/
void ra_append(roaring_array_t *ra, uint16_t s, void *c, uint8_t typecode);
/**
* Append a new key-value pair to ra, cloning (in COW sense) a value from sa
* at index index
*/
void ra_append_copy(roaring_array_t *ra, const roaring_array_t *sa,
uint16_t index, bool copy_on_write);
/**
* Append new key-value pairs to ra, cloning (in COW sense) values from sa
* at indexes
* [start_index, end_index)
*/
void ra_append_copy_range(roaring_array_t *ra, const roaring_array_t *sa,
int32_t start_index, int32_t end_index,
bool copy_on_write);
/** appends from sa to ra, ending with the greatest key that is
* is less or equal stopping_key
*/
void ra_append_copies_until(roaring_array_t *ra, const roaring_array_t *sa,
uint16_t stopping_key, bool copy_on_write);
/** appends from sa to ra, starting with the smallest key that is
* is strictly greater than before_start
*/
void ra_append_copies_after(roaring_array_t *ra, const roaring_array_t *sa,
uint16_t before_start, bool copy_on_write);
/**
* Move the key-value pairs to ra from sa at indexes
* [start_index, end_index), old array should not be freed
* (use ra_clear_without_containers)
**/
void ra_append_move_range(roaring_array_t *ra, roaring_array_t *sa,
int32_t start_index, int32_t end_index);
/**
* Append new key-value pairs to ra, from sa at indexes
* [start_index, end_index)
*/
void ra_append_range(roaring_array_t *ra, roaring_array_t *sa,
int32_t start_index, int32_t end_index,
bool copy_on_write);
/**
* Set the container at the corresponding index using the specified
* typecode.
*/
static inline void ra_set_container_at_index(const roaring_array_t *ra, int32_t i,
void *c, uint8_t typecode) {
assert(i < ra->size);
ra->containers[i] = c;
ra->typecodes[i] = typecode;
}
/**
* If needed, increase the capacity of the array so that it can fit k values
* (at
* least);
*/
bool extend_array(roaring_array_t *ra, int32_t k);
static inline int32_t ra_get_size(const roaring_array_t *ra) { return ra->size; }
static inline int32_t ra_advance_until(const roaring_array_t *ra, uint16_t x,
int32_t pos) {
return advanceUntil(ra->keys, pos, ra->size, x);
}
int32_t ra_advance_until_freeing(roaring_array_t *ra, uint16_t x, int32_t pos);
void ra_downsize(roaring_array_t *ra, int32_t new_length);
static inline void ra_replace_key_and_container_at_index(roaring_array_t *ra,
int32_t i, uint16_t key,
void *c, uint8_t typecode) {
assert(i < ra->size);
ra->keys[i] = key;
ra->containers[i] = c;
ra->typecodes[i] = typecode;
}
// write set bits to an array
void ra_to_uint32_array(const roaring_array_t *ra, uint32_t *ans);
bool ra_range_uint32_array(const roaring_array_t *ra, size_t offset, size_t limit, uint32_t *ans);
/**
* write a bitmap to a buffer. This is meant to be compatible with
* the
* Java and Go versions. Return the size in bytes of the serialized
* output (which should be ra_portable_size_in_bytes(ra)).
*/
size_t ra_portable_serialize(const roaring_array_t *ra, char *buf);
/**
* read a bitmap from a serialized version. This is meant to be compatible
* with the Java and Go versions.
* maxbytes indicates how many bytes available from buf.
* When the function returns true, roaring_array_t is populated with the data
* and *readbytes indicates how many bytes were read. In all cases, if the function
* returns true, then maxbytes >= *readbytes.
*/
bool ra_portable_deserialize(roaring_array_t *ra, const char *buf, const size_t maxbytes, size_t * readbytes);
/**
* Quickly checks whether there is a serialized bitmap at the pointer,
* not exceeding size "maxbytes" in bytes. This function does not allocate
* memory dynamically.
*
* This function returns 0 if and only if no valid bitmap is found.
* Otherwise, it returns how many bytes are occupied by the bitmap data.
*/
size_t ra_portable_deserialize_size(const char *buf, const size_t maxbytes);
/**
* How many bytes are required to serialize this bitmap (meant to be
* compatible
* with Java and Go versions)
*/
size_t ra_portable_size_in_bytes(const roaring_array_t *ra);
/**
* return true if it contains at least one run container.
*/
bool ra_has_run_container(const roaring_array_t *ra);
/**
* Size of the header when serializing (meant to be compatible
* with Java and Go versions)
*/
uint32_t ra_portable_header_size(const roaring_array_t *ra);
/**
* If the container at the index i is share, unshare it (creating a local
* copy if needed).
*/
static inline void ra_unshare_container_at_index(roaring_array_t *ra,
uint16_t i) {
assert(i < ra->size);
ra->containers[i] =
get_writable_copy_if_shared(ra->containers[i], &ra->typecodes[i]);
}
/**
* remove at index i, sliding over all entries after i
*/
void ra_remove_at_index(roaring_array_t *ra, int32_t i);
/**
* clears all containers, sets the size at 0 and shrinks the memory usage.
*/
void ra_reset(roaring_array_t *ra);
/**
* remove at index i, sliding over all entries after i. Free removed container.
*/
void ra_remove_at_index_and_free(roaring_array_t *ra, int32_t i);
/**
* remove a chunk of indices, sliding over entries after it
*/
// void ra_remove_index_range(roaring_array_t *ra, int32_t begin, int32_t end);
// used in inplace andNot only, to slide left the containers from
// the mutated RoaringBitmap that are after the largest container of
// the argument RoaringBitmap. It is followed by a call to resize.
//
void ra_copy_range(roaring_array_t *ra, uint32_t begin, uint32_t end,
uint32_t new_begin);
/**
* Shifts rightmost $count containers to the left (distance < 0) or
* to the right (distance > 0).
* Allocates memory if necessary.
* This function doesn't free or create new containers.
* Caller is responsible for that.
*/
void ra_shift_tail(roaring_array_t *ra, int32_t count, int32_t distance);
#ifdef __cplusplus
}
#endif
#endif
/* end file include/roaring/roaring_array.h */
/* begin file include/roaring/misc/configreport.h */
/*
* configreport.h
*
*/
#ifndef INCLUDE_MISC_CONFIGREPORT_H_
#define INCLUDE_MISC_CONFIGREPORT_H_
#include <stddef.h> // for size_t
#include <stdint.h>
#include <stdio.h>
#ifdef IS_X64
// useful for basic info (0)
static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx) {
#ifdef ROARING_INLINE_ASM
__asm volatile("cpuid"
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx), "=d"(*edx)
: "0"(*eax), "2"(*ecx));
#endif /* not sure what to do when inline assembly is unavailable*/
}
// CPUID instruction takes no parameters as CPUID implicitly uses the EAX
// register.
// The EAX register should be loaded with a value specifying what information to
// return
static inline void cpuinfo(int code, int *eax, int *ebx, int *ecx, int *edx) {
#ifdef ROARING_INLINE_ASM
__asm__ volatile("cpuid;" // call cpuid instruction
: "=a"(*eax), "=b"(*ebx), "=c"(*ecx),
"=d"(*edx) // output equal to "movl %%eax %1"
: "a"(code) // input equal to "movl %1, %%eax"
//:"%eax","%ebx","%ecx","%edx"// clobbered register
);
#endif /* not sure what to do when inline assembly is unavailable*/
}
static inline int computecacheline(void) {
int eax = 0, ebx = 0, ecx = 0, edx = 0;
cpuinfo((int)0x80000006, &eax, &ebx, &ecx, &edx);
return ecx & 0xFF;
}
// this is quite imperfect, but can be handy
static inline const char *guessprocessor(void) {
unsigned eax = 1, ebx = 0, ecx = 0, edx = 0;
native_cpuid(&eax, &ebx, &ecx, &edx);
const char *codename;
switch (eax >> 4) {
case 0x506E:
codename = "Skylake";
break;
case 0x406C:
codename = "CherryTrail";
break;
case 0x306D:
codename = "Broadwell";
break;
case 0x306C:
codename = "Haswell";
break;
case 0x306A:
codename = "IvyBridge";
break;
case 0x206A:
case 0x206D:
codename = "SandyBridge";
break;
case 0x2065:
case 0x206C:
case 0x206F:
codename = "Westmere";
break;
case 0x106E:
case 0x106A:
case 0x206E:
codename = "Nehalem";
break;
case 0x1067:
case 0x106D:
codename = "Penryn";
break;
case 0x006F:
case 0x1066:
codename = "Merom";
break;
case 0x0066:
codename = "Presler";
break;
case 0x0063:
case 0x0064:
codename = "Prescott";
break;
case 0x006D:
codename = "Dothan";
break;
case 0x0366:
codename = "Cedarview";
break;
case 0x0266:
codename = "Lincroft";
break;
case 0x016C:
codename = "Pineview";
break;
default:
codename = "UNKNOWN";
break;
}
return codename;
}
static inline void tellmeall(void) {
printf("Intel processor: %s\t", guessprocessor());
#ifdef __VERSION__
printf(" compiler version: %s\t", __VERSION__);
#endif
printf("\tBuild option USEAVX ");
#ifdef USEAVX
printf("enabled\n");
#else
printf("disabled\n");
#endif
#ifndef __AVX2__
printf("AVX2 is NOT available.\n");
#endif
if ((sizeof(int) != 4) || (sizeof(long) != 8)) {
printf("number of bytes: int = %lu long = %lu \n",
(long unsigned int)sizeof(size_t),
(long unsigned int)sizeof(int));
}
#if defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__
// This is what we expect!
// printf("you have little endian machine");
#endif
#if defined(__BIG_ENDIAN__) && __BIG_ENDIAN__
printf("you have a big endian machine");
#endif
#if __CHAR_BIT__
if (__CHAR_BIT__ != 8) printf("on your machine, chars don't have 8bits???");
#endif
if (computecacheline() != 64)
printf("cache line: %d bytes\n", computecacheline());
}
#else
static inline void tellmeall(void) {
printf("Non-X64 processor\n");
#ifdef __arm__
printf("ARM processor detected\n");
#endif
#ifdef __VERSION__
printf(" compiler version: %s\t", __VERSION__);
#endif
if ((sizeof(int) != 4) || (sizeof(long) != 8)) {
printf("number of bytes: int = %lu long = %lu \n",
(long unsigned int)sizeof(size_t),
(long unsigned int)sizeof(int));
}
#if __LITTLE_ENDIAN__
// This is what we expect!
// printf("you have little endian machine");
#endif
#if __BIG_ENDIAN__
printf("you have a big endian machine");
#endif
#if __CHAR_BIT__
if (__CHAR_BIT__ != 8) printf("on your machine, chars don't have 8bits???");
#endif
}
#endif
#endif /* INCLUDE_MISC_CONFIGREPORT_H_ */
/* end file include/roaring/misc/configreport.h */
/* begin file include/roaring/roaring.h */
/*
An implementation of Roaring Bitmaps in C.
*/
#ifndef ROARING_H
#define ROARING_H
#ifdef __cplusplus
extern "C" {
#endif
#include <stdbool.h>
typedef struct roaring_bitmap_s {
roaring_array_t high_low_container;
} roaring_bitmap_t;
/**
* Creates a new bitmap (initially empty)
*/
roaring_bitmap_t *roaring_bitmap_create(void);
/**
* Add all the values between min (included) and max (excluded) that are at a
* distance k*step from min.
*/
roaring_bitmap_t *roaring_bitmap_from_range(uint64_t min, uint64_t max,
uint32_t step);
/**
* Creates a new bitmap (initially empty) with a provided
* container-storage capacity (it is a performance hint).
*/
roaring_bitmap_t *roaring_bitmap_create_with_capacity(uint32_t cap);
/**
* Creates a new bitmap from a pointer of uint32_t integers
*/
roaring_bitmap_t *roaring_bitmap_of_ptr(size_t n_args, const uint32_t *vals);
/*
* Whether you want to use copy-on-write.
* Saves memory and avoids copies but needs more care in a threaded context.
* Most users should ignore this flag.
* Note: if you do turn this flag to 'true', enabling COW,
* then ensure that you do so for all of your bitmaps since
* interactions between bitmaps with and without COW is unsafe.
*/
static inline bool roaring_bitmap_get_copy_on_write(const roaring_bitmap_t* r) {
return r->high_low_container.flags & ROARING_FLAG_COW;
}
static inline void roaring_bitmap_set_copy_on_write(roaring_bitmap_t* r, bool cow) {
if (cow) {
r->high_low_container.flags |= ROARING_FLAG_COW;
} else {
r->high_low_container.flags &= ~ROARING_FLAG_COW;
}
}
/**
* Describe the inner structure of the bitmap.
*/
void roaring_bitmap_printf_describe(const roaring_bitmap_t *ra);
/**
* Creates a new bitmap from a list of uint32_t integers
*/
roaring_bitmap_t *roaring_bitmap_of(size_t n, ...);
/**
* Copies a bitmap. This does memory allocation. The caller is responsible for
* memory management.
*
*/
roaring_bitmap_t *roaring_bitmap_copy(const roaring_bitmap_t *r);
/**
* Copies a bitmap from src to dest. It is assumed that the pointer dest
* is to an already allocated bitmap. The content of the dest bitmap is
* freed/deleted.
*
* It might be preferable and simpler to call roaring_bitmap_copy except
* that roaring_bitmap_overwrite can save on memory allocations.
*
*/
bool roaring_bitmap_overwrite(roaring_bitmap_t *dest,
const roaring_bitmap_t *src);
/**
* Print the content of the bitmap.
*/
void roaring_bitmap_printf(const roaring_bitmap_t *ra);
/**
* Computes the intersection between two bitmaps and returns new bitmap. The
* caller is
* responsible for memory management.
*
*/
roaring_bitmap_t *roaring_bitmap_and(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Computes the size of the intersection between two bitmaps.
*
*/
uint64_t roaring_bitmap_and_cardinality(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Check whether two bitmaps intersect.
*
*/
bool roaring_bitmap_intersect(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Computes the Jaccard index between two bitmaps. (Also known as the Tanimoto
* distance,
* or the Jaccard similarity coefficient)
*
* The Jaccard index is undefined if both bitmaps are empty.
*
*/
double roaring_bitmap_jaccard_index(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Computes the size of the union between two bitmaps.
*
*/
uint64_t roaring_bitmap_or_cardinality(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Computes the size of the difference (andnot) between two bitmaps.
*
*/
uint64_t roaring_bitmap_andnot_cardinality(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Computes the size of the symmetric difference (andnot) between two bitmaps.
*
*/
uint64_t roaring_bitmap_xor_cardinality(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Inplace version modifies x1, x1 == x2 is allowed
*/
void roaring_bitmap_and_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Computes the union between two bitmaps and returns new bitmap. The caller is
* responsible for memory management.
*/
roaring_bitmap_t *roaring_bitmap_or(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Inplace version of roaring_bitmap_or, modifies x1. TDOO: decide whether x1 ==
*x2 ok
*
*/
void roaring_bitmap_or_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Compute the union of 'number' bitmaps. See also roaring_bitmap_or_many_heap.
* Caller is responsible for freeing the
* result.
*
*/
roaring_bitmap_t *roaring_bitmap_or_many(size_t number,
const roaring_bitmap_t **x);
/**
* Compute the union of 'number' bitmaps using a heap. This can
* sometimes be faster than roaring_bitmap_or_many which uses
* a naive algorithm. Caller is responsible for freeing the
* result.
*
*/
roaring_bitmap_t *roaring_bitmap_or_many_heap(uint32_t number,
const roaring_bitmap_t **x);
/**
* Computes the symmetric difference (xor) between two bitmaps
* and returns new bitmap. The caller is responsible for memory management.
*/
roaring_bitmap_t *roaring_bitmap_xor(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Inplace version of roaring_bitmap_xor, modifies x1. x1 != x2.
*
*/
void roaring_bitmap_xor_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Compute the xor of 'number' bitmaps.
* Caller is responsible for freeing the
* result.
*
*/
roaring_bitmap_t *roaring_bitmap_xor_many(size_t number,
const roaring_bitmap_t **x);
/**
* Computes the difference (andnot) between two bitmaps
* and returns new bitmap. The caller is responsible for memory management.
*/
roaring_bitmap_t *roaring_bitmap_andnot(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* Inplace version of roaring_bitmap_andnot, modifies x1. x1 != x2.
*
*/
void roaring_bitmap_andnot_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* TODO: consider implementing:
* Compute the xor of 'number' bitmaps using a heap. This can
* sometimes be faster than roaring_bitmap_xor_many which uses
* a naive algorithm. Caller is responsible for freeing the
* result.
*
* roaring_bitmap_t *roaring_bitmap_xor_many_heap(uint32_t number,
* const roaring_bitmap_t **x);
*/
/**
* Frees the memory.
*/
void roaring_bitmap_free(const roaring_bitmap_t *r);
/**
* Add value n_args from pointer vals, faster than repeatedly calling
* roaring_bitmap_add
*
*/
void roaring_bitmap_add_many(roaring_bitmap_t *r, size_t n_args,
const uint32_t *vals);
/**
* Add value x
*
*/
void roaring_bitmap_add(roaring_bitmap_t *r, uint32_t x);
/**
* Add value x
* Returns true if a new value was added, false if the value was already existing.
*/
bool roaring_bitmap_add_checked(roaring_bitmap_t *r, uint32_t x);
/**
* Add all values in range [min, max]
*/
void roaring_bitmap_add_range_closed(roaring_bitmap_t *ra, uint32_t min, uint32_t max);
/**
* Add all values in range [min, max)
*/
static inline void roaring_bitmap_add_range(roaring_bitmap_t *ra, uint64_t min, uint64_t max) {
if(max == min) return;
roaring_bitmap_add_range_closed(ra, (uint32_t)min, (uint32_t)(max - 1));
}
/**
* Remove value x
*
*/
void roaring_bitmap_remove(roaring_bitmap_t *r, uint32_t x);
/** Remove all values in range [min, max] */
void roaring_bitmap_remove_range_closed(roaring_bitmap_t *ra, uint32_t min, uint32_t max);
/** Remove all values in range [min, max) */
static inline void roaring_bitmap_remove_range(roaring_bitmap_t *ra, uint64_t min, uint64_t max) {
if(max == min) return;
roaring_bitmap_remove_range_closed(ra, (uint32_t)min, (uint32_t)(max - 1));
}
/** Remove multiple values */
void roaring_bitmap_remove_many(roaring_bitmap_t *r, size_t n_args,
const uint32_t *vals);
/**
* Remove value x
* Returns true if a new value was removed, false if the value was not existing.
*/
bool roaring_bitmap_remove_checked(roaring_bitmap_t *r, uint32_t x);
/**
* Check if value x is present
*/
static inline bool roaring_bitmap_contains(const roaring_bitmap_t *r, uint32_t val) {
const uint16_t hb = val >> 16;
/*
* the next function call involves a binary search and lots of branching.
*/
int32_t i = ra_get_index(&r->high_low_container, hb);
if (i < 0) return false;
uint8_t typecode;
// next call ought to be cheap
void *container =
ra_get_container_at_index(&r->high_low_container, i, &typecode);
// rest might be a tad expensive, possibly involving another round of binary search
return container_contains(container, val & 0xFFFF, typecode);
}
/**
* Check whether a range of values from range_start (included) to range_end (excluded) is present
*/
bool roaring_bitmap_contains_range(const roaring_bitmap_t *r, uint64_t range_start, uint64_t range_end);
/**
* Get the cardinality of the bitmap (number of elements).
*/
uint64_t roaring_bitmap_get_cardinality(const roaring_bitmap_t *ra);
/**
* Returns the number of elements in the range [range_start, range_end).
*/
uint64_t roaring_bitmap_range_cardinality(const roaring_bitmap_t *ra,
uint64_t range_start, uint64_t range_end);
/**
* Returns true if the bitmap is empty (cardinality is zero).
*/
bool roaring_bitmap_is_empty(const roaring_bitmap_t *ra);
/**
* Empties the bitmap
*/
void roaring_bitmap_clear(roaring_bitmap_t *ra);
/**
* Convert the bitmap to an array. Write the output to "ans",
* caller is responsible to ensure that there is enough memory
* allocated
* (e.g., ans = malloc(roaring_bitmap_get_cardinality(mybitmap)
* * sizeof(uint32_t))
*/
void roaring_bitmap_to_uint32_array(const roaring_bitmap_t *ra, uint32_t *ans);
/**
* Convert the bitmap to an array from "offset" by "limit". Write the output to "ans".
* so, you can get data in paging.
* caller is responsible to ensure that there is enough memory
* allocated
* (e.g., ans = malloc(roaring_bitmap_get_cardinality(limit)
* * sizeof(uint32_t))
* Return false in case of failure (e.g., insufficient memory)
*/
bool roaring_bitmap_range_uint32_array(const roaring_bitmap_t *ra, size_t offset, size_t limit, uint32_t *ans);
/**
* Remove run-length encoding even when it is more space efficient
* return whether a change was applied
*/
bool roaring_bitmap_remove_run_compression(roaring_bitmap_t *r);
/** convert array and bitmap containers to run containers when it is more
* efficient;
* also convert from run containers when more space efficient. Returns
* true if the result has at least one run container.
* Additional savings might be possible by calling shrinkToFit().
*/
bool roaring_bitmap_run_optimize(roaring_bitmap_t *r);
/**
* If needed, reallocate memory to shrink the memory usage. Returns
* the number of bytes saved.
*/
size_t roaring_bitmap_shrink_to_fit(roaring_bitmap_t *r);
/**
* write the bitmap to an output pointer, this output buffer should refer to
* at least roaring_bitmap_size_in_bytes(ra) allocated bytes.
*
* see roaring_bitmap_portable_serialize if you want a format that's compatible
* with Java and Go implementations
*
* this format has the benefit of being sometimes more space efficient than
* roaring_bitmap_portable_serialize
* e.g., when the data is sparse.
*
* Returns how many bytes were written which should be
* roaring_bitmap_size_in_bytes(ra).
*/
size_t roaring_bitmap_serialize(const roaring_bitmap_t *ra, char *buf);
/** use with roaring_bitmap_serialize
* see roaring_bitmap_portable_deserialize if you want a format that's
* compatible with Java and Go implementations
*/
roaring_bitmap_t *roaring_bitmap_deserialize(const void *buf);
/**
* How many bytes are required to serialize this bitmap (NOT compatible
* with Java and Go versions)
*/
size_t roaring_bitmap_size_in_bytes(const roaring_bitmap_t *ra);
/**
* read a bitmap from a serialized version. This is meant to be compatible with
* the Java and Go versions. See format specification at
* https://github.com/RoaringBitmap/RoaringFormatSpec
* In case of failure, a null pointer is returned.
* This function is unsafe in the sense that if there is no valid serialized
* bitmap at the pointer, then many bytes could be read, possibly causing a buffer
* overflow. For a safer approach,
* call roaring_bitmap_portable_deserialize_safe.
*/
roaring_bitmap_t *roaring_bitmap_portable_deserialize(const char *buf);
/**
* read a bitmap from a serialized version in a safe manner (reading up to maxbytes).
* This is meant to be compatible with
* the Java and Go versions. See format specification at
* https://github.com/RoaringBitmap/RoaringFormatSpec
* In case of failure, a null pointer is returned.
*/
roaring_bitmap_t *roaring_bitmap_portable_deserialize_safe(const char *buf, size_t maxbytes);
/**
* Check how many bytes would be read (up to maxbytes) at this pointer if there
* is a bitmap, returns zero if there is no valid bitmap.
* This is meant to be compatible with
* the Java and Go versions. See format specification at
* https://github.com/RoaringBitmap/RoaringFormatSpec
*/
size_t roaring_bitmap_portable_deserialize_size(const char *buf, size_t maxbytes);
/**
* How many bytes are required to serialize this bitmap (meant to be compatible
* with Java and Go versions). See format specification at
* https://github.com/RoaringBitmap/RoaringFormatSpec
*/
size_t roaring_bitmap_portable_size_in_bytes(const roaring_bitmap_t *ra);
/**
* write a bitmap to a char buffer. The output buffer should refer to at least
* roaring_bitmap_portable_size_in_bytes(ra) bytes of allocated memory.
* This is meant to be compatible with
* the
* Java and Go versions. Returns how many bytes were written which should be
* roaring_bitmap_portable_size_in_bytes(ra). See format specification at
* https://github.com/RoaringBitmap/RoaringFormatSpec
*/
size_t roaring_bitmap_portable_serialize(const roaring_bitmap_t *ra, char *buf);
/*
* "Frozen" serialization format imitates memory layout of roaring_bitmap_t.
* Deserialized bitmap is a constant view of the underlying buffer.
* This significantly reduces amount of allocations and copying required during
* deserialization.
* It can be used with memory mapped files.
* Example can be found in benchmarks/frozen_benchmark.c
*
* [#####] const roaring_bitmap_t *
* | | |
* +----+ | +-+
* | | |
* [#####################################] underlying buffer
*
* Note that because frozen serialization format imitates C memory layout
* of roaring_bitmap_t, it is not fixed. It is different on big/little endian
* platforms and can be changed in future.
*/
/**
* Returns number of bytes required to serialize bitmap using frozen format.
*/
size_t roaring_bitmap_frozen_size_in_bytes(const roaring_bitmap_t *ra);
/**
* Serializes bitmap using frozen format.
* Buffer size must be at least roaring_bitmap_frozen_size_in_bytes().
*/
void roaring_bitmap_frozen_serialize(const roaring_bitmap_t *ra, char *buf);
/**
* Creates constant bitmap that is a view of a given buffer.
* Buffer must contain data previously written by roaring_bitmap_frozen_serialize(),
* and additionally its beginning must be aligned by 32 bytes.
* Length must be equal exactly to roaring_bitmap_frozen_size_in_bytes().
*
* On error, NULL is returned.
*
* Bitmap returned by this function can be used in all readonly contexts.
* Bitmap must be freed as usual, by calling roaring_bitmap_free().
* Underlying buffer must not be freed or modified while it backs any bitmaps.
*/
const roaring_bitmap_t *roaring_bitmap_frozen_view(const char *buf, size_t length);
/**
* Iterate over the bitmap elements. The function iterator is called once for
* all the values with ptr (can be NULL) as the second parameter of each call.
*
* roaring_iterator is simply a pointer to a function that returns bool
* (true means that the iteration should continue while false means that it
* should stop),
* and takes (uint32_t,void*) as inputs.
*
* Returns true if the roaring_iterator returned true throughout (so that
* all data points were necessarily visited).
*/
bool roaring_iterate(const roaring_bitmap_t *ra, roaring_iterator iterator,
void *ptr);
bool roaring_iterate64(const roaring_bitmap_t *ra, roaring_iterator64 iterator,
uint64_t high_bits, void *ptr);
/**
* Return true if the two bitmaps contain the same elements.
*/
bool roaring_bitmap_equals(const roaring_bitmap_t *ra1,
const roaring_bitmap_t *ra2);
/**
* Return true if all the elements of ra1 are also in ra2.
*/
bool roaring_bitmap_is_subset(const roaring_bitmap_t *ra1,
const roaring_bitmap_t *ra2);
/**
* Return true if all the elements of ra1 are also in ra2 and ra2 is strictly
* greater
* than ra1.
*/
bool roaring_bitmap_is_strict_subset(const roaring_bitmap_t *ra1,
const roaring_bitmap_t *ra2);
/**
* (For expert users who seek high performance.)
*
* Computes the union between two bitmaps and returns new bitmap. The caller is
* responsible for memory management.
*
* The lazy version defers some computations such as the maintenance of the
* cardinality counts. Thus you need
* to call roaring_bitmap_repair_after_lazy after executing "lazy" computations.
* It is safe to repeatedly call roaring_bitmap_lazy_or_inplace on the result.
* The bitsetconversion conversion is a flag which determines
* whether container-container operations force a bitset conversion.
**/
roaring_bitmap_t *roaring_bitmap_lazy_or(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2,
const bool bitsetconversion);
/**
* (For expert users who seek high performance.)
* Inplace version of roaring_bitmap_lazy_or, modifies x1
* The bitsetconversion conversion is a flag which determines
* whether container-container operations force a bitset conversion.
*/
void roaring_bitmap_lazy_or_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2,
const bool bitsetconversion);
/**
* (For expert users who seek high performance.)
*
* Execute maintenance operations on a bitmap created from
* roaring_bitmap_lazy_or
* or modified with roaring_bitmap_lazy_or_inplace.
*/
void roaring_bitmap_repair_after_lazy(roaring_bitmap_t *x1);
/**
* Computes the symmetric difference between two bitmaps and returns new bitmap.
*The caller is
* responsible for memory management.
*
* The lazy version defers some computations such as the maintenance of the
* cardinality counts. Thus you need
* to call roaring_bitmap_repair_after_lazy after executing "lazy" computations.
* It is safe to repeatedly call roaring_bitmap_lazy_xor_inplace on the result.
*
*/
roaring_bitmap_t *roaring_bitmap_lazy_xor(const roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* (For expert users who seek high performance.)
* Inplace version of roaring_bitmap_lazy_xor, modifies x1. x1 != x2
*
*/
void roaring_bitmap_lazy_xor_inplace(roaring_bitmap_t *x1,
const roaring_bitmap_t *x2);
/**
* compute the negation of the roaring bitmap within a specified
* interval: [range_start, range_end). The number of negated values is
* range_end - range_start.
* Areas outside the range are passed through unchanged.
*/
roaring_bitmap_t *roaring_bitmap_flip(const roaring_bitmap_t *x1,
uint64_t range_start, uint64_t range_end);
/**
* compute (in place) the negation of the roaring bitmap within a specified
* interval: [range_start, range_end). The number of negated values is
* range_end - range_start.
* Areas outside the range are passed through unchanged.
*/
void roaring_bitmap_flip_inplace(roaring_bitmap_t *x1, uint64_t range_start,
uint64_t range_end);
/**
* Selects the element at index 'rank' where the smallest element is at index 0.
* If the size of the roaring bitmap is strictly greater than rank, then this
function returns true and sets element to the element of given rank.
Otherwise, it returns false.
*/
bool roaring_bitmap_select(const roaring_bitmap_t *ra, uint32_t rank,
uint32_t *element);
/**
* roaring_bitmap_rank returns the number of integers that are smaller or equal
* to x. Thus if x is the first element, this function will return 1. If
* x is smaller than the smallest element, this function will return 0.
*
* The indexing convention differs between roaring_bitmap_select and
* roaring_bitmap_rank: roaring_bitmap_select refers to the smallest value
* as having index 0, whereas roaring_bitmap_rank returns 1 when ranking
* the smallest value.
*/
uint64_t roaring_bitmap_rank(const roaring_bitmap_t *bm, uint32_t x);
/**
* roaring_bitmap_smallest returns the smallest value in the set.
* Returns UINT32_MAX if the set is empty.
*/
uint32_t roaring_bitmap_minimum(const roaring_bitmap_t *bm);
/**
* roaring_bitmap_smallest returns the greatest value in the set.
* Returns 0 if the set is empty.
*/
uint32_t roaring_bitmap_maximum(const roaring_bitmap_t *bm);
/**
* (For advanced users.)
* Collect statistics about the bitmap, see roaring_types.h for
* a description of roaring_statistics_t
*/
void roaring_bitmap_statistics(const roaring_bitmap_t *ra,
roaring_statistics_t *stat);
/*********************
* What follows is code use to iterate through values in a roaring bitmap
roaring_bitmap_t *ra =...
roaring_uint32_iterator_t i;
roaring_create_iterator(ra, &i);
while(i.has_value) {
printf("value = %d\n", i.current_value);
roaring_advance_uint32_iterator(&i);
}
Obviously, if you modify the underlying bitmap, the iterator
becomes invalid. So don't.
*/
typedef struct roaring_uint32_iterator_s {
const roaring_bitmap_t *parent; // owner
int32_t container_index; // point to the current container index
int32_t in_container_index; // for bitset and array container, this is out
// index
int32_t run_index; // for run container, this points at the run
uint32_t current_value;
bool has_value;
const void
*container; // should be:
// parent->high_low_container.containers[container_index];
uint8_t typecode; // should be:
// parent->high_low_container.typecodes[container_index];
uint32_t highbits; // should be:
// parent->high_low_container.keys[container_index]) <<
// 16;
} roaring_uint32_iterator_t;
/**
* Initialize an iterator object that can be used to iterate through the
* values. If there is a value, then this iterator points to the first value
* and it->has_value is true. The value is in it->current_value.
*/
void roaring_init_iterator(const roaring_bitmap_t *ra,
roaring_uint32_iterator_t *newit);
/**
* Initialize an iterator object that can be used to iterate through the
* values. If there is a value, then this iterator points to the last value
* and it->has_value is true. The value is in it->current_value.
*/
void roaring_init_iterator_last(const roaring_bitmap_t *ra,
roaring_uint32_iterator_t *newit);
/**
* Create an iterator object that can be used to iterate through the
* values. Caller is responsible for calling roaring_free_iterator.
* The iterator is initialized. If there is a value, then this iterator
* points to the first value and it->has_value is true.
* The value is in it->current_value.
*
* This function calls roaring_init_iterator.
*/
roaring_uint32_iterator_t *roaring_create_iterator(const roaring_bitmap_t *ra);
/**
* Advance the iterator. If there is a new value, then it->has_value is true.
* The new value is in it->current_value. Values are traversed in increasing
* orders. For convenience, returns it->has_value.
*/
bool roaring_advance_uint32_iterator(roaring_uint32_iterator_t *it);
/**
* Decrement the iterator. If there is a new value, then it->has_value is true.
* The new value is in it->current_value. Values are traversed in decreasing
* orders. For convenience, returns it->has_value.
*/
bool roaring_previous_uint32_iterator(roaring_uint32_iterator_t *it);
/**
* Move the iterator to the first value >= val. If there is a such a value, then it->has_value is true.
* The new value is in it->current_value. For convenience, returns it->has_value.
*/
bool roaring_move_uint32_iterator_equalorlarger(roaring_uint32_iterator_t *it, uint32_t val) ;
/**
* Creates a copy of an iterator.
* Caller must free it.
*/
roaring_uint32_iterator_t *roaring_copy_uint32_iterator(
const roaring_uint32_iterator_t *it);
/**
* Free memory following roaring_create_iterator
*/
void roaring_free_uint32_iterator(roaring_uint32_iterator_t *it);
/*
* Reads next ${count} values from iterator into user-supplied ${buf}.
* Returns the number of read elements.
* This number can be smaller than ${count}, which means that iterator is drained.
*
* This function satisfies semantics of iteration and can be used together with
* other iterator functions.
* - first value is copied from ${it}->current_value
* - after function returns, iterator is positioned at the next element
*/
uint32_t roaring_read_uint32_iterator(roaring_uint32_iterator_t *it, uint32_t* buf, uint32_t count);
#ifdef __cplusplus
}
#endif
#endif
/* end file include/roaring/roaring.h */
|