Compare commits
2 Commits
main
...
f35583e079
| Author | SHA1 | Date | |
|---|---|---|---|
|
f35583e079
|
|||
|
ce41d4fba2
|
@@ -114,6 +114,9 @@ linters:
|
||||
# Reports uses of functions with replacement inside the testing package.
|
||||
- usetesting
|
||||
|
||||
# Reports mixed receiver types in structs/interfaces.
|
||||
- recvcheck
|
||||
|
||||
settings:
|
||||
revive:
|
||||
rules:
|
||||
@@ -198,7 +201,7 @@ linters:
|
||||
|
||||
# warns when initialism, variable or package naming conventions are not followed.
|
||||
- name: var-naming
|
||||
|
||||
|
||||
misspell:
|
||||
# Correct spellings using locale preferences for US or UK.
|
||||
# Setting locale to US will correct the British spelling of 'colour' to 'color'.
|
||||
|
||||
@@ -19,11 +19,11 @@ type bucket[K, V any] struct {
|
||||
|
||||
// location determines where in the bucket a certain key would be placed. If the
|
||||
// capacity is 0, this will panic.
|
||||
func (b bucket[K, V]) location(key K) uint64 {
|
||||
func (b *bucket[K, V]) location(key K) uint64 {
|
||||
return b.hash(key) % b.capacity
|
||||
}
|
||||
|
||||
func (b bucket[K, V]) get(key K) (value V, found bool) {
|
||||
func (b *bucket[K, V]) get(key K) (value V, found bool) {
|
||||
if b.capacity == 0 {
|
||||
return
|
||||
}
|
||||
@@ -54,7 +54,7 @@ func (b *bucket[K, V]) resize(capacity uint64) {
|
||||
b.size = 0
|
||||
}
|
||||
|
||||
func (b bucket[K, V]) update(key K, value V) (updated bool) {
|
||||
func (b *bucket[K, V]) update(key K, value V) (updated bool) {
|
||||
if b.capacity == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
18
table.go
18
table.go
@@ -27,12 +27,12 @@ type Table[K, V any] struct {
|
||||
|
||||
// TotalCapacity returns the number of slots allocated for the [Table]. To get the
|
||||
// number of slots filled, look at [Table.Size].
|
||||
func (t Table[K, V]) TotalCapacity() uint64 {
|
||||
func (t *Table[K, V]) TotalCapacity() uint64 {
|
||||
return t.bucketA.capacity + t.bucketB.capacity
|
||||
}
|
||||
|
||||
// Size returns how many slots are filled in the [Table].
|
||||
func (t Table[K, V]) Size() int {
|
||||
func (t *Table[K, V]) Size() int {
|
||||
return int(t.bucketA.size + t.bucketB.size)
|
||||
}
|
||||
|
||||
@@ -40,11 +40,11 @@ func log2(n uint64) (m int) {
|
||||
return max(0, bits.Len64(n)-1)
|
||||
}
|
||||
|
||||
func (t Table[K, V]) maxEvictions() int {
|
||||
func (t *Table[K, V]) maxEvictions() int {
|
||||
return 3 * log2(t.TotalCapacity())
|
||||
}
|
||||
|
||||
func (t Table[K, V]) load() float64 {
|
||||
func (t *Table[K, V]) load() float64 {
|
||||
// When there are no slots in the table, we still treat the load as 100%.
|
||||
// Every slot in the table is full.
|
||||
if t.TotalCapacity() == 0 {
|
||||
@@ -97,7 +97,7 @@ func (t *Table[K, V]) shrink() error {
|
||||
|
||||
// Get fetches the value for a key in the [Table]. Matches the comma-ok pattern
|
||||
// of a builtin map; see [Table.Find] for plain indexing.
|
||||
func (t Table[K, V]) Get(key K) (value V, ok bool) {
|
||||
func (t *Table[K, V]) Get(key K) (value V, ok bool) {
|
||||
if item, ok := t.bucketA.get(key); ok {
|
||||
return item, true
|
||||
}
|
||||
@@ -111,13 +111,13 @@ func (t Table[K, V]) Get(key K) (value V, ok bool) {
|
||||
|
||||
// Find fetches the value of a key. Matches direct indexing of a builtin map;
|
||||
// see [Table.Get] for a comma-ok pattern.
|
||||
func (t Table[K, V]) Find(key K) (value V) {
|
||||
func (t *Table[K, V]) Find(key K) (value V) {
|
||||
value, _ = t.Get(key)
|
||||
return
|
||||
}
|
||||
|
||||
// Has returns true if a key has a value in the table.
|
||||
func (t Table[K, V]) Has(key K) (exists bool) {
|
||||
func (t *Table[K, V]) Has(key K) (exists bool) {
|
||||
_, exists = t.Get(key)
|
||||
return
|
||||
}
|
||||
@@ -168,7 +168,7 @@ func (t *Table[K, V]) Drop(key K) (err error) {
|
||||
}
|
||||
|
||||
// Entries returns an unordered sequence of all key-value pairs in the table.
|
||||
func (t Table[K, V]) Entries() iter.Seq2[K, V] {
|
||||
func (t *Table[K, V]) Entries() iter.Seq2[K, V] {
|
||||
return func(yield func(K, V) bool) {
|
||||
for _, slot := range t.bucketA.slots {
|
||||
if slot.occupied {
|
||||
@@ -190,7 +190,7 @@ func (t Table[K, V]) Entries() iter.Seq2[K, V] {
|
||||
|
||||
// String returns the entries of the table as a string in the format:
|
||||
// "table[k1:v1 h2:v2 ...]".
|
||||
func (t Table[K, V]) String() string {
|
||||
func (t *Table[K, V]) String() string {
|
||||
var sb strings.Builder
|
||||
sb.WriteString("table[")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user