Skip to content

Commit e5568f4

Browse files
Adding code for hashmap
1 parent 7780537 commit e5568f4

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed

Go/hashing/hashmap.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package hashing
2+
3+
import (
4+
"crypto/sha1"
5+
"errors"
6+
"hash"
7+
"strconv"
8+
)
9+
10+
type Node struct {
11+
key string
12+
value interface{}
13+
}
14+
15+
type HashMap struct {
16+
count int
17+
size int
18+
buckets [][]Node
19+
}
20+
21+
func getHash(s string) int {
22+
// hsha2 := sha256.Sum256([]byte(s))
23+
// fmt.Println(hsha2)
24+
// hashbytes := hsha2.Sum(nil)
25+
// var mySlice = []byte{244, 244, 244, 244, 244, 244, 244, 244}
26+
// data := binary.BigEndian.Uint64(mySlice)
27+
// hash, err := strconv.Atoi(fmt.Sprintf("%s", hsha2))
28+
// if err != nil {
29+
// fmt.Printf("Error in getHash function: %v", err)
30+
// return -1
31+
// }
32+
// return hash
33+
34+
var hashVal hash.Hash
35+
hashVal = sha1.New()
36+
hashVal.Write([]byte(s))
37+
38+
var bytes []byte
39+
40+
bytes = hashVal.Sum(nil)
41+
byteToInt, _ := strconv.Atoi(string(bytes))
42+
return byteToInt
43+
}
44+
45+
func (h *HashMap) getIndex(key string) int {
46+
hashInt := getHash(key)
47+
if hashInt != -1 {
48+
return getHash(key) % h.size
49+
}
50+
return -1
51+
}
52+
53+
func (h *HashMap) getValue(key string) (*Node, bool) {
54+
index := h.getIndex(key)
55+
if index == -1 {
56+
return nil, false
57+
}
58+
chain := h.buckets[index]
59+
60+
for _, node := range chain {
61+
if node.key == key {
62+
return &node, true
63+
}
64+
}
65+
return nil, false
66+
}
67+
68+
func (h *HashMap) putValue(key string, value interface{}) bool {
69+
index := h.getIndex(key)
70+
if index == -1 {
71+
return false
72+
}
73+
chain := h.buckets[index]
74+
75+
//found := false
76+
77+
for i := range chain {
78+
node := &chain[i]
79+
if node.key == key {
80+
node.value = value
81+
return true
82+
}
83+
}
84+
85+
if h.size == h.count {
86+
return false
87+
}
88+
89+
node := Node{key: key, value: value}
90+
chain = append(chain, node)
91+
h.buckets[index] = chain
92+
h.count++
93+
return true
94+
}
95+
96+
func createHashMap(expectedSize int) (*HashMap, error) {
97+
if expectedSize < 1 {
98+
return nil, errors.New("Size of the hashmap cannot be less than 1")
99+
}
100+
b := make([][]Node, expectedSize)
101+
for i := 0; i < expectedSize; i++ {
102+
b[i] = make([]Node, 0)
103+
}
104+
h := &HashMap{
105+
count: 0,
106+
size: expectedSize,
107+
buckets: b,
108+
}
109+
return h, nil
110+
}
111+
112+
func (h *HashMap) getCount() int {
113+
return h.count
114+
}
115+
116+
func (h *HashMap) getsize() int {
117+
return h.size
118+
}

Go/hashing/hashmap_impl.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package hashing
2+
3+
import "fmt"
4+
5+
func TestHashing() {
6+
hashmap, err := createHashMap(5)
7+
if err != nil {
8+
fmt.Printf("Error in creating hashmap. Error:%v", err)
9+
}
10+
11+
done := hashmap.putValue("test", "hashmap")
12+
if !done {
13+
fmt.Printf("Error adding value in hashmap. Error:%v", err)
14+
}
15+
fmt.Println(hashmap, done)
16+
// value, found := hashmap.getValue("test")
17+
// if !found {
18+
// fmt.Printf("Value not found in hashmap")
19+
// }
20+
// fmt.Println("Value found:", value.value)
21+
}

Go/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"fmt"
55

6+
"github.com/bhavnavarshney/Algorithms-and-Data-Structures/Go/hashing"
67
"github.com/bhavnavarshney/Algorithms-and-Data-Structures/Go/linkedlist"
78
)
89

@@ -35,4 +36,7 @@ func main() {
3536
l2.DeleteAtIndex(1)
3637
l2.Print("Delete at Index 1") // 5
3738

39+
fmt.Println("\n******** HASHING *********")
40+
hashing.TestHashing()
41+
3842
}

0 commit comments

Comments
 (0)