From ee497b7a6d3c751aa05c67bbc602ef1913742e92 Mon Sep 17 00:00:00 2001 From: phuslu Date: Wed, 3 Jan 2024 17:13:47 +0800 Subject: [PATCH] rename rhh.go to table.go --- shard.go | 4 ++-- rhh.go => table.go | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 13 deletions(-) rename rhh.go => table.go (85%) diff --git a/shard.go b/shard.go index aa837dd..cb2a924 100644 --- a/shard.go +++ b/shard.go @@ -13,9 +13,9 @@ import ( type shard[K comparable, V any] struct { mu sync.Mutex list list[K, V] - table rhh[K] + table table[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(table[K]{})]byte } func (s *shard[K, V]) Get(hash uint32, key K) (value V, ok bool) { diff --git a/rhh.go b/table.go similarity index 85% rename from rhh.go rename to table.go index 608abac..73e7492 100644 --- a/rhh.go +++ b/table.go @@ -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 { +// table is a robin hood hashing, only stores node index and key getter to reduce GC efforts. +type table[K comparable] struct { buckets []struct { hdib uint32 // bitfield { hash:24 dib:8 } index uint32 // node index @@ -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 *table[K]) init(cap int, getkey func(i uint32) K) { m.cap = cap m.length = 0 sz := 8 @@ -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 *table[K]) resize(newCap int) { + var nmap table[K] nmap.init(newCap, m.getkey) for i := 0; i < len(m.buckets); i++ { if int(m.buckets[i].hdib&maxDIB) > 0 { @@ -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 *table[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 *table[K]) set(hash uint32, key K, value uint32) (prev uint32, ok bool) { hdib := hash<> dibBitSize) & m.mask for { @@ -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 *table[K]) Get(hash uint32, key K) (prev uint32, ok bool) { if len(m.buckets) == 0 { return } @@ -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 *table[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 *table[K]) Delete(hash uint32, key K) (v uint32, ok bool) { if len(m.buckets) == 0 { return } @@ -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 *table[K]) delete(i uint32) { m.buckets[i].hdib = m.buckets[i].hdib>>dibBitSize<