Skip to content

Commit

Permalink
Merge pull request #30 from kelbyers/master
Browse files Browse the repository at this point in the history
Add full support for `[]byte` key type
  • Loading branch information
cornelk committed Nov 18, 2018
2 parents a8997b1 + 5b28a97 commit b893f49
Show file tree
Hide file tree
Showing 4 changed files with 364 additions and 170 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ language: go
go:
- 1.9.x
- 1.10.x
- 1.11.x

script:
- go test -race -coverprofile=coverage.txt -covermode=atomic
Expand Down
14 changes: 12 additions & 2 deletions hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,19 @@ func (m *HashMap) Del(key interface{}) {
h := getKeyHash(key)

var element *ListElement
ElementLoop:
for _, element = m.indexElement(h); element != nil; element = element.Next() {
if element.keyHash == h && element.key == key {
break
if element.keyHash == h {
switch key.(type) {
case []byte:
if bytes.Compare(element.key.([]byte), key.([]byte)) == 0 {
break ElementLoop
}
default:
if element.key == key {
break ElementLoop
}
}
}

if element.keyHash > h {
Expand Down
29 changes: 24 additions & 5 deletions hashmap_get.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package hashmap

import (
"bytes"
"reflect"
"unsafe"

Expand All @@ -19,8 +20,17 @@ func (m *HashMap) Get(key interface{}) (value interface{}, ok bool) {

// inline HashMap.searchItem()
for element != nil {
if element.keyHash == h && element.key == key {
return element.Value(), true
if element.keyHash == h {
switch key.(type) {
case []byte:
if bytes.Compare(element.key.([]byte), key.([]byte)) == 0 {
return element.Value(), true
}
default:
if element.key == key {
return element.Value(), true
}
}
}

if element.keyHash > h {
Expand Down Expand Up @@ -132,9 +142,18 @@ func (m *HashMap) GetOrInsert(key interface{}, value interface{}) (actual interf
}

for element != nil {
if element.keyHash == h && element.key == key {
actual = element.Value()
return actual, true
if element.keyHash == h {
switch key.(type) {
case []byte:
if bytes.Compare(element.key.([]byte), key.([]byte)) == 0 {
return element.Value(), true
}
default:
if element.key == key {
actual = element.Value()
return actual, true
}
}
}

if element.keyHash > h {
Expand Down

0 comments on commit b893f49

Please sign in to comment.