{"id":22543122,"url":"https://github.com/cryptocode/zigwick","last_synced_at":"2025-04-09T22:53:26.593Z","repository":{"id":137352331,"uuid":"455658795","full_name":"cryptocode/zigwick","owner":"cryptocode","description":"A Fenwick tree for Zig","archived":false,"fork":false,"pushed_at":"2024-05-01T12:18:35.000Z","size":5,"stargazers_count":13,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-09T22:53:20.754Z","etag":null,"topics":["binary-indexed-tree","fenwick","zig"],"latest_commit_sha":null,"homepage":"","language":"Zig","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/cryptocode.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":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-02-04T18:42:29.000Z","updated_at":"2024-12-28T00:57:09.000Z","dependencies_parsed_at":"2024-05-01T14:00:40.260Z","dependency_job_id":null,"html_url":"https://github.com/cryptocode/zigwick","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptocode%2Fzigwick","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptocode%2Fzigwick/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptocode%2Fzigwick/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cryptocode%2Fzigwick/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cryptocode","download_url":"https://codeload.github.com/cryptocode/zigwick/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248125634,"owners_count":21051766,"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":["binary-indexed-tree","fenwick","zig"],"created_at":"2024-12-07T13:14:23.286Z","updated_at":"2025-04-09T22:53:26.570Z","avatar_url":"https://github.com/cryptocode.png","language":"Zig","readme":"zig**wick** is a [Fenwick tree](https://en.wikipedia.org/wiki/Fenwick_tree) implementation in [Zig](https://ziglang.org).\n\n## Overview\nThe main purpose of a Fenwick tree is to efficiently maintain prefix sums. The data structure supports O(log N) range queries and point updates.  \n\nThe need to update and query partial sums frequently comes up in competitive programming challenges, but also crop up in real-world applications. \n\nzigwick never allocates and have no dependencies, not even the standard library (except for tests)\n\n## Building\nThis library requires a recent Zig build. Last tested with `0.13.0-dev.47+c231d9496`.\n\nTo build the library and test it:\n\n```\nzig build\nzig build test\n```\n\n\n## Types of use\n\nFenwick trees are generally used in two ways:\n\n1) As an auxiliary data structure for keeping track of partial sums. In this case, the Fenwick tree is kept separate from the actual array of numbers to allow fast indexed access to specific numbers.\n\n2) As the store of both numbers and prefix sums. This reduces memory requirements, but you lose O(1) access to specific numbers by index - this is now a O(log n) operation.\n\n## Create an instance\n\nIn the first example we chose to keep numbers and sums separate. This way we can quickly look up numbers by index.\n\n```zig\nvar data: [5]i32 = .{ -1, -5, -1, 0, 5 };\nvar tree = [_]i32{0} ** 5;\nvar partial_sums = Fenwick(i32).initFrom(\u0026tree, \u0026data);\n\n// At this point we can query sums up to an index, or in a range\n// Indices are zero-based.\npartial_sums.sum(4);\npartial_sums.sum(1);\npartial_sums.sumRange(2, 4);\n\n// We can add or subtract at given indices, which will efficiently update all prefix sums\npartial_sums.update(3, 2);\n```\n\nThe `tree` array is the backing buffer for the prefix sums. You can chose to allocate this on the heap, of course. The library itself never allocates.\n\nThe next example use the tree buffer both for the data and the prefix sums. This saves memory at the cost of O(log n) rather than O(1) access to actual numbers (this isn't needed in many applications, but it's an important consideration if so)\n\n```zig\nvar tree = [_]i32{0} ** 10;\nvar fenwick = Fenwick(i32).initZero(\u0026tree);\nfenwick.update(4, 10);\nfenwick.set(5, 9);\nfenwick.get(5);\nfenwick.sum(5);\n```\n\nSee tests for complete examples.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptocode%2Fzigwick","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcryptocode%2Fzigwick","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcryptocode%2Fzigwick/lists"}