hello-algo/codes/go/chapter_hashing/array_hash_map.go

98 lines
1.7 KiB
Go
Raw Normal View History

// File: array_hash_map.go
// Created Time: 2022-12-14
// Author: msk397 (machangxinq@gmail.com)
package chapter_hashing
import "fmt"
/* 键值对 */
type pair struct {
key int
val string
}
/* 基于数组简易实现的哈希表 */
type arrayHashMap struct {
buckets []*pair
}
/* 初始化哈希表 */
func newArrayHashMap() *arrayHashMap {
2023-03-15 03:48:31 +08:00
// 初始化数组,包含 100 个桶
buckets := make([]*pair, 100)
2023-03-15 03:11:43 +08:00
return &arrayHashMap{buckets: buckets}
}
/* 哈希函数 */
func (a *arrayHashMap) hashFunc(key int) int {
index := key % 100
return index
}
/* 查询操作 */
func (a *arrayHashMap) get(key int) string {
index := a.hashFunc(key)
2023-03-15 03:11:43 +08:00
pair := a.buckets[index]
if pair == nil {
return "Not Found"
}
return pair.val
}
/* 添加操作 */
func (a *arrayHashMap) put(key int, val string) {
pair := &pair{key: key, val: val}
index := a.hashFunc(key)
2023-03-15 03:11:43 +08:00
a.buckets[index] = pair
}
/* 删除操作 */
func (a *arrayHashMap) remove(key int) {
index := a.hashFunc(key)
2022-12-15 23:08:07 +08:00
// 置为 nil ,代表删除
2023-03-15 03:11:43 +08:00
a.buckets[index] = nil
}
/* 获取所有键对 */
func (a *arrayHashMap) pairSet() []*pair {
var pairs []*pair
2023-03-15 03:11:43 +08:00
for _, pair := range a.buckets {
if pair != nil {
pairs = append(pairs, pair)
}
}
return pairs
}
/* 获取所有键 */
func (a *arrayHashMap) keySet() []int {
var keys []int
2023-03-15 03:11:43 +08:00
for _, pair := range a.buckets {
if pair != nil {
keys = append(keys, pair.key)
}
}
return keys
}
/* 获取所有值 */
func (a *arrayHashMap) valueSet() []string {
var values []string
2023-03-15 03:11:43 +08:00
for _, pair := range a.buckets {
if pair != nil {
values = append(values, pair.val)
}
}
return values
}
/* 打印哈希表 */
func (a *arrayHashMap) print() {
2023-03-15 03:11:43 +08:00
for _, pair := range a.buckets {
if pair != nil {
fmt.Println(pair.key, "->", pair.val)
}
}
2022-12-15 23:08:07 +08:00
}