Class: Tangle::Graph
- Inherits:
-
Object
- Object
- Tangle::Graph
- Defined in:
- lib/tangle/graph.rb
Overview
Base class for all kinds of graphs
Direct Known Subclasses
Constant Summary collapse
Instance Method Summary collapse
-
#add_edge(*vertices, **kvargs) ⇒ Object
Add a new edge to the graph.
-
#add_vertex(**kvargs) ⇒ Object
Add a new vertex to the graph.
-
#edges(&selector) ⇒ Object
Get all edges.
- #get_vertex(name_or_vertex) ⇒ Object
-
#initialize(vertices: nil, edges: nil) ⇒ Graph
constructor
Initialize a new graph, optionally preloading it with vertices and edges.
-
#subgraph(&selector) ⇒ Object
(also: #dup)
Return a subgraph, optionally filtered by a vertex selector block.
-
#vertices ⇒ Object
Get all vertices.
Constructor Details
#initialize(vertices: nil, edges: nil) ⇒ Graph
Initialize a new graph, optionally preloading it with vertices and edges
Graph.new() => Graph Graph.new(vertices: array_or_hash) => Graph Graph.new(vertices: array_or_hash, edges: array_or_hash) => Graph
When array_or_hash is a hash, it contains the objects as values and their names as keys. When array_or_hash is an array the objects will get assigned unique names (within the graph).
vertices can contain anything, and the Vertex object that is created will delegate all missing methods to its content.
edges can contain an array of exactly two, either names of vertices or vertices.
Any subclass of Graph should also subclass Edge to manage its unique constraints.
30 31 32 33 34 35 36 37 |
# File 'lib/tangle/graph.rb', line 30 def initialize(vertices: nil, edges: nil) @vertices_by_id = {} @vertices_by_name = {} @edges ||= [] initialize_vertices(vertices) initialize_edges(edges) end |
Instance Method Details
#add_edge(*vertices, **kvargs) ⇒ Object
Add a new edge to the graph
add_edge(vtx1, vtx2, …) => Edge
55 56 57 58 |
# File 'lib/tangle/graph.rb', line 55 def add_edge(*vertices, **kvargs) vertices = vertices.map { |v| get_vertex(v) } insert_edge(self.class::Edge.new(*vertices, graph: self, **kvargs)) end |
#add_vertex(**kvargs) ⇒ Object
Add a new vertex to the graph
add_vertex(…) => Vertex
Optional named arguments:
name: unique name or label for vertex
contents: delegate object for missing methods
80 81 82 |
# File 'lib/tangle/graph.rb', line 80 def add_vertex(**kvargs) insert_vertex(Vertex.new(graph: self, **kvargs)) end |
#edges(&selector) ⇒ Object
Get all edges.
edges => Array
43 44 45 46 47 48 49 |
# File 'lib/tangle/graph.rb', line 43 def edges(&selector) if block_given? @edges.select(&selector) else @edges.to_a end end |
#get_vertex(name_or_vertex) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/tangle/graph.rb', line 84 def get_vertex(name_or_vertex) case name_or_vertex when Vertex name_or_vertex else @vertices_by_name[name_or_vertex] || @vertices_by_id.fetch(name_or_vertex) end end |
#subgraph(&selector) ⇒ Object Also known as: dup
Return a subgraph, optionally filtered by a vertex selector block
subgraph => Graph subgraph { |vertex| … } => Graph
Unless a selector is provided, the subgraph contains the entire graph.
101 102 103 104 105 106 107 108 |
# File 'lib/tangle/graph.rb', line 101 def subgraph(&selector) graph = self.class.new dup_vertices_into(graph, &selector) dup_edges_into(graph) graph end |
#vertices ⇒ Object
Get all vertices.
vertices => Array
64 65 66 67 68 69 70 |
# File 'lib/tangle/graph.rb', line 64 def vertices if block_given? @vertices_by_id.select { |_, vertex| yield(vertex) } else @vertices_by_id end.values end |