graph-rb 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 93d4da34a650617118bc4cd8d5f9ad9d750fbc2fe397b83328427eabe8eed6f2
4
- data.tar.gz: 3da2095e933f194205b8cffa5499e1aabe1e838bbe98d65241ec05baa60a9560
3
+ metadata.gz: 211e4392a9c71a1a017a86abb884ad207dc3ae87fdfe7a280c47a809c70ea28e
4
+ data.tar.gz: cc14689cb908518d23f66c7743542246f24b5336417b89cb8e963e1c485f73b9
5
5
  SHA512:
6
- metadata.gz: 14bed0dbb327c706db60cb94689afa95ffc0164c52aaedcacccfbcd3e0681f34994a437e52909653f12a0545d812a4631641ff4e211a1db50daf59091aa72a65
7
- data.tar.gz: 82b4fb9e3d5220cf2f86c634f17fe00dd8dab4c18af23e0a867e333fe01c56e3feb0a622c0a7fa86192231d8397feea4bfb29f07dada688f99bb4cdbf0822a8a
6
+ metadata.gz: afe5678f0f1ad8539d155705a6136ce138dc2ce8c2c12c523bddf500ed9f7b759d8e014ad895635b04fee4e52d7b674aba70163dd1cbe3d7316395760ee646ab
7
+ data.tar.gz: fc3db12719a15d5714c834e60970b1a643663ebc13db5bddf360f0a987e128a63c5e8bf520c6cac6f3a60dce1fc78be563d4c7033c28e398c546102fdc2765d4
data/CHANGELOG.md ADDED
@@ -0,0 +1,32 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.1.1] - 2019-04-26
10
+
11
+ ### Added
12
+
13
+ - `Graph::Edge` have a new attribue named `weight`
14
+ - `Graph::Vertex` and `Graph::Edge` have a new method named `to_h`
15
+
16
+ ### Changed
17
+
18
+ - `Graph::Edge` attribute `ukey` rename to `key`
19
+ - `Graph::Vertex` attribute `uid` rename to `key`
20
+ - `Graph#add`, `Graph#delete` and `Graph#index_of` now identify vertex or edge by attribute `key`
21
+
22
+ ## [0.1.0] - 2019-04-26
23
+
24
+ ### Added
25
+
26
+ - class `Graph`
27
+ - class `Graph::Vertex`
28
+ - class `Graph::Edge`
29
+
30
+ [Unreleased]: https://github.com/jk195417/graph-rb/compare/v0.1.1...HEAD
31
+ [0.1.1]: https://github.com/jk195417/graph-rb/compare/v0.1.0...v0.1.1
32
+ [0.1.0]: https://github.com/jk195417/graph-rb/releases/tag/v0.1.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- graph-rb (0.1.0)
4
+ graph-rb (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Graph
2
2
 
3
- A ruby gem for graph, data structure.
3
+ A ruby gem for graph, data structure. https://rubygems.org/gems/graph-rb
4
4
 
5
5
  ## Installation
6
6
 
@@ -29,9 +29,16 @@ edge1 = Graph::Edge.new(vertex1, vertex2)
29
29
  edge2 = Graph::Edge.new(vertex1, vertex3)
30
30
  edge3 = Graph::Edge.new(vertex2, vertex3)
31
31
 
32
- graph.add edge1
33
- graph.add edge2
34
- graph.add edge3
32
+ # add vertex or edge to graph
33
+ graph << vertex1
34
+ graph << edge3
35
+
36
+ # delete vertex or edge to graph
37
+ graph.delete edge3
38
+ graph.delete vertex1
39
+
40
+ # show graph in a hash
41
+ graph.to_h
35
42
  ```
36
43
 
37
44
  ## Development
data/lib/graph.rb CHANGED
@@ -15,6 +15,11 @@ class Graph
15
15
  return include_edge?(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Edge)
16
16
  end
17
17
 
18
+ def index_of(vertex_or_edge)
19
+ return index_of_vertex(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Vertex)
20
+ return index_of_edge(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Edge)
21
+ end
22
+
18
23
  def add(vertex_or_edge)
19
24
  return add_vertex(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Vertex)
20
25
  return add_edge(vertex_or_edge) if vertex_or_edge.is_a?(Graph::Edge)
@@ -26,15 +31,14 @@ class Graph
26
31
  end
27
32
 
28
33
  def to_h
29
- result = {
30
- vertices: [],
31
- edges: []
32
- }
34
+ # O(vertices.size) + O(edges.size)
35
+ result = {}
33
36
  @vertices.each do |vertex|
34
- result[:vertices] << vertex.uid
37
+ result[vertex.key] = vertex.to_h
35
38
  end
36
39
  @edges.each do |edge|
37
- result[:edges] << { from: edge.from.uid, to: edge.to.uid }
40
+ result[edge.from.key][:edges] << edge.to_h
41
+ result[edge.to.key][:edges] << edge.to_h
38
42
  end
39
43
  result
40
44
  end
@@ -49,8 +53,16 @@ class Graph
49
53
  @edges.include? edge
50
54
  end
51
55
 
56
+ def index_of_vertex(target)
57
+ @vertices.index { |vertex| vertex.key == target.key }
58
+ end
59
+
60
+ def index_of_edge(target)
61
+ @edges.index { |edge| edge.key == target.key }
62
+ end
63
+
52
64
  def add_vertex(vertex)
53
- return false if include_vertex?(vertex)
65
+ return false if index_of_vertex(vertex)
54
66
 
55
67
  vertex.send(:add_to, self)
56
68
  @vertices << vertex
@@ -58,30 +70,35 @@ class Graph
58
70
  end
59
71
 
60
72
  def add_edge(edge)
61
- return false if include_edge?(edge)
73
+ return false if index_of_edge(edge)
62
74
 
63
- edge.send(:add_to, self)
64
75
  add_vertex(edge.from)
65
76
  add_vertex(edge.to)
77
+ edge.send(:add_to, self)
66
78
  @edges << edge
67
79
  self
68
80
  end
69
81
 
70
82
  def delete_vertex(vertex)
71
- return false unless include_vertex?(vertex)
83
+ idx = index_of_vertex vertex
84
+ return false unless idx
85
+
72
86
  edges.each do |edge|
73
- delete_edge(edge) if (edge.from == vertex) || (edge.to == vertex)
87
+ delete_edge(edge) if (edge.from.key == vertex.key) || (edge.to.key == vertex.key)
74
88
  end
75
89
  vertex.send(:add_to, nil)
76
- @vertices.delete(vertex)
90
+ @vertices.delete_at idx
77
91
  self
78
92
  end
79
93
 
80
94
  def delete_edge(edge)
81
- return false unless include_edge?(edge)
82
-
95
+ idx = index_of_edge edge
96
+ return false unless idx
97
+
83
98
  edge.send(:add_to, nil)
84
- @edges.delete(edge)
99
+ @edges.delete_at idx
85
100
  self
86
101
  end
87
- end
102
+
103
+ alias << add
104
+ end
data/lib/graph/edge.rb CHANGED
@@ -1,10 +1,16 @@
1
1
  class Graph::Edge
2
- attr_reader :ukey, :graph, :from, :to
2
+ attr_reader :key, :graph, :from, :to
3
+ attr_accessor :weight
3
4
 
4
- def initialize(from, to)
5
+ def initialize(from, to, weight: 1)
5
6
  @from = from
6
7
  @to = to
7
- @ukey = "from_#{from.uid}_to_#{to.uid}"
8
+ @key = "from_#{from.key}_to_#{to.key}"
9
+ @weight = weight
10
+ end
11
+
12
+ def to_h
13
+ { from: from.key, to: to.key, weight: weight }
8
14
  end
9
15
 
10
16
  private
data/lib/graph/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  class Graph
2
- VERSION = "0.1.0"
2
+ VERSION = '0.1.1'
3
3
  end
data/lib/graph/vertex.rb CHANGED
@@ -1,14 +1,18 @@
1
1
  class Graph::Vertex
2
+ attr_reader :key, :graph
2
3
  attr_accessor :attachment
3
- attr_reader :uid, :graph
4
+
4
5
  @@autoincrement_counter = 0
5
6
 
6
- def initialize(attachment = nil)
7
- uid = @@autoincrement_counter += 1
8
- @uid = uid
7
+ def initialize(key: nil, attachment: nil)
8
+ @key = key || (@@autoincrement_counter += 1)
9
9
  @attachment = attachment
10
10
  end
11
11
 
12
+ def to_h
13
+ { edges: [], attachment: attachment.to_h }
14
+ end
15
+
12
16
  private
13
17
 
14
18
  def add_to(graph)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: graph-rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - 楊竑昕
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-25 00:00:00.000000000 Z
11
+ date: 2019-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -62,6 +62,7 @@ files:
62
62
  - ".gitignore"
63
63
  - ".rspec"
64
64
  - ".travis.yml"
65
+ - CHANGELOG.md
65
66
  - CODE_OF_CONDUCT.md
66
67
  - Gemfile
67
68
  - Gemfile.lock