Skip to content

Commit

Permalink
simplify key constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
cornelk committed Aug 9, 2022
1 parent 01b6573 commit 8114649
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ bug that can cause writes to be lost.***

## Usage

Only Go built-in types beside any/interface{} are supported.
Only Go comparable types are supported as keys.

Set a value for a key in the map:

Expand Down
13 changes: 4 additions & 9 deletions hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ const DefaultSize = 8
// MaxFillRate is the maximum fill rate for the slice before a resize will happen.
const MaxFillRate = 50

type keyConstraint interface {
string | int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr |
float32 | float64 | complex64 | complex128
}

// HashMap implements a read optimized hash map.
type HashMap[Key keyConstraint, Value any] struct {
type HashMap[Key comparable, Value any] struct {
hasher func(Key) uintptr
store atomic.Pointer[store[Key, Value]] // pointer to a map instance that gets replaced if the map resizes
linkedList atomic.Pointer[List[Key, Value]] // key sorted linked list of elements
Expand All @@ -32,18 +27,18 @@ type HashMap[Key keyConstraint, Value any] struct {
}

// KeyValue represents a key/value that is returned by the iterator.
type KeyValue[Key keyConstraint, Value any] struct {
type KeyValue[Key comparable, Value any] struct {
Key Key
Value Value
}

// New returns a new HashMap instance.
func New[Key keyConstraint, Value any]() *HashMap[Key, Value] {
func New[Key comparable, Value any]() *HashMap[Key, Value] {
return NewSized[Key, Value](DefaultSize)
}

// NewSized returns a new HashMap instance with a specific initialization size.
func NewSized[Key keyConstraint, Value any](size uintptr) *HashMap[Key, Value] {
func NewSized[Key comparable, Value any](size uintptr) *HashMap[Key, Value] {
m := &HashMap[Key, Value]{}
m.allocate(size)
m.setHasher()
Expand Down
4 changes: 2 additions & 2 deletions list.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import (
)

// List is a sorted doubly linked list.
type List[Key keyConstraint, Value any] struct {
type List[Key comparable, Value any] struct {
count atomic.Uintptr
head *ListElement[Key, Value]
}

// NewList returns an initialized list.
func NewList[Key keyConstraint, Value any]() *List[Key, Value] {
func NewList[Key comparable, Value any]() *List[Key, Value] {
return &List[Key, Value]{
head: &ListElement[Key, Value]{},
}
Expand Down
2 changes: 1 addition & 1 deletion list_element.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

// ListElement is an element of a list.
type ListElement[Key keyConstraint, Value any] struct {
type ListElement[Key comparable, Value any] struct {
keyHash uintptr
// deleted marks the item as deleting or deleted
// this is using uintptr instead of atomic.Bool to avoid using 32 bit int on 64 bit systems
Expand Down
2 changes: 1 addition & 1 deletion store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"unsafe"
)

type store[Key keyConstraint, Value any] struct {
type store[Key comparable, Value any] struct {
keyShifts uintptr // Pointer size - log2 of array size, to be used as index in the data array
count atomic.Uintptr // count of filled elements in the slice
array unsafe.Pointer // pointer to slice data array
Expand Down

0 comments on commit 8114649

Please sign in to comment.