Skip to content

Commit 619c6f5

Browse files
committed
Add methods from new files
1 parent 909dabe commit 619c6f5

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

keys.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package sortedmap
2+
3+
func (sm *SortedMap) keys(lowerBound, upperBound interface{}) ([]interface{}, bool) {
4+
idxBounds := sm.boundsIdxSearch(lowerBound, upperBound)
5+
if idxBounds == nil {
6+
return nil, false
7+
}
8+
return sm.sorted[idxBounds[0]:idxBounds[1] + 1], true
9+
}
10+
11+
// Keys returns a slice containing sorted keys.
12+
// The returned slice is valid until the next modification to the SortedMap structure.
13+
func (sm *SortedMap) Keys() []interface{} {
14+
keys, _ := sm.keys(nil, nil)
15+
return keys
16+
}
17+
18+
// Keys returns a slice containing sorted keys.
19+
// The returned slice is valid until the next modification to the SortedMap structure.
20+
func (sm *SortedMap) BoundedKeys(lowerBound, upperBound interface{}) ([]interface{}, bool) {
21+
return sm.keys(lowerBound, upperBound)
22+
}

keys_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package sortedmap
2+
3+
import (
4+
"testing"
5+
"time"
6+
)
7+
8+
func TestKeys(t *testing.T) {
9+
sm, _, err := newSortedMapFromRandRecords(300)
10+
if err != nil {
11+
t.Fatal(err)
12+
}
13+
i := 0
14+
keys := sm.Keys()
15+
for _, key := range keys {
16+
if key == nil {
17+
t.Fatal("Key's value is nil.")
18+
}
19+
i++
20+
}
21+
if i == 0 {
22+
t.Fatal("The returned slice was empty.")
23+
}
24+
}
25+
26+
func TestBoundedKeys(t *testing.T) {
27+
sm, _, err := newSortedMapFromRandRecords(300)
28+
if err != nil {
29+
t.Fatal(err)
30+
}
31+
i := 0
32+
keys, ok := sm.BoundedKeys(time.Time{}, time.Now())
33+
if !ok {
34+
t.Fatal("No values fall between or are equal to the given bounds.")
35+
}
36+
for _, key := range keys {
37+
if key == nil {
38+
t.Fatal("Key's value is nil.")
39+
}
40+
i++
41+
}
42+
if i == 0 {
43+
t.Fatal("The returned slice was empty.")
44+
}
45+
}

map.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package sortedmap
2+
3+
// Map returns a map containing keys mapped to values.
4+
// The returned map is valid until the next modification to the SortedMap structure.
5+
// The map can be used with ether the Keys or BoundedKeys methods to select a range of items
6+
// and iterate over them using a slice for-range loop, rather than a channel for-range loop.
7+
func (sm *SortedMap) Map() map[interface{}]interface{} {
8+
return sm.idx
9+
}

map_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package sortedmap
2+
3+
import "testing"
4+
5+
func TestMap(t *testing.T) {
6+
sm, _, err := newSortedMapFromRandRecords(300)
7+
if err != nil {
8+
t.Fatal(err)
9+
}
10+
i := 0
11+
m := sm.Map()
12+
for _, val := range m {
13+
if val == nil {
14+
t.Fatal("Map key's value is nil.")
15+
}
16+
i++
17+
}
18+
if i == 0 {
19+
t.Fatal("The returned map was empty.")
20+
}
21+
}

0 commit comments

Comments
 (0)