{"id":26202249,"url":"https://github.com/astef/bitmask","last_synced_at":"2025-04-15T06:11:52.665Z","repository":{"id":143541354,"uuid":"616031174","full_name":"astef/bitmask","owner":"astef","description":"Arbitrary size bitmask (aka bitset) with efficient Slice method","archived":false,"fork":false,"pushed_at":"2024-10-12T19:00:02.000Z","size":60,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-15T06:11:47.692Z","etag":null,"topics":["binary","bitmask","bits","bitset","go","mask","slice"],"latest_commit_sha":null,"homepage":"","language":"Go","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/astef.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":"2023-03-19T12:20:22.000Z","updated_at":"2024-10-12T18:57:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"58e5882a-f9f3-4a0c-bad0-460c95693851","html_url":"https://github.com/astef/bitmask","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astef%2Fbitmask","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astef%2Fbitmask/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astef%2Fbitmask/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/astef%2Fbitmask/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/astef","download_url":"https://codeload.github.com/astef/bitmask/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249016636,"owners_count":21198833,"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","bitmask","bits","bitset","go","mask","slice"],"created_at":"2025-03-12T03:33:58.635Z","updated_at":"2025-04-15T06:11:52.647Z","avatar_url":"https://github.com/astef.png","language":"Go","readme":"# bitmask\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/astef/bitmask.svg)](https://pkg.go.dev/github.com/astef/bitmask) ![Coverage Badge](https://img.shields.io/badge/coverage-97.6%25-green.svg)\n\nArbitrary size bitmask (aka bitset) with efficient Slice method.\n\n### `.Slice` doesn't create copies of the underlying buffer, just like Go slices\n\n```go\nbm := bitmask.New(4)        // [4]{0000}\nbm.Set(3)                   // [4]{0001}\nbm.Slice(2, 4).ToggleAll()  // [4]{0010}\nbm.Slice(1, 3).ToggleAll()  // [4]{0100}\nbm.Slice(0, 2).ToggleAll()  // [4]{1000}\nbm.ClearAll()               // [4]{0000}\n```\n\n### It's safe to copy overlapping bitmasks, which were created by slicing the original one\n\n```go\nbase := bitmask.New(10)\nbase.Set(0)\nbase.Set(9)                 // [10]{1000000001}\n\nsrc := base.Slice(0, 8)     // [8]{10000000}\ndst := base.Slice(2, 10)    // [8]{00000001}\n\nbitmask.Copy(dst, src)\n\nfmt.Println(base)\n```\n\n```\n[10]{1010000000}\n```\n\n### Iterator\n\nGo \u003e=1.23 iterator is exposed by `.Bits()` method:\n\n```go\nbm := bitmask.New(5)\nbm.Set(0)\nbm.Set(3)\n\nfor idx, isSet := range bm.Bits() {\n    // use the value\n    fmt.Printf(\"%v) %v\\n\", index, value)\n}\n```\n\nThere's also an old-style equivalent:\n\n```go\nit := bm.Iterator()\nfor {\n    ok, value, index := it.Next()\n    if !ok {\n        break\n    }\n\n    // use the value\n    fmt.Printf(\"%v) %v\\n\", index, value)\n}\n```\n\n```\n0) true\n1) false\n2) false\n3) true\n4) false\n```\n\n### `.String()` is O(1), it starts stripping after 512 bits\n\n```go\nbm := bitmask.New(513)\nbm.Slice(255, 321).SetAll()\nfmt.Println(bm)\n```\n\n```\n[513]{0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000001 \u003cmore 64 bits\u003e 1000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0}\n```\n\n### Non-copying constructor to avoid heap-allocating bit buffer\n\n```go\nbm := bitmask.NewFromUintRawNocopy(1, 2, 3)\n```\n\nFrom `go build -gcflags=-m`\n```\ninlining call to bitmask.NewFromUintRawNocopy\n... argument does not escape\n\u0026bitmask.BitMask{...} does not escape\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastef%2Fbitmask","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fastef%2Fbitmask","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fastef%2Fbitmask/lists"}