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

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, match = nil, direction = :out) ⇒ Object



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

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



69
70
71
# File 'lib/mementus/structure/incidence_list.rb', line 69

def edge(id)
  @edges[id]
end

#edges(match = nil) ⇒ Object



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

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, target = nil) ⇒ Boolean

Returns:

  • (Boolean)


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

def has_edge?(edge, target=nil)
  if target
    return false unless source = node(edge)

    return source.outgoing.any? do |candidate|
      candidate.id == target
    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

Returns:

  • (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



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

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



134
135
136
# File 'lib/mementus/structure/incidence_list.rb', line 134

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

#incoming_edges(id, match = nil) ⇒ Object



163
164
165
# File 'lib/mementus/structure/incidence_list.rb', line 163

def incoming_edges(id, match=nil)
  incident_edges(id, match, :in)
end

#is_directed?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/mementus/structure/incidence_list.rb', line 14

def is_directed?
  @is_directed
end

#node(id) ⇒ Object



73
74
75
# File 'lib/mementus/structure/incidence_list.rb', line 73

def node(id)
  @nodes[id]
end

#nodes(match = nil) ⇒ Object



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

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



130
131
132
# File 'lib/mementus/structure/incidence_list.rb', line 130

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

#outgoing_edges(id, match = nil) ⇒ Object



159
160
161
# File 'lib/mementus/structure/incidence_list.rb', line 159

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

#remove_edge(edge) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/mementus/structure/incidence_list.rb', line 189

def remove_edge(edge)
  if edge.is_a?(Mementus::Edge) || edge.is_a?(Mementus::EdgeProxy)
    edge_id = edge.id
  else
    edge_id = edge
  end

  edge = @edges[edge_id]

  @outgoing[edge.from.id].delete(edge.to.id)
  @incoming[edge.to.id].delete(edge.from.id)

  @outgoing_e[edge.from.id].delete(edge_id)
  @incoming_e[edge.to.id].delete(edge_id)

  @edges.delete(edge_id)
end

#remove_node(node) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mementus/structure/incidence_list.rb', line 167

def remove_node(node)
  if node.is_a?(Mementus::Node) || node.is_a?(Mementus::NodeProxy)
    node_id = node.id
  else
    node_id = node
  end

  @outgoing_e[node_id].each do |edge_id|
    @incoming[@edges[edge_id].to.id].delete(node_id)
    @incoming_e[@edges[edge_id].to.id].delete(edge_id)
    @edges.delete(edge_id)
  end

  @incoming_e[node_id].each do |edge_id|
    @outgoing[@edges[edge_id].from.id].delete(node_id)
    @outgoing_e[@edges[edge_id].from.id].delete(edge_id)
    @edges.delete(edge_id)
  end

  @nodes.delete(node_id)
end

#set_edge(edge) ⇒ Object



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

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



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

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