{"id":13492597,"url":"https://github.com/scylladb/go-set","last_synced_at":"2025-05-14T20:02:03.956Z","repository":{"id":57480443,"uuid":"143122800","full_name":"scylladb/go-set","owner":"scylladb","description":"Type-safe, zero-allocation sets for Go","archived":false,"fork":false,"pushed_at":"2020-02-25T12:20:02.000Z","size":126,"stargazers_count":815,"open_issues_count":2,"forks_count":29,"subscribers_count":25,"default_branch":"master","last_synced_at":"2025-04-13T14:00:00.688Z","etag":null,"topics":["golang","set","zero-allocation"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/scylladb.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}},"created_at":"2018-08-01T07:53:14.000Z","updated_at":"2025-02-11T21:34:11.000Z","dependencies_parsed_at":"2022-09-26T17:41:14.047Z","dependency_job_id":null,"html_url":"https://github.com/scylladb/go-set","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scylladb%2Fgo-set","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scylladb%2Fgo-set/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scylladb%2Fgo-set/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/scylladb%2Fgo-set/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/scylladb","download_url":"https://codeload.github.com/scylladb/go-set/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248724585,"owners_count":21151560,"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":["golang","set","zero-allocation"],"created_at":"2024-07-31T19:01:07.387Z","updated_at":"2025-04-13T14:00:04.695Z","avatar_url":"https://github.com/scylladb.png","language":"Go","funding_links":[],"categories":["Misc","开源类库","Go","Open source library"],"sub_categories":["数据结构","Data Structure"],"readme":"# Set [![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/scylladb/go-set) [![Go Report Card](https://goreportcard.com/badge/github.com/scylladb/go-set)](https://goreportcard.com/report/github.com/scylladb/go-set) [![Build Status](https://travis-ci.org/scylladb/go-set.svg?branch=master)](https://travis-ci.org/scylladb/go-set)\n\nPackage set is a type-safe, zero-allocation port of the excellent package [fatih/set](https://github.com/fatih/set). It contains sets for most of the basic types and you can generate sets for your own types with ease.\n\n## Example\n\nExample code using the generated string set:\n\n```go\nimport \"github.com/scylladb/go-set/strset\"\n\ns1 := strset.New(\"entry 1\", \"entry 2\")\ns2 := strset.New(\"entry 2\", \"entry 3\")\ns3 := strset.Intersection(s1, s2)\n// s3 now contains only \"entry 2\"\n```\n\nThe library exposes a number of top level factory functions that can be used to create a specific instances of the set type you want to use. For example to create a set to store `int` you could do like this:\n\n```go\nimport \"github.com/scylladb/go-set\"\n\ns := set.NewIntSet()\n// use the set...\n```\n\n## Usage\n\nIn every subpackage Set is the main set structure that holds all the data and methods used to working with the set.\n\n#### func  Difference\n\n```go\nfunc Difference(set1 *Set, sets ...*Set) *Set\n```\nDifference returns a new set which contains items which are in in the first set\nbut not in the others.\n\n#### func  Intersection\n\n```go\nfunc Intersection(sets ...*Set) *Set\n```\nIntersection returns a new set which contains items that only exist in all given\nsets.\n\n#### func  New\n\n```go\nfunc New(ts ...T) *Set\n```\nNew creates and initializes a new Set.\n\n#### func  NewWithSize\n\n```go\nfunc NewWithSize(size int) *Set\n```\nNewWithSize creates a new Set and gives make map a size hint.\n\n#### func  SymmetricDifference\n\n```go\nfunc SymmetricDifference(s *Set, t *Set) *Set\n```\nSymmetricDifference returns a new set which s is the difference of items which\nare in one of either, but not in both.\n\n#### func  Union\n\n```go\nfunc Union(sets ...*Set) *Set\n```\nUnion is the merger of multiple sets. It returns a new set with all the elements\npresent in all the sets that are passed.\n\n#### func (*Set) Add\n\n```go\nfunc (s *Set) Add(items ...T)\n```\nAdd includes the specified items (one or more) to the Set. The underlying Set s\nis modified. If passed nothing it silently returns.\n\n#### func (*Set) Clear\n\n```go\nfunc (s *Set) Clear()\n```\nClear removes all items from the Set.\n\n#### func (*Set) Copy\n\n```go\nfunc (s *Set) Copy() *Set\n```\nCopy returns a new Set with a copy of s.\n\n#### func (*Set) Each\n\n```go\nfunc (s *Set) Each(f func(item T) bool)\n```\nEach traverses the items in the Set, calling the provided function for each Set\nmember. Traversal will continue until all items in the Set have been visited, or\nif the closure returns false.\n\n#### func (*Set) Has\n\n```go\nfunc (s *Set) Has(items ...T) bool\n```\nHas looks for the existence of items passed. It returns false if nothing is\npassed. For multiple items it returns true only if all of the items exist.\n\n#### func (*Set) IsEmpty\n\n```go\nfunc (s *Set) IsEmpty() bool\n```\nIsEmpty reports whether the Set is empty.\n\n#### func (*Set) IsEqual\n\n```go\nfunc (s *Set) IsEqual(t *Set) bool\n```\nIsEqual test whether s and t are the same in size and have the same items.\n\n#### func (*Set) IsSubset\n\n```go\nfunc (s *Set) IsSubset(t *Set) bool\n```\nIsSubset tests whether t is a subset of s.\n\n#### func (*Set) IsSuperset\n\n```go\nfunc (s *Set) IsSuperset(t *Set) bool\n```\nIsSuperset tests whether t is a superset of s.\n\n#### func (*Set) List\n\n```go\nfunc (s *Set) List() []T\n```\nList returns a slice of all items. There is also StringSlice() and IntSlice()\nmethods for returning slices of type string or int.\n\n#### func (*Set) Merge\n\n```go\nfunc (s *Set) Merge(t *Set)\n```\nMerge is like Union, however it modifies the current Set it's applied on with\nthe given t Set.\n\n#### func (*Set) Pop\n\n```go\nfunc (s *Set) Pop() T\n```\nPop deletes and returns an item from the Set. The underlying Set s is modified.\nIf Set is empty, the zero value is returned.\n\n#### func (*Set) Pop2\n\n```go\nfunc (s *Set) Pop2() (T, bool)\n```\nPop2 tries to delete and return an item from the Set. The underlying Set s is modified.\nThe second value is a bool that is true if the item existed in the set, and false if not.\nIf Set is empty, the zero value and false are returned.\n\n#### func (*Set) Remove\n\n```go\nfunc (s *Set) Remove(items ...T)\n```\nRemove deletes the specified items from the Set. The underlying Set s is\nmodified. If passed nothing it silently returns.\n\n#### func (*Set) Separate\n\n```go\nfunc (s *Set) Separate(t *Set)\n```\nSeparate removes the Set items containing in t from Set s. Please aware that\nit's not the opposite of Merge.\n\n#### func (*Set) Size\n\n```go\nfunc (s *Set) Size() int\n```\nSize returns the number of items in a Set.\n\n#### func (*Set) String\n\n```go\nfunc (s *Set) String() string\n```\nString returns a string representation of s\n \n## Performance\n\nThe improvement in performance by using concrete types over `interface{}` is notable. Below you will find benchmark results comparing type-safe sets to `fatih/set` counterparts for `string`, `int64`, `int32`, `float64` and `float32` on a local machine, Intel(R) Core(TM) i7-7500U CPU @ 2.70GHz.\n\n```\npkg: github.com/scylladb/go-set/strset\nBenchmarkTypeSafeSetHasNonExisting-4            200000000                7.02 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasNonExisting-4           20000000                60.0 ns/op            32 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExisting-4               200000000                9.02 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExisting-4              20000000                97.0 ns/op            32 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExistingMany-4           100000000               16.8 ns/op             0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExistingMany-4          30000000               106 ns/op              32 B/op          2 allocs/op\nBenchmarkTypeSafeSetAdd-4                        3000000               469 ns/op              58 B/op          0 allocs/op\nBenchmarkInterfaceSetAdd-4                       2000000               909 ns/op             117 B/op          2 allocs/op\n\npkg: github.com/scylladb/go-set/i64set\nBenchmarkTypeSafeSetHasNonExisting-4            300000000                5.51 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasNonExisting-4           30000000                49.4 ns/op            24 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExisting-4               200000000                7.53 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExisting-4              20000000                68.5 ns/op            24 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExistingMany-4           100000000               11.0 ns/op             0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExistingMany-4          20000000                74.5 ns/op            24 B/op          2 allocs/op\nBenchmarkTypeSafeSetAdd-4                       10000000               225 ns/op              40 B/op          0 allocs/op\nBenchmarkInterfaceSetAdd-4                       3000000               403 ns/op              82 B/op          2 allocs/op\n\npkg: github.com/scylladb/go-set/i32set\nBenchmarkTypeSafeSetHasNonExisting-4            300000000                5.61 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasNonExisting-4           30000000                48.8 ns/op            20 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExisting-4               200000000                7.07 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExisting-4              20000000                69.3 ns/op            20 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExistingMany-4           100000000               11.7 ns/op             0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExistingMany-4          20000000                71.1 ns/op            20 B/op          2 allocs/op\nBenchmarkTypeSafeSetAdd-4                       10000000               206 ns/op              25 B/op          0 allocs/op\nBenchmarkInterfaceSetAdd-4                       3000000               394 ns/op              78 B/op          2 allocs/op\n\npkg: github.com/scylladb/go-set/f64set\nBenchmarkTypeSafeSetHasNonExisting-4            300000000                5.82 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasNonExisting-4           30000000                49.8 ns/op            24 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExisting-4               50000000                26.8 ns/op             0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExisting-4              20000000                77.6 ns/op            24 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExistingMany-4           50000000                27.6 ns/op             0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExistingMany-4          20000000                82.3 ns/op            24 B/op          2 allocs/op\nBenchmarkTypeSafeSetAdd-4                       10000000               270 ns/op              40 B/op          0 allocs/op\nBenchmarkInterfaceSetAdd-4                       3000000               428 ns/op              82 B/op          2 allocs/op\n\npkg: github.com/scylladb/go-set/f32set\nBenchmarkTypeSafeSetHasNonExisting-4            300000000                5.78 ns/op            0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasNonExisting-4           30000000                49.3 ns/op            20 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExisting-4               50000000                24.9 ns/op             0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExisting-4              20000000                78.6 ns/op            20 B/op          2 allocs/op\nBenchmarkTypeSafeSetHasExistingMany-4           50000000                25.1 ns/op             0 B/op          0 allocs/op\nBenchmarkInterfaceSetHasExistingMany-4          20000000                81.1 ns/op            20 B/op          2 allocs/op\nBenchmarkTypeSafeSetAdd-4                       10000000               246 ns/op              24 B/op          0 allocs/op\nBenchmarkInterfaceSetAdd-4                       3000000               408 ns/op              78 B/op          2 allocs/op\n```\n\n## Code generation\nFor code generation we use [Google go_generics tool](https://github.com/mmatczuk/go_generics) that we forked to provide bazel-free installation, to install run:\n\n```bash\ngo get -u github.com/mmatczuk/go_generics/cmd/go_generics\n``` \n\nOnce you have `go_generics` installed properly you can regenerate the code using `go generate` in the top level directory.\n\n### Your custom types\n\nIf you have types that you would like to use but the are not amenable for inclusion in this library you can simply generate code on your own and put it in your package.\n\nFor example, to generate a set for `SomeType` in package `sometypeset` call:\n\n```bash\n./gen_set.sh SomeType sometypeset\n```\n\nthis would generate a new directory `sometypeset` in current working directory.\n\nIf you think your addition belongs here we are open to accept pull requests.\n\n## License\n\nCopyright (C) 2018 ScyllaDB\n\nThis project is distributed under the Apache 2.0 license. See the [LICENSE](https://github.com/scylladb/gocqlx/blob/master/LICENSE) file for details.\nIt contains software from:\n\n* [github.com/fatih/set](https://github.com/fatih/set), licensed under the MIT license\n\nGitHub star is always appreciated!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscylladb%2Fgo-set","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fscylladb%2Fgo-set","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fscylladb%2Fgo-set/lists"}