使用地图来存储具有用户定义类型的集合属性

huangapple go评论90阅读模式
英文:

Using a map for its set properties with user defined types

问题

我试图使用内置的map类型作为我自己的类型(在这种情况下是Point)的集合。问题是,当我将一个Point赋值给map,并且稍后创建一个新的但相等的point并将其用作键时,map的行为就好像该键不在map中一样。这样做是不可能的吗?

// 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 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
}
英文:

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 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
}

答案1

得分: 3

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]

如果我们创建指向具有相同坐标的两个Point p1p2的指针,它们指向不同的地址。

s := make(map[*Point]bool) 创建一个映射,其中键是指向分配给Point的内存的指针,值是布尔值。因此,如果我们将元素p1p2分配给映射s,那么我们就有了两个不同的映射键和两个具有相同坐标的映射元素。

t := make(map[int64]*Point) 创建一个映射,其中键是Point的坐标的组合,值是指向Point坐标的指针。因此,如果我们将元素p1p2分配给映射t,那么我们就有了两个相等的映射键和一个具有共享坐标的映射元素。

英文:
package main

import &quot;fmt&quot;

type Point struct {
	row int
	col int
}

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

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

	t := make(map[int64]*Point)
t[int64(p1.row)&lt;&lt;32+int64(p1.col)] = p1
t[int64(p2.row)&lt;&lt;32+int64(p2.col)] = p2
	fmt.Println(&quot;t:&quot;, 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.

huangapple
  • 本文由 发表于 2011年1月17日 04:18:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/4707790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定