From 5c037bd458b414bdf56037d53f1dc0c94dfc3504 Mon Sep 17 00:00:00 2001 From: Antoine Date: Sun, 20 Dec 2020 16:18:34 +0100 Subject: [PATCH] update neighboor node --- Makefile | 11 +++++++---- cmd/resolve.go | 25 ++++++++++++++++++++----- go.mod | 5 ++++- go.sum | 13 +++++++++++++ internal/graph.go | 32 ++++++++++++++++++++++++++++---- 5 files changed, 72 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 8043403..9e9de17 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,10 @@ .PHONY: build -build: - go build -o bin/resolve cmd/resolve.go -run: - ./bin/resolve -i graph.hcl +SRC=$(shell find ./ -name *.go -type f) + +build: $(SRC) + @go build -o bin/resolve cmd/resolve.go + +run: build + @./bin/resolve -i graph.hcl diff --git a/cmd/resolve.go b/cmd/resolve.go index 44dbd1a..a72e42d 100644 --- a/cmd/resolve.go +++ b/cmd/resolve.go @@ -7,6 +7,7 @@ import ( "log" "antoine-roux.ml/projects/go/dijkstra/internal" + stringset "antoine-roux.ml/projects/go/stringset/pkg" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/gohcl" "github.com/hashicorp/hcl/v2/hclsyntax" @@ -33,17 +34,31 @@ func main() { log.Fatalf("config parse: %s", fmt.Errorf("%w", diags)) } - fmt.Printf("%#v\n", *graph) - for _, p := range graph.Nodes { - fmt.Printf("%+v\n", p) - } + graph.InitDefaultValue() + + /* + fmt.Printf("%#v\n", *graph) + for _, p := range graph.Nodes { + fmt.Printf("%+v\n", p) + } + */ start, err := graph.GetStartingNode() fmt.Printf("%#v\n", start) - fmt.Printf("neighbourNode of %+v : \n", start) + ss := stringset.New(start) + ss.ToString() + + fmt.Printf("neighbourNode of start %+v : \n", start) neig := graph.GetNeighbour(start) for _, n := range neig { fmt.Printf("%+v\n", n) + g.UpdateNodeValue(start, n) + } + + fmt.Println("level 2 neighbour") + neig_level2 := graph.GetNeighbourForList(neig) + for _, n := range neig_level2 { + fmt.Printf("%+v\n", n) } } diff --git a/go.mod b/go.mod index e65bdf5..a6195d4 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,9 @@ module antoine-roux.ml/projects/go/dijkstra go 1.15 -require github.com/hashicorp/hcl/v2 v2.7.1 // indirect +require ( + antoine-roux.ml/projects/go/stringset v0.0.0-20201210194816-217c0c6b0107 // indirect + github.com/hashicorp/hcl/v2 v2.7.1 // indirect +) replace antoine-roux.ml/projects/go/dijkstra/internal => ./internal diff --git a/go.sum b/go.sum index d53ee18..40558bc 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,16 @@ +antoine-roux.ml/projects/go/stringset v0.0.0-20201210000104-ec47cfc5deb8 h1:naE6AbkrQrQ2Qjf9DNv7LyJCnXTTaeWh29uYeq6Gqj8= +antoine-roux.ml/projects/go/stringset v0.0.0-20201210000104-ec47cfc5deb8/go.mod h1:GIIinJ3Sk+3SAeD5mgCIZA41nHQPQugBPRWuovH7A04= +antoine-roux.ml/projects/go/stringset v0.0.0-20201210011018-5e719ecd03b8 h1:psgKgVoX0mjIEkSpph8FwaWksUPx+yBBfPUk2+kQB6w= +antoine-roux.ml/projects/go/stringset v0.0.0-20201210194816-217c0c6b0107 h1:OZVm6yoUyTwqSN4lvsPtC277bD9yLUCUdXJ6EAxY5Uo= +antoine-roux.ml/projects/go/stringset v0.0.0-20201210194816-217c0c6b0107/go.mod h1:GIIinJ3Sk+3SAeD5mgCIZA41nHQPQugBPRWuovH7A04= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210000104-ec47cfc5deb8 h1:Wd0y/9Q2/HykAgrtjzHCAWYwtMp0FY5rLTN4BLmTiDc= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210000104-ec47cfc5deb8/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210010243-7fbab5b6f83c h1:HmZ2AqLnsY8cXtUT9fVRD2r/9pgLsOuEr01FZSL49Ow= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210010243-7fbab5b6f83c/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210011018-5e719ecd03b8 h1:AdMbWCM8OChpBWtKnTsuDbZyTFNINx20DL/fwB06/DA= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210011018-5e719ecd03b8/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210194816-217c0c6b0107 h1:Jm9nLeJy25ICJauRSVqFqsiZgQ0MuKcTkT5I287Eq5s= +antoine-roux.ml/projects/go/stringset.git v0.0.0-20201210194816-217c0c6b0107/go.mod h1:fJ7Mch8To4ZrL7lX5Dym4p+BAdSPtXnEq4Cf9/14Ei4= github.com/agext/levenshtein v1.2.1 h1:QmvMAjj2aEICytGiWzmxoE0x2KZvE0fvmqMOfy2tjT8= github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/apparentlymart/go-dump v0.0.0-20180507223929-23540a00eaa3/go.mod h1:oL81AME2rN47vu18xqj1S1jPIPuN7afo62yKTNn3XMM= diff --git a/internal/graph.go b/internal/graph.go index 3eabe43..0922915 100644 --- a/internal/graph.go +++ b/internal/graph.go @@ -20,7 +20,7 @@ func (g *Graph) GetStartingNode() (Node, error) { return Node{}, errors.New("No start node found") } -func (g *Graph) GetNeighbour(n Node) []Node { +func (g *Graph) GetNeighbour(n Node) []*Node { var neighbourNodeName []string for i := 0; i < len(g.Links); i++ { if g.Links[i].From == n.Name { @@ -29,14 +29,38 @@ func (g *Graph) GetNeighbour(n Node) []Node { } // create temporary map of node by here name for direct future access - var nodeByName = make(map[string]Node) + var nodeByName = make(map[string]*Node) for i := 0; i < len(g.Nodes); i++ { - nodeByName[g.Nodes[i].Name] = (*g.Nodes[i]) + nodeByName[g.Nodes[i].Name] = g.Nodes[i] } - var neighbourNode = make([]Node, len(neighbourNodeName)) + var neighbourNode = make([]*Node, len(neighbourNodeName)) for i := 0; i < len(neighbourNodeName); i++ { neighbourNode[i] = nodeByName[neighbourNodeName[i]] } return neighbourNode } + +func (g *Graph) GetNeighbourForList(nodes []*Node) []*Node { + var allNeighbour []*Node + for i := 0; i < len(nodes); i++ { + allNeighbour = append(allNeighbour, g.GetNeighbour(*nodes[i])...) + } + return allNeighbour +} + +func (g *Graph) InitDefaultValue() { + for i := 0; i < len(g.Nodes); i++ { + if !g.Nodes[i].IsStart { + g.Nodes[i].Value = -1 + } + } +} + +func (g *Graph) UpdateNodeValue(source *Node, nodeToUpdate *[]Node) { + for _, n := range nodeToUpdate { + newPotentialValue = n.Value + + + n.Value += source.Value + } +}