/
edges.go
114 lines (103 loc) · 2.45 KB
/
edges.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package primitive
// edgeMap is a map of edges. edgeMap are not concurrency safe.
type edgeMap map[string]map[string]*Edge
func (e edgeMap) Types() []string {
var typs []string
for t, _ := range e {
typs = append(typs, t)
}
return typs
}
// RangeType executes the function over a list of edges with the given type. If the function returns false, the iteration stops.
func (e edgeMap) RangeType(typ Type, fn func(e *Edge) bool) {
if typ.Type() == AnyType {
for _, edges := range e {
for _, edge := range edges {
if !fn(edge) {
break
}
}
}
} else {
if e[typ.Type()] == nil {
return
}
for _, e := range e[typ.Type()] {
if !fn(e) {
break
}
}
}
}
// Range executes the function over every edge. If the function returns false, the iteration stops.
func (e edgeMap) Range(fn func(e *Edge) bool) {
for _, m := range e {
for _, e := range m {
if !fn(e) {
break
}
}
}
}
// Filter executes the function over every edge. If the function returns true, the edges will be added to the returned array of edges.
func (e edgeMap) Filter(fn func(e *Edge) bool) []*Edge {
var edges []*Edge
for _, m := range e {
for _, e := range m {
if fn(e) {
edges = append(edges, e)
}
}
}
return edges
}
// FilterType executes the function over every edge of the given type. If the function returns true, the edges will be added to the returned array of edges.
func (e edgeMap) FilterType(typ Type, fn func(e *Edge) bool) []*Edge {
var edges []*Edge
e.RangeType(typ, func(e *Edge) bool {
if fn(e) {
edges = append(edges, e)
}
return true
})
return edges
}
// DelEdge deletes the edge
func (e edgeMap) DelEdge(id TypedID) {
if _, ok := e[id.Type()]; !ok {
return
}
delete(e[id.Type()], id.ID())
}
// AddEdge adds the edge to the map
func (e edgeMap) AddEdge(edge *Edge) {
if _, ok := e[edge.Type()]; !ok {
e[edge.Type()] = map[string]*Edge{
edge.ID(): edge,
}
} else {
e[edge.Type()][edge.ID()] = edge
}
}
// HasEdge returns true if the edge exists
func (e edgeMap) HasEdge(id TypedID) bool {
_, ok := e.GetEdge(id)
return ok
}
// GetEdge gets an edge by id
func (e edgeMap) GetEdge(id TypedID) (*Edge, bool) {
if _, ok := e[id.Type()]; !ok {
return nil, false
}
if e, ok := e[id.Type()][id.ID()]; ok {
return e, true
}
return nil, false
}
// Len returns the number of edges of the given type
func (e edgeMap) Len(typ Type) int {
if rels, ok := e[typ.Type()]; ok {
return len(rels)
}
return 0
}