Skip to content

Commit

Permalink
Use any for value generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobshub committed Dec 7, 2023
1 parent 12dcc1f commit 996f627
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 deletions.
13 changes: 7 additions & 6 deletions bounds.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package sortedmap

import "sort"
import (
"reflect"
"sort"
)

func (sm *SortedMap[K, V]) setBoundIdx(boundVal V) int {
return sort.Search(len(sm.sorted), func(i int) bool {
Expand All @@ -14,16 +17,14 @@ func (sm *SortedMap[K, V]) boundsIdxSearch(lowerBound, upperBound V) []int {
return nil
}

NilV := *new(V)

if lowerBound != NilV && upperBound != NilV {
if !reflect.ValueOf(lowerBound).IsZero() && !reflect.ValueOf(upperBound).IsZero() {
if sm.lessFn(upperBound, lowerBound) {
return nil
}
}

lowerBoundIdx := 0
if lowerBound != NilV {
if !reflect.ValueOf(lowerBound).IsZero() {
lowerBoundIdx = sm.setBoundIdx(lowerBound)

if lowerBoundIdx == smLen {
Expand All @@ -35,7 +36,7 @@ func (sm *SortedMap[K, V]) boundsIdxSearch(lowerBound, upperBound V) []int {
}

upperBoundIdx := smLen - 1
if upperBound != NilV {
if !reflect.ValueOf(upperBound).IsZero() {
upperBoundIdx = sm.setBoundIdx(upperBound)
if upperBoundIdx == smLen {
upperBoundIdx--
Expand Down
6 changes: 3 additions & 3 deletions iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

// IterChCloser allows records to be read through a channel that is returned by the Records method.
// IterChCloser values should be closed after use using the Close method.
type IterChCloser[K comparable, V comparable] struct {
type IterChCloser[K comparable, V any] struct {
ch chan Record[K, V]
canceled chan struct{}
}
Expand All @@ -29,7 +29,7 @@ func (iterCh *IterChCloser[K, V]) Records() <-chan Record[K, V] {
// channel send goroutines to time-out.
// BufSize is set to 1 if its field is set to a lower value.
// LowerBound and UpperBound default to regular iteration when left unset.
type IterChParams[V comparable] struct {
type IterChParams[V any] struct {
Reversed bool
SendTimeout time.Duration
BufSize int
Expand All @@ -38,7 +38,7 @@ type IterChParams[V comparable] struct {

// IterCallbackFunc defines the type of function that is passed into an IterFunc method.
// The function is passed a record value argument.
type IterCallbackFunc[K comparable, V comparable] func(rec Record[K, V]) bool
type IterCallbackFunc[K comparable, V any] func(rec Record[K, V]) bool

func setBufSize(bufSize int) int {
// initialBufSize must be >= 1 or a blocked channel send goroutine may not exit.
Expand Down
6 changes: 3 additions & 3 deletions sortedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package sortedmap

// SortedMap contains a map, a slice, and references to one or more comparison functions.
// SortedMap is not concurrency-safe, though it can be easily wrapped by a developer-defined type.
type SortedMap[K, V comparable] struct {
type SortedMap[K comparable, V any] struct {
idx map[K]V
sorted []K
lessFn ComparisonFunc[V]
}

// Record defines a type used in batching and iterations, where keys and values are used together.
type Record[K, V comparable] struct {
type Record[K comparable, V any] struct {
Key K
Val V
}
Expand All @@ -30,7 +30,7 @@ func setComparisonFunc[V any](cmpFn ComparisonFunc[V]) ComparisonFunc[V] {

// New creates and initializes a new SortedMap structure and then returns a reference to it.
// New SortedMaps are created with a backing map/slice of length/capacity n.
func New[K, V comparable](n int, cmpFn ComparisonFunc[V]) *SortedMap[K, V] {
func New[K comparable, V any](n int, cmpFn ComparisonFunc[V]) *SortedMap[K, V] {
return &SortedMap[K, V]{
idx: make(map[K]V, n),
sorted: make([]K, 0, n),
Expand Down

0 comments on commit 996f627

Please sign in to comment.