Class: Mementus::Structure::AdjacencyList
- Inherits:
-
Object
- Object
- Mementus::Structure::AdjacencyList
- Defined in:
- lib/mementus/structure/adjacency_list.rb
Instance Method Summary collapse
- #adjacent(id) ⇒ Object
- #directed? ⇒ Boolean
- #each_edge(&blk) ⇒ Object
- #edges_count ⇒ Object
- #has_edge?(edge) ⇒ Boolean
- #has_node?(node) ⇒ Boolean
-
#initialize(is_directed = true) ⇒ AdjacencyList
constructor
A new instance of AdjacencyList.
- #node(id) ⇒ Object
- #nodes ⇒ Object
- #nodes_count ⇒ Object
- #set_edge(edge) ⇒ Object
- #set_node(node) ⇒ Object
Constructor Details
#initialize(is_directed = true) ⇒ AdjacencyList
Returns a new instance of AdjacencyList.
4 5 6 7 |
# File 'lib/mementus/structure/adjacency_list.rb', line 4 def initialize(is_directed=true) @index = {} @is_directed = is_directed end |
Instance Method Details
#adjacent(id) ⇒ Object
59 60 61 |
# File 'lib/mementus/structure/adjacency_list.rb', line 59 def adjacent(id) @index[id].to_a end |
#directed? ⇒ Boolean
19 20 21 |
# File 'lib/mementus/structure/adjacency_list.rb', line 19 def directed? @is_directed end |
#each_edge(&blk) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/mementus/structure/adjacency_list.rb', line 63 def each_edge(&blk) if directed? nodes.each do |from| adjacent(from.id).each do |to| yield EdgeProxy.new(Edge.new(from: from, to: to), self) end end else raise 'Edge traversal unsupported for undirected graphs' end end |
#edges_count ⇒ Object
13 14 15 16 17 |
# File 'lib/mementus/structure/adjacency_list.rb', line 13 def edges_count c = 0 each_edge { c += 1 } c end |
#has_edge?(edge) ⇒ Boolean
43 44 45 46 47 48 49 |
# File 'lib/mementus/structure/adjacency_list.rb', line 43 def has_edge?(edge) if edge.is_a?(Mementus::Edge) || edge.is_a?(Mementus::EdgeProxy) has_node?(edge.from.id) && @index[edge.from.id].include?(edge.to.id) else raise 'Edge IDs are not supported by this data structure' end end |
#has_node?(node) ⇒ Boolean
35 36 37 38 39 40 41 |
# File 'lib/mementus/structure/adjacency_list.rb', line 35 def has_node?(node) if node.is_a?(Mementus::Node) || node.is_a?(Mementus::NodeProxy) @index.key?(node.id) else @index.key?(node) end end |
#node(id) ⇒ Object
51 52 53 |
# File 'lib/mementus/structure/adjacency_list.rb', line 51 def node(id) NodeProxy.new(Node.new(id: id), self) end |
#nodes ⇒ Object
55 56 57 |
# File 'lib/mementus/structure/adjacency_list.rb', line 55 def nodes @index.keys.map { |id| NodeProxy.new(Node.new(id: id), self) } end |
#nodes_count ⇒ Object
9 10 11 |
# File 'lib/mementus/structure/adjacency_list.rb', line 9 def nodes_count @index.size end |
#set_edge(edge) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/mementus/structure/adjacency_list.rb', line 27 def set_edge(edge) set_node(edge.from) unless has_node?(edge.from.id) set_node(edge.to) unless has_node?(edge.to.id) @index[edge.from.id].add(edge.to.id) @index[edge.to.id].add(edge.from.id) unless directed? end |
#set_node(node) ⇒ Object
23 24 25 |
# File 'lib/mementus/structure/adjacency_list.rb', line 23 def set_node(node) @index[node.id] ||= Set.new end |