1
- # Heap structure, using go generics
2
- [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/lispad/go-generics-tools )] ( https://goreportcard.com/report/github.com/lispad/go-generics-tools )
1
+ # GoLang Generics tools: Heap structure, sharded rw-locked map.
2
+ [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/lispad/go-generics-tools )] ( https://goreportcard.com/report/github.com/lispad/go-generics-tools )
3
3
[ ![ MIT License] ( https://img.shields.io/badge/License-MIT-blue.svg )] ( LICENSE )
4
4
5
5
Introduction
6
6
------------
7
7
8
- The Heap package contains simple [ binary heap] ( https://en.wikipedia.org/wiki/Binary_heap ) implementation, using Golang
8
+ The [ Heap] ( binheap/README.md ) package contains simple [ binary heap] ( https://en.wikipedia.org/wiki/Binary_heap ) implementation, using Golang
9
9
generics. There are several heap implementations
10
+ [ Details] ( binheap/README.md ) .
10
11
11
- - generic Heap implementation, that could be used for ` any ` type,
12
- - ` ComparableHeap ` for [ comparable] ( https://go.dev/ref/spec#Comparison_operators ) types. Additional ` Search `
13
- and ` Delete ` are implemented,
14
- - for [ ` constraints.Ordered ` ] ( https://pkg.go.dev/golang.org/x/exp/constraints#Ordered ) there are
15
- constructors for min, max heaps;
16
12
17
- Also use-cases provided:
18
-
19
- - ` TopN ` that allows getting N top elements from slice.
20
- ` TopN ` swaps top N elements to first N elements of slice, no additional allocations are done. All slice elements are
21
- kept, only order is changed.
22
- - ` TopNHeap ` allows to get N top, pushing elements from stream without allocation slice for all elements. Only O(N)
23
- memory is used.
24
- - ` TopNImmutable ` allocated new slice for heap, input slice is not mutated.
25
-
26
- Both TopN and TopNImmutable has methods for creating min and max tops for ` constraints.Ordered ` .
27
-
28
- Usage Example
29
- -----------------
30
-
31
- package main
32
-
33
- import (
34
- "fmt"
35
-
36
- "github.com/lispad/go-generics-tools/binheap"
37
- )
38
-
39
- func main() {
40
- someData := []float64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
41
- mins := binheap.MinN[float64](someData, 3)
42
- fmt.Printf("--- top 3 min elements: %v\n", mins)
43
- maxs := binheap.MaxN[float64](someData, 3)
44
- fmt.Printf("--- top 3 max elements: %v\n\n", maxs)
45
-
46
- heap := binheap.EmptyMaxHeap[string]()
47
- heap.Push("foo")
48
- heap.Push("zzz")
49
- heap.Push("bar")
50
- heap.Push("baz")
51
- heap.Push("foobar")
52
- heap.Push("foobaz")
53
- fmt.Printf("--- heap has %d elements, max element:\n%s\n\n", heap.Len(), heap.Peak())
54
- }
55
-
56
- A bit more examples could be found in ` examples ` directory
57
-
58
- Benchmark
59
- -----------------
60
- Theoretical complexity for getting TopN from slice with size M, N <= M: O(N* ln(M)). When N << M, the heap-based TopN
61
- could be much faster than sorting slice and getting top. E.g. For top-3 from 10k elements approach is ln(10^5)/ln(3) ~ =
62
- 8.38 times faster.
63
-
64
- #### Benchmark
65
-
66
- BenchmarkSortedMaxN-8 10303648 136.0 ns/op 0 B/op 0 allocs/op
67
- BenchmarkMaxNImmutable-8 398996316 3.029 ns/op 0 B/op 0 allocs/op
68
- BenchmarkMaxN-8 804041455 1.819 ns/op 0 B/op 0 allocs/op
13
+ The [ ShardedLockMap] ( smap/README.md ) package contains implementation of sharded lock map.
14
+ Interface is similar to sync.map, but sharded lock map is faster on scenarios with huge read load with rare updates,
15
+ and uses less memory, doing less allocations.
16
+ [ Details] ( smap/README.md )
69
17
70
18
Compatibility
71
19
-------------
@@ -77,6 +25,10 @@ Installation
77
25
To install package, run:
78
26
79
27
go get github.com/lispad/go-generics-tools/binheap
28
+ or
29
+
30
+ go get github.com/lispad/go-generics-tools/smap
31
+
80
32
81
33
License
82
34
-------
0 commit comments