Ai
4 Star 12 Fork 9

ShirDon-廖显东/零基础Go语言算法实战源码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
hashTable.go 1.60 KB
一键复制 编辑 原始数据 按行查看 历史
ShirDon-廖显东 提交于 2024-04-22 14:56 +08:00 . first commit
// ++++++++++++++++++++++++++++++++++++++++
// 《零基础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
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/shirdonl/goAlgorithms.git
git@gitee.com:shirdonl/goAlgorithms.git
shirdonl
goAlgorithms
零基础Go语言算法实战源码
3e77a12194dd

搜索帮助