Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VertexAttributes func caching last values #177

Open
davemcphee opened this issue May 30, 2024 · 1 comment
Open

VertexAttributes func caching last values #177

davemcphee opened this issue May 30, 2024 · 1 comment

Comments

@davemcphee
Copy link

Calling AddVertex() with VertexAttribute() functions will sometimes cache the last value on all vertices. I've been unable to reproduce this via your unit tests, but this shows the issue:

package main

import (
	"bytes"
	"fmt"
	"github.com/dominikbraun/graph"
	"github.com/dominikbraun/graph/draw"
)

var (
	hashingFunction = func(d someVertex) string { return d.Name }
	redVertexAttr   = graph.VertexAttributes(map[string]string{"color": "red"})
	dotBuffer       bytes.Buffer
)

type someVertex struct {
	Name string
}

func main() {

	g := graph.New(hashingFunction, graph.Directed())

	for i, name := range []string{"foo", "bar", "baz"} {
		vertex := someVertex{Name: name}

		label := fmt.Sprintf("%s [%d]", name, i)
		fmt.Printf("adding someVertex{Name: %s} with label %s\n", name, label)

		_ = g.AddVertex(
			vertex,
			redVertexAttr,
			graph.VertexAttribute("label", label),
		)

		// read the new vertex back
		newVertex, newVertexProps, _ := g.VertexWithProperties(vertex.Name)
		fmt.Printf("new Vertex %s: %#v\nProps: %#v\n\n", vertex.Name, newVertex, newVertexProps)
	}

        fmt.Println("Here be issues:")

	fooVertex, fooProps, _ := g.VertexWithProperties("bar")
	fmt.Printf("Vertex bar: %#v\n", fooVertex)
	fmt.Printf("Vertex bar properties: %#v\n", fooProps)

	if dotErr := draw.DOT(g, &dotBuffer); dotErr != nil {
		panic(dotErr)
	}

	fmt.Printf("\n%s", dotBuffer.String())
}

---

adding someVertex{Name: foo} with label foo [0]
new Vertex foo: main.someVertex{Name:"foo"}
Props: graph.VertexProperties{Attributes:map[string]string{"color":"red", "label":"foo [0]"}, Weight:0}

adding someVertex{Name: bar} with label bar [1]
new Vertex bar: main.someVertex{Name:"bar"}
Props: graph.VertexProperties{Attributes:map[string]string{"color":"red", "label":"bar [1]"}, Weight:0}

adding someVertex{Name: baz} with label baz [2]
new Vertex baz: main.someVertex{Name:"baz"}
Props: graph.VertexProperties{Attributes:map[string]string{"color":"red", "label":"baz [2]"}, Weight:0}

Here be issues:
Vertex bar: main.someVertex{Name:"bar"}
Vertex bar properties: graph.VertexProperties{Attributes:map[string]string{"color":"red", "label":"baz [2]"}, Weight:0}

strict digraph {


        "baz" [ color="red", label="baz [2]",  weight=0 ];

        "foo" [ color="red", label="baz [2]",  weight=0 ];

        "bar" [ color="red", label="baz [2]",  weight=0 ];

}

Each vertices' Attributes map contains the Attributes of the last created vertex. Am I creating these VertexProperties functions incorrectly, or mis-using them?

tzq0301 added a commit to tzq0301/graph that referenced this issue Jun 15, 2024
tzq0301 added a commit to tzq0301/graph that referenced this issue Jun 15, 2024
tzq0301 added a commit to tzq0301/graph that referenced this issue Jun 15, 2024
tzq0301 added a commit to tzq0301/graph that referenced this issue Jun 15, 2024
tzq0301 added a commit to tzq0301/graph that referenced this issue Jun 15, 2024
…the user, preventing two maps from actually being the same reference (issue dominikbraun#177)
tzq0301 added a commit to tzq0301/graph that referenced this issue Jun 15, 2024
…the user, preventing two maps from actually being the same reference (issue dominikbraun#177)
tzq0301 added a commit to tzq0301/graph that referenced this issue Jun 15, 2024
…the user, preventing two maps from actually being the same reference (issue dominikbraun#177)
@tzq0301
Copy link
Contributor

tzq0301 commented Jun 15, 2024

Attributes map of all VertexProperties share the same map declared in the line of:

    redVertexAttr   = graph.VertexAttributes(map[string]string{"color": "red"})

This was a bug of the library, and I fixed it in PR #179, cc. @dominikbraun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants