Skip to content

Commit bb1abb8

Browse files
authored
hash: refactoring (#341)
1 parent 413839d commit bb1abb8

File tree

5 files changed

+43
-41
lines changed

5 files changed

+43
-41
lines changed

symboltable/chain_hash_table_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package symboltable
33
import (
44
"testing"
55

6-
. "github.com/moorara/algo/generic"
7-
. "github.com/moorara/algo/hash"
6+
"github.com/moorara/algo/generic"
7+
"github.com/moorara/algo/hash"
88
)
99

1010
func getChainHashTableTests() []symbolTableTest[string, int] {
11-
hashFunc := HashFuncForString[string](nil)
12-
eqKey := NewEqualFunc[string]()
13-
eqVal := NewEqualFunc[int]()
11+
hashFunc := hash.HashFuncForString[string](nil)
12+
eqKey := generic.NewEqualFunc[string]()
13+
eqVal := generic.NewEqualFunc[int]()
1414
opts := HashOpts{}
1515

1616
tests := getSymbolTableTests()
1717

1818
tests[0].symbolTable = "Separate Chaining Hash Table"
19-
tests[0].equal = NewChainHashTable[string, int](hashFunc, eqKey, eqVal, opts)
19+
tests[0].equal = NewChainHashTable(hashFunc, eqKey, eqVal, opts)
2020
tests[0].equal.Put("Apple", 182)
2121
tests[0].equal.Put("Avocado", 200)
2222
tests[0].equal.Put("Banana", 120)
@@ -35,7 +35,7 @@ func getChainHashTableTests() []symbolTableTest[string, int] {
3535
tests[0].expectedEqual = true
3636

3737
tests[1].symbolTable = "Separate Chaining Hash Table"
38-
tests[1].equal = NewChainHashTable[string, int](hashFunc, eqKey, eqVal, opts)
38+
tests[1].equal = NewChainHashTable(hashFunc, eqKey, eqVal, opts)
3939
tests[1].equal.Put("Golden Pheasant", 15)
4040
tests[1].equal.Put("Harpy Eagle", 35)
4141
tests[1].equal.Put("Kingfisher", 15)
@@ -47,7 +47,7 @@ func getChainHashTableTests() []symbolTableTest[string, int] {
4747
tests[1].expectedEqual = true
4848

4949
tests[2].symbolTable = "Separate Chaining Hash Table"
50-
tests[2].equal = NewChainHashTable[string, int](hashFunc, eqKey, eqVal, opts)
50+
tests[2].equal = NewChainHashTable(hashFunc, eqKey, eqVal, opts)
5151
tests[2].equal.Put("Accordion", 50)
5252
tests[2].equal.Put("Bassoon", 140)
5353
tests[2].equal.Put("Cello", 120)
@@ -67,7 +67,7 @@ func getChainHashTableTests() []symbolTableTest[string, int] {
6767
tests[2].expectedEqual = false
6868

6969
tests[3].symbolTable = "Separate Chaining Hash Table"
70-
tests[3].equal = NewChainHashTable[string, int](hashFunc, eqKey, eqVal, opts)
70+
tests[3].equal = NewChainHashTable(hashFunc, eqKey, eqVal, opts)
7171
tests[3].equal.Put("Berlin", 10)
7272
// tests[3].equal.Put("London", 11)
7373
tests[3].equal.Put("Montreal", 6)
@@ -91,7 +91,7 @@ func TestChainHashTable(t *testing.T) {
9191
tests := getChainHashTableTests()
9292

9393
for _, tc := range tests {
94-
ht := NewChainHashTable[string, int](tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
94+
ht := NewChainHashTable(tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
9595
runSymbolTableTest(t, ht, tc)
9696
}
9797
}

symboltable/double_hash_table_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package symboltable
33
import (
44
"testing"
55

6-
. "github.com/moorara/algo/generic"
7-
. "github.com/moorara/algo/hash"
6+
"github.com/moorara/algo/generic"
7+
"github.com/moorara/algo/hash"
88
)
99

1010
func getDoubleHashTableTests() []symbolTableTest[string, int] {
11-
hashFunc := HashFuncForString[string](nil)
12-
eqKey := NewEqualFunc[string]()
13-
eqVal := NewEqualFunc[int]()
11+
hashFunc := hash.HashFuncForString[string](nil)
12+
eqKey := generic.NewEqualFunc[string]()
13+
eqVal := generic.NewEqualFunc[int]()
1414
opts := HashOpts{}
1515

1616
tests := getSymbolTableTests()
1717

1818
tests[0].symbolTable = "Double Hashing Hash Table"
19-
tests[0].equal = NewDoubleHashTable[string, int](hashFunc, eqKey, eqVal, opts)
19+
tests[0].equal = NewDoubleHashTable(hashFunc, eqKey, eqVal, opts)
2020
tests[0].equal.Put("Apple", 182)
2121
tests[0].equal.Put("Avocado", 200)
2222
tests[0].equal.Put("Banana", 120)
@@ -35,7 +35,7 @@ func getDoubleHashTableTests() []symbolTableTest[string, int] {
3535
tests[0].expectedEqual = true
3636

3737
tests[1].symbolTable = "Double Hashing Hash Table"
38-
tests[1].equal = NewDoubleHashTable[string, int](hashFunc, eqKey, eqVal, opts)
38+
tests[1].equal = NewDoubleHashTable(hashFunc, eqKey, eqVal, opts)
3939
tests[1].equal.Put("Golden Pheasant", 15)
4040
tests[1].equal.Put("Harpy Eagle", 35)
4141
tests[1].equal.Put("Kingfisher", 15)
@@ -47,7 +47,7 @@ func getDoubleHashTableTests() []symbolTableTest[string, int] {
4747
tests[1].expectedEqual = true
4848

4949
tests[2].symbolTable = "Double Hashing Hash Table"
50-
tests[2].equal = NewDoubleHashTable[string, int](hashFunc, eqKey, eqVal, opts)
50+
tests[2].equal = NewDoubleHashTable(hashFunc, eqKey, eqVal, opts)
5151
tests[2].equal.Put("Accordion", 50)
5252
tests[2].equal.Put("Bassoon", 140)
5353
tests[2].equal.Put("Cello", 120)
@@ -67,7 +67,7 @@ func getDoubleHashTableTests() []symbolTableTest[string, int] {
6767
tests[2].expectedEqual = false
6868

6969
tests[3].symbolTable = "Double Hashing Hash Table"
70-
tests[3].equal = NewDoubleHashTable[string, int](hashFunc, eqKey, eqVal, opts)
70+
tests[3].equal = NewDoubleHashTable(hashFunc, eqKey, eqVal, opts)
7171
tests[3].equal.Put("Berlin", 10)
7272
// tests[3].equal.Put("London", 11)
7373
tests[3].equal.Put("Montreal", 6)
@@ -91,7 +91,7 @@ func TestDoubleHashTable(t *testing.T) {
9191
tests := getDoubleHashTableTests()
9292

9393
for _, tc := range tests {
94-
ht := NewDoubleHashTable[string, int](tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
94+
ht := NewDoubleHashTable(tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
9595
runSymbolTableTest(t, ht, tc)
9696
}
9797
}

symboltable/hash_table.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package symboltable
22

33
// HashOpts represents configuration options for a hash table.
44
type HashOpts struct {
5-
// The initial capacity of the hash table (must be a power of 2 for efficient hashing).
5+
// The initial capacity of the hash table.
6+
// For chain and linear hash tables, it must be a power of 2 for efficient hashing.
7+
// For quadratic and double hash tables, it must be a prime number for better distribution.
68
InitialCap int
79
// The minimum load factor before resizing (shrinking) the hash table.
810
MinLoadFactor float32

symboltable/linear_hash_table_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package symboltable
33
import (
44
"testing"
55

6-
. "github.com/moorara/algo/generic"
7-
. "github.com/moorara/algo/hash"
6+
"github.com/moorara/algo/generic"
7+
"github.com/moorara/algo/hash"
88
)
99

1010
func getLinearHashTableTests() []symbolTableTest[string, int] {
11-
hashFunc := HashFuncForString[string](nil)
12-
eqKey := NewEqualFunc[string]()
13-
eqVal := NewEqualFunc[int]()
11+
hashFunc := hash.HashFuncForString[string](nil)
12+
eqKey := generic.NewEqualFunc[string]()
13+
eqVal := generic.NewEqualFunc[int]()
1414
opts := HashOpts{}
1515

1616
tests := getSymbolTableTests()
1717

1818
tests[0].symbolTable = "Linear Probing Hash Table"
19-
tests[0].equal = NewLinearHashTable[string, int](hashFunc, eqKey, eqVal, opts)
19+
tests[0].equal = NewLinearHashTable(hashFunc, eqKey, eqVal, opts)
2020
tests[0].equal.Put("Apple", 182)
2121
tests[0].equal.Put("Avocado", 200)
2222
tests[0].equal.Put("Banana", 120)
@@ -35,7 +35,7 @@ func getLinearHashTableTests() []symbolTableTest[string, int] {
3535
tests[0].expectedEqual = true
3636

3737
tests[1].symbolTable = "Linear Probing Hash Table"
38-
tests[1].equal = NewLinearHashTable[string, int](hashFunc, eqKey, eqVal, opts)
38+
tests[1].equal = NewLinearHashTable(hashFunc, eqKey, eqVal, opts)
3939
tests[1].equal.Put("Golden Pheasant", 15)
4040
tests[1].equal.Put("Harpy Eagle", 35)
4141
tests[1].equal.Put("Kingfisher", 15)
@@ -47,7 +47,7 @@ func getLinearHashTableTests() []symbolTableTest[string, int] {
4747
tests[1].expectedEqual = true
4848

4949
tests[2].symbolTable = "Linear Probing Hash Table"
50-
tests[2].equal = NewLinearHashTable[string, int](hashFunc, eqKey, eqVal, opts)
50+
tests[2].equal = NewLinearHashTable(hashFunc, eqKey, eqVal, opts)
5151
tests[2].equal.Put("Accordion", 50)
5252
tests[2].equal.Put("Bassoon", 140)
5353
tests[2].equal.Put("Cello", 120)
@@ -67,7 +67,7 @@ func getLinearHashTableTests() []symbolTableTest[string, int] {
6767
tests[2].expectedEqual = false
6868

6969
tests[3].symbolTable = "Linear Probing Hash Table"
70-
tests[3].equal = NewLinearHashTable[string, int](hashFunc, eqKey, eqVal, opts)
70+
tests[3].equal = NewLinearHashTable(hashFunc, eqKey, eqVal, opts)
7171
tests[3].equal.Put("Berlin", 10)
7272
// tests[3].equal.Put("London", 11)
7373
tests[3].equal.Put("Montreal", 6)
@@ -91,7 +91,7 @@ func TestLinearHashTable(t *testing.T) {
9191
tests := getLinearHashTableTests()
9292

9393
for _, tc := range tests {
94-
ht := NewLinearHashTable[string, int](tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
94+
ht := NewLinearHashTable(tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
9595
runSymbolTableTest(t, ht, tc)
9696
}
9797
}

symboltable/quadratic_hash_table_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@ package symboltable
33
import (
44
"testing"
55

6-
. "github.com/moorara/algo/generic"
7-
. "github.com/moorara/algo/hash"
6+
"github.com/moorara/algo/generic"
7+
"github.com/moorara/algo/hash"
88
)
99

1010
func getQuadHashTableTests() []symbolTableTest[string, int] {
11-
hashFunc := HashFuncForString[string](nil)
12-
eqKey := NewEqualFunc[string]()
13-
eqVal := NewEqualFunc[int]()
11+
hashFunc := hash.HashFuncForString[string](nil)
12+
eqKey := generic.NewEqualFunc[string]()
13+
eqVal := generic.NewEqualFunc[int]()
1414
opts := HashOpts{}
1515

1616
tests := getSymbolTableTests()
1717

1818
tests[0].symbolTable = "Quadratic Probing Hash Table"
19-
tests[0].equal = NewQuadraticHashTable[string, int](hashFunc, eqKey, eqVal, opts)
19+
tests[0].equal = NewQuadraticHashTable(hashFunc, eqKey, eqVal, opts)
2020
tests[0].equal.Put("Apple", 182)
2121
tests[0].equal.Put("Avocado", 200)
2222
tests[0].equal.Put("Banana", 120)
@@ -35,7 +35,7 @@ func getQuadHashTableTests() []symbolTableTest[string, int] {
3535
tests[0].expectedEqual = true
3636

3737
tests[1].symbolTable = "Quadratic Probing Hash Table"
38-
tests[1].equal = NewQuadraticHashTable[string, int](hashFunc, eqKey, eqVal, opts)
38+
tests[1].equal = NewQuadraticHashTable(hashFunc, eqKey, eqVal, opts)
3939
tests[1].equal.Put("Golden Pheasant", 15)
4040
tests[1].equal.Put("Harpy Eagle", 35)
4141
tests[1].equal.Put("Kingfisher", 15)
@@ -47,7 +47,7 @@ func getQuadHashTableTests() []symbolTableTest[string, int] {
4747
tests[1].expectedEqual = true
4848

4949
tests[2].symbolTable = "Quadratic Probing Hash Table"
50-
tests[2].equal = NewQuadraticHashTable[string, int](hashFunc, eqKey, eqVal, opts)
50+
tests[2].equal = NewQuadraticHashTable(hashFunc, eqKey, eqVal, opts)
5151
tests[2].equal.Put("Accordion", 50)
5252
tests[2].equal.Put("Bassoon", 140)
5353
tests[2].equal.Put("Cello", 120)
@@ -67,7 +67,7 @@ func getQuadHashTableTests() []symbolTableTest[string, int] {
6767
tests[2].expectedEqual = false
6868

6969
tests[3].symbolTable = "Quadratic Probing Hash Table"
70-
tests[3].equal = NewQuadraticHashTable[string, int](hashFunc, eqKey, eqVal, opts)
70+
tests[3].equal = NewQuadraticHashTable(hashFunc, eqKey, eqVal, opts)
7171
tests[3].equal.Put("Berlin", 10)
7272
// tests[3].equal.Put("London", 11)
7373
tests[3].equal.Put("Montreal", 6)
@@ -91,7 +91,7 @@ func TestQuadraticHashTable(t *testing.T) {
9191
tests := getQuadHashTableTests()
9292

9393
for _, tc := range tests {
94-
ht := NewQuadraticHashTable[string, int](tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
94+
ht := NewQuadraticHashTable(tc.hashKey, tc.eqKey, tc.eqVal, tc.opts)
9595
runSymbolTableTest(t, ht, tc)
9696
}
9797
}

0 commit comments

Comments
 (0)