代码拉取完成,页面将自动刷新
// ++++++++++++++++++++++++++++++++++++++++
// 《零基础Go语言算法实战》源码
// ++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// Gitee:https://gitee.com/shirdonl/goAlgorithms.git
// Buy link :https://item.jd.com/14101229.html
// ++++++++++++++++++++++++++++++++++++++++
package main
import (
"fmt"
"sync"
)
// 散列表
type HashTable struct {
items map[int]Value
lock sync.RWMutex
}
// 键
type Key interface{}
// 值
type Value interface{}
// 具有值 v 和键 k 的项目进入散列表
func (ht *HashTable) Put(k Key, v Value) {
ht.lock.Lock()
defer ht.lock.Unlock()
i := hash(k)
if ht.items == nil {
ht.items = make(map[int]Value)
}
ht.items[i] = v
}
// 从散列表中删除键为 k 的项
func (ht *HashTable) Remove(k Key) {
ht.lock.Lock()
defer ht.lock.Unlock()
i := hash(k)
delete(ht.items, i)
}
// 从散列表中获取键为 k 的项
func (ht *HashTable) Get(k Key) Value {
ht.lock.RLock()
defer ht.lock.RUnlock()
i := hash(k)
return ht.items[i]
}
// 返回散列表元素的数量
func (ht *HashTable) Size() int {
ht.lock.RLock()
defer ht.lock.RUnlock()
return len(ht.items)
}
// 生成时间复杂度为 O(n) 的字符串
func hash(k Key) int {
key := fmt.Sprintf("%s", k)
h := 0
for i := 0; i < len(key); i++ {
h = 31*h + int(key[i])
}
return h
}
func main() {
hashTable := HashTable{}
for i := 1; i < 5; i++ {
hashTable.Put(fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i))
value2 := hashTable.Get("key2")
fmt.Println(value2)
}
}
//$ go run hashTable.go
//<nil>
//value2
//value2
//value2
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。