Skip to content

Commit 3a6ca15

Browse files
authored
Merge pull request #1 from TheAlgorithms/master
Go naming convention updates
2 parents f3b9ece + 5059797 commit 3a6ca15

File tree

3 files changed

+145
-2
lines changed

3 files changed

+145
-2
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* [Binary-Tree](https://github.com/TheAlgorithms/Go/blob/master/data-structures/binary-tree/binary-tree.go)
99
* Dynamic-Array
1010
* [Dynamic Array](https://github.com/TheAlgorithms/Go/blob/master/data-structures/dynamic-array/dynamic_array.go)
11+
* Hash-Map
12+
* [Hash Map](https://github.com/TheAlgorithms/Go/blob/master/data-structures/hash-map/hash_map.go)
1113
* Linked-List
1214
* [Double-Linkedlist](https://github.com/TheAlgorithms/Go/blob/master/data-structures/linked-list/double-linkedlist.go)
1315
* [Linkedlist](https://github.com/TheAlgorithms/Go/blob/master/data-structures/linked-list/Linkedlist.go)

data-structures/hash-map/hash_map.go

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package data-structures
2+
3+
import (
4+
"fmt"
5+
"hash/fnv"
6+
)
7+
8+
var defaultCapacity uint64 = 1 << 10
9+
10+
type node struct {
11+
key interface{}
12+
value interface{}
13+
next *node
14+
}
15+
16+
type hashMap struct {
17+
capacity uint64
18+
size uint64
19+
table []*node
20+
}
21+
22+
func newHashMap() *hashMap {
23+
return &hashMap{
24+
capacity: defaultCapacity,
25+
table: make([]*node, defaultCapacity),
26+
}
27+
}
28+
29+
func (hm *hashMap) get(key interface{}) interface{} {
30+
node := hm.getNodeByHash(hm.hash(key))
31+
32+
if node != nil {
33+
return node.value
34+
}
35+
36+
return nil
37+
}
38+
39+
func (hm *hashMap) put(key interface{}, value interface{}) interface{} {
40+
return hm.putValue(hm.hash(key), key, value)
41+
}
42+
43+
func (hm *hashMap) putValue(hash uint64, key interface{}, value interface{}) interface{} {
44+
if hm.capacity == 0 {
45+
hm.capacity = defaultCapacity
46+
hm.table = make([]*node, defaultCapacity)
47+
}
48+
49+
node := hm.getNodeByHash(hash)
50+
51+
if node == nil {
52+
hm.table[hash] = newNode(key, value)
53+
54+
} else if node.key == key {
55+
hm.table[hash] = newNodeWithNext(key, value, node)
56+
return value
57+
58+
} else {
59+
hm.resize()
60+
return hm.putValue(hash, key, value)
61+
}
62+
63+
hm.size++
64+
65+
return value
66+
67+
}
68+
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 {
80+
return hm.table[hash]
81+
}
82+
83+
func (hm *hashMap) resize() {
84+
hm.capacity <<= 1
85+
86+
tempTable := hm.table
87+
88+
hm.table = make([]*node, hm.capacity)
89+
90+
for i := 0; i < len(tempTable); i++ {
91+
node := tempTable[i]
92+
if node == nil {
93+
continue
94+
}
95+
96+
hm.table[hm.hash(node.key)] = node
97+
}
98+
}
99+
100+
func newNode(key interface{}, value interface{}) *node {
101+
return &node{
102+
key: key,
103+
value: value,
104+
}
105+
}
106+
107+
func newNodeWithNext(key interface{}, value interface{}, next *node) *node {
108+
return &node{
109+
key: key,
110+
value: value,
111+
next: next,
112+
}
113+
}
114+
115+
func (hm *hashMap) hash(key interface{}) uint64 {
116+
h := fnv.New64a()
117+
h.Write([]byte(fmt.Sprintf("%v", key)))
118+
119+
hashValue := h.Sum64()
120+
121+
return (hm.capacity - 1) & (hashValue ^ (hashValue >> 16))
122+
}
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+
// }

other/maxSubarraySum.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ func Max(x int, y int) int {
1717
func maxSubarraySum(array []int) int {
1818
var currentMax int = 0
1919
var maxTillNow int = 0
20-
for i := 0; i < len(array); i++ {
21-
currentMax = Max(array[i], currentMax + array[i])
20+
for _,v :=range array{
21+
currentMax = Max(v, currentMax + v)
2222
maxTillNow = Max(maxTillNow, currentMax)
2323
}
2424
return maxTillNow

0 commit comments

Comments
 (0)