-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathinterface.jl
346 lines (252 loc) · 6.25 KB
/
interface.jl
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# This file contains the common interface for Graphs.
"""
NotImplementedError{M}(m)
`Exception` thrown when a method from the `AbstractGraph` interface
is not implemented by a given graph type.
"""
struct NotImplementedError{M} <: Exception
m::M
NotImplementedError(m::M) where {M} = new{M}(m)
end
function Base.showerror(io::IO, ie::NotImplementedError)
return print(io, "method $(ie.m) not implemented.")
end
_NI(m) = throw(NotImplementedError(m))
"""
AbstractEdge
An abstract type representing a single edge between two vertices of a graph.
"""
abstract type AbstractEdge{T} end
"""
AbstractEdgeIter
An abstract type representing an edge iterator.
"""
abstract type AbstractEdgeIter end
"""
AbstractGraph
An abstract type representing a graph.
"""
abstract type AbstractGraph{T} end
@traitdef IsDirected{G<:AbstractGraph}
@traitimpl IsDirected{G} < -is_directed(G)
#
# Interface for AbstractEdges
#
"""
src(e)
Return the source vertex of edge `e`.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleGraph(2);
julia> add_edge!(g, 1, 2);
julia> src(first(edges(g)))
1
```
"""
src(e::AbstractEdge) = _NI("src")
"""
dst(e)
Return the destination vertex of edge `e`.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleGraph(2);
julia> add_edge!(g, 1, 2);
julia> dst(first(edges(g)))
2
```
"""
dst(e::AbstractEdge) = _NI("dst")
Pair(e::AbstractEdge) = _NI("Pair")
Tuple(e::AbstractEdge) = _NI("Tuple")
"""
reverse(e)
Create a new edge from `e` with source and destination vertices reversed.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleDiGraph(2);
julia> add_edge!(g, 1, 2);
julia> reverse(first(edges(g)))
Edge 2 => 1
```
"""
reverse(e::AbstractEdge) = _NI("reverse")
==(e1::AbstractEdge, e2::AbstractEdge) = _NI("==")
#
# Interface for AbstractGraphs
#
"""
edgetype(g)
Return the type of graph `g`'s edge
"""
edgetype(g::AbstractGraph) = _NI("edgetype")
"""
eltype(G)
Return the type of the graph's vertices (must be <: Integer)
"""
eltype(::Type{<:AbstractGraph{T}}) where {T} = T
"""
nv(g)
Return the number of vertices in `g`.
# Examples
```jldoctest
julia> using Graphs
julia> nv(SimpleGraph(3))
3
```
"""
nv(g::AbstractGraph) = _NI("nv")
"""
ne(g)
Return the number of edges in `g`.
# Examples
```jldoctest
julia> using Graphs
julia> g = path_graph(3);
julia> ne(g)
2
```
"""
ne(g::AbstractGraph) = _NI("ne")
"""
vertices(g)
Return (an iterator to or collection of) the vertices of a graph.
### Implementation Notes
A returned iterator is valid for one pass over the vertices, and
is invalidated by changes to `g`.
# Examples
```jldoctest
julia> using Graphs
julia> collect(vertices(SimpleGraph(4)))
4-element Vector{Int64}:
1
2
3
4
```
"""
vertices(g::AbstractGraph) = _NI("vertices")
"""
edges(g)
Return (an iterator to or collection of) the edges of a graph.
For `AbstractSimpleGraph`s it returns a `SimpleEdgeIter`.
The expressions `e in edges(g)` and `e ∈ edges(g)` evaluate as
calls to [`has_edge`](@ref).
### Implementation Notes
A returned iterator is valid for one pass over the edges, and
is invalidated by changes to `g`.
# Examples
```jldoctest
julia> using Graphs
julia> g = path_graph(3);
julia> collect(edges(g))
2-element Vector{Graphs.SimpleGraphs.SimpleEdge{Int64}}:
Edge 1 => 2
Edge 2 => 3
```
"""
edges(g) = _NI("edges")
"""
is_directed(G)
Return `true` if the graph type `G` is a directed graph; `false` otherwise.
New graph types must implement `is_directed(::Type{<:G})`.
The method can also be called with `is_directed(g::G)`
# Examples
```jldoctest
julia> using Graphs
julia> is_directed(SimpleGraph(2))
false
julia> is_directed(SimpleGraph)
false
julia> is_directed(SimpleDiGraph(2))
true
```
"""
is_directed(::G) where {G} = is_directed(G)
is_directed(::Type{T}) where {T} = _NI("is_directed")
"""
has_vertex(g, v)
Return true if `v` is a vertex of `g`.
# Examples
```jldoctest
julia> using Graphs
julia> has_vertex(SimpleGraph(2), 1)
true
julia> has_vertex(SimpleGraph(2), 3)
false
```
"""
has_vertex(x, v) = _NI("has_vertex")
"""
has_edge(g, s, d)
Return true if the graph `g` has an edge from node `s` to node `d`.
An optional `has_edge(g, e)` can be implemented to check if an edge belongs
to a graph, including any data other than source and destination node.
`e in edges(g)` or `e ∈ edges(g)` evaluate as
calls to `has_edge`, c.f. [`edges`](@ref).
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleDiGraph(2);
julia> add_edge!(g, 1, 2);
julia> has_edge(g, 1, 2)
true
julia> has_edge(g, 2, 1)
false
```
"""
has_edge(g, s, d) = _NI("has_edge")
has_edge(g, e) = has_edge(g, src(e), dst(e))
"""
inneighbors(g, v)
Return a list of all neighbors connected to vertex `v` by an incoming edge.
### Implementation Notes
Returns a reference to the current graph's internal structures, not a copy.
Do not modify result. If the graph is modified, the behavior is undefined:
the array behind this reference may be modified too, but this is not guaranteed.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0]);
julia> inneighbors(g, 4)
2-element Vector{Int64}:
3
5
```
"""
inneighbors(x, v) = _NI("inneighbors")
"""
outneighbors(g, v)
Return a list of all neighbors connected to vertex `v` by an outgoing edge.
# Implementation Notes
Returns a reference to the current graph's internal structures, not a copy.
Do not modify result. If the graph is modified, the behavior is undefined:
the array behind this reference may be modified too, but this is not guaranteed.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0]);
julia> outneighbors(g, 4)
1-element Vector{Int64}:
5
```
"""
outneighbors(x, v) = _NI("outneighbors")
"""
zero(G)
Return a zero-vertex, zero-edge version of the graph type `G`.
The fallback is defined for graph values `zero(g::G) = zero(G)`.
# Examples
```jldoctest
julia> using Graphs
julia> g = SimpleDiGraph([0 1 0 0 0; 0 0 1 0 0; 1 0 0 1 0; 0 0 0 0 1; 0 0 0 1 0]);
julia> zero(typeof(g))
{0, 0} directed simple Int64 graph
julia> zero(g)
{0, 0} directed simple Int64 graph
```
"""
zero(::Type{<:AbstractGraph}) = _NI("zero")
zero(g::G) where {G<:AbstractGraph} = zero(G)