Skip to content

Commit

Permalink
rename rhh.go to hashtable.go
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Jan 3, 2024
1 parent 7ed6425 commit 2cea36d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
22 changes: 11 additions & 11 deletions rhh.go → hashtable.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ const (
maxDIB = ^uint32(0) >> hashBitSize // max 255
)

// rhh is a robin hood hashing, only stores node index and key getter to reduce GC efforts.
type rhh[K comparable] struct {
// hashtable is a robin hood hashing, only stores node index and key getter to reduce GC efforts.
type hashtable[K comparable] struct {
buckets []struct {
hdib uint32 // bitfield { hash:24 dib:8 }
index uint32 // node index
Expand All @@ -27,7 +27,7 @@ type rhh[K comparable] struct {
shrinkAt int
}

func (m *rhh[K]) init(cap int, getkey func(i uint32) K) {
func (m *hashtable[K]) init(cap int, getkey func(i uint32) K) {
m.cap = cap
m.length = 0
sz := 8
Expand All @@ -44,8 +44,8 @@ func (m *rhh[K]) init(cap int, getkey func(i uint32) K) {
m.shrinkAt = int(float64(len(m.buckets)) * (1 - loadFactor))
}

func (m *rhh[K]) resize(newCap int) {
var nmap rhh[K]
func (m *hashtable[K]) resize(newCap int) {
var nmap hashtable[K]
nmap.init(newCap, m.getkey)
for i := 0; i < len(m.buckets); i++ {
if int(m.buckets[i].hdib&maxDIB) > 0 {
Expand All @@ -59,14 +59,14 @@ func (m *rhh[K]) resize(newCap int) {

// Set assigns a value to a key.
// Returns the previous value, or false when no value was assigned.
func (m *rhh[K]) Set(hash uint32, key K, value uint32) (uint32, bool) {
func (m *hashtable[K]) Set(hash uint32, key K, value uint32) (uint32, bool) {
if m.length >= m.growAt {
m.resize(len(m.buckets) * 2)
}
return m.set(hash>>dibBitSize, key, value)
}

func (m *rhh[K]) set(hash uint32, key K, value uint32) (prev uint32, ok bool) {
func (m *hashtable[K]) set(hash uint32, key K, value uint32) (prev uint32, ok bool) {
hdib := hash<<dibBitSize | uint32(1)&maxDIB
i := (hdib >> dibBitSize) & m.mask
for {
Expand All @@ -93,7 +93,7 @@ func (m *rhh[K]) set(hash uint32, key K, value uint32) (prev uint32, ok bool) {

// Get returns a value for a key.
// Returns false when no value has been assign for key.
func (m *rhh[K]) Get(hash uint32, key K) (prev uint32, ok bool) {
func (m *hashtable[K]) Get(hash uint32, key K) (prev uint32, ok bool) {
if len(m.buckets) == 0 {
return
}
Expand All @@ -111,13 +111,13 @@ func (m *rhh[K]) Get(hash uint32, key K) (prev uint32, ok bool) {
}

// Len returns the number of values in map.
func (m *rhh[K]) Len() int {
func (m *hashtable[K]) Len() int {
return m.length
}

// Delete deletes a value for a key.
// Returns the deleted value, or false when no value was assigned.
func (m *rhh[K]) Delete(hash uint32, key K) (v uint32, ok bool) {
func (m *hashtable[K]) Delete(hash uint32, key K) (v uint32, ok bool) {
if len(m.buckets) == 0 {
return
}
Expand All @@ -136,7 +136,7 @@ func (m *rhh[K]) Delete(hash uint32, key K) (v uint32, ok bool) {
}
}

func (m *rhh[K]) delete(i uint32) {
func (m *hashtable[K]) delete(i uint32) {
m.buckets[i].hdib = m.buckets[i].hdib>>dibBitSize<<dibBitSize | uint32(0)&maxDIB
for {
pi := i
Expand Down
4 changes: 2 additions & 2 deletions shard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
type shard[K comparable, V any] struct {
mu sync.Mutex
list list[K, V]
table rhh[K]
table hashtable[K]

_ [128 - unsafe.Sizeof(sync.Mutex{}) - unsafe.Sizeof(list[K, V]{}) - unsafe.Sizeof(rhh[K]{})]byte
_ [128 - unsafe.Sizeof(sync.Mutex{}) - unsafe.Sizeof(list[K, V]{}) - unsafe.Sizeof(hashtable[K]{})]byte
}

func (s *shard[K, V]) Get(hash uint32, key K) (value V, ok bool) {
Expand Down

0 comments on commit 2cea36d

Please sign in to comment.