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