1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 09:12:28 +08:00

refactor: update HashMap Put method

This commit is contained in:
dudaodong
2022-08-24 10:20:02 +08:00
parent 4eeeabb227
commit 551e66ba29

View File

@@ -53,11 +53,11 @@ func (hm *HashMap) Get(key any) any {
} }
// Put new key value in hashmap // Put new key value in hashmap
func (hm *HashMap) Put(key any, value any) any { func (hm *HashMap) Put(key any, value any) {
return hm.putValue(hm.hash(key), key, value) hm.putValue(hm.hash(key), key, value)
} }
func (hm *HashMap) putValue(hash uint64, key, value any) any { func (hm *HashMap) putValue(hash uint64, key, value any) {
if hm.capacity == 0 { if hm.capacity == 0 {
hm.capacity = defaultMapCapacity hm.capacity = defaultMapCapacity
hm.table = make([]*mapNode, defaultMapCapacity) hm.table = make([]*mapNode, defaultMapCapacity)
@@ -68,14 +68,11 @@ func (hm *HashMap) putValue(hash uint64, key, value any) any {
hm.table[hash] = newMapNode(key, value) hm.table[hash] = newMapNode(key, value)
} else if node.key == key { } else if node.key == key {
hm.table[hash] = newMapNodeWithNext(key, value, node) hm.table[hash] = newMapNodeWithNext(key, value, node)
return value
} else { } else {
hm.resize() hm.resize()
return hm.putValue(hash, value, value) hm.putValue(hash, value, value)
} }
hm.size++ hm.size++
return value
} }
// Delete item by given key in hashmap // Delete item by given key in hashmap