smallnest / safemap

a thread-safe concurrent map for go
MIT License
49 stars 6 forks source link

the comparable structs hash key #1

Open shikbupt opened 2 years ago

shikbupt commented 2 years ago

the comparable type contains structs whose fields are all comparable types. the hash key of the comparable structs is its pointer, but i think the values of the struct's fields should be the hash key. otherwise, the struct should be saved in the process lifecycle.

smallnest commented 2 years ago

The buildin map looks use unsade.Pointer, not the values of struct:

package main

import "fmt"

type S struct {
    I int
}

func main() {
    var m = make(map[*S]int)
    s1 := &S{I: 1}
    m[s1] = 1
    fmt.Println(m[s1]) // 1

    s2 := &S{I: 1}
    fmt.Println(m[s2]) // 0

    s1.I = 2
    fmt.Println(m[s1]) // 1
}
shikbupt commented 2 years ago

the struct can be the map key.

package main

import "fmt"

type S struct {
    I int
}

func main() {
    var m = make(map[S]int)
    s1 := S{I: 1}
    m[s1] = 1
    fmt.Println(m[s1]) // 1

    s2 := S{I: 1}
    fmt.Println(m[s2]) // 1

    s1.I = 2
    fmt.Println(m[s1]) // 0
}