https://github.com/gozeloglu/set
Thread(Safe/Unsafe) Set data structure for Go.
https://github.com/gozeloglu/set
data-structure go golang hash hash-se set thread-safe thread-safety
Last synced: 3 months ago
JSON representation
Thread(Safe/Unsafe) Set data structure for Go.
- Host: GitHub
- URL: https://github.com/gozeloglu/set
- Owner: gozeloglu
- License: mit
- Created: 2022-02-27T20:15:33.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-03-30T19:05:22.000Z (about 3 years ago)
- Last Synced: 2025-01-25T16:22:39.507Z (4 months ago)
- Topics: data-structure, go, golang, hash, hash-se, set, thread-safe, thread-safety
- Language: Go
- Homepage: https://pkg.go.dev/github.com/gozeloglu/set
- Size: 124 KB
- Stars: 3
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# set [](https://pkg.go.dev/github.com/gozeloglu/set) [](https://goreportcard.com/report/github.com/gozeloglu/set) [](http://gocover.io/github.com/gozeloglu/set)
`set` is a data structure package written by **Go**. It provides some basic set functionalities of the user. It uses
`map` data structure under the hood.* Written in vanilla Go with no dependency.
* Supports thread-safety.
* Two different set options. Thread-safe and Thread-unsafe.
* Supports any data type(integer, float, string, byte, and so on).## Installation
```shell
go get github.com/gozeloglu/set
```## Thread Unsafe Set Example
```go
package mainimport (
"fmt"
"github.com/gozeloglu/set"
)func main() {
unsafeSet := set.New(set.ThreadUnsafe)
unsafeSet.Add(123)exist := unsafeSet.Contains(123)
if !exist {
fmt.Println("123 not exist")
}unsafeSet.Append(1, 2, 3, 4, "abc") // Add multiple values
values := []interface{}{"github", 100, 640, 0.43, false}
unsafeSet.Append(values...) // Append the array of elementsunsafeSet.Remove(4)
size := unsafeSet.Size()
fmt.Println(size) // Prints 5unsafeSet.Pop() // Returns random value from the set
unsafeSet.Clear()
fmt.Println(unsafeSet.Size()) // Prints 0
if unsafeSet.Empty() {
fmt.Println("set is empty")
}
}
```## Thread Safe Set Example
```go
package mainimport (
"fmt"
"github.com/gozeloglu/set"
)func main() {
safeSet := set.New(set.ThreadSafe)
safeSet.Append(1, 2, 3, 4) // Add multiple valuesexist := safeSet.Contains(2)
if !exist {
fmt.Println("2 not exist")
}values := []interface{}{"github", 100, 640, 0.43, false}
safeSet.Append(values...) // Append the array of elementssafeSet.Remove(4)
size := safeSet.Size()
fmt.Println(size)safeSet.Pop()
safeSet.Clear()
fmt.Println(safeSet.Size())
if safeSet.Empty() {
fmt.Println("set is empty")
}
}
```## Supported methods
* `Add(val interface{})`
* `Append(val ...interface{})`
* `Remove(val interface{})`
* `Contains(val interface{})`
* `Size()`
* `Pop()`
* `Clear()`
* `Empty()`
* `Slice()`
* `Union()`
* `Intersection()`
* `Difference()`
* `IsSubset()`
* `IsSuperset()`
* `IsDisjoint()`
* `Equal()`
* `SymmetricDifference()`## Tests
You can run the tests with the following command.
```shell
make test # Runs all tests
make test-v # Runs all tests with -v option
make cover # Runs all tests with -cover option. Prints out coverage result
make race # Runs all tests with -race option.
make bench # Runs benchmarks
```## LICENSE
[MIT](https://github.com/gozeloglu/set/blob/main/LICENSE)