Skip to content

Commit

Permalink
graph: remove Has method from Graph and Multigraph
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Sep 30, 2018
1 parent 7327b25 commit e0f98d2
Show file tree
Hide file tree
Showing 50 changed files with 135 additions and 210 deletions.
12 changes: 6 additions & 6 deletions graph/community/bisect_test.go
Expand Up @@ -62,7 +62,7 @@ func init() {
friends = simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range middleEast.friends {
// Ensure unconnected nodes are included.
if !friends.Has(int64(u)) {
if friends.Node(int64(u)) == nil {
friends.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -72,7 +72,7 @@ func init() {
enemies = simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range middleEast.enemies {
// Ensure unconnected nodes are included.
if !enemies.Has(int64(u)) {
if enemies.Node(int64(u)) == nil {
enemies.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -127,7 +127,7 @@ func TestProfileUndirected(t *testing.T) {
g := simple.NewUndirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -144,7 +144,7 @@ func TestProfileWeightedUndirected(t *testing.T) {
g := simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -186,7 +186,7 @@ func TestProfileDirected(t *testing.T) {
g := simple.NewDirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -203,7 +203,7 @@ func TestProfileWeightedDirected(t *testing.T) {
g := simple.NewWeightedDirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down
2 changes: 1 addition & 1 deletion graph/community/k_communities_test.go
Expand Up @@ -111,7 +111,7 @@ func TestKCliqueCommunities(t *testing.T) {
g := simple.NewUndirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down
2 changes: 1 addition & 1 deletion graph/community/louvain_directed_multiplex_test.go
Expand Up @@ -701,7 +701,7 @@ func directedMultiplexFrom(raw []layer) (DirectedLayers, []float64, error) {
g := simple.NewWeightedDirectedGraph(0, 0)
for u, e := range l.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down
20 changes: 10 additions & 10 deletions graph/community/louvain_directed_test.go
Expand Up @@ -204,7 +204,7 @@ func TestCommunityQDirected(t *testing.T) {
g := simple.NewDirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -221,7 +221,7 @@ func TestCommunityQWeightedDirected(t *testing.T) {
g := simple.NewWeightedDirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -257,7 +257,7 @@ func TestCommunityDeltaQDirected(t *testing.T) {
g := simple.NewDirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -274,7 +274,7 @@ func TestCommunityDeltaQWeightedDirected(t *testing.T) {
g := simple.NewWeightedDirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -382,7 +382,7 @@ func TestReduceQConsistencyDirected(t *testing.T) {
g := simple.NewDirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -399,7 +399,7 @@ func TestReduceQConsistencyWeightedDirected(t *testing.T) {
g := simple.NewWeightedDirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -500,7 +500,7 @@ func TestMoveLocalDirected(t *testing.T) {
g := simple.NewDirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -517,7 +517,7 @@ func TestMoveLocalWeightedDirected(t *testing.T) {
g := simple.NewWeightedDirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -562,7 +562,7 @@ func TestModularizeDirected(t *testing.T) {
g := simple.NewDirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -579,7 +579,7 @@ func TestModularizeWeightedDirected(t *testing.T) {
g := simple.NewWeightedDirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down
2 changes: 1 addition & 1 deletion graph/community/louvain_undirected_multiplex_test.go
Expand Up @@ -670,7 +670,7 @@ func undirectedMultiplexFrom(raw []layer) (UndirectedLayers, []float64, error) {
g := simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range l.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down
20 changes: 10 additions & 10 deletions graph/community/louvain_undirected_test.go
Expand Up @@ -267,7 +267,7 @@ func TestCommunityQUndirected(t *testing.T) {
g := simple.NewUndirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -284,7 +284,7 @@ func TestCommunityQWeightedUndirected(t *testing.T) {
g := simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestCommunityDeltaQUndirected(t *testing.T) {
g := simple.NewUndirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -337,7 +337,7 @@ func TestCommunityDeltaQWeightedUndirected(t *testing.T) {
g := simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -445,7 +445,7 @@ func TestReduceQConsistencyUndirected(t *testing.T) {
g := simple.NewUndirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -462,7 +462,7 @@ func TestReduceQConsistencyWeightedUndirected(t *testing.T) {
g := simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -563,7 +563,7 @@ func TestMoveLocalUndirected(t *testing.T) {
g := simple.NewUndirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -580,7 +580,7 @@ func TestMoveLocalWeightedUndirected(t *testing.T) {
g := simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down Expand Up @@ -625,7 +625,7 @@ func TestModularizeUndirected(t *testing.T) {
g := simple.NewUndirectedGraph()
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand All @@ -642,7 +642,7 @@ func TestModularizeWeightedUndirected(t *testing.T) {
g := simple.NewWeightedUndirectedGraph(0, 0)
for u, e := range test.g {
// Add nodes that are not defined by an edge.
if !g.Has(int64(u)) {
if g.Node(int64(u)) == nil {
g.AddNode(simple.Node(u))
}
for v := range e {
Expand Down
4 changes: 0 additions & 4 deletions graph/ex/fdpclust/gn.go
Expand Up @@ -16,10 +16,6 @@ type GraphNode struct {
roots []*GraphNode
}

func (g *GraphNode) Has(id int64) bool {
return g.Node(id) != nil
}

func (g *GraphNode) Node(id int64) graph.Node {
if id == g.id {
return g
Expand Down
4 changes: 0 additions & 4 deletions graph/graph.go
Expand Up @@ -27,10 +27,6 @@ type WeightedEdge interface {

// Graph is a generalized graph.
type Graph interface {
// Has returns whether a node with the given ID exists
// within the graph.
Has(id int64) bool

// Node returns the node with the given ID if it exists
// in the graph, and nil otherwise.
Node(id int64) Node
Expand Down
6 changes: 3 additions & 3 deletions graph/graphs/gen/batagelj_brandes.go
Expand Up @@ -36,7 +36,7 @@ func Gnp(dst GraphBuilder, n int, p float64, src rand.Source) error {
}

for i := 0; i < n; i++ {
if !dst.Has(int64(i)) {
if dst.Node(int64(i)) == nil {
dst.AddNode(simple.Node(i))
}
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func Gnm(dst GraphBuilder, n, m int, src rand.Source) error {
}

for i := 0; i < n; i++ {
if !dst.Has(int64(i)) {
if dst.Node(int64(i)) == nil {
dst.AddNode(simple.Node(i))
}
}
Expand Down Expand Up @@ -183,7 +183,7 @@ func SmallWorldsBB(dst GraphBuilder, n, d int, p float64, src rand.Source) error
}

for i := 0; i < n; i++ {
if !dst.Has(int64(i)) {
if dst.Node(int64(i)) == nil {
dst.AddNode(simple.Node(i))
}
}
Expand Down
2 changes: 1 addition & 1 deletion graph/graphs/gen/gen.go
Expand Up @@ -8,7 +8,7 @@ import "gonum.org/v1/gonum/graph"

// GraphBuilder is a graph that can have nodes and edges added.
type GraphBuilder interface {
Has(id int64) bool
Node(id int64) graph.Node
HasEdgeBetween(xid, yid int64) bool
graph.Builder
}
Expand Down
4 changes: 2 additions & 2 deletions graph/graphs/gen/holme_kim.go
Expand Up @@ -48,7 +48,7 @@ func TunableClusteringScaleFree(dst graph.UndirectedBuilder, n, m int, p float64
// Initial condition.
wt := make([]float64, n)
for u := 0; u < m; u++ {
if !dst.Has(int64(0)) {
if dst.Node(int64(0)) == nil {
dst.AddNode(simple.Node(u))
}
// We need to give equal probability for
Expand Down Expand Up @@ -129,7 +129,7 @@ func PreferentialAttachment(dst graph.UndirectedBuilder, n, m int, src rand.Sour
// Initial condition.
wt := make([]float64, n)
for u := 0; u < m; u++ {
if !dst.Has(int64(u)) {
if dst.Node(int64(u)) == nil {
dst.AddNode(simple.Node(u))
}
// We need to give equal probability for
Expand Down
2 changes: 1 addition & 1 deletion graph/graphs/gen/small_world.go
Expand Up @@ -40,7 +40,7 @@ func NavigableSmallWorld(dst GraphBuilder, dims []int, p, q int, r float64, src
n *= d
}
for i := 0; i < n; i++ {
if !dst.Has(int64(i)) {
if dst.Node(int64(i)) == nil {
dst.AddNode(simple.Node(i))
}
}
Expand Down
4 changes: 2 additions & 2 deletions graph/multi/directed.go
Expand Up @@ -109,15 +109,15 @@ func (g *DirectedGraph) SetLine(l graph.Line) {
lid = l.ID()
)

if !g.Has(fid) {
if _, ok := g.nodes[fid]; !ok {
g.AddNode(from)
} else {
g.nodes[fid] = from
}
if g.from[fid][tid] == nil {
g.from[fid][tid] = make(map[int64]graph.Line)
}
if !g.Has(tid) {
if _, ok := g.nodes[tid]; !ok {
g.AddNode(to)
} else {
g.nodes[tid] = to
Expand Down

0 comments on commit e0f98d2

Please sign in to comment.