Class: Mementus::Structure::IncidenceList

Inherits:
Object
  • Object
show all
Defined in:
lib/mementus/structure/incidence_list.rb

Instance Method Summary collapse

Constructor Details

#initialize(is_directed = true) ⇒ 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, match = nil, direction = :out) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/mementus/structure/incidence_list.rb', line 107

def adjacent(id, match=nil, direction=:out)
  directional_index = case direction
  when :out then @outgoing
  when :in then @incoming
  end

  return @nodes.values_at(*directional_index[id]) unless match

  if match.is_a?(Hash)
    @nodes.values_at(*directional_index[id]).select do |node|
      key = match.first.first
      val = match.first.last
      node[key] == val
    end
  elsif match.is_a?(Symbol)
    @nodes.values_at(*directional_index[id]).select do |node|
      node.label == match
    end
  end
end

#edge(id) ⇒ Object



67
68
69
# File 'lib/mementus/structure/incidence_list.rb', line 67

def edge(id)
  @edges[id]
end

#edges(match = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/mementus/structure/incidence_list.rb', line 91

def edges(match=nil)
  return @edges.values unless match

  if match.is_a?(Hash)
    @edges.values.select do |edge|
      key = match.first.first
      val = match.first.last
      edge[key] == val
    end
  elsif match.is_a?(Symbol)
    @edges.values.select do |edge|
      edge.label == match
    end
  end
end

#edges_countObject



22
23
24
# File 'lib/mementus/structure/incidence_list.rb', line 22

def edges_count
  @edges.count
end

#has_edge?(edge, to = nil) ⇒ Boolean



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/mementus/structure/incidence_list.rb', line 34

def has_edge?(edge, to=nil)
  if to
    return node(edge).outgoing.any? do |target_node|
      target_node.id == to
    end
  end

  if edge.is_a?(Mementus::Edge) || edge.is_a?(Mementus::EdgeProxy)
    @edges.key?(edge.id)
  else
    @edges.key?(edge)
  end
end

#has_node?(node) ⇒ Boolean



26
27
28
29
30
31
32
# File 'lib/mementus/structure/incidence_list.rb', line 26

def has_node?(node)
  if node.is_a?(Mementus::Node) || node.is_a?(Mementus::NodeProxy)
    @nodes.key?(node.id)
  else
    @nodes.key?(node)
  end
end

#incident_edges(id, match = nil, direction = :out) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mementus/structure/incidence_list.rb', line 136

def incident_edges(id, match=nil, direction=:out)
  directional_index = case direction
  when :out then @outgoing_e
  when :in then @incoming_e
  end

  return @edges.values_at(*directional_index[id]) unless match

  if match.is_a?(Hash)
    @edges.values_at(*directional_index[id]).select do |edge|
      key = match.first.first
      val = match.first.last
      edge[key] == val
    end
  elsif match.is_a?(Symbol)
    @edges.values_at(*directional_index[id]).select do |edge|
      edge.label == match
    end
  end
end

#incoming(id, match = nil) ⇒ Object



132
133
134
# File 'lib/mementus/structure/incidence_list.rb', line 132

def incoming(id, match=nil)
  adjacent(id, match, :in)
end

#incoming_edges(id, match = nil) ⇒ Object



161
162
163
# File 'lib/mementus/structure/incidence_list.rb', line 161

def incoming_edges(id, match=nil)
  incident_edges(id, match, :in)
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



71
72
73
# File 'lib/mementus/structure/incidence_list.rb', line 71

def node(id)
  @nodes[id]
end

#nodes(match = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mementus/structure/incidence_list.rb', line 75

def nodes(match=nil)
  return @nodes.values unless match

  if match.is_a?(Hash)
    @nodes.values.select do |node|
      key = match.first.first
      val = match.first.last
      node[key] == val
    end
  elsif match.is_a?(Symbol)
    @nodes.values.select do |node|
      node.label == match
    end
  end
end

#nodes_countObject



18
19
20
# File 'lib/mementus/structure/incidence_list.rb', line 18

def nodes_count
  @nodes.count
end

#outgoing(id, match = nil) ⇒ Object



128
129
130
# File 'lib/mementus/structure/incidence_list.rb', line 128

def outgoing(id, match=nil)
  adjacent(id, match, :out)
end

#outgoing_edges(id, match = nil) ⇒ Object



157
158
159
# File 'lib/mementus/structure/incidence_list.rb', line 157

def outgoing_edges(id, match=nil)
  incident_edges(id, match, :out)
end

#set_edge(edge) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/mementus/structure/incidence_list.rb', line 56

def set_edge(edge)
  set_node(edge.from) unless has_node?(edge.from.id)
  set_node(edge.to) unless has_node?(edge.to.id)

  @edges[edge.id] = EdgeProxy.new(edge, self)
  @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



48
49
50
51
52
53
54
# File 'lib/mementus/structure/incidence_list.rb', line 48

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