https://github.com/codr7/gollies
Essential Go utilities
https://github.com/codr7/gollies
Last synced: 2 months ago
JSON representation
Essential Go utilities
- Host: GitHub
- URL: https://github.com/codr7/gollies
- Owner: codr7
- Created: 2021-07-13T22:52:46.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-07-19T02:22:12.000Z (over 4 years ago)
- Last Synced: 2024-06-20T12:03:45.579Z (almost 2 years ago)
- Language: Go
- Homepage:
- Size: 17.6 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
## gollies
### order
Ordered collections share the same function signature for comparisons.
```
type Order = int
const (
Lt = Order(-1)
Eq = Order(0)
Gt = Order(1)
)
type Compare = func(x, y interface{}) Order
func CompareInt(x, y interface{}) Order {
xv, yv := x.(int), y.(int)
if xv < yv {
return Lt
}
if xv > yv {
return Gt
}
return Eq
}
```
### maps
```
type Map interface {
Clone() Map
Add(key interface{}, val interface{}) interface{}
Remove(key interface{}) interface{}
Find(key interface{}) interface{}
Each(func (key, val interface {}) bool) bool
Keys() []interface{}
Values() []interface{}
Len() int
AddAll(src Map)
KeepAll(src Map)
RemoveAll(src Map)
Difference(rhs Map) Map
Intersection(rhs Map) Map
Union(rhs Map) Map
}
```
#### hash maps
Hash maps simply wrap the regular native implementation.
```
func NewHashMap() *HashMap
```
#### slice maps
Slice maps are implemented as ordered slices of items, each map may be configured with a custom compare function.
```
func NewSliceMap(cmp Compare) *SliceMap
```
### performance
Slice maps break even with hash maps around 1000 items on my machine, being significantly cheaper to initialize is the main reason they're able to compete at all.
```
$ ./bench
goos: darwin
goarch: amd64
pkg: github.com/codr7/gollies/m/v2
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkSliceMap/SliceMap-12 2482 469806 ns/op
BenchmarkSliceMap/HashMap-12 3806 308998 ns/op
PASS
ok github.com/codr7/gollies/m/v2 2.526s
```