Skip to content

Commit

Permalink
chore: small API refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Jun 27, 2019
1 parent fff2477 commit e3026b9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
2 changes: 1 addition & 1 deletion edge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ func ExampleNewEdge() {
va := NewVertex("aaa")
vb := NewVertex("bbb")
vc := NewVertex("ccc")
vc.SetAttr(NewAttr("ddd", "eee"))
vc.AddAttr(NewAttr("ddd", "eee"))

eab := NewEdge(va, vb)
eac := NewEdge(va, vc)
Expand Down
16 changes: 8 additions & 8 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Graph interface {
AddEdge(...Edge)
Vertices() []Vertex
Edges() []Edge
StandaloneVertices() []Vertex
IsolatedVertices() []Vertex
}

type graph struct {
Expand All @@ -31,17 +31,17 @@ func NewGraph() Graph {
func (g *graph) Vertices() []Vertex { return g.vertices }
func (g *graph) Edges() []Edge { return g.edges }

func (g *graph) StandaloneVertices() []Vertex {
standaloneVertices := map[string]Vertex{}
func (g *graph) IsolatedVertices() []Vertex {
isolatedVertices := map[string]Vertex{}
for _, vertex := range g.vertices {
standaloneVertices[vertex.ID()] = vertex
isolatedVertices[vertex.ID()] = vertex
}
for _, edge := range g.edges {
standaloneVertices[edge.Src().ID()] = nil
standaloneVertices[edge.Dst().ID()] = nil
isolatedVertices[edge.Src().ID()] = nil
isolatedVertices[edge.Dst().ID()] = nil
}
filtered := []Vertex{}
for _, vertex := range standaloneVertices {
for _, vertex := range isolatedVertices {
if vertex != nil {
filtered = append(filtered, vertex)
}
Expand All @@ -56,7 +56,7 @@ func (g *graph) String() string {
for _, edge := range g.edges {
elems = append(elems, edge.String())
}
for _, vertex := range g.StandaloneVertices() {
for _, vertex := range g.IsolatedVertices() {
elems = append(elems, vertex.ID())
}
return fmt.Sprintf("{%s}", strings.Join(elems, ","))
Expand Down
30 changes: 17 additions & 13 deletions vertex.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ type Vertex interface {

ID() string

AddEdge(edge Edge)
AddEdge(edges ...Edge)
Edges() []Edge
CleanEdges()

SetAttr(Attr)
DelAttr(key interface{})
AddAttr(attrs ...Attr)
DelAttr(keys ...interface{})
GetAttr(key interface{}) interface{}
HasAttr(key interface{}) bool
CleanAttrs()
Expand Down Expand Up @@ -44,11 +44,11 @@ func (n *vertex) CleanAttrs() {
n.attrs = nil
}

func (n *vertex) AddEdge(edge Edge) {
func (n *vertex) AddEdge(edge ...Edge) {
if n.edges == nil {
n.edges = make([]Edge, 0)
}
n.edges = append(n.edges, edge)
n.edges = append(n.edges, edge...)
}

func (n *vertex) HasAttr(key interface{}) bool {
Expand All @@ -75,22 +75,26 @@ func (n *vertex) GetAttr(key interface{}) interface{} {
return nil
}

func (n *vertex) SetAttr(attr Attr) {
n.DelAttr(attr.Key())
func (n *vertex) AddAttr(attrs ...Attr) {
for _, attr := range attrs {
n.DelAttr(attr.Key())
}
if n.attrs == nil {
n.attrs = make([]Attr, 0)
}
n.attrs = append(n.attrs, attr)
n.attrs = append(n.attrs, attrs...)
}

func (n *vertex) DelAttr(key interface{}) {
func (n *vertex) DelAttr(keys ...interface{}) {
if n.attrs == nil {
return
}
for idx, attr := range n.attrs {
if attr.Key() == key {
n.attrs = append(n.attrs[:idx], n.attrs[idx+1:]...)
return
for _, key := range keys {
for idx, attr := range n.attrs {
if attr.Key() == key {
n.attrs = append(n.attrs[:idx], n.attrs[idx+1:]...)
return
}
}
}
}
Expand Down
23 changes: 12 additions & 11 deletions vertex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package graphman
import "fmt"

func ExampleNewVertex() {
a := NewVertex("aaa")
fmt.Println(a)
fmt.Println(a.ID())
va := NewVertex("aaa")
fmt.Println(va)
fmt.Println(va.ID())

fmt.Println()

b := NewVertex("bbb")
b.SetAttr(NewAttr("ccc", "ddd"))
b.SetAttr(NewAttr(42, []string{"eee", "fff"}))
b.SetAttr(NewAttr("ggg", "hhh"))
b.DelAttr("ggg")
b.DelAttr("iii")
fmt.Println(b)
fmt.Println(b.ID())
vb := NewVertex("bbb")
vb.AddAttr(
NewAttr("ccc", "ddd"),
NewAttr(42, []string{"eee", "fff"}),
NewAttr("ggg", "hhh"),
)
vb.DelAttr("ggg", "iii")
fmt.Println(vb)
fmt.Println(vb.ID())

// Output:
// aaa
Expand Down

0 comments on commit e3026b9

Please sign in to comment.