{"id":15049277,"url":"https://github.com/clibraries/array-algorithms","last_synced_at":"2025-08-19T22:32:24.868Z","repository":{"id":178522061,"uuid":"661965674","full_name":"clibraries/array-algorithms","owner":"clibraries","description":"Unintrusive algorithms for C arrays OR a C implementation of \u003calgorithm\u003e from C++","archived":false,"fork":false,"pushed_at":"2023-07-16T05:43:20.000Z","size":51,"stargazers_count":212,"open_issues_count":3,"forks_count":7,"subscribers_count":5,"default_branch":"master","last_synced_at":"2024-12-07T16:11:36.404Z","etag":null,"topics":["c-generic","c99","generic-programming","single-header-library","sorting-algorithms","stl-algorithms"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/clibraries.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-07-04T04:35:34.000Z","updated_at":"2024-12-05T16:15:30.000Z","dependencies_parsed_at":null,"dependency_job_id":"0f94c1e0-3538-4a10-b64e-320a4f25c4af","html_url":"https://github.com/clibraries/array-algorithms","commit_stats":null,"previous_names":["clibraries/array-algorithms"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clibraries%2Farray-algorithms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clibraries%2Farray-algorithms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clibraries%2Farray-algorithms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/clibraries%2Farray-algorithms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/clibraries","download_url":"https://codeload.github.com/clibraries/array-algorithms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":230374118,"owners_count":18216041,"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":["c-generic","c99","generic-programming","single-header-library","sorting-algorithms","stl-algorithms"],"created_at":"2024-09-24T21:19:25.874Z","updated_at":"2024-12-19T04:07:15.743Z","avatar_url":"https://github.com/clibraries.png","language":"C","readme":"# array_alg.h\n\nUnintrusive algorithms for C arrays OR a C implementation of \\\u003calgorithm\u003e from C++\n\n## Pitch\n\nThe C++ STL is one of the most complete and reusable algorithm libraries available.\nThis single header file brings 80% of that functionality to C99 in a non-intrusive way.\nThere are no new data structures. Just include the library and call functions on C arrays.\n\nFeatures:\n\n- Sets (intersection, union, subset, etc)\n- Heaps (priority queues)\n- Binary search (lower bound, upper bound, etc)\n- Sorts (insertion sort, quick sort, merge/stable sort, heap sort, partial sort, etc)\n- Partitioning (partition, unique, etc)\n- Predicates (all of, any of, none of, etc)\n- Uniform random sampling and shuffling\n\n## Usage\n\nThis library uses the preprocessor to implement generic functions.\nEach time you include the library, you will need to define the array element type and a function prefix:\n\n    #define ARRAY_ALG_TYPE int\n    #define ARRAY_ALG_PREFIX intv_\n    #include \"array_alg.h\"\n\nThe above will only generate the declarations.\nIn at least one C file, you will also need to generate implementations.\nTo generate implementations, define `ARRAY_ALG_IMPLEMENTATION` in a C file and include the library:\n\n    #define ARRAY_ALG_TYPE int\n    #define ARRAY_ALG_PREFIX intv_\n    #define ARRAY_ALG_IMPLEMENTATION\n    #include \"array_alg.h\"\n\n\nAlternatively, add `#define ARRAY_ALG_STATIC` before the original declaration\nto avoid the need for separate implementations.\n\nRepeat this process for each array type you want to use.\n\n## Examples\n\nRemove duplicate entries:\n\n    #define ARRAY_ALG_TYPE int\n    #define ARRAY_ALG_PREFIX intv_\n    #include \"array_alg.h\"\n\n    int compare_int(const int *a, const int *b, void *ctx) {\n        return *a - *b;\n    }\n\n    ...\n\n    int nums[100] = ...;\n    intv_sort(nums, nums + 100, compare_int, NULL);\n    int* end = intv_unique(nums, nums + 100, compare_int, NULL);\n\n\n## Design\n\n### 1. Iterators and Arrays\n\nThe C++ STL is designed around the concept of iterators.\nWith iterators, one algorithm can be reused not just for multiple types, but also for many data structures.\nThis is an ingenious design.\nHowever, in practice, this capability is rarely needed.\nThe vast majority of real world \\\u003calgorithm\u003e invocations are on contiguous arrays/vectors.\n\nFor those cases where you do have a fancy data structure (graphs, trees, etc),\ncopy its contents to an array, perform the algorithm, and then copy the contents back.\nThis will often help it perform better anyway!\n\n### 2. Bounds vs counted ranges\n\nSTL algorithms typically operate on half-open ranges bounded by iterators [first, last).\nThis convention is not used as often in C, but we think it offers some benefits.\nInternally, the functions can maintain less state by simply incrementing pointers\nrather than keeping track of pointers, indices, and counts.\n\nOperations also compose a little easier.\nWhen a pointer is returned to an element of interest,\nthat same pointer can be used as an argument for another algorithm.\n\n### 3. What's left out\n\nBecause it's a bit verbose to define a C closure (function pointer and context), some STL algorithms are less useful in C.\nIf an algorithm can be written as a simple for loop with no additional state or control flow, this library doesn't implement it.\n\n    transform -\u003e for (int i = 0; i \u003c n; ++i) out[i] = f(in[i])\n    fill -\u003e for (int i = 0; i \u003c n; ++i) out[i] = x;\n    iota -\u003e for (int i = 0; i \u003c n; ++i) out[i] = i;\n    generate -\u003e for (int i = 0; i \u003c n; ++i) out[i] = f();\n\nThe algorithms which rely on ordered types always require a comparison function.\nWe do not include any variants that operate on the `==` operator, as operators cannot be overloaded in C.\n\n### 4. Generics vs `void*`\n\nIncluding a header multiple times with various `#define`s is a little cumbersome.\nHowever, we think it's a superior way to achieve C generics compared to the `void*` style used by `qsort` and `bsearch`.\nThe preprocessor approach provides:\n\n- Better type safety and avoids verbose casting logic.\n\n- Better peformance (as `void*` functions are difficult to optimize).\n\n    Note: The C compiler can only create one non-inlined version of each function.\n    For example, it could not choose to use `int` instructions, even if it knew the type at compile time.\n    With the single header approach you get a new instance of each function optimized for each application.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclibraries%2Farray-algorithms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fclibraries%2Farray-algorithms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fclibraries%2Farray-algorithms/lists"}