Skip to content

Commit

Permalink
Merge branch 'master' of github.com:goccy/go-graphviz into get_api
Browse files Browse the repository at this point in the history
  • Loading branch information
denk0403 committed Dec 13, 2023
2 parents fde9f99 + 9f8e121 commit 056cc36
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 45 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
go-version: [ "1.18", "1.19", "1.20" ]
go-version: [ "1.20", "1.21" ]
runs-on: ${{ matrix.os }}
steps:
- name: checkout
uses: actions/checkout@v4
- name: setup Go ${{ matrix.go-version }}
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
- name: checkout
uses: actions/checkout@v3
- name: build dot command
run: go build -v cmd/dot/dot.go
- name: test
Expand Down
36 changes: 36 additions & 0 deletions cgraph/cgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -1135,10 +1135,46 @@ func (g *Graph) NumberSubGraph() int {
return ccall.Agnsubg(g.Agraph)
}

// Returns the degree of the given node in the graph, where arguments "in" and
// "out" are C-like booleans that select which edge sets to query.
//
// g.Degree(node, 0, 0) // always returns 0
// g.Degree(node, 0, 1) // returns the node's outdegree
// g.Degree(node, 1, 0) // returns the node's indegree
// g.Degree(node, 1, 1) // returns the node's total degree (indegree + outdegree)
func (g *Graph) Degree(n *Node, in, out int) int {
return ccall.Agdegree(g.Agraph, n.Agnode, in, out)
}

// Returns the indegree of the given node in the graph.
//
// Note: While undirected graphs don't normally have a
// notion of indegrees, calling this method on an
// undirected graph will treat it as if it's directed.
// As a result, it's best to avoid calling this method
// on an undirected graph.
func (g *Graph) Indegree(n *Node) int {
return ccall.Agdegree(g.Agraph, n.Agnode, 1, 0)
}

// Returns the outdegree of the given node in the graph.
//
// Note: While undirected graphs don't normally have a
// notion of outdegrees, calling this method on an
// undirected graph will treat it as if it's directed.
// As a result, it's best to avoid calling this method
// on an undirected graph.
func (g *Graph) Outdegree(n *Node) int {
return ccall.Agdegree(g.Agraph, n.Agnode, 0, 1)
}

// Returns the total degree of the given node in the graph.
// This can be thought of as the total number of edges coming
// in and out of a node.
func (g *Graph) TotalDegree(n *Node) int {
return ccall.Agdegree(g.Agraph, n.Agnode, 1, 1)
}

func (g *Graph) CountUniqueEdges(n *Node, in, out int) int {
return ccall.Agcountuniqedges(g.Agraph, n.Agnode, in, out)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/goccy/go-graphviz

go 1.18
go 1.20

require (
github.com/corona10/goimagehash v1.0.2
Expand All @@ -9,7 +9,7 @@ require (
github.com/jessevdk/go-flags v1.4.0
github.com/pkg/errors v0.9.1
golang.org/x/crypto v0.7.0
golang.org/x/image v0.6.0
golang.org/x/image v0.14.0
)

require (
Expand Down
35 changes: 2 additions & 33 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,11 @@ github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5 h1:BvoENQQU+fZ9uukda/R
github.com/nfnt/resize v0.0.0-20160724205520-891127d8d1b5/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A=
golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU=
golang.org/x/image v0.6.0 h1:bR8b5okrPI3g/gyZakLZHeWxAR8Dn5CyxXv1hLH5g/4=
golang.org/x/image v0.6.0/go.mod h1:MXLdDR43H7cDJq5GEGXEVeeNhPgi+YYEQ2pC1byI1x0=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/image v0.14.0 h1:tNgSxAFe3jC4uYqvZdTr84SZoM1KfwdC9SKIFrLjFn4=
golang.org/x/image v0.14.0/go.mod h1:HUYqC05R2ZcZ3ejNQsIHQDQiwWM4JBqmm6MKANTp4LE=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.8.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
65 changes: 65 additions & 0 deletions graphviz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,68 @@ func TestGetEdge(t *testing.T) {
t.Fatalf("expected graph to have 1 edge, but found %d", edges)
}
}

func TestNodeDegree(t *testing.T) {
type test struct {
node_name string
expected_indegree int
expected_outdegree int
expected_total_degree int
}

type graphtest struct {
input string
tests []test
}

graphtests := []graphtest{
{input: "digraph test { a -> b }", tests: []test{
{node_name: "a", expected_indegree: 0, expected_outdegree: 1, expected_total_degree: 1},
{node_name: "b", expected_indegree: 1, expected_outdegree: 0, expected_total_degree: 1},
}},
{input: "digraph test { a -> b; a -> b; a -> a; c -> a }", tests: []test{
{node_name: "a", expected_indegree: 2, expected_outdegree: 3, expected_total_degree: 5},
{node_name: "b", expected_indegree: 2, expected_outdegree: 0, expected_total_degree: 2},
{node_name: "c", expected_indegree: 0, expected_outdegree: 1, expected_total_degree: 1},
}},
{input: "graph test { a -- b; a -- b; a -- a; c -- a }", tests: []test{
{node_name: "a", expected_indegree: 2, expected_outdegree: 3, expected_total_degree: 5},
{node_name: "b", expected_indegree: 2, expected_outdegree: 0, expected_total_degree: 2},
{node_name: "c", expected_indegree: 0, expected_outdegree: 1, expected_total_degree: 1},
}},
{input: "strict graph test { a -- b; b -- a; a -- a; c -- a }", tests: []test{
{node_name: "a", expected_indegree: 2, expected_outdegree: 2, expected_total_degree: 4},
{node_name: "b", expected_indegree: 1, expected_outdegree: 0, expected_total_degree: 1},
{node_name: "c", expected_indegree: 0, expected_outdegree: 1, expected_total_degree: 1},
}},
}

for _, graphtest := range graphtests {
input := graphtest.input
graph, err := graphviz.ParseBytes([]byte(input))
if err != nil {
t.Fatalf("Input: %s. Error: %+v", input, err)
}

for _, test := range graphtest.tests {
node_name := test.node_name
node, err := graph.Node(node_name)
if err != nil || node == nil {
t.Fatalf("Unable to retrieve node '%s'. Input: %s. Error: %+v", node_name, input, err)
}

indegree := graph.Indegree(node)
if test.expected_indegree != indegree {
t.Errorf("Unexpected indegree for node '%s'. Input: %s. Expected: %d. Actual: %d.", node_name, input, test.expected_indegree, indegree)
}
outdegree := graph.Outdegree(node)
if test.expected_outdegree != outdegree {
t.Errorf("Unexpected outdegree for node '%s'. Input: %s. Expected: %d. Actual: %d.", node_name, input, test.expected_outdegree, outdegree)
}
total_degree := graph.TotalDegree(node)
if test.expected_total_degree != total_degree {
t.Errorf("Unexpected total degree for node '%s'. Input: %s. Expected: %d. Actual: %d.", node_name, input, test.expected_total_degree, total_degree)
}
}
}
}
8 changes: 2 additions & 6 deletions internal/ccall/dotgen/dotsplines.c
Original file line number Diff line number Diff line change
Expand Up @@ -2307,9 +2307,7 @@ static void recover_slack(edge_t * e, path * p)
}
}

static void resize_vn(vn, lx, cx, rx)
node_t *vn;
int lx, cx, rx;
static void resize_vn(node_t * vn, int lx, int cx, int rx)
{
ND_coord(vn).x = cx;
ND_lw(vn) = cx - lx, ND_rw(vn) = rx - cx;
Expand Down Expand Up @@ -2501,9 +2499,7 @@ _neighbor(graph_t* g, node_t *vn, edge_t *ie, edge_t *oe, int dir)
return rv;
}

static boolean pathscross(n0, n1, ie1, oe1)
node_t *n0, *n1;
edge_t *ie1, *oe1;
static boolean pathscross(node_t * n0, node_t * n1, edge_t * ie1, edge_t * oe1)
{
edge_t *e0, *e1;
node_t *na, *nb;
Expand Down

0 comments on commit 056cc36

Please sign in to comment.