An open API service indexing awesome lists of open source software.

https://github.com/oniani/set

A generic set implementation for the Go programming language
https://github.com/oniani/set

Last synced: 11 months ago
JSON representation

A generic set implementation for the Go programming language

Awesome Lists containing this project

README

          

# set

A generic set implementation for the Go programming language.

The implementation makes use of the built-in `map` data structure.

**TODO**:

- Add tests
- Add docs

## API

```go
import "github.com/oniani/set"

func main() {
// Two ways to create and populate a set
s := Set.New("Apple", "Banana", "Cherry")
t := Set.New[string]()
t.Add("Raspberry")
t.Add("Strawberry")
t.Add("Apple")

// Check the results
fmt.Println(s, s.Len())
fmt.Println(t, t.Len())

// Union, intersection, and difference
m := s.Union(t)
n := s.Intersection(t)
o := s.Difference(t)

// Check the Results
fmt.Println(m, m.Len())
fmt.Println(n, n.Len())
fmt.Println(o, o.Len())

sHasApple, sHasCar = s.Contains("Apple"), s.Contains("Car")
sHasAll1, sHasAll2 = s.All("Car", "Laptop", "Popcorn"), s.All("Car", "Laptop", "Apple")
sHasAny1, sHasAny2 = m.Any("Car", "Laptop", "Popcorn"), m.Any("Car", "Laptop", "Apple")

// Check the results
fmt.Println(sHasApple, sHasCar)
fmt.Println(sHasAll1, sHasAll2)
fmt.Println(s_has_any1, sHasAny2)

// Clone, remove, and clear
p := s.Clone()
p.Remove("Apple")
s.Clear()

// Check the results
fmt.Println(s, s.Len())
fmt.Println(p, p.Len())
}
```

## Testing

- Unit Tests: Done (partially)

```console
$ go test -v
```

- Fuzz Tests: Done (partially)

```console
$ go test -v -fuzz FuzzAddRemoveString -fuzztime 16s
```

## License

[MIT License][license]

[godoc]: https://pkg.go.dev/golang.org/x/tools/cmd/godoc
[license]: LICENSE