开发者

Using a map for its set properties with user defined types

开发者 https://www.devze.com 2023-02-04 07:52 出处:网络
I\'m trying to use the built-in map type as a set for a type of my own (Point, in this case). The problem is, when I assign a Point to the map, and then later create a new, but equal point and use it

I'm trying to use the built-in map type as a set for a type of my own (Point, in this case). The problem is, when I assign a Point to the map, and then later create a new, but equal point and use it as a key, the map behaves as though that key is not in the map. Is this not possible to do?

// maptest.go

package main

import "fmt"

func main() {
    set := make(map[*Point]bool)

    printSet(set)
    set[NewPoint(0, 0)] = true
    printSet(set)
    set[NewPoint(0, 2)] = true
    printSet(set)

    _, ok := set[NewPoint(3, 3)] // not in map
    if !ok {
        fmt.Print("correct error code for non existent element\n")
    } else {
        fmt.Print("incorrect error code for non existent element\n")
    }

    c, ok := set[NewPoint(0, 2)] // another one just like it already in map
    if ok {
        fmt.Print("correct error code for existent element\n") // should 开发者_StackOverflow中文版get this
    } else {
        fmt.Print("incorrect error code for existent element\n") // get this
    }

    fmt.Printf("c: %t\n", c)
}

func printSet(stuff map[*Point]bool) {
    fmt.Print("Set:\n")
    for k, v := range stuff {
        fmt.Printf("%s: %t\n", k, v)
    }
}

type Point struct {
    row int
    col int
}

func NewPoint(r, c int) *Point {
    return &Point{r, c}
}

func (p *Point) String() string {
    return fmt.Sprintf("{%d, %d}", p.row, p.col)
}

func (p *Point) Eq(o *Point) bool {
    return p.row == o.row && p.col == o.col
}


package main

import "fmt"

type Point struct {
    row int
    col int
}

func main() {
    p1 := &Point{1, 2}
    p2 := &Point{1, 2}
    fmt.Printf("p1: %p %v p2: %p %v\n", p1, *p1, p2, *p2)

    s := make(map[*Point]bool)
    s[p1] = true
    s[p2] = true
    fmt.Println("s:", s)

    t := make(map[int64]*Point)
t[int64(p1.row)<<32+int64(p1.col)] = p1
t[int64(p2.row)<<32+int64(p2.col)] = p2
    fmt.Println("t:", t)
}

Output:
p1: 0x7fc1def5e040 {1 2} p2: 0x7fc1def5e0f8 {1 2}
s: map[0x7fc1def5e0f8:true 0x7fc1def5e040:true]
t: map[4294967298:0x7fc1def5e0f8]

If we create pointers to two Points p1 and p2 with the same coordinates they point to different addresses.

s := make(map[*Point]bool) creates a map where the key is a pointer to the memory allocated to a Point and the value is boolean value. Therefore, if we assign elements p1 and p2 to the map s then we have two distinct map keys and two distinct map elements with the same coordinates.

t := make(map[int64]*Point) creates a map where the key is a composite of the coordinates of a Point and the value is a pointer to the Point coordinates. Therefore, if we assign elements p1 and p2 to the map t then we have two equal map keys and one map element with the shared coordinates.

0

精彩评论

暂无评论...
验证码 换一张
取 消