These are small simple implementations of many Data Structures and Algorithms using Go. I'm doing this to refresh my memory of them, and to learn Go better.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
go-dsa-study/hashmap/impl.go

52 lines
815 B

package hashmap
import (
"hash/crc32"
)
const (
BucketCount = 256
)
type HashNode struct {
key string
val any
}
type HashBucket struct {
contents []HashNode
}
type Hashmap struct {
buckets [BucketCount]HashBucket
}
func New() *Hashmap {
hmap := new(Hashmap)
return hmap
}
func (hmap *Hashmap) Hash(key string) uint32 {
return crc32.ChecksumIEEE([]byte(key)) % BucketCount
}
func (hmap *Hashmap) Add(key string, val any) {
id := hmap.Hash(key)
the_bucket := &hmap.buckets[id]
node := HashNode{key, val}
the_bucket.contents = append(the_bucket.contents, node)
}
func (hmap *Hashmap) Get(key string) any {
id := hmap.Hash(key)
the_bucket := &hmap.buckets[id]
for _, node := range the_bucket.contents {
if node.key == key {
return node.val
}
}
return nil
}