Class: Mementus::Structure::IncidenceList
- Inherits:
-
Object
- Object
- Mementus::Structure::IncidenceList
- Defined in:
- lib/mementus/structure/incidence_list.rb
Instance Method Summary collapse
- #adjacent(id) ⇒ Object
- #adjacent_edges(id) ⇒ Object
- #each_adjacent(id, &blk) ⇒ Object
- #each_node(&block) ⇒ Object
- #edge(id) ⇒ Object
- #edges(match = nil) ⇒ Object
- #edges_count ⇒ Object
- #has_edge?(edge) ⇒ Boolean
- #has_node?(node) ⇒ Boolean
-
#initialize(is_directed = true) ⇒ IncidenceList
constructor
A new instance of IncidenceList.
- #is_directed? ⇒ Boolean
- #node(id) ⇒ Object
- #nodes(match = nil) ⇒ Object
- #nodes_count ⇒ Object
- #set_edge(edge) ⇒ Object
- #set_node(node) ⇒ Object
Constructor Details
#initialize(is_directed = true) ⇒ IncidenceList
Returns a new instance of IncidenceList.
4 5 6 7 8 9 10 11 12 |
# File 'lib/mementus/structure/incidence_list.rb', line 4 def initialize(is_directed=true) @outgoing = {} @incoming = {} @outgoing_e = {} @incoming_e = {} @nodes = {} @edges = {} @is_directed = is_directed end |
Instance Method Details
#adjacent(id) ⇒ Object
85 86 87 |
# File 'lib/mementus/structure/incidence_list.rb', line 85 def adjacent(id) @nodes.values_at(*@outgoing[id]) end |
#adjacent_edges(id) ⇒ Object
93 94 95 |
# File 'lib/mementus/structure/incidence_list.rb', line 93 def adjacent_edges(id) @edges.values_at(*@outgoing_e[id]) end |
#each_adjacent(id, &blk) ⇒ Object
89 90 91 |
# File 'lib/mementus/structure/incidence_list.rb', line 89 def each_adjacent(id, &blk) adjacent(id).each(&blk) end |
#each_node(&block) ⇒ Object
61 62 63 |
# File 'lib/mementus/structure/incidence_list.rb', line 61 def each_node(&block) @nodes.values.each(&block) end |
#edge(id) ⇒ Object
53 54 55 |
# File 'lib/mementus/structure/incidence_list.rb', line 53 def edge(id) @edges[id] end |
#edges(match = nil) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/mementus/structure/incidence_list.rb', line 75 def edges(match=nil) return @edges.values unless match @edges.values.select do |edge| key = match.first.first val = match.first.last edge[key] == val end end |
#edges_count ⇒ Object
22 23 24 |
# File 'lib/mementus/structure/incidence_list.rb', line 22 def edges_count @edges.count end |
#has_edge?(edge) ⇒ Boolean
30 31 32 |
# File 'lib/mementus/structure/incidence_list.rb', line 30 def has_edge?(edge) @edges.key?(edge.id) end |
#has_node?(node) ⇒ Boolean
26 27 28 |
# File 'lib/mementus/structure/incidence_list.rb', line 26 def has_node?(node) @nodes.key?(node.id) end |
#is_directed? ⇒ Boolean
14 15 16 |
# File 'lib/mementus/structure/incidence_list.rb', line 14 def is_directed? @is_directed end |
#node(id) ⇒ Object
57 58 59 |
# File 'lib/mementus/structure/incidence_list.rb', line 57 def node(id) @nodes[id] end |
#nodes(match = nil) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/mementus/structure/incidence_list.rb', line 65 def nodes(match=nil) return @nodes.values unless match @nodes.values.select do |node| key = match.first.first val = match.first.last node[key] == val end end |
#nodes_count ⇒ Object
18 19 20 |
# File 'lib/mementus/structure/incidence_list.rb', line 18 def nodes_count @nodes.count end |
#set_edge(edge) ⇒ Object
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mementus/structure/incidence_list.rb', line 42 def set_edge(edge) set_node(edge.from) unless has_node?(edge.from) set_node(edge.to) unless has_node?(edge.to) @edges[edge.id] = edge @outgoing[edge.from.id] << edge.to.id @incoming[edge.to.id] << edge.from.id @outgoing_e[edge.from.id] << edge.id @incoming_e[edge.to.id] << edge.id end |
#set_node(node) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/mementus/structure/incidence_list.rb', line 34 def set_node(node) @nodes[node.id] = NodeProxy.new(node, self) @outgoing[node.id] ||= [] @incoming[node.id] ||= [] @outgoing_e[node.id] ||= [] @incoming_e[node.id] ||= [] end |