{"id":13411678,"url":"https://github.com/zoumo/goset","last_synced_at":"2025-04-08T15:31:39.000Z","repository":{"id":56433170,"uuid":"101388126","full_name":"zoumo/goset","owner":"zoumo","description":"Set is a useful collection but there is no built-in implementation in Go lang.","archived":false,"fork":false,"pushed_at":"2020-12-11T10:18:54.000Z","size":30,"stargazers_count":52,"open_issues_count":0,"forks_count":16,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-10-31T16:12:07.290Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zoumo.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":"2017-08-25T09:21:30.000Z","updated_at":"2024-01-10T14:02:49.000Z","dependencies_parsed_at":"2022-08-15T18:40:16.920Z","dependency_job_id":null,"html_url":"https://github.com/zoumo/goset","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoumo%2Fgoset","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoumo%2Fgoset/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoumo%2Fgoset/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zoumo%2Fgoset/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zoumo","download_url":"https://codeload.github.com/zoumo/goset/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247871209,"owners_count":21010006,"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":[],"created_at":"2024-07-30T20:01:15.683Z","updated_at":"2025-04-08T15:31:38.726Z","avatar_url":"https://github.com/zoumo.png","language":"Go","funding_links":[],"categories":["数据结构与算法","Data Structures","Data Structures and Algorithms","Uncategorized","数据结构","Data Integration Frameworks","Generators","\u003cspan id=\"数据结构-data-structures\"\u003e数据结构 Data Structures\u003c/span\u003e","数据结构`go语言实现的数据结构与算法`"],"sub_categories":["集","Advanced Console UIs","Standard CLI","Sets","Uncategorized","\u003cspan id=\"高级控制台用户界面-advanced-console-uis\"\u003e高级控制台用户界面 Advanced Console UIs\u003c/span\u003e","标准 CLI"],"readme":"# goset\n[![Go Report Card](https://goreportcard.com/badge/github.com/zoumo/goset)](https://goreportcard.com/report/github.com/zoumo/goset)\n[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](https://godoc.org/github.com/zoumo/goset)\n[![Coverage Status](https://coveralls.io/repos/github/zoumo/goset/badge.svg?branch=master)](https://coveralls.io/github/zoumo/goset?branch=master)\n[![Build Status](https://travis-ci.org/zoumo/goset.svg?branch=master)](https://travis-ci.org/zoumo/goset)\n\n\nSet is a useful collection but there is no built-in implementation in Go lang.\n\n## Why?\n\nThe only one pkg which provides set operations now is [golang-set](https://github.com/deckarep/golang-set)\n\nUnfortunately, the api of golang-set is not good enough.\n\nFor example, I want to generate a set from a int slice\n\n```go\nimport \"github.com/deckarep/golang-set\"\n\nfunc main() {\n\tints := []int{1, 2, 3, 4}\n\tmapset.NewSet(ints...)\n\tmapset.NewSetFromSlice(ints)\n\tmapset.NewSetWith(ints...)\n}\n```\n\nthe code above can not work, according to\n\n\u003e    cannot use ints (type []int) as type []interface{}\n\nYou can not assign any slice to an `[]interface{}`  in Go lang.\n\n\u003e   https://github.com/golang/go/wiki/InterfaceSlice\n\nSo you need to copy your elements from `[]int` to `[]interface` by a loop.\n\nThat means you must do this manually every time you want to generate a set from slice.\n\n**It is ugly. So I create my own set**\n\n## Usage\n\n```go\nimport \"github.com/zoumo/goset\"\n\nfunc main() {\n\tgoset.NewSet(1, 2, 3, 4)\n\t// or\n\tgoset.NewSetFrom([]int{1, 2, 3, 4})\n\n\tgoset.NewSet(\"1\", \"2\", \"3\")\n\t// or\n\tgoset.NewSetFrom([]string{\"1\", \"2\", \"3\"})\n}\n```\n\nFull API\n\n```go\n// Set provides a collection of operations for sets\n//\n// The implementation of Set is base on hash table. So the elements must be\n// hashable, functions, maps, slices are unhashable type, adding these elements\n// will cause panic.\n//\n// There are two implementations of Set:\n// 1. default is unsafe based on hash table(map)\n// 2. thread safe based on sync.RWMutex\n//\n// The two kinds of sets can easily convert to the other one. But you must know\n// exactly what you are doing to avoid the concurrent race\ntype Set interface {\n\tSetToSlice\n\t// Add adds all given elements to the set anyway, no matter if it whether already exists.\n\t//\n\tAdd(elem ...interface{}) error\n\n\t// Extend adds all elements in the given interface b to this set\n\t// the given interface must be array, slice or Set.\n\tExtend(b interface{}) error\n\n\t// Remove deletes all given elements from the set.\n\tRemove(elem ...interface{})\n\n\t// Contains checks whether the given elem is in the set.\n\tContains(elem interface{}) bool\n\n\t// ContainsAll checks whether all the given elems are in the set.\n\tContainsAll(elems ...interface{}) bool\n\n\tContainsAny(elems ...interface{}) bool\n\n\t// Copy clones the set.\n\tCopy() Set\n\n\t// Len returns the size of set. aka Cardinality.\n\tLen() int\n\n\t// String returns the string representation of the set.\n\tString() string\n\n\t// Range calls f sequentially for each element present in the set.\n\t// If f returns false, range stops the iteration.\n\t//\n\t// Note: the iteration order is not specified and is not guaranteed\n\t// to be the same from one iteration to the next. The index only\n\t// means how many elements have been visited in the iteration, it not\n\t// specifies the index of an element in the set\n\tRange(foreach func(index int, elem interface{}) bool)\n\n\t// ---------------------------------------------------------------------\n\t// Convert\n\n\t// ToThreadUnsafe returns a thread unsafe set.\n\t// Carefully use the method.\n\tToThreadUnsafe() Set\n\n\t// ToThreadSafe returns a thread safe set.\n\t// Carefully use the method.\n\tToThreadSafe() Set\n\n\t// ---------------------------------------------------------------------\n\t// Compare\n\n\t// Equal checks whether this set is equal to the given one.\n\t// There are two constraints if set a is equal to set b.\n\t// the two set must have the same size and contain the same elements.\n\tEqual(b Set) bool\n\n\t// IsSubsetOf checks whether this set is the subset of the given set\n\t// In other words, all elements in this set are also the elements\n\t// of the given set.\n\tIsSubsetOf(b Set) bool\n\n\t// IsSupersetOf checks whether this set is the superset of the given set\n\t// In other words, all elements in the given set are also the elements\n\t// of this set.\n\tIsSupersetOf(b Set) bool\n\n\t// ---------------------------------------------------------------------\n\t// Set Operations\n\n\t// Diff returns the difference between the set and this given\n\t// one, aka Difference Set\n\t// math formula: a - b\n\tDiff(b Set) Set\n\n\t// SymmetricDiff returns the symmetric difference between this set\n\t// and the given one. aka Symmetric Difference Set\n\t// math formula: (a - b) ∪ (b - a)\n\tSymmetricDiff(b Set) Set\n\n\t// Unite combines two sets into a new one, aka Union Set\n\t// math formula: a ∪ b\n\tUnite(b Set) Set\n\n\t// Intersect returns the intersection of two set, aka Intersection Set\n\t// math formula: a ∩ b\n\tIntersect(b Set) Set\n}\n\n// SetToSlice contains methods that knows how to convert set to slice.\ntype SetToSlice interface {\n\t// ToStrings returns all string elements in this set.\n\tToStrings() []string\n\t// ToInts returns all int elements in this set.\n\tToInts() []int\n\t// Elements returns all elements in this set.\n\tElements() []interface{}\n}\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoumo%2Fgoset","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzoumo%2Fgoset","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzoumo%2Fgoset/lists"}