{"id":28023154,"url":"https://github.com/willf/bitset","last_synced_at":"2025-05-11T00:01:49.058Z","repository":{"id":1484397,"uuid":"1731413","full_name":"bits-and-blooms/bitset","owner":"bits-and-blooms","description":"Go package implementing bitsets","archived":false,"fork":false,"pushed_at":"2025-05-01T20:30:57.000Z","size":457,"stargazers_count":1410,"open_issues_count":3,"forks_count":186,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-05-07T10:52:37.169Z","etag":null,"topics":["bitset","go"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bits-and-blooms.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"patreon":null,"open_collective":null,"ko_fi":null,"custom":"https://donate.mcc.org/"}},"created_at":"2011-05-11T03:33:44.000Z","updated_at":"2025-05-05T14:45:49.000Z","dependencies_parsed_at":"2023-07-05T15:45:34.742Z","dependency_job_id":"c16ba7fd-d703-4174-ab77-7ccb2a1acfaf","html_url":"https://github.com/bits-and-blooms/bitset","commit_stats":{"total_commits":292,"total_committers":48,"mean_commits":6.083333333333333,"dds":0.8424657534246576,"last_synced_commit":"5f9e4378e8839525fb326e459ba3eed4ddf6aa9a"},"previous_names":["willf/bitset"],"tags_count":42,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bits-and-blooms%2Fbitset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bits-and-blooms%2Fbitset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bits-and-blooms%2Fbitset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bits-and-blooms%2Fbitset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bits-and-blooms","download_url":"https://codeload.github.com/bits-and-blooms/bitset/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253497374,"owners_count":21917691,"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":["bitset","go"],"created_at":"2025-05-11T00:01:24.996Z","updated_at":"2025-05-11T00:01:48.978Z","avatar_url":"https://github.com/bits-and-blooms.png","language":"Go","funding_links":["https://donate.mcc.org/"],"categories":["Data Structures","\u003cspan id=\"数据结构-data-structures\"\u003e数据结构 Data Structures\u003c/span\u003e","Database","数据结构","數據結構","数据结构`go语言实现的数据结构与算法`"],"sub_categories":["Advanced Console UIs","Standard CLI","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","高级控制台界面","高級控制台界面","标准 CLI"],"readme":"# bitset\n\n*Go language library to map between non-negative integers and boolean values*\n\n[![Test](https://github.com/bits-and-blooms/bitset/workflows/Test/badge.svg)](https://github.com/willf/bitset/actions?query=workflow%3ATest)\n[![Go Report Card](https://goreportcard.com/badge/github.com/willf/bitset)](https://goreportcard.com/report/github.com/willf/bitset)\n[![PkgGoDev](https://pkg.go.dev/badge/github.com/bits-and-blooms/bitset?tab=doc)](https://pkg.go.dev/github.com/bits-and-blooms/bitset?tab=doc)\n\n\nThis library is part of the [awesome go collection](https://github.com/avelino/awesome-go). It is used in production by several important systems:\n\n* [beego](https://github.com/beego/beego)\n* [CubeFS](https://github.com/cubefs/cubefs)\n* [Amazon EKS Distro](https://github.com/aws/eks-distro)\n* [sourcegraph](https://github.com/sourcegraph/sourcegraph-public-snapshot)\n* [torrent](https://github.com/anacrolix/torrent)\n\n\n## Description\n\nPackage bitset implements bitsets, a mapping between non-negative integers and boolean values.\nIt should be more efficient than map[uint] bool.\n\nIt provides methods for setting, clearing, flipping, and testing individual integers.\n\nBut it also provides set intersection, union, difference, complement, and symmetric operations, as well as tests to check whether any, all, or no bits are set, and querying a bitset's current length and number of positive bits.\n\nBitSets are expanded to the size of the largest set bit; the memory allocation is approximately Max bits, where Max is the largest set bit. BitSets are never shrunk automatically, but `Shrink` and `Compact` methods are available. On creation, a hint can be given for the number of bits that will be used.\n\nMany of the methods, including Set, Clear, and Flip, return a BitSet pointer, which allows for chaining.\n\n### Example use:\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"math/rand\"\n\n\t\"github.com/bits-and-blooms/bitset\"\n)\n\nfunc main() {\n\tfmt.Printf(\"Hello from BitSet!\\n\")\n\tvar b bitset.BitSet\n\t// play some Go Fish\n\tfor i := 0; i \u003c 100; i++ {\n\t\tcard1 := uint(rand.Intn(52))\n\t\tcard2 := uint(rand.Intn(52))\n\t\tb.Set(card1)\n\t\tif b.Test(card2) {\n\t\t\tfmt.Println(\"Go Fish!\")\n\t\t}\n\t\tb.Clear(card1)\n\t}\n\n\t// Chaining\n\tb.Set(10).Set(11)\n\n\tfor i, e := b.NextSet(0); e; i, e = b.NextSet(i + 1) {\n\t\tfmt.Println(\"The following bit is set:\", i)\n\t}\n\tif b.Intersection(bitset.New(100).Set(10)).Count() == 1 {\n\t\tfmt.Println(\"Intersection works.\")\n\t} else {\n\t\tfmt.Println(\"Intersection doesn't work???\")\n\t}\n}\n```\n\nIf you have Go 1.23 or better, you can iterate over the set bits like so:\n\n```go\nfor i := range b.EachSet() {}\n```\n\n\n\nPackage documentation is at: https://pkg.go.dev/github.com/bits-and-blooms/bitset?tab=doc\n\n## Serialization\n\n\nYou may serialize a bitset safely and portably to a stream\nof bytes as follows:\n```Go\n    const length = 9585\n\tconst oneEvery = 97\n\tbs := bitset.New(length)\n\t// Add some bits\n\tfor i := uint(0); i \u003c length; i += oneEvery {\n\t\tbs = bs.Set(i)\n\t}\n\n\tvar buf bytes.Buffer\n\tn, err := bs.WriteTo(\u0026buf)\n\tif err != nil {\n\t\t// failure\n\t}\n\t// Here n == buf.Len()\n```\nYou can later deserialize the result as follows:\n\n```Go\n\t// Read back from buf\n\tbs = bitset.New()\n\tn, err = bs.ReadFrom(\u0026buf)\n\tif err != nil {\n\t\t// error\n\t}\n\t// n is the number of bytes read\n```\n\nThe `ReadFrom` function attempts to read the data into the existing\nBitSet instance, to minimize memory allocations.\n\n\n*Performance tip*: \nWhen reading and writing to a file or a network connection, you may get better performance by \nwrapping your streams with `bufio` instances.\n\nE.g., \n```Go\n\tf, err := os.Create(\"myfile\")\n\tw := bufio.NewWriter(f)\n```\n```Go\n\tf, err := os.Open(\"myfile\")\n\tr := bufio.NewReader(f)\n```\n\n## Memory Usage\n\nThe memory usage of a bitset using `N` bits is at least `N/8` bytes. The number of bits in a bitset is at least as large as one plus the greatest bit index you have accessed. Thus it is possible to run out of memory while using a bitset. If you have lots of bits, you might prefer compressed bitsets, like the [Roaring bitmaps](https://roaringbitmap.org) and its [Go implementation](https://github.com/RoaringBitmap/roaring).\n\nThe `roaring` library allows you to go back and forth between compressed Roaring bitmaps and the conventional bitset instances:\n```Go\n\t\t\tmybitset := roaringbitmap.ToBitSet()\n\t\t\tnewroaringbitmap := roaring.FromBitSet(mybitset)\n```\n\n\n### Goroutine safety\n\nIn general, it's not safe to access the same BitSet using different goroutines--they are unsynchronized for performance.\n\nShould you want to access a BitSet from more than one goroutine, you should provide synchronization. Typically this is done by using channels to pass the *BitSet around (in Go style; so there is only ever one owner), or by using `sync.Mutex` to serialize operations on BitSets.\n\n## Installation\n\n```bash\ngo get github.com/bits-and-blooms/bitset\n```\n\n## Contributing\n\nIf you wish to contribute to this project, please branch and issue a pull request against master (\"[GitHub Flow](https://guides.github.com/introduction/flow/)\")\n\n## Running all tests\n\nBefore committing the code, please check if it passes tests, has adequate coverage, etc.\n```bash\ngo test\ngo test -cover\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillf%2Fbitset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwillf%2Fbitset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwillf%2Fbitset/lists"}