Skip to content

Commit 733d0d1

Browse files
authored
Merge pull request #3 from egregors/#2_CollectKeys-CollectValues
Add some Collect# methods and examples, #2 solved
2 parents e672041 + db61eb2 commit 733d0d1

File tree

4 files changed

+455
-64
lines changed

4 files changed

+455
-64
lines changed

CHANGES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
Changes
22
=======
33

4+
#### ver.: 0.3.0 (06.02.2025)
5+
6+
* 🛠️ Add `CollectAll()` method – get slice of key-value pairs
7+
* 🛠️ Add `CollectKeys()` method – get slice of keys
8+
* 🛠️ Add `CollectValues()` method – get slice of values
9+
* 📚 Add examples for all methods
10+
411
#### ver.: 0.2.0 (30.01.2025)
512

613
* 🛠️ Add `Len()` method in order to get len of the map by O(1)

README.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -97,37 +97,38 @@ func main() {
9797

9898
## API and Complexity
9999

100-
| Method | Description | Complexity |
101-
|--------------|------------------------------------------------------------------|------------|
102-
| `New` | Creates a new `SortedMap` with a comparison function | O(1) |
103-
| `NewFromMap` | Creates a new `SortedMap` from an existing map with a comparison | O(n log n) |
104-
| `Get` | Retrieves the value associated with a key | O(1) |
105-
| `Delete` | Removes a key-value pair from the map | O(n) |
106-
| `All` | Returns a sequence of all key-value pairs in the map | O(n log n) |
107-
| `Keys` | Returns a sequence of all keys in the map | O(n log n) |
108-
| `Values` | Returns a sequence of all values in the map | O(n log n) |
109-
| `Insert` | Adds or updates a key-value pair in the map | O(log n) |
110-
| `Collect` | Returns a map with the same contents as the `SortedMap` | O(n log n) |
111-
| `Len` | Returns length of underlying map | O(1) |
100+
| Method | Description | Complexity |
101+
|-----------------|----------------------------------------------------------------------|------------|
102+
| `New` | Creates a new `SortedMap` with a comparison function | O(1) |
103+
| `NewFromMap` | Creates a new `SortedMap` from an existing map with a comparison | O(n log n) |
104+
| `Get` | Retrieves the value associated with a key | O(1) |
105+
| `Delete` | Removes a key-value pair from the map | O(n) |
106+
| `All` | Returns a sequence of all key-value pairs in the map | O(n log n) |
107+
| `Keys` | Returns a sequence of all keys in the map | O(n log n) |
108+
| `Values` | Returns a sequence of all values in the map | O(n log n) |
109+
| `Insert` | Adds or updates a key-value pair in the map | O(log n) |
110+
| `Collect` | Returns a regular map with an *unordered* content off the SortedMap | O(n log n) |
111+
| `CollectAll` | Returns a slice of key-value pairs | O(n log n) |
112+
| `CollectKeys` | Returns a slice of the map’s keys | O(n log n) |
113+
| `CollectValues` | Returns a slice of the map's values | O(n log n) |
114+
| `Len` | Returns length of underlying map | O(1) |
112115

113116
## Benchmarks
114117

115118
```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-
119+
BenchmarkNew-10 165887913 7.037 ns/op
120+
BenchmarkNewFromMap-10 419106 2716 ns/op
121+
BenchmarkSortedMap_Get-10 191580795 5.327 ns/op
122+
BenchmarkSortedMap_Delete-10 3328420 365.0 ns/op
123+
BenchmarkSortedMap_All-10 1000000000 0.3116 ns/op
124+
BenchmarkSortedMap_Keys-10 1000000000 0.3118 ns/op
125+
BenchmarkSortedMap_Values-10 1000000000 0.3117 ns/op
126+
BenchmarkSortedMap_Insert-10 6665839 182.5 ns/op
127+
BenchmarkSortedMap_Collect-10 649450 1835 ns/op
128+
BenchmarkSortedMap_CollectAll-10 1237276 972.4 ns/op
129+
BenchmarkSortedMap_CollectKeys-10 1250041 964.9 ns/op
130+
BenchmarkSortedMap_CollectValues-10 1294760 927.7 ns/op
131+
BenchmarkSortedMap_Len-10 1000000000 0.3176 ns/op
131132
```
132133

133134
## License

sortedmap.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (sm *SortedMap[Map, K, V]) Delete(key K) (val *V, existed bool) {
6464
return (*V)(nil), false
6565
}
6666

67-
// All returns a sequence of key-value pairs in the map
67+
// All returns a sequence of key-value pairs
6868
func (sm *SortedMap[Map, K, V]) All() iter.Seq2[K, V] {
6969
return func(yield func(K, V) bool) {
7070
tempHeap := *sm.h
@@ -77,7 +77,7 @@ func (sm *SortedMap[Map, K, V]) All() iter.Seq2[K, V] {
7777
}
7878
}
7979

80-
// Keys returns a sequence of keys in the map
80+
// Keys returns a sequence of keys
8181
func (sm *SortedMap[Map, K, V]) Keys() iter.Seq[K] {
8282
return func(yield func(K) bool) {
8383
tempHeap := *sm.h
@@ -90,7 +90,7 @@ func (sm *SortedMap[Map, K, V]) Keys() iter.Seq[K] {
9090
}
9191
}
9292

93-
// Values returns a sequence of values in the map
93+
// Values returns a sequence of values
9494
func (sm *SortedMap[Map, K, V]) Values() iter.Seq[V] {
9595
return func(yield func(V) bool) {
9696
tempHeap := *sm.h
@@ -103,7 +103,7 @@ func (sm *SortedMap[Map, K, V]) Values() iter.Seq[V] {
103103
}
104104
}
105105

106-
// Insert adds a key-value pair to the map. If the key already exists, the value is updated.
106+
// Insert adds a key-value pair to the map. If the key already exists, the value is updated
107107
func (sm *SortedMap[Map, K, V]) Insert(key K, val V) {
108108
if _, exists := sm.m[key]; exists {
109109
sm.Delete(key)
@@ -112,7 +112,7 @@ func (sm *SortedMap[Map, K, V]) Insert(key K, val V) {
112112
heap.Push(sm.h, KV[K, V]{key, val})
113113
}
114114

115-
// Collect returns a map with the same contents as the SortedMap
115+
// Collect returns a regular map with an *unordered* content off the SortedMap
116116
func (sm *SortedMap[Map, K, V]) Collect() Map {
117117
m := make(Map)
118118
for key, val := range sm.All() {
@@ -122,6 +122,36 @@ func (sm *SortedMap[Map, K, V]) Collect() Map {
122122
return m
123123
}
124124

125+
// CollectAll returns a slice of key-value pairs
126+
func (sm *SortedMap[Map, K, V]) CollectAll() []KV[K, V] {
127+
pairs := make([]KV[K, V], 0, sm.Len())
128+
for k, v := range sm.All() {
129+
pairs = append(pairs, KV[K, V]{k, v})
130+
}
131+
132+
return pairs
133+
}
134+
135+
// CollectKeys returns a slice of the map’s keys
136+
func (sm *SortedMap[Map, K, V]) CollectKeys() []K {
137+
ks := make([]K, 0, sm.Len())
138+
for k := range sm.Keys() {
139+
ks = append(ks, k)
140+
}
141+
142+
return ks
143+
}
144+
145+
// CollectValues returns a slice of the map's values
146+
func (sm *SortedMap[Map, K, V]) CollectValues() []V {
147+
vals := make([]V, 0, sm.Len())
148+
for val := range sm.Values() {
149+
vals = append(vals, val)
150+
}
151+
152+
return vals
153+
}
154+
125155
// Len returns length of underlying map
126156
func (sm *SortedMap[Map, K, V]) Len() int {
127157
return len(sm.m)

0 commit comments

Comments
 (0)