Class: GoNodes::EdgeList

Inherits:
Set
  • Object
show all
Defined in:
lib/gonodes/edge_list.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Set

#last

Constructor Details

#initialize(node_list) ⇒ EdgeList

Returns a new instance of EdgeList.



7
8
9
10
11
# File 'lib/gonodes/edge_list.rb', line 7

def initialize(node_list)
  @node_list = node_list
  @edges = Set.new
  super(@edges)
end

Instance Attribute Details

#completenessObject (readonly)

Returns the value of attribute completeness.



5
6
7
# File 'lib/gonodes/edge_list.rb', line 5

def completeness
  @completeness
end

Class Method Details

.new_with_completeness(params) ⇒ Object



22
23
24
25
# File 'lib/gonodes/edge_list.rb', line 22

def self.new_with_completeness(params)
  EdgeList.new(params[:node_list]).
    populate_with_completeness(params[:completeness])
end

.new_with_count(params) ⇒ Object



17
18
19
20
# File 'lib/gonodes/edge_list.rb', line 17

def self.new_with_count(params)
  EdgeList.new(params[:node_list]).
    populate_with_count(params[:count])
end

Instance Method Details

#==(other_edge_list) ⇒ Object



27
28
29
# File 'lib/gonodes/edge_list.rb', line 27

def ==(other_edge_list)
  self.sort == other_edge_list.sort
end

#add_edge(edge_params) ⇒ Object



52
53
54
55
# File 'lib/gonodes/edge_list.rb', line 52

def add_edge(edge_params)
  @edges << Edge.new(edge_params)
  self
end

#maximum_edgesObject



67
68
69
# File 'lib/gonodes/edge_list.rb', line 67

def maximum_edges
  (@node_list.count * (@node_list.count - 1)) / 2
end

#normalized_edge_count(completeness) ⇒ Object



57
58
59
# File 'lib/gonodes/edge_list.rb', line 57

def normalized_edge_count(completeness)
  (maximum_edges * completeness).to_i
end

#populate_with_completeness(completeness) ⇒ Object



47
48
49
50
# File 'lib/gonodes/edge_list.rb', line 47

def populate_with_completeness(completeness)
  edge_count = normalized_edge_count(completeness)
  populate_with_count(edge_count)
end

#populate_with_count(edge_count) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gonodes/edge_list.rb', line 31

def populate_with_count(edge_count)
  @completeness = (edge_count.to_f / maximum_edges).round(2)
  
  node_list_combo = possible_edges.cycle
  
  @edges.clear
  edge_count.times do
    start_node, end_node = node_list_combo.next
    @edges << Edge.new(
      start_node: start_node,
      end_node:   end_node 
    )
  end
  self
end

#possible_edgesObject

refactor after everything works - slow with more than a few hundred if loopback edges are required I could use repeated_combination



63
64
65
# File 'lib/gonodes/edge_list.rb', line 63

def possible_edges
  @possible_edges ||= @node_list.to_a.combination(2).to_a
end

#to_sObject



13
14
15
# File 'lib/gonodes/edge_list.rb', line 13

def to_s
  @edges.map{|edge| edge.to_s}.join("\n\n")
end