Skip to content

Commit

Permalink
take equals instead of ==/!=
Browse files Browse the repository at this point in the history
  • Loading branch information
flaneur2020 committed Oct 29, 2019
1 parent 2dfbae5 commit ea54fb0
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions skiplist/skiplist.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (s SkipList) randomLevel() (n int) {
func (s *SkipList) Get(key interface{}) (value interface{}, ok bool) {
candidate := s.getPath(s.header, nil, key)

if candidate == nil || candidate.key != key {
if candidate == nil || !s.equals(candidate.key, key) {
return nil, false
}

Expand Down Expand Up @@ -413,7 +413,7 @@ func (s *SkipList) Set(key, value interface{}) {
update := make([]*node, s.level()+1, s.effectiveMaxLevel()+1)
candidate := s.getPath(s.header, update, key)

if candidate != nil && candidate.key == key {
if candidate != nil && s.equals(candidate.key, key) {
candidate.value = value
return
}
Expand Down Expand Up @@ -468,7 +468,7 @@ func (s *SkipList) Delete(key interface{}) (value interface{}, ok bool) {
update := make([]*node, s.level()+1, s.effectiveMaxLevel())
candidate := s.getPath(s.header, update, key)

if candidate == nil || candidate.key != key {
if candidate == nil || !s.equals(candidate.key, key) {
return nil, false
}

Expand All @@ -494,6 +494,10 @@ func (s *SkipList) Delete(key interface{}) (value interface{}, ok bool) {
return candidate.value, true
}

func (s *SkipList) equals(l, r interface{}) bool {
return (!s.lessThan(l, r)) && !s.lessThan(r, l)
}

// NewCustomMap returns a new SkipList that will use lessThan as the
// comparison function. lessThan should define a linear order on keys
// you intend to use with the SkipList.
Expand Down

0 comments on commit ea54fb0

Please sign in to comment.