1
- # sortedmap
1
+ # 📚 sortedmap
2
2
3
3
` sortedmap ` provides an effective sorted map implementation for Go.
4
- Below you will find information about the repository, its usage, and the API with complexity details.
4
+ It uses a heap to maintain order and iterators under the hood.
5
+
6
+ ---
7
+
8
+ [ ![ Build Status] ( https://github.com/egregors/sortedmap/workflows/build/badge.svg )] ( https://github.com/egregors/sortedmap/actions )
9
+ [ ![ Go Report Card] ( https://goreportcard.com/badge/github.com/egregors/sortedmap )] ( https://goreportcard.com/report/github.com/egregors/sortedmap )
10
+ [ ![ Coverage Status] ( https://coveralls.io/repos/github/egregors/sortedmap/badge.svg?branch=main )] ( https://coveralls.io/github/egregors/sortedmap?branch=main )
11
+ [ ![ godoc] ( https://godoc.org/github.com/egregors/sortedmap?status.svg )] ( https://godoc.org/github.com/egregors/sortedmap )
12
+
5
13
6
14
## Features
7
15
8
16
* 🚀 Efficient sorted map implementation
9
17
* 🔧 Customizable sorting by key or value
18
+ * 🐈 Zero dependencies
19
+ * 📦 Easy to use API (inspired by the stdlib ` maps ` and ` slices ` packages)
10
20
11
21
## Installation
12
22
@@ -26,39 +36,100 @@ package main
26
36
import (
27
37
" fmt"
28
38
29
- " github.com/egregors/sortedmap"
39
+ sm " github.com/egregors/sortedmap"
30
40
)
31
41
42
+ type Person struct {
43
+ Name string
44
+ Age int
45
+ }
46
+
32
47
func main () {
33
- sm := sortedmap.NewFromMap (map [string ]int {
34
- " Bob" : 42 ,
35
- " Alice" : 30 ,
36
- " Charlie" : 25 ,
37
- }, func (i, j sortedmap.KV [string , int ]) bool {
38
- return i.key < j.key
48
+ // Create a new map sorted by keys
49
+ m := sm.NewFromMap (map [string ]int {
50
+ " Bob" : 31 ,
51
+ " Alice" : 26 ,
52
+ " Eve" : 84 ,
53
+ }, func (i, j sm.KV [string , int ]) bool {
54
+ return i.Key < j.Key
55
+ })
56
+
57
+ fmt.Println (m.Collect ())
58
+ // Output: map[Alice:26 Bob:31 Eve:84]
59
+
60
+ m.Insert (" Charlie" , 34 )
61
+ fmt.Println (m.Collect ())
62
+ // Output: map[Alice:26 Bob:31 Charlie:34 Eve:84]
63
+
64
+ m.Delete (" Bob" )
65
+ fmt.Println (m.Collect ())
66
+ // Output: map[Alice:26 Charlie:34 Eve:84]
67
+
68
+ // Create a new map sorted by values
69
+ m2 := sm.NewFromMap (map [string ]Person{
70
+ " Bob" : {" Bob" , 31 },
71
+ " Alice" : {" Alice" , 26 },
72
+ " Eve" : {" Eve" , 84 },
73
+ }, func (i, j sm.KV [string , Person]) bool {
74
+ return i.Val .Age < j.Val .Age
75
+ })
76
+
77
+ fmt.Println (m2.Collect ())
78
+ // Output: map[Alice:{Alice 26} Bob:{Bob 31} Eve:{Eve 84}]
79
+
80
+ // Create a new map sorted by values but if the values are equal, sort by keys
81
+ m3 := sm.NewFromMap (map [string ]Person{
82
+ " Bob" : {" Bob" , 26 },
83
+ " Alice" : {" Alice" , 26 },
84
+ " Eve" : {" Eve" , 84 },
85
+ }, func (i, j sm.KV [string , Person]) bool {
86
+ if i.Val .Age == j.Val .Age {
87
+ return i.Key < j.Key
88
+ }
89
+
90
+ return i.Val .Age < j.Val .Age
39
91
})
40
92
41
- fmt.Println (sm.Collect ())
93
+ fmt.Println (m3.Collect ())
94
+ // Output: map[Alice:{Alice 26} Bob:{Bob 26} Eve:{Eve 84}]
42
95
}
96
+
43
97
```
44
98
45
99
## API and Complexity
46
100
47
- | Method | Description | Complexity |
48
- | --------------| -------------------------------------------------------------------------| ------------|
49
- | ` New ` | Creates a new ` SortedMap ` with a custom comparison function | O(1) |
50
- | ` NewFromMap ` | Creates a new ` SortedMap ` from an existing map with a custom comparison | O(n log n) |
51
- | ` Get ` | Retrieves the value associated with a key | O(1) |
52
- | ` Delete ` | Removes a key-value pair from the map | O(n) |
53
- | ` All ` | Returns a sequence of all key-value pairs in the map | O(n log n) |
54
- | ` Keys ` | Returns a sequence of all keys in the map | O(n log n) |
55
- | ` Values ` | Returns a sequence of all values in the map | O(n log n) |
56
- | ` Insert ` | Adds or updates a key-value pair in the map | O(log n) |
57
- | ` Collect ` | Returns a map with the same contents as the ` SortedMap ` | O(n log n) |
58
-
59
- ## Contributing
60
-
61
- We welcome contributions! Please see the ` CONTRIBUTING.md ` file for guidelines on how to contribute to this project.
101
+ | Method | Description | Complexity |
102
+ | --------------| ------------------------------------------------------------------| ------------|
103
+ | ` New ` | Creates a new ` SortedMap ` with a comparison function | O(1) |
104
+ | ` NewFromMap ` | Creates a new ` SortedMap ` from an existing map with a comparison | O(n log n) |
105
+ | ` Get ` | Retrieves the value associated with a key | O(1) |
106
+ | ` Delete ` | Removes a key-value pair from the map | O(n) |
107
+ | ` All ` | Returns a sequence of all key-value pairs in the map | O(n log n) |
108
+ | ` Keys ` | Returns a sequence of all keys in the map | O(n log n) |
109
+ | ` Values ` | Returns a sequence of all values in the map | O(n log n) |
110
+ | ` Insert ` | Adds or updates a key-value pair in the map | O(log n) |
111
+ | ` Collect ` | Returns a map with the same contents as the ` SortedMap ` | O(n log n) |
112
+
113
+ ## Benchmarks
114
+
115
+ ``` shell
116
+ goos: darwin
117
+ goarch: arm64
118
+ pkg: github.com/egregors/sortedmap
119
+ cpu: Apple M1 Max
120
+ BenchmarkNew-10 165633163 7.143 ns/op
121
+ BenchmarkNewFromMap-10 406633 2806 ns/op
122
+ BenchmarkSortedMap_Get-10 154206174 7.849 ns/op
123
+ BenchmarkSortedMap_All-10 1000000000 0.3153 ns/op
124
+ BenchmarkSortedMap_Collect-10 627693 1929 ns/op
125
+ BenchmarkSortedMap_Keys-10 1000000000 0.3150 ns/op
126
+ BenchmarkSortedMap_Values-10 1000000000 0.3233 ns/op
127
+ BenchmarkSortedMap_Insert-10 6625201 182.0 ns/op
128
+ BenchmarkSortedMap_Delete-10 3301149 373.4 ns/op
129
+ PASS
130
+
131
+ ```
132
+
62
133
63
134
## License
64
135
0 commit comments