{"id":15037145,"url":"https://github.com/roaringbitmap/croaring","last_synced_at":"2025-05-15T06:08:26.362Z","repository":{"id":37548974,"uuid":"47219360","full_name":"RoaringBitmap/CRoaring","owner":"RoaringBitmap","description":"Roaring bitmaps in C (and C++), with SIMD (AVX2, AVX-512 and NEON) optimizations: used by Apache Doris, ClickHouse, and StarRocks","archived":false,"fork":false,"pushed_at":"2025-04-08T14:50:31.000Z","size":52388,"stargazers_count":1638,"open_issues_count":55,"forks_count":279,"subscribers_count":51,"default_branch":"master","last_synced_at":"2025-05-15T06:07:59.894Z","etag":null,"topics":["arm","avx-512","avx2","bitset","bitset-library","c","clang","gcc","neon","roaring-bitmaps","visual-studio"],"latest_commit_sha":null,"homepage":"http://roaringbitmap.org/","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RoaringBitmap.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":"AUTHORS","dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2015-12-01T21:45:40.000Z","updated_at":"2025-05-11T22:03:36.000Z","dependencies_parsed_at":"2025-04-11T02:51:39.379Z","dependency_job_id":null,"html_url":"https://github.com/RoaringBitmap/CRoaring","commit_stats":{"total_commits":1344,"total_committers":89,"mean_commits":"15.101123595505618","dds":0.4263392857142857,"last_synced_commit":"857c3208b49ea2b998caee2dcdb39eb2f66d5159"},"previous_names":[],"tags_count":131,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoaringBitmap%2FCRoaring","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoaringBitmap%2FCRoaring/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoaringBitmap%2FCRoaring/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RoaringBitmap%2FCRoaring/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RoaringBitmap","download_url":"https://codeload.github.com/RoaringBitmap/CRoaring/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254283350,"owners_count":22045141,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arm","avx-512","avx2","bitset","bitset-library","c","clang","gcc","neon","roaring-bitmaps","visual-studio"],"created_at":"2024-09-24T20:33:36.290Z","updated_at":"2025-05-15T06:08:21.346Z","avatar_url":"https://github.com/RoaringBitmap.png","language":"C","readme":"# CRoaring\n\n[![Ubuntu-CI](https://github.com/RoaringBitmap/CRoaring/actions/workflows/ubuntu-noexcept-ci.yml/badge.svg)](https://github.com/RoaringBitmap/CRoaring/actions/workflows/ubuntu-noexcept-ci.yml) [![VS17-CI](https://github.com/RoaringBitmap/CRoaring/actions/workflows/vs17-ci.yml/badge.svg)](https://github.com/RoaringBitmap/CRoaring/actions/workflows/vs17-ci.yml)\n[![Fuzzing Status](https://oss-fuzz-build-logs.storage.googleapis.com/badges/croaring.svg)](https://bugs.chromium.org/p/oss-fuzz/issues/list?sort=-opened\u0026can=1\u0026q=proj:croaring)\n\n[![Doxygen Documentation](https://img.shields.io/badge/docs-doxygen-green.svg)](http://roaringbitmap.github.io/CRoaring/)\n\n\n\nPortable Roaring bitmaps in C (and C++) with full support for your favorite compiler (GNU GCC, LLVM's clang, Visual Studio, Apple Xcode, Intel oneAPI). Included in the [Awesome C](https://github.com/kozross/awesome-c) list of open source C software.\n\n# Introduction\n\nBitsets, also called bitmaps, are commonly used as fast data structures. Unfortunately, they can use too much memory.\n To compensate, we often use compressed bitmaps.\n\nRoaring bitmaps are compressed bitmaps which tend to outperform conventional compressed bitmaps such as WAH, EWAH or Concise.\nThey are used by several major systems such as [Apache Lucene][lucene] and derivative systems such as [Solr][solr] and\n[Elasticsearch][elasticsearch], [Metamarkets' Druid][druid], [LinkedIn Pinot][pinot], [Netflix Atlas][atlas], [Apache Spark][spark], [OpenSearchServer][opensearchserver], [Cloud Torrent][cloudtorrent], [Whoosh][whoosh], [InfluxDB](https://www.influxdata.com), [Pilosa][pilosa], [Bleve](http://www.blevesearch.com), [Microsoft Visual Studio Team Services (VSTS)][vsts], and eBay's [Apache Kylin][kylin]. The CRoaring library is used in several systems such as [Apache Doris](http://doris.incubator.apache.org), [ClickHouse](https://github.com/ClickHouse/ClickHouse), [Redpanda](https://github.com/redpanda-data/redpanda), and [StarRocks](https://github.com/StarRocks/starrocks). The YouTube SQL Engine, [Google Procella](https://research.google/pubs/pub48388/), uses Roaring bitmaps for indexing.\n\nWe published a peer-reviewed article on the design and evaluation of this library:\n\n- Roaring Bitmaps: Implementation of an Optimized Software Library, Software: Practice and Experience 48 (4), 2018 [arXiv:1709.07821](https://arxiv.org/abs/1709.07821)\n\n[lucene]: https://lucene.apache.org/\n[solr]: https://lucene.apache.org/solr/\n[elasticsearch]: https://www.elastic.co/products/elasticsearch\n[druid]: http://druid.io/\n[spark]: https://spark.apache.org/\n[opensearchserver]: http://www.opensearchserver.com\n[cloudtorrent]: https://github.com/jpillora/cloud-torrent\n[whoosh]: https://bitbucket.org/mchaput/whoosh/wiki/Home\n[pilosa]: https://www.pilosa.com/\n[kylin]: http://kylin.apache.org/\n[pinot]: http://github.com/linkedin/pinot/wiki\n[vsts]: https://www.visualstudio.com/team-services/\n[atlas]: https://github.com/Netflix/atlas\n\nRoaring bitmaps are found to work well in many important applications:\n\n\u003e Use Roaring for bitmap compression whenever possible. Do not use other bitmap compression methods ([Wang et al., SIGMOD 2017](http://db.ucsd.edu/wp-content/uploads/2017/03/sidm338-wangA.pdf))\n\n\n[There is a serialized format specification for interoperability between implementations](https://github.com/RoaringBitmap/RoaringFormatSpec/). Hence, it is possible to serialize a Roaring Bitmap from C++, read it in Java, modify it, serialize it back and read it in Go and Python.\n\n# Objective\n\nThe primary goal of the CRoaring is to provide a high performance low-level implementation that fully take advantage\nof the latest hardware. Roaring bitmaps are already available on a variety of platform through Java, Go, Rust... implementations. CRoaring is a library that seeks to achieve superior performance by staying close to the latest hardware.\n\n\n(c) 2016-... The CRoaring authors.\n\n\n\n# Requirements\n\n- Linux, macOS, FreeBSD, Windows (MSYS2 and Microsoft Visual studio).\n- We test the library with ARM, x64/x86 and POWER processors. We only support little endian systems (big endian systems are vanishingly rare).\n- Recent C compiler supporting the C11 standard (GCC 7 or better, LLVM 8 or better (clang), Xcode 11 or better, Microsoft Visual Studio 2022 or better, Intel oneAPI Compiler 2023.2 or better), there is also an optional C++ class that requires a C++ compiler supporting the C++11 standard.\n- CMake (to contribute to the project, users can rely on amalgamation/unity builds if they do not wish to use CMake).\n- The CMake system assumes that git is available.\n- Under x64 systems, the library provides runtime dispatch so that optimized functions are called based on the detected CPU features. It works with GCC, clang (version 9 and up) and Visual Studio (2017 and up). Other systems (e.g., ARM) do not need runtime dispatch.\n\nHardly anyone has access to an actual big-endian system. Nevertheless,\nWe support big-endian systems such as IBM s390x through emulators---except for\nIO serialization which is only supported on little-endian systems (see [issue 423](https://github.com/RoaringBitmap/CRoaring/issues/423)).\n\n\n# Quick Start\n\nThe CRoaring library can be amalgamated into a single source file that makes it easier\nfor integration into other projects. Moreover, by making it possible to compile\nall the critical code into one compilation unit, it can improve the performance. For\nthe rationale, please see the [SQLite documentation](https://www.sqlite.org/amalgamation.html),\nor the corresponding [Wikipedia entry](https://en.wikipedia.org/wiki/Single_Compilation_Unit).\nUsers who choose this route, do not need to rely on CRoaring's build system (based on CMake).\n\nWe offer amalgamated files as part of each release.\n\nLinux or macOS users might follow the following instructions if they have a recent C or C++ compiler installed and a standard utility (`wget`).\n\n\n 1. Pull the library in a directory\n    ```\n    wget https://github.com/RoaringBitmap/CRoaring/releases/download/v2.1.0/roaring.c\n    wget https://github.com/RoaringBitmap/CRoaring/releases/download/v2.1.0/roaring.h\n    wget https://github.com/RoaringBitmap/CRoaring/releases/download/v2.1.0/roaring.hh\n    ```\n 2. Create a new file named `demo.c` with this content:\n    ```C\n    #include \u003cstdio.h\u003e\n    #include \u003cstdlib.h\u003e\n    #include \"roaring.c\"\n    int main() {\n        roaring_bitmap_t *r1 = roaring_bitmap_create();\n        for (uint32_t i = 100; i \u003c 1000; i++) roaring_bitmap_add(r1, i);\n        printf(\"cardinality = %d\\n\", (int) roaring_bitmap_get_cardinality(r1));\n        roaring_bitmap_free(r1);\n\n        bitset_t *b = bitset_create();\n        for (int k = 0; k \u003c 1000; ++k) {\n                bitset_set(b, 3 * k);\n        }\n        printf(\"%zu \\n\", bitset_count(b));\n        bitset_free(b);\n        return EXIT_SUCCESS;\n    }\n    ```\n 2. Create a new file named `demo.cpp` with this content:\n    ```C++\n    #include \u003ciostream\u003e\n    #include \"roaring.hh\" // the amalgamated roaring.hh includes roaring64map.hh\n    #include \"roaring.c\"\n    int main() {\n        roaring::Roaring r1;\n        for (uint32_t i = 100; i \u003c 1000; i++) {\n            r1.add(i);\n        }\n        std::cout \u003c\u003c \"cardinality = \" \u003c\u003c r1.cardinality() \u003c\u003c std::endl;\n\n        roaring::Roaring64Map r2;\n        for (uint64_t i = 18000000000000000100ull; i \u003c 18000000000000001000ull; i++) {\n            r2.add(i);\n        }\n        std::cout \u003c\u003c \"cardinality = \" \u003c\u003c r2.cardinality() \u003c\u003c std::endl;\n        return 0;\n    }\n    ```\n 2. Compile\n    ```\n    cc -o demo demo.c\n    c++ -std=c++11 -o demopp demo.cpp\n    ```\n 3. `./demo`\n    ```\n    cardinality = 900\n    1000\n    ```\n 4. `./demopp`\n    ```\n    cardinality = 900\n    cardinality = 900\n    ```\n\n\n# Using Roaring as a CPM dependency\n\n\nIf you like CMake and CPM, you can add just a few lines in your `CMakeLists.txt` file to grab a `CRoaring` release. [See our CPM demonstration for further details](https://github.com/RoaringBitmap/CPMdemo).\n\n\n\n```CMake\ncmake_minimum_required(VERSION 3.10)\nproject(roaring_demo\n  LANGUAGES CXX C\n)\nset(CMAKE_CXX_STANDARD 17)\nset(CMAKE_C_STANDARD 11)\n\nadd_executable(hello hello.cpp)\n# You can add CPM.cmake like so:\n# mkdir -p cmake\n# wget -O cmake/CPM.cmake https://github.com/cpm-cmake/CPM.cmake/releases/latest/download/get_cpm.cmake\ninclude(cmake/CPM.cmake)\nCPMAddPackage(\n  NAME roaring\n  GITHUB_REPOSITORY \"RoaringBitmap/CRoaring\"\n  GIT_TAG v2.0.4\n  OPTIONS \"BUILD_TESTING OFF\"\n)\n\ntarget_link_libraries(hello roaring::roaring)\n```\n\n\n# Using as a CMake dependency with FetchContent\n\nIf you like CMake, you can add just a few lines in your `CMakeLists.txt` file to grab a `CRoaring` release. [See our demonstration for further details](https://github.com/RoaringBitmap/croaring_cmake_demo_single_file).\n\nIf you installed the CRoaring library locally, you may use it with CMake's `find_package` function as in this example:\n\n```CMake\ncmake_minimum_required(VERSION 3.15)\n\nproject(test_roaring_install VERSION 0.1.0 LANGUAGES CXX C)\n\nset(CMAKE_CXX_STANDARD 11)\nset(CMAKE_CXX_STANDARD_REQUIRED ON)\n\n\nset(CMAKE_C_STANDARD 11)\nset(CMAKE_C_STANDARD_REQUIRED ON)\n\nfind_package(roaring REQUIRED)\n\nfile(WRITE main.cpp \"\n#include \u003ciostream\u003e\n#include \\\"roaring/roaring.hh\\\"\nint main() {\n  roaring::Roaring r1;\n  for (uint32_t i = 100; i \u003c 1000; i++) {\n    r1.add(i);\n  }\n  std::cout \u003c\u003c \\\"cardinality = \\\" \u003c\u003c r1.cardinality() \u003c\u003c std::endl;\n  return 0;\n}\")\n\nadd_executable(repro main.cpp)\ntarget_link_libraries(repro PUBLIC roaring::roaring)\n```\n\n\n# Amalgamating\n\nTo generate the amalgamated files yourself, you can invoke a bash script...\n\n```bash\n./amalgamation.sh\n```\n\nIf you prefer a silent output, you can use the following command to redirect ``stdout`` :\n\n```bash\n./amalgamation.sh \u003e /dev/null\n```\n\n\n(Bash shells are standard under Linux and macOS. Bash shells are available under Windows as part of the  [GitHub Desktop](https://desktop.github.com/) under the name ``Git Shell``. So if you have cloned the ``CRoaring`` GitHub repository from within the GitHub Desktop, you can right-click on ``CRoaring``, select ``Git Shell`` and then enter the above commands.)\n\nIt is not necessary to invoke the script in the CRoaring directory. You can invoke\nit from any directory where you want the amalgamation files to be written.\n\nIt will generate three files for C users: ``roaring.h``, ``roaring.c`` and ``amalgamation_demo.c``... as well as some brief instructions. The ``amalgamation_demo.c`` file is a short example, whereas ``roaring.h`` and ``roaring.c`` are \"amalgamated\" files (including all source and header files for the project). This means that you can simply copy the files ``roaring.h`` and ``roaring.c`` into your project and be ready to go! No need to produce a library! See the ``amalgamation_demo.c`` file.\n\n# API\n\nThe C interface is found in the files\n\n- [roaring.h](https://github.com/RoaringBitmap/CRoaring/blob/master/include/roaring/roaring.h),\n- [roaring64.h](https://github.com/RoaringBitmap/CRoaring/blob/master/include/roaring/roaring64.h).\n\nWe also have a C++ interface:\n\n- [roaring.hh](https://github.com/RoaringBitmap/CRoaring/blob/master/cpp/roaring.hh),\n- [roaring64map.hh](https://github.com/RoaringBitmap/CRoaring/blob/master/cpp/roaring64map.hh).\n\n\n# Dealing with large volumes\n\nSome users have to deal with large volumes of data. It  may be important for these users to be aware of the `addMany` (C++) `roaring_bitmap_or_many` (C) functions as it is much faster and economical to add values in batches when possible. Furthermore, calling periodically the `runOptimize` (C++) or `roaring_bitmap_run_optimize` (C) functions may help.\n\n\n# Running microbenchmarks\n\nWe have microbenchmarks constructed with the Google Benchmarks.\nUnder Linux or macOS, you may run them as follows:\n\n```\ncmake -B build -D ENABLE_ROARING_MICROBENCHMARKS=ON\ncmake --build build\n./build/microbenchmarks/bench\n```\n\nBy default, the benchmark tools picks one data set (e.g., `CRoaring/benchmarks/realdata/census1881`).\nWe have several data sets and you may pick others:\n\n```\n./build/microbenchmarks/bench benchmarks/realdata/wikileaks-noquotes\n```\n\nYou may disable some functionality for the purpose of benchmarking. For example, assuming you\nhave an x64 processor, you could benchmark the code without AVX-512 even if both your processor\nand compiler supports it:\n\n```\ncmake -B buildnoavx512 -D ROARING_DISABLE_AVX512=ON -D ENABLE_ROARING_MICROBENCHMARKS=ON\ncmake --build buildnoavx512\n./buildnoavx512/microbenchmarks/bench\n```\n\nYou can benchmark without AVX or AVX-512 as well:\n\n```\ncmake -B buildnoavx -D ROARING_DISABLE_AVX=ON -D ENABLE_ROARING_MICROBENCHMARKS=ON\ncmake --build buildnoavx\n./buildnoavx/microbenchmarks/bench\n```\n\n# Custom memory allocators\nFor general users, CRoaring would apply default allocator without extra codes. But global memory hook is also provided for those who want a custom memory allocator. Here is an example:\n```C\n#include \u003croaring.h\u003e\n\nint main(){\n    // define with your own memory hook\n    roaring_memory_t my_hook{my_malloc, my_free ...};\n    // initialize global memory hook\n    roaring_init_memory_hook(my_hook);\n    // write you code here\n    ...\n}\n```\n\nBy default we use:\n```C\nstatic roaring_memory_t global_memory_hook = {\n    .malloc = malloc,\n    .realloc = realloc,\n    .calloc = calloc,\n    .free = free,\n    .aligned_malloc = roaring_bitmap_aligned_malloc,\n    .aligned_free = roaring_bitmap_aligned_free,\n};\n```\n\nWe require that the `free`/`aligned_free` functions follow the C\nconvention where `free(NULL)`/`aligned_free(NULL)` have no effect.\n\n\n# Example (C)\n\n\nThis example assumes that CRoaring has been build and that you are linking against the corresponding library. By default, CRoaring will install its header files in a `roaring` directory. If you are working from the amalgamation script, you may add the line `#include \"roaring.c\"` if you are not linking against a prebuilt CRoaring library and replace `#include \u003croaring/roaring.h\u003e` by `#include \"roaring.h\"`.\n\n```c\n#include \u003croaring/roaring.h\u003e\n#include \u003cstdio.h\u003e\n#include \u003cstdlib.h\u003e\n#include \u003cassert.h\u003e\n\nbool roaring_iterator_sumall(uint32_t value, void *param) {\n    *(uint32_t *)param += value;\n    return true;  // iterate till the end\n}\n\nint main() {\n    // create a new empty bitmap\n    roaring_bitmap_t *r1 = roaring_bitmap_create();\n    // then we can add values\n    for (uint32_t i = 100; i \u003c 1000; i++) roaring_bitmap_add(r1, i);\n    // check whether a value is contained\n    assert(roaring_bitmap_contains(r1, 500));\n    // compute how many bits there are:\n    uint32_t cardinality = roaring_bitmap_get_cardinality(r1);\n    printf(\"Cardinality = %d \\n\", cardinality);\n\n    // if your bitmaps have long runs, you can compress them by calling\n    // run_optimize\n    uint32_t expectedsizebasic = roaring_bitmap_portable_size_in_bytes(r1);\n    roaring_bitmap_run_optimize(r1);\n    uint32_t expectedsizerun = roaring_bitmap_portable_size_in_bytes(r1);\n    printf(\"size before run optimize %d bytes, and after %d bytes\\n\",\n           expectedsizebasic, expectedsizerun);\n\n    // create a new bitmap containing the values {1,2,3,5,6}\n    roaring_bitmap_t *r2 = roaring_bitmap_from(1, 2, 3, 5, 6);\n    roaring_bitmap_printf(r2);  // print it\n\n    // we can also create a bitmap from a pointer to 32-bit integers\n    uint32_t somevalues[] = {2, 3, 4};\n    roaring_bitmap_t *r3 = roaring_bitmap_of_ptr(3, somevalues);\n\n    // we can also go in reverse and go from arrays to bitmaps\n    uint64_t card1 = roaring_bitmap_get_cardinality(r1);\n    uint32_t *arr1 = (uint32_t *)malloc(card1 * sizeof(uint32_t));\n    assert(arr1 != NULL);\n    roaring_bitmap_to_uint32_array(r1, arr1);\n    roaring_bitmap_t *r1f = roaring_bitmap_of_ptr(card1, arr1);\n    free(arr1);\n    assert(roaring_bitmap_equals(r1, r1f));  // what we recover is equal\n    roaring_bitmap_free(r1f);\n\n    // we can go from arrays to bitmaps from \"offset\" by \"limit\"\n    size_t offset = 100;\n    size_t limit = 1000;\n    uint32_t *arr3 = (uint32_t *)malloc(limit * sizeof(uint32_t));\n    assert(arr3 != NULL);\n    roaring_bitmap_range_uint32_array(r1, offset, limit, arr3);\n    free(arr3);\n\n    // we can copy and compare bitmaps\n    roaring_bitmap_t *z = roaring_bitmap_copy(r3);\n    assert(roaring_bitmap_equals(r3, z));  // what we recover is equal\n    roaring_bitmap_free(z);\n\n    // we can compute union two-by-two\n    roaring_bitmap_t *r1_2_3 = roaring_bitmap_or(r1, r2);\n    roaring_bitmap_or_inplace(r1_2_3, r3);\n\n    // we can compute a big union\n    const roaring_bitmap_t *allmybitmaps[] = {r1, r2, r3};\n    roaring_bitmap_t *bigunion = roaring_bitmap_or_many(3, allmybitmaps);\n    assert(\n        roaring_bitmap_equals(r1_2_3, bigunion));  // what we recover is equal\n    // can also do the big union with a heap\n    roaring_bitmap_t *bigunionheap =\n        roaring_bitmap_or_many_heap(3, allmybitmaps);\n    assert(roaring_bitmap_equals(r1_2_3, bigunionheap));\n\n    roaring_bitmap_free(r1_2_3);\n    roaring_bitmap_free(bigunion);\n    roaring_bitmap_free(bigunionheap);\n\n    // we can compute intersection two-by-two\n    roaring_bitmap_t *i1_2 = roaring_bitmap_and(r1, r2);\n    roaring_bitmap_free(i1_2);\n\n    // we can write a bitmap to a pointer and recover it later\n    uint32_t expectedsize = roaring_bitmap_portable_size_in_bytes(r1);\n    char *serializedbytes = malloc(expectedsize);\n    // When serializing data to a file, we recommend that you also use\n    // checksums so that, at deserialization, you can be confident\n    // that you are recovering the correct data.\n    roaring_bitmap_portable_serialize(r1, serializedbytes);\n    // Note: it is expected that the input follows the specification\n    // https://github.com/RoaringBitmap/RoaringFormatSpec\n    // otherwise the result may be unusable.\n    // The 'roaring_bitmap_portable_deserialize_safe' function will not read\n    // beyond expectedsize bytes.\n    // We also recommend that you use checksums to check that serialized data corresponds\n    // to the serialized bitmap. The CRoaring library does not provide checksumming.\n    roaring_bitmap_t *t = roaring_bitmap_portable_deserialize_safe(serializedbytes, expectedsize);\n    if(t == NULL) { return EXIT_FAILURE; }\n    const char *reason = NULL;\n    // If your input came from an untrusted source, then you need to validate the\n    // resulting bitmap. Failing to do so could lead to undefined behavior, crashes and so forth.\n    if (!roaring_bitmap_internal_validate(t, \u0026reason)) {\n        return EXIT_FAILURE;\n    }\n    // At this point, the bitmap is safe.\n    assert(roaring_bitmap_equals(r1, t));  // what we recover is equal\n    roaring_bitmap_free(t);\n    // we can also check whether there is a bitmap at a memory location without\n    // reading it\n    size_t sizeofbitmap =\n        roaring_bitmap_portable_deserialize_size(serializedbytes, expectedsize);\n    assert(sizeofbitmap ==\n           expectedsize);  // sizeofbitmap would be zero if no bitmap were found\n    // We can also read the bitmap \"safely\" by specifying a byte size limit.\n    // The 'roaring_bitmap_portable_deserialize_safe' function will not read\n    // beyond expectedsize bytes.\n    // We also recommend that you use checksums to check that serialized data corresponds\n    // to the serialized bitmap. The CRoaring library does not provide checksumming.\n    t = roaring_bitmap_portable_deserialize_safe(serializedbytes, expectedsize);\n    if(t == NULL) {\n        printf(\"Problem during deserialization.\\n\");\n        // We could clear any memory and close any file here.\n        return EXIT_FAILURE;\n    }\n    // We can validate the bitmap we recovered to make sure it is proper.\n    // If the data came from an untrusted source, you should call\n    // roaring_bitmap_internal_validate.\n    const char *reason_failure = NULL;\n    if (!roaring_bitmap_internal_validate(t, \u0026reason_failure)) {\n        printf(\"safely deserialized invalid bitmap: %s\\n\", reason_failure);\n        // We could clear any memory and close any file here.\n        return EXIT_FAILURE;\n    }\n    assert(roaring_bitmap_equals(r1, t));  // what we recover is equal\n    roaring_bitmap_free(t);\n\n    free(serializedbytes);\n\n    // we can iterate over all values using custom functions\n    uint32_t counter = 0;\n    roaring_iterate(r1, roaring_iterator_sumall, \u0026counter);\n\n    // we can also create iterator structs\n    counter = 0;\n    roaring_uint32_iterator_t *i = roaring_iterator_create(r1);\n    while (i-\u003ehas_value) {\n        counter++;  // could use    i-\u003ecurrent_value\n        roaring_uint32_iterator_advance(i);\n    }\n    // you can skip over values and move the iterator with\n    // roaring_uint32_iterator_move_equalorlarger(i,someintvalue)\n\n    roaring_uint32_iterator_free(i);\n    // roaring_bitmap_get_cardinality(r1) == counter\n\n    // for greater speed, you can iterate over the data in bulk\n    i = roaring_iterator_create(r1);\n    uint32_t buffer[256];\n    while (1) {\n        uint32_t ret = roaring_uint32_iterator_read(i, buffer, 256);\n        for (uint32_t j = 0; j \u003c ret; j++) {\n            counter += buffer[j];\n        }\n        if (ret \u003c 256) {\n            break;\n        }\n    }\n    roaring_uint32_iterator_free(i);\n\n    roaring_bitmap_free(r1);\n    roaring_bitmap_free(r2);\n    roaring_bitmap_free(r3);\n    return EXIT_SUCCESS;\n}\n```\n\n# Compressed 64-bit Roaring bitmaps (C)\n\n\nWe also support efficient 64-bit compressed bitmaps in C:\n\n```c++\n  roaring64_bitmap_t *r2 = roaring64_bitmap_create();\n  for (uint64_t i = 100; i \u003c 1000; i++) roaring64_bitmap_add(r2, i);\n  printf(\"cardinality (64-bit) = %d\\n\", (int) roaring64_bitmap_get_cardinality(r2));\n  roaring64_bitmap_free(r2);\n```\n\nThe API is similar to the conventional 32-bit bitmaps. Please see\nthe header file `roaring64.h` (compare with `roaring.h`).\n\n# Conventional bitsets (C)\n\nWe support convention bitsets (uncompressed) as part of the library.\n\nSimple example:\n\n```C\nbitset_t * b = bitset_create();\nbitset_set(b,10);\nbitset_get(b,10);// returns true\nbitset_free(b); // frees memory\n```\n\nMore advanced example:\n\n```C\nbitset_t *b = bitset_create();\nfor (int k = 0; k \u003c 1000; ++k) {\n    bitset_set(b, 3 * k);\n}\n// We have bitset_count(b) == 1000.\n// We have bitset_get(b, 3) is true\n// You can iterate through the values:\nsize_t k = 0;\nfor (size_t i = 0; bitset_next_set_bit(b, \u0026i); i++) {\n    // You will have i == k\n    k += 3;\n}\n// We support a wide range of operations on two bitsets such as\n// bitset_inplace_symmetric_difference(b1,b2);\n// bitset_inplace_symmetric_difference(b1,b2);\n// bitset_inplace_difference(b1,b2);// should make no difference\n// bitset_inplace_union(b1,b2);\n// bitset_inplace_intersection(b1,b2);\n// bitsets_disjoint\n// bitsets_intersect\n```\n\nIn some instances, you may want to convert a Roaring bitmap into a conventional (uncompressed) bitset.\nIndeed, bitsets have advantages such as higher query performances in some cases. The following code\nillustrates how you may do so:\n\n```C\nroaring_bitmap_t *r1 = roaring_bitmap_create();\nfor (uint32_t i = 100; i \u003c 100000; i+= 1 + (i%5)) {\n     roaring_bitmap_add(r1, i);\n}\nfor (uint32_t i = 100000; i \u003c 500000; i+= 100) {\n     roaring_bitmap_add(r1, i);\n}\nroaring_bitmap_add_range(r1, 500000, 600000);\nbitset_t * bitset = bitset_create();\nbool success = roaring_bitmap_to_bitset(r1, bitset);\nassert(success); // could fail due to memory allocation.\nassert(bitset_count(bitset) == roaring_bitmap_get_cardinality(r1));\n// You can then query the bitset:\nfor (uint32_t i = 100; i \u003c 100000; i+= 1 + (i%5)) {\n    assert(bitset_get(bitset,i));\n}\nfor (uint32_t i = 100000; i \u003c 500000; i+= 100) {\n    assert(bitset_get(bitset,i));\n}\n// you must free the memory:\nbitset_free(bitset);\nroaring_bitmap_free(r1);\n```\n\nYou should be aware that a convention bitset (`bitset_t *`) may use much more\nmemory than a Roaring bitmap in some cases. You should run benchmarks to determine\nwhether the conversion to a bitset has performance benefits in your case.\n\n# Example (C++)\n\n\nThis example assumes that CRoaring has been build and that you are linking against the corresponding library. By default, CRoaring will install its header files in a `roaring` directory so you may need to replace `#include \"roaring.hh\"` by `#include \u003croaring/roaring.hh\u003e`. If you are working from the amalgamation script, you may add the line `#include \"roaring.c\"` if you are not linking against a CRoaring prebuilt library.\n\n```c++\n#include \u003ciostream\u003e\n\n#include \"roaring.hh\"\n\nusing namespace roaring;\n\nint main() {\n    Roaring r1;\n    for (uint32_t i = 100; i \u003c 1000; i++) {\n        r1.add(i);\n    }\n\n    // check whether a value is contained\n    assert(r1.contains(500));\n\n    // compute how many bits there are:\n    uint32_t cardinality = r1.cardinality();\n\n    // if your bitmaps have long runs, you can compress them by calling\n    // run_optimize\n    uint32_t size = r1.getSizeInBytes();\n    r1.runOptimize();\n\n    // you can enable \"copy-on-write\" for fast and shallow copies\n    r1.setCopyOnWrite(true);\n\n    uint32_t compact_size = r1.getSizeInBytes();\n    std::cout \u003c\u003c \"size before run optimize \" \u003c\u003c size \u003c\u003c \" bytes, and after \"\n              \u003c\u003c compact_size \u003c\u003c \" bytes.\" \u003c\u003c std::endl;\n\n    // create a new bitmap with varargs\n    Roaring r2 = Roaring::bitmapOf(5, 1, 2, 3, 5, 6);\n\n    r2.printf();\n    printf(\"\\n\");\n\n    // create a new bitmap with initializer list\n    Roaring r2i = Roaring::bitmapOfList({1, 2, 3, 5, 6});\n\n    assert(r2i == r2);\n\n    // we can also create a bitmap from a pointer to 32-bit integers\n    const uint32_t values[] = {2, 3, 4};\n    Roaring r3(3, values);\n\n    // we can also go in reverse and go from arrays to bitmaps\n    uint64_t card1 = r1.cardinality();\n    uint32_t *arr1 = new uint32_t[card1];\n    r1.toUint32Array(arr1);\n    Roaring r1f(card1, arr1);\n    delete[] arr1;\n\n    // bitmaps shall be equal\n    assert(r1 == r1f);\n\n    // we can copy and compare bitmaps\n    Roaring z(r3);\n    assert(r3 == z);\n\n    // we can compute union two-by-two\n    Roaring r1_2_3 = r1 | r2;\n    r1_2_3 |= r3;\n\n    // we can compute a big union\n    const Roaring *allmybitmaps[] = {\u0026r1, \u0026r2, \u0026r3};\n    Roaring bigunion = Roaring::fastunion(3, allmybitmaps);\n    assert(r1_2_3 == bigunion);\n\n    // we can compute intersection two-by-two\n    Roaring i1_2 = r1 \u0026 r2;\n\n    // we can write a bitmap to a pointer and recover it later\n    uint32_t expectedsize = r1.getSizeInBytes();\n    char *serializedbytes = new char[expectedsize];\n    r1.write(serializedbytes);\n    // readSafe will not overflow, but the resulting bitmap\n    // is only valid and usable if the input follows the\n    // Roaring specification: https://github.com/RoaringBitmap/RoaringFormatSpec/\n    Roaring t = Roaring::readSafe(serializedbytes, expectedsize);\n    assert(r1 == t);\n    delete[] serializedbytes;\n\n    // we can iterate over all values using custom functions\n    uint32_t counter = 0;\n    r1.iterate(\n        [](uint32_t value, void *param) {\n            *(uint32_t *)param += value;\n            return true;\n        },\n        \u0026counter);\n\n    // we can also iterate the C++ way\n    counter = 0;\n    for (Roaring::const_iterator i = t.begin(); i != t.end(); i++) {\n        ++counter;\n    }\n    // counter == t.cardinality()\n\n    // we can move iterators to skip values\n    const uint32_t manyvalues[] = {2, 3, 4, 7, 8};\n    Roaring rogue(5, manyvalues);\n    Roaring::const_iterator j = rogue.begin();\n    j.equalorlarger(4);  // *j == 4\n    return EXIT_SUCCESS;\n}\n\n```\n\n\n\n# Building with cmake (Linux and macOS, Visual Studio users should see below)\n\nCRoaring follows the standard cmake workflow. Starting from the root directory of\nthe project (CRoaring), you can do:\n\n```\nmkdir -p build\ncd build\ncmake ..\ncmake --build .\n# follow by 'ctest' if you want to test.\n# you can also type 'make install' to install the library on your system\n# C header files typically get installed to /usr/local/include/roaring\n# whereas C++ header files get installed to /usr/local/include/roaring\n```\n(You can replace the ``build`` directory with any other directory name.)\nBy default all tests are built on all platforms, to skip building and running tests add `` -DENABLE_ROARING_TESTS=OFF `` to the command line.\n\nAs with all ``cmake`` projects, you can specify the compilers you wish to use by adding (for example) ``-DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++`` to the ``cmake`` command line.\n\nIf you are using clang or gcc and you know your target architecture,  you can set the architecture by specifying `-DROARING_ARCH=arch`. For example, if you have many server but the oldest server is running the Intel `haswell` architecture, you can specify -`DROARING_ARCH=haswell`. In such cases, the produced binary will be optimized for processors having the characteristics of a haswell process and may not run on older architectures. You can find out the list of valid architecture values by typing `man gcc`.\n\n ```\n mkdir -p build_haswell\n cd build_haswell\n cmake -DROARING_ARCH=haswell ..\n cmake --build .\n ```\n\nFor a debug release, starting from the root directory of the project (CRoaring), try\n\n```\nmkdir -p debug\ncd debug\ncmake -DCMAKE_BUILD_TYPE=Debug -DROARING_SANITIZE=ON ..\nctest\n```\n\n\nTo check that your code abides by the style convention (make sure that ``clang-format`` is installed):\n\n```\n./tools/clang-format-check.sh\n```\n\nTo reformat your code according to the style convention (make sure that ``clang-format`` is installed):\n\n```\n./tools/clang-format.sh\n```\n\n# Building (Visual Studio under Windows)\n\nWe are assuming that you have a common Windows PC with at least Visual Studio 2015, and an x64 processor.\n\nTo build with at least Visual Studio 2015 from the command line:\n- Grab the CRoaring code from GitHub, e.g., by cloning it using [GitHub Desktop](https://desktop.github.com/).\n- Install [CMake](https://cmake.org/download/). When you install it, make sure to ask that ``cmake`` be made available from the command line.\n- Create a subdirectory within CRoaring, such as ``VisualStudio``.\n- Using a shell, go to this newly created directory. For example, within GitHub Desktop, you can right-click on  ``CRoaring`` in your GitHub repository list, and select ``Open in Git Shell``, then type ``cd VisualStudio`` in the newly created shell.\n- Type ``cmake -DCMAKE_GENERATOR_PLATFORM=x64 ..`` in the shell while in the ``VisualStudio`` repository. (Alternatively, if you want to build a static library, you may use the command line ``cmake -DCMAKE_GENERATOR_PLATFORM=x64 -DROARING_BUILD_STATIC=ON  ..``.)\n- This last command created a Visual Studio solution file in the newly created directory (e.g., ``RoaringBitmap.sln``). Open this file in Visual Studio. You should now be able to build the project and run the tests. For example, in the ``Solution Explorer`` window (available from the ``View`` menu), right-click ``ALL_BUILD`` and select ``Build``. To test the code, still in the ``Solution Explorer`` window, select ``RUN_TESTS`` and select ``Build``.\n\nTo build with at least Visual Studio 2017 directly in the IDE:\n- Grab the CRoaring code from GitHub, e.g., by cloning it using [GitHub Desktop](https://desktop.github.com/).\n- Select the ``Visual C++ tools for CMake`` optional component when installing the C++ Development Workload within Visual Studio.\n- Within Visual Studio use ``File \u003e Open \u003e Folder...`` to open the CRoaring folder.\n- Right click on ``CMakeLists.txt`` in the parent directory within ``Solution Explorer`` and select ``Build`` to build the project.\n- For testing, in the Standard toolbar, drop the ``Select Startup Item...`` menu and choose one of the tests. Run the test by pressing the button to the left of the dropdown.\n\n\nWe have optimizations specific to AVX2 and AVX-512 in the code, and they are turned dynamically based on the detected hardware at runtime.\n\n\n## Usage (Using `conan`)\n\nYou can install pre-built binaries for `roaring` or build it from source using [Conan](https://conan.io/). Use the following command to install latest version:\n\n```\nconan install --requires=\"roaring/[*]\" --build=missing\n```\n\nFor detailed instructions on how to use Conan, please refer to the [Conan documentation](https://docs.conan.io/2/).\n\nThe `roaring` Conan recipe is kept up to date by Conan maintainers and community contributors.\nIf the version is out of date, please [create an issue or pull request](https://github.com/conan-io/conan-center-index) on the ConanCenterIndex repository.\n\n\n## Usage (Using `vcpkg` on Windows, Linux and macOS)\n\n[vcpkg](https://github.com/Microsoft/vcpkg) users on Windows, Linux and macOS can download and install `roaring` with one single command from their favorite shell.\n\nOn Linux and macOS:\n\n```\n$ ./vcpkg install roaring\n```\n\nwill build and install `roaring` as a static library.\n\nOn Windows (64-bit):\n\n```\n.\\vcpkg.exe install roaring:x64-windows\n```\n\nwill build and install `roaring` as a shared library.\n\n```\n.\\vcpkg.exe install roaring:x64-windows-static\n```\n\nwill build and install `roaring` as a static library.\n\nThese commands will also print out instructions on how to use the library from MSBuild or CMake-based projects.\n\nIf you find the version of `roaring` shipped with `vcpkg` is out-of-date, feel free to report it to `vcpkg` community either by submiting an issue or by creating a PR.\n\n# SIMD-related throttling\n\nOur AVX2 code does not use floating-point numbers or multiplications, so it is not subject to turbo frequency throttling on many-core Intel processors.\n\nOur AVX-512 code is only enabled on recent hardware (Intel Ice Lake or better and AMD Zen 4) where SIMD-specific frequency throttling is not observed.\n\n# Thread safety\n\nLike, for example, STL containers, the CRoaring library has no built-in thread support. Thus whenever you modify a bitmap in one thread, it is unsafe to query it in others. However, you can safely copy a bitmap and use both copies in concurrently.\n\nIf you use  \"copy-on-write\" (default to disabled), then you should pass copies to the different threads. They will create shared containers, and for shared containers, we use reference counting with an atomic counter.\n\n\n\nTo summarize:\n- If you do not use copy-on-write, you can access concurrent the same bitmap safely as long as you do not modify it. If you plan on modifying it, you should pass different copies to the different threads.\n- If you use copy-on-write, you should always pass copies to the different threads. The copies and then lightweight (shared containers).\n\nThus the following pattern where you copy bitmaps and pass them to different threads is safe with or without COW:\n\n```C\n    roaring_bitmap_set_copy_on_write(r1, true);\n    roaring_bitmap_set_copy_on_write(r2, true);\n    roaring_bitmap_set_copy_on_write(r3, true);\n\n    roaring_bitmap_t * r1a = roaring_bitmap_copy(r1);\n    roaring_bitmap_t * r1b = roaring_bitmap_copy(r1);\n\n    roaring_bitmap_t * r2a = roaring_bitmap_copy(r2);\n    roaring_bitmap_t * r2b = roaring_bitmap_copy(r2);\n\n    roaring_bitmap_t * r3a = roaring_bitmap_copy(r3);\n    roaring_bitmap_t * r3b = roaring_bitmap_copy(r3);\n\n    roaring_bitmap_t *rarray1[3] = {r1a, r2a, r3a};\n    roaring_bitmap_t *rarray2[3] = {r1b, r2b, r3b};\n    std::thread thread1(run, rarray1);\n    std::thread thread2(run, rarray2);\n```\n\n# How to best aggregate bitmaps?\n\nSuppose you want to compute the union (OR) of many bitmaps. How do you proceed? There are many\ndifferent strategies.\n\nYou can use `roaring_bitmap_or_many(bitmapcount, bitmaps)` or `roaring_bitmap_or_many_heap(bitmapcount, bitmaps)` or you may\neven roll your own aggregation:\n\n```C\nroaring_bitmap_t *answer = roaring_bitmap_copy(bitmaps[0]);\nfor (size_t i = 1; i \u003c bitmapcount; i++) {\n  roaring_bitmap_or_inplace(answer, bitmaps[i]);\n}\n```\n\nAll of them will work but they have different performance characteristics. The `roaring_bitmap_or_many_heap` should\nprobably only be used if, after benchmarking, you find that it is faster by a good margin: it uses more memory.\n\nThe `roaring_bitmap_or_many` is meant as a good default. It works by trying to delay work as much as possible.\nHowever, because it delays computations, it also does not optimize the format as the computation runs. It might\nthus fail to see some useful pattern in the data such as long consecutive values.\n\nThe approach based on repeated calls to `roaring_bitmap_or_inplace`\nis also fine, and might even be faster in some cases. You can expect it to be faster if, after\na few calls, you get long sequences of consecutive values in the answer. That is, if the\nfinal answer is all integers in the range [0,1000000), and this is apparent quickly, then the\nlater `roaring_bitmap_or_inplace` will be very fast.\n\nYou should benchmark these alternatives on your own data to decide what is best.\n\n# Wrappers\n\n## Python\nTom Cornebize wrote a Python wrapper available at https://github.com/Ezibenroc/PyRoaringBitMap\nInstalling it is as easy as typing...\n\n```\npip install pyroaring\n```\n\n## JavaScript\n\nSalvatore Previti  wrote a Node/JavaScript wrapper available at https://github.com/SalvatorePreviti/roaring-node\nInstalling it is as easy as typing...\n\n```\nnpm install roaring\n```\n\n## Swift\n\nJérémie Piotte wrote a [Swift wrapper](https://github.com/RoaringBitmap/SwiftRoaring).\n\n\n## C#\n\nBrandon Smith wrote a C# wrapper available at https://github.com/RogueException/CRoaring.Net (works for Windows and Linux under x64 processors)\n\n\n## Go (golang)\n\nThere is a Go (golang) wrapper available at https://github.com/RoaringBitmap/gocroaring\n\n## Rust\n\nSaulius Grigaliunas wrote a Rust wrapper available at https://github.com/saulius/croaring-rs\n\n## D\n\nYuce Tekol wrote a D wrapper available at https://github.com/yuce/droaring\n\n## Redis\n\nAntonio Guilherme Ferreira Viggiano wrote a Redis Module available at https://github.com/aviggiano/redis-roaring\n\n## Zig\n\nJustin Whear wrote a Zig wrapper available at https://github.com/jwhear/roaring-zig\n\n\n# Mailing list/discussion group\n\nhttps://groups.google.com/forum/#!forum/roaring-bitmaps\n\n# Contributing\n\nWhen contributing a change to the project, please run `tools/clang-format.sh` after making any changes. A github action runs on all PRs to ensure formatting is consistent with this.\n\n# References about Roaring\n\n- Daniel Lemire, Owen Kaser, Nathan Kurz, Luca Deri, Chris O'Hara, François Saint-Jacques, Gregory Ssi-Yan-Kai, Roaring Bitmaps: Implementation of an Optimized Software Library, Software: Practice and Experience Volume 48, Issue 4 April 2018 Pages 867-895 [arXiv:1709.07821](https://arxiv.org/abs/1709.07821)\n-  Samy Chambi, Daniel Lemire, Owen Kaser, Robert Godin,\nBetter bitmap performance with Roaring bitmaps,\nSoftware: Practice and Experience Volume 46, Issue 5, pages 709–719, May 2016  [arXiv:1402.6407](http://arxiv.org/abs/1402.6407)\n- Daniel Lemire, Gregory Ssi-Yan-Kai, Owen Kaser, Consistently faster and smaller compressed bitmaps with Roaring, Software: Practice and Experience Volume 46, Issue 11, pages 1547-1569, November 2016 [arXiv:1603.06549](http://arxiv.org/abs/1603.06549)\n- Samy Chambi, Daniel Lemire, Robert Godin, Kamel Boukhalfa, Charles Allen, Fangjin Yang, Optimizing Druid with Roaring bitmaps, IDEAS 2016, 2016. http://r-libre.teluq.ca/950/\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froaringbitmap%2Fcroaring","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Froaringbitmap%2Fcroaring","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Froaringbitmap%2Fcroaring/lists"}