Skip to content

Commit 6486e60

Browse files
committed
switched key type to interface{}
1 parent 68c6223 commit 6486e60

14 files changed

+116
-113
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![Build Status](https://travis-ci.org/umpc/go-sortedmap.svg?branch=master)](https://travis-ci.org/umpc/go-sortedmap) [![Coverage Status](https://codecov.io/github/umpc/go-sortedmap/badge.svg?branch=master)](https://codecov.io/github/umpc/go-sortedmap?branch=master) [![GoDoc](https://godoc.org/github.com/umpc/go-sortedmap?status.svg)](https://godoc.org/github.com/umpc/go-sortedmap)
44

5-
Sorted Map is a small library that provides a value-sorted ```map[string]interface``` type and methods combined from Go 1 map and slice primitives.
5+
SortedMap is a small library that provides a value-sorted ```map[interface{}]interface{}``` type and methods combined from Go 1 map and slice primitives.
66

77
This data structure allows for roughly constant-time reads and for efficiently iterating over only a subsection of the stored values. While this works for the project it was built for, because of the structure's reliance on a sorted slice of keys, worst-case delete operations are roughly ```O(n)```, where ```n``` is the number of items in the collection.
88

asc/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package asc
22

3-
const greaterThanErr = "the lesser value was greater than the higher value"
3+
const greaterThanErr = "the higher value was less than the lesser value!"

delete.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package sortedmap
22

3-
func (sm *SortedMap) delete(key string) bool {
3+
func (sm *SortedMap) delete(key interface{}) bool {
44
if _, ok := sm.idx[key]; ok {
55
delete(sm.idx, key)
66

@@ -9,7 +9,7 @@ func (sm *SortedMap) delete(key string) bool {
99

1010
for i := 0; i < smLen - deleted; i++ {
1111
if sm.sorted[i] == key {
12-
sm.sorted = deleteString(sm.sorted, i)
12+
sm.sorted = deleteInterface(sm.sorted, i)
1313
deleted++
1414
}
1515
}
@@ -21,12 +21,12 @@ func (sm *SortedMap) delete(key string) bool {
2121

2222
// Delete removes a value from the collection, using the given key.
2323
// Because the index position of each sorted key changes on each insert and a simpler structure was ideal, deletes can have a worse-case complexity of O(n), meaning the goroutine must loop through the sorted slice to find and delete the given key.
24-
func (sm *SortedMap) Delete(key string) bool {
24+
func (sm *SortedMap) Delete(key interface{}) bool {
2525
return sm.delete(key)
2626
}
2727

2828
// BatchDelete removes values from the collection, using the given keys, returning a slice of the results.
29-
func (sm *SortedMap) BatchDelete(keys ...string) []bool {
29+
func (sm *SortedMap) BatchDelete(keys []interface{}) []bool {
3030
results := make([]bool, len(keys))
3131
for i, key := range keys {
3232
results[i] = sm.delete(key)

desc/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package desc
22

3-
const greaterThanErr = "the lesser value was greater than the higher value"
3+
const greaterThanErr = "the lesser value was greater than the higher value!"

get.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package sortedmap
22

33
// Get retrieves a value from the collection, using the given key.
4-
func (sm *SortedMap) Get(key string) (interface{}, bool) {
4+
func (sm *SortedMap) Get(key interface{}) (interface{}, bool) {
55
val, ok := sm.idx[key]
66
return val, ok
77
}
88

99
// BatchGet retrieves values with their read statuses from the collection, using the given keys.
10-
func (sm *SortedMap) BatchGet(keys ...string) ([]interface{}, []bool) {
10+
func (sm *SortedMap) BatchGet(keys []interface{}) ([]interface{}, []bool) {
1111
vals := make([]interface{}, len(keys))
1212
results := make([]bool, len(keys))
1313

has.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package sortedmap
22

33
// Has checks if the key exists in the collection.
4-
func (sm *SortedMap) Has(key string) bool {
4+
func (sm *SortedMap) Has(key interface{}) bool {
55
_, ok := sm.idx[key]
66
return ok
77
}
88

99
// BatchHas checks if the keys exist in the collection and returns a slice containing the results.
10-
func (sm *SortedMap) BatchHas(keys ...string) []bool {
10+
func (sm *SortedMap) BatchHas(keys []interface{}) []bool {
1111
results := make([]bool, len(keys))
1212
for i, key := range keys {
1313
_, results[i] = sm.idx[key]

insert.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package sortedmap
22

3-
func (sm *SortedMap) insert(key string, val interface{}) bool {
3+
func (sm *SortedMap) insert(key, val interface{}) bool {
44
if _, ok := sm.idx[key]; !ok {
55
sm.idx[key] = val
66
sm.sorted = sm.insertSort(key, val)
@@ -11,13 +11,13 @@ func (sm *SortedMap) insert(key string, val interface{}) bool {
1111

1212
// Insert uses the provided 'less than' function to insert sort and add the value to the collection and returns a value containing the record's insert status.
1313
// If the key already exists, the value will not be inserted. Use Replace for the alternative functionality.
14-
func (sm *SortedMap) Insert(key string, val interface{}) bool {
14+
func (sm *SortedMap) Insert(key, val interface{}) bool {
1515
return sm.insert(key, val)
1616
}
1717

1818
// BatchInsert adds all given records to the collection and returns a slice containing each record's insert status.
1919
// If a key already exists, the value will not be inserted. Use BatchReplace for the alternative functionality.
20-
func (sm *SortedMap) BatchInsert(recs ...*Record) []bool {
20+
func (sm *SortedMap) BatchInsert(recs []*Record) []bool {
2121
results := make([]bool, len(recs))
2222
for i, rec := range recs {
2323
results[i] = sm.insert(rec.Key, rec.Val)

insertsort.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@ package sortedmap
22

33
import "sort"
44

5-
func (sm *SortedMap) insertSort(key string, val interface{}) []string {
5+
func (sm *SortedMap) insertSort(key, val interface{}) []interface{} {
66
smLen := len(sm.sorted)
77
if smLen == 0 {
8-
return []string{key}
8+
return []interface{}{key}
99
}
10-
i := sort.Search(smLen, func(i int) bool {
10+
return insertInterface(sm.sorted, key, sort.Search(smLen, func(i int) bool {
1111
return sm.lessFn(val, sm.idx[sm.sorted[i]])
12-
})
13-
return insertString(sm.sorted, i, key)
12+
}))
1413
}

replace.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package sortedmap
22

3-
func (sm *SortedMap) replace(key string, val interface{}) {
3+
func (sm *SortedMap) replace(key, val interface{}) {
44
if _, ok := sm.idx[key]; ok {
55
sm.delete(key)
66
}
@@ -10,13 +10,13 @@ func (sm *SortedMap) replace(key string, val interface{}) {
1010

1111
// Replace uses the provided 'less than' function to insert sort.
1212
// Even if the key already exists, the value will be inserted. Use Insert for the alternative functionality.
13-
func (sm *SortedMap) Replace(key string, val interface{}) {
13+
func (sm *SortedMap) Replace(key, val interface{}) {
1414
sm.replace(key, val)
1515
}
1616

1717
// BatchReplace adds all given records to the collection.
1818
// Even if a key already exists, the value will be inserted. Use BatchInsert for the alternative functionality.
19-
func (sm *SortedMap) BatchReplace(recs ...*Record) {
19+
func (sm *SortedMap) BatchReplace(recs []*Record) {
2020
for _, rec := range recs {
2121
sm.replace(rec.Key, rec.Val)
2222
}

sliceutils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package sortedmap
22

3-
func insertString(s []string, i int, str string) []string {
4-
s = append(s, "")
3+
func insertInterface(s []interface{}, v interface{}, i int) []interface{} {
4+
s = append(s, nil)
55
copy(s[i + 1:], s[i:])
6-
s[i] = str
6+
s[i] = v
77

88
return s
99
}
1010

11-
func deleteString(s []string, i int) []string {
11+
func deleteInterface(s []interface{}, i int) []interface{} {
1212
copy(s[i:], s[i + 1:])
13-
s[len(s) - 1] = ""
13+
s[len(s) - 1] = nil
1414

1515
return s[:len(s) - 1]
1616
}

0 commit comments

Comments
 (0)