graphgem 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/graph.rb +10 -15
  3. data/lib/node/graph_node.rb +4 -10
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24f9d8040af2b947df326f91df55c7ffd6c57dd6
4
- data.tar.gz: 9f35f2666e1ff6017b575692860e021fe2078c87
3
+ metadata.gz: 77d8439a0f689df1237faa891b682d77e47a5424
4
+ data.tar.gz: 6a2024130d9f5d19ae664389283957eabc04ea70
5
5
  SHA512:
6
- metadata.gz: 8423bc3956b1e7e24fabcd4826787b481f3ffd0cb9009d2d06a7ac810eb16608a37cafb0fe1ad135fef7a0308e9fda972c8fee0969558d7da4d5c5fcb17b8a55
7
- data.tar.gz: b0591b6bf911da20be338b4c8ba5cf4e43347ce4d1371ea47077a38b5be226a95280eb39b25a8ca28476e6f7de1e9fc225f892ad92c37be9ab9c86604c625254
6
+ metadata.gz: df2e5fa3017a80b7a3928e9e3e4b463e56b83f595cbd1e711df03c8e0f20465957d9498355860b0e9c8531c98eae25ec74572c4253edb3f98c2817ec8be6b403
7
+ data.tar.gz: fbe05350a84d67b59c1f876e5372a593b71018392bef12911030c7df03871857054e740d8f6380bca20bc0b669b828cbdab1a918896f4e298e2183ff7de41b03
data/lib/graph.rb CHANGED
@@ -6,36 +6,31 @@ class Graph
6
6
 
7
7
  # creates a blank graph
8
8
  def initialize
9
- @nodes = []
9
+ @nodes = {}
10
10
  end
11
11
 
12
12
  def addNode name, content
13
- @nodes.push(GraphNode.new(name, content))
13
+ @nodes[name] = GraphNode.new(name, content)
14
14
  end
15
15
 
16
16
  # returns true if added
17
17
  def addEdge from, to, directed, weight=nil
18
18
  return false if !isNode?(to) || !isNode?(from)
19
- getNode(from).addConnection(to,weight)
20
- getNode(to).addConnection(from,weight) if(!directed)
19
+ getNode(from).addConnection(getNode(to),weight)
20
+ getNode(to).addConnection(getNode(from),weight) if(!directed)
21
21
  end
22
22
 
23
23
  def isNode? name
24
- @nodes.each do |n|
25
- return true if n.name == name
26
- end
27
- return false
24
+ return @nodes[name] != nil
28
25
  end
29
26
 
30
27
  def getNode name
31
- @nodes.each do |n|
32
- return n if n.name == name
33
- end
34
- return nil
28
+ return @nodes[name]
35
29
  end
36
30
 
37
31
  def getConnectionWeight from, to
38
32
  return nil if !isNode?(from) || !isNode?(to)
33
+ return nil if !isConnected?(from,to)
39
34
  return getNode(from).getConnection(to).weight
40
35
  end
41
36
 
@@ -65,9 +60,9 @@ class Graph
65
60
 
66
61
  def convertToCSVString
67
62
  result = "From,FromContent,To,ToContent,Weight\n"
68
- @nodes.each do |n|
69
- n.connections.each do |c|
70
- result += n.name + ',' + n.content + ',' + c.to.name + ',' + c.to.content + ',' + c.weight + "\n"
63
+ @nodes.each do |key, n|
64
+ n.connections.each do |key, c|
65
+ result += n.name + ',' + n.content + ',' + c.to.name + ',' + c.to.content + ',' + c.weight.to_s + "\n"
71
66
  end
72
67
  end
73
68
  return result
@@ -9,25 +9,19 @@ class GraphNode
9
9
  def initialize name, content
10
10
  @name = name
11
11
  @content = content
12
- @connections = []
12
+ @connections = {}
13
13
  @marked = false
14
14
  end
15
15
 
16
16
  def addConnection to, weight
17
- @connections.push(Connection.new(to, weight))
17
+ @connections[to] = Connection.new(to,weight)
18
18
  end
19
19
 
20
20
  def getConnection name
21
- @connections.each do |c|
22
- return c if c.to.name == name
23
- end
24
- return nil
21
+ return @connections[name]
25
22
  end
26
23
 
27
24
  def connectedTo? name
28
- @connections.each do |c|
29
- return true if c.to.name == name
30
- end
31
- return false
25
+ return getConnection(name) != nil
32
26
  end
33
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graphgem
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Huelsman