Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jabolopes/go-sparseset
Sparse sets with efficient traversal, joining, sorting, etc.
https://github.com/jabolopes/go-sparseset
data-structures generics go golang golang-library sets sparse-sets utility
Last synced: 27 days ago
JSON representation
Sparse sets with efficient traversal, joining, sorting, etc.
- Host: GitHub
- URL: https://github.com/jabolopes/go-sparseset
- Owner: jabolopes
- License: bsd-3-clause
- Created: 2022-08-20T16:51:32.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-07-29T18:54:43.000Z (6 months ago)
- Last Synced: 2024-07-30T01:08:59.605Z (6 months ago)
- Topics: data-structures, generics, go, golang, golang-library, sets, sparse-sets, utility
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-sparseset
[![PkgGoDev](https://pkg.go.dev/badge/github.com/jabolopes/go-sparseset)](https://pkg.go.dev/github.com/jabolopes/go-sparseset)
This library provides an implementation of [sparse
sets](https://dl.acm.org/doi/10.1145/176454.176484) and utilities to operate on
sparse sets, including, efficient traversal, joining, sorting, etc.Values are stored contiguously in memory therefore traversing a set is
particularly fast because it takes advantage of the underlying CPU caching.```go
// Construction
set := sparseset.New[string](4096, 1<<20)
*set.Add(id) = value
...// Properties.
length := set.Length()// Removal.
set.Remove(id)// Lookup.
if value, ok := set.Get(id); ok {
// Do something with value...
} else {
// Set does not contain id...
}// Traversal.
for iterator := sparseset.Iterate(set); ; {
key, value, ok := iterator.Next()
if !ok {
break
}// Do something with key and value...
}// Joining 2 sets.
for iterator := sparseset.Join(set1, set2); ; {
key, value1, value2, ok := iterator.Next()
if !ok {
break
}// Do something with key, value1, and value2...
}// Joining 3 sets.
for iterator := sparseset.Join3(set1, set2, set3); ; {
key, value1, value2, value3, ok := iterator.Next()
if !ok {
break
}// Do something with key, value1, value2, and value3...
}
```