{"id":13643621,"url":"https://github.com/hashicorp/go-set","last_synced_at":"2025-05-15T14:06:48.177Z","repository":{"id":46941387,"uuid":"515316882","full_name":"hashicorp/go-set","owner":"hashicorp","description":"The go-set package provides generic Set implementations for Go, including HashSet for types with a Hash() function and TreeSet for orderable data","archived":false,"fork":false,"pushed_at":"2025-03-03T08:20:17.000Z","size":191,"stargazers_count":140,"open_issues_count":1,"forks_count":11,"subscribers_count":14,"default_branch":"main","last_synced_at":"2025-04-18T07:35:21.091Z","etag":null,"topics":["generics","go","golang","golang-library","hashset","red-black-tree","set","treeset"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hashicorp.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":"CODEOWNERS","security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-07-18T19:33:51.000Z","updated_at":"2025-04-03T02:37:27.000Z","dependencies_parsed_at":"2023-10-13T04:27:25.982Z","dependency_job_id":"efb449f1-52ab-4370-a955-6a7dd1e3a44a","html_url":"https://github.com/hashicorp/go-set","commit_stats":{"total_commits":113,"total_committers":11,"mean_commits":"10.272727272727273","dds":0.3097345132743363,"last_synced_commit":"7aa83ecf17ede61362eae7ee3fe7bd7ea9b6c5f3"},"previous_names":[],"tags_count":21,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-set","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-set/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-set/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hashicorp%2Fgo-set/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hashicorp","download_url":"https://codeload.github.com/hashicorp/go-set/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254355335,"owners_count":22057354,"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":["generics","go","golang","golang-library","hashset","red-black-tree","set","treeset"],"created_at":"2024-08-02T01:01:50.301Z","updated_at":"2025-05-15T14:06:43.137Z","avatar_url":"https://github.com/hashicorp.png","language":"Go","readme":"# go-set\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/hashicorp/go-set.svg)](https://pkg.go.dev/github.com/hashicorp/go-set/v3)\n[![Run CI Tests](https://github.com/hashicorp/go-set/actions/workflows/ci.yaml/badge.svg)](https://github.com/hashicorp/go-set/actions/workflows/ci.yaml)\n[![GitHub](https://img.shields.io/github/license/hashicorp/go-set)](LICENSE)\n\nThe `go-set` repository provides a `set` package containing a few\ngeneric [Set](https://en.wikipedia.org/wiki/Set) implementations for Go.\n\n---\n\n**PSA** August 2024 - The **v3** version of this package has been published,\nstarting at tag version `v3.0.0`. A description of the changes including\nbackwards incompatibilities can be found in https://github.com/hashicorp/go-set/issues/90\n\n_Requires `go1.23` or later._\n\n---\n\n**PSA** October 2023 - The **v2** version of this package has been published,\nstarting at tag version `v2.1.0`. A description of the changes including\nbackwards incompatibilities can be found in https://github.com/hashicorp/go-set/issues/73\n\n---\n\nEach implementation is optimal for a particular use case.\n\n**Set[T]** is ideal for `comparable` types.\n  - backed by `map` builtin\n  - commonly used with `string`, `int`, simple `struct` types, etc.\n\n**HashSet[T]** is useful for types that implement a `Hash()` function.\n  - backed by `map` builtin\n  - commonly used with complex structs\n  - also works with custom `HashFunc[T]` implementations\n\n**TreeSet[T]** is useful for comparable data (via `CompareFunc[T]`)\n  - backed by Red-Black Binary Search Tree\n  - commonly used with complex structs with extrinsic order\n  - efficient iteration in sort order\n  - additional methods `Min` / `Max` / `TopK` / `BottomK`\n\nThis package is not thread-safe.\n\n---\n\n# Documentation\n\nThe full `go-set` package reference is available on [pkg.go.dev](https://pkg.go.dev/github.com/hashicorp/go-set/v3).\n\n# Install\n\n```shell\ngo get github.com/hashicorp/go-set/v3@latest\n```\n\n```shell\nimport \"github.com/hashicorp/go-set/v3\"\n```\n\n# Motivation\n\nPackage `set` helps reduce the boiler plate of using a `map[\u003ctype\u003e]struct{}` as a set.\n\nSay we want to de-duplicate a slice of strings\n```go\nitems := []string{\"mitchell\", \"armon\", \"jack\", \"dave\", \"armon\", \"dave\"}\n```\n\nA typical example of the classic way using `map` built-in:\n```go\nm := make(map[string]struct{})\nfor _, item := range items {\n  m[item] = struct{}{}\n}\nlist := make([]string, 0, len(items))\nfor k := range m {\n  list = append(list, k)\n}\n```\n\nThe same result, but in one line using package `go-set`.\n```go\nlist := set.From[string](items).Slice()\n```\n\n# Set\n\nThe `go-set` package includes `Set` for types that satisfy the `comparable` constraint.\nUniqueness of a set elements is guaranteed via shallow comparison (result of == operator).\n\nNote: if pointers or structs with pointer fields are stored in the `Set`, they will\nbe compared in the sense of pointer addresses, not in the sense of referenced values.\nDue to this fact the `Set` type is recommended to be used with builtin types like\n`string`, `int`, or simple struct types with no pointers. `Set` usage with pointers or \nstructs with pointer is also possible if shallow equality is acceptable.\n\n# HashSet\n\nThe `go-set` package includes `HashSet` for types that implement a `Hash()` function.\nThe custom type must satisfy `HashFunc[H Hash]` - essentially any `Hash()` function\nthat returns a `string` or `integer`. This enables types to use string-y hash\nfunctions like `md5`, `sha1`, or even `GoString()`, but also enables types to\nimplement an efficient hash function using a hash code based on prime multiples.\n\n# TreeSet\n\nThe `go-set` package includes `TreeSet` for creating sorted sets. A `TreeSet` may\nbe used with any type `T` as the comparison between elements is provided by implementing\n`CompareFunc[T]`. The standard library `cmp.Compare` function provides a convenient\nimplementation of `CompareFunc` for `cmp.Ordered` types like `string` or `int`. A\n`TreeSet` is backed by an underlying balanced binary search tree, making operations\nlike in-order traversal efficient, in addition to enabling functions like `Min()`,\n`Max()`, `TopK()`, and `BottomK()`.\n\n# Collection[T]\n\nThe `Collection[T]` interface is implemented by each of `Set`, `HashSet`, and `TreeSet`.\n\nIt serves as a useful abstraction over the common methods implemented by each set type.\n\n### Iteration\n\nStarting with `v3` each of `Set`, `HashSet`, and `TreeSet` implement an `Items`\nmethod. It can be used with the `range` keyword for iterating through each \nelement in the set.\n\n```go\n// e.g. print each element in the set\nfor _, item := range s.Items() {\n  fmt.Println(item)\n}\n```\n\n# Set Examples\n\nBelow are simple example usages of `Set`\n\n```go\ns := set.New[int](10)\ns.Insert(1)\ns.InsertSlice([]int{2, 3, 4})\ns.Size()\n```\n\n```go\ns := set.From[string]([]string{\"one\", \"two\", \"three\"})\ns.Contains(\"three\")\ns.Remove(\"one\")\n```\n\n\n```go\na := set.From[int]([]int{2, 4, 6, 8})\nb := set.From[int]([]int{4, 5, 6})\na.Intersect(b)\n```\n\n# HashSet Examples\n\nBelow are simple example usages of `HashSet`\n\n(using a hash code)\n```go\ntype inventory struct {\n    item   int\n    serial int\n}\n\nfunc (i *inventory) Hash() int {\n    code := 3 * item * 5 * serial\n    return code\n}\n\ni1 := \u0026inventory{item: 42, serial: 101}\n\ns := set.NewHashSet[*inventory, int](10)\ns.Insert(i1)\n```\n\n(using a string hash)\n```go\ntype employee struct {\n    name string\n    id   int\n}\n\nfunc (e *employee) Hash() string {\n    return fmt.Sprintf(\"%s:%d\", e.name, e.id)\n}\n\ne1 := \u0026employee{name: \"armon\", id: 2}\n\ns := set.NewHashSet[*employee, string](10)\ns.Insert(e1)\n```\n\n# TreeSet Examples\n\nBelow are simple example usages of `TreeSet`\n\n```go\nts := NewTreeSet[int](Compare[int])\nts.Insert(5)\n```\n\n```go\ntype waypoint struct {\n    distance int\n    name     string\n}\n\n// compare implements CompareFunc\ncompare := func(w1, w2 *waypoint) int {\n    return w1.distance - w2.distance\n}\n\nts := NewTreeSet[*waypoint](compare)\nts.Insert(\u0026waypoint{distance: 42, name: \"tango\"})\nts.Insert(\u0026waypoint{distance: 13, name: \"alpha\"})\nts.Insert(\u0026waypoint{distance: 71, name: \"xray\"})\n```\n\n","funding_links":[],"categories":["Data structures","Go"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fgo-set","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhashicorp%2Fgo-set","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhashicorp%2Fgo-set/lists"}