Skip to content

Commit 49d54c7

Browse files
authored
Merge pull request #142 from bakurits/master
Refactored hashmap
2 parents 95872dc + 0f34f65 commit 49d54c7

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

data-structures/hash-map/hash_map.go

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package data-structures
1+
package hashmap
22

33
import (
44
"fmt"
@@ -13,20 +13,23 @@ type node struct {
1313
next *node
1414
}
1515

16-
type hashMap struct {
16+
// HashMap is golang implementation of hashmap
17+
type HashMap struct {
1718
capacity uint64
1819
size uint64
1920
table []*node
2021
}
2122

22-
func newHashMap() *hashMap {
23-
return &hashMap{
23+
// New return new HashMap instance
24+
func New() *HashMap {
25+
return &HashMap{
2426
capacity: defaultCapacity,
2527
table: make([]*node, defaultCapacity),
2628
}
2729
}
2830

29-
func (hm *hashMap) get(key interface{}) interface{} {
31+
// Get returns value associated with given key
32+
func (hm *HashMap) Get(key interface{}) interface{} {
3033
node := hm.getNodeByHash(hm.hash(key))
3134

3235
if node != nil {
@@ -36,11 +39,23 @@ func (hm *hashMap) get(key interface{}) interface{} {
3639
return nil
3740
}
3841

39-
func (hm *hashMap) put(key interface{}, value interface{}) interface{} {
42+
// Put puts new key value in hashmap
43+
func (hm *HashMap) Put(key interface{}, value interface{}) interface{} {
4044
return hm.putValue(hm.hash(key), key, value)
4145
}
4246

43-
func (hm *hashMap) putValue(hash uint64, key interface{}, value interface{}) interface{} {
47+
// Contains checks if given key is stored in hashmap
48+
func (hm *HashMap) Contains(key interface{}) bool {
49+
node := hm.getNodeByHash(hm.hash(key))
50+
51+
if node != nil {
52+
return true
53+
}
54+
55+
return false
56+
}
57+
58+
func (hm *HashMap) putValue(hash uint64, key interface{}, value interface{}) interface{} {
4459
if hm.capacity == 0 {
4560
hm.capacity = defaultCapacity
4661
hm.table = make([]*node, defaultCapacity)
@@ -66,21 +81,11 @@ func (hm *hashMap) putValue(hash uint64, key interface{}, value interface{}) int
6681

6782
}
6883

69-
func (hm *hashMap) contains(key interface{}) bool {
70-
node := hm.getNodeByHash(hm.hash(key))
71-
72-
if node != nil {
73-
return true
74-
}
75-
76-
return false
77-
}
78-
79-
func (hm *hashMap) getNodeByHash(hash uint64) *node {
84+
func (hm *HashMap) getNodeByHash(hash uint64) *node {
8085
return hm.table[hash]
8186
}
8287

83-
func (hm *hashMap) resize() {
88+
func (hm *HashMap) resize() {
8489
hm.capacity <<= 1
8590

8691
tempTable := hm.table
@@ -112,30 +117,11 @@ func newNodeWithNext(key interface{}, value interface{}, next *node) *node {
112117
}
113118
}
114119

115-
func (hm *hashMap) hash(key interface{}) uint64 {
120+
func (hm *HashMap) hash(key interface{}) uint64 {
116121
h := fnv.New64a()
117-
h.Write([]byte(fmt.Sprintf("%v", key)))
122+
_, _ = h.Write([]byte(fmt.Sprintf("%v", key)))
118123

119124
hashValue := h.Sum64()
120125

121126
return (hm.capacity - 1) & (hashValue ^ (hashValue >> 16))
122127
}
123-
124-
// func main() {
125-
// hashMap := newHashMap()
126-
127-
// hashMap.put("test-1", 10)
128-
// fmt.Println(hashMap.get("test-1"))
129-
130-
// hashMap.put("test-1", 20)
131-
// hashMap.put("test-2", 30)
132-
// hashMap.put(1, 40)
133-
134-
// fmt.Println(hashMap.get("test-1"))
135-
// fmt.Println(hashMap.get("test-2"))
136-
// fmt.Println(hashMap.get(1))
137-
138-
// fmt.Println(hashMap.contains(2))
139-
// fmt.Println(hashMap.contains(1))
140-
// fmt.Println(hashMap.contains("test-1"))
141-
// }
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package hashmap_test
2+
3+
import (
4+
hashmap "TheAlgorithms/Go/data-structures/hash-map"
5+
"fmt"
6+
"testing"
7+
)
8+
9+
func TestHashMap_Contains(t *testing.T) {
10+
11+
mp := hashmap.New()
12+
13+
mp.Put("test-1", 10)
14+
fmt.Println(mp.Get("test-1"))
15+
16+
mp.Put("test-1", 20)
17+
mp.Put("test-2", 30)
18+
mp.Put(1, 40)
19+
20+
fmt.Println(mp.Get("test-1"))
21+
fmt.Println(mp.Get("test-2"))
22+
fmt.Println(mp.Get(1))
23+
24+
fmt.Println(mp.Contains(2))
25+
fmt.Println(mp.Contains(1))
26+
fmt.Println(mp.Contains("test-1"))
27+
28+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module TheAlgorithms/Go
2+
3+
go 1.14

0 commit comments

Comments
 (0)