hierarchical_graph 1.0.0 → 1.0.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a3224dabda32c9f8a4b7ba2daff315a9c36282362acc8b509f703039e34d0cb
4
- data.tar.gz: 8a290679d5683ce0566d6433c829684961fa440581c49ccedec02c27c22839f8
3
+ metadata.gz: 501ca870e56fee37c942917a8471f238c82cac2c84a9eecb06ae4fb9440f0f9a
4
+ data.tar.gz: b5a59a609d92864e3132bb0f5d86154b1ec082f3697fdf97f07d02c2ebb37302
5
5
  SHA512:
6
- metadata.gz: 0b5540cc426ca14fbd4ca929442ee493fcb6396665e801c6755dafaa01bc17ff03010d10d80cfe75354e0345b60d01b57f68575a17a08a3904463825aad54947
7
- data.tar.gz: 52d18fa51a604a5b8f3724e604d430ef286c93bec32a1e7248d40b4808d2a50d6a7abb23bc5f4c6ba4bf81ef7c86ef73e3264a34c02f2dd0961e7e3888faabee
6
+ metadata.gz: 20ab16944cc5455d6f62278e291426d4e10c66f21e4be0cc6c0491cdd540872163a224bdfd9bf8d161f65b59e7a32bec7a8311aa4ff5ad3970248761d035eca4
7
+ data.tar.gz: 43f9f1e3cbe70fe4f2df57f326fa116f44c7c0d928025aae98c4dd00fea12c1be0554cd5d20432e983ea914459ef83a7be9238ad73675bf563b39ae8cea685d1
@@ -8,8 +8,8 @@ class HierarchicalGraph
8
8
 
9
9
  def initialize
10
10
  @nodes = {}
11
- @parent_to_children = Hash.new { |h,k| h[k] = Set.new }
12
- @child_to_parents = Hash.new { |h,k| h[k] = Set.new }
11
+ @parent_to_children = {}
12
+ @child_to_parents = {}
13
13
  @ancestors_cache = {}
14
14
  @descendants_cache = {}
15
15
  end
@@ -32,6 +32,8 @@ class HierarchicalGraph
32
32
 
33
33
  def add_node(id, attributes={})
34
34
  clear_cache
35
+ parent_to_children[id] = Set.new
36
+ child_to_parents[id] = Set.new
35
37
  nodes[id] = Node.new self, id, attributes
36
38
  end
37
39
 
@@ -52,11 +54,11 @@ class HierarchicalGraph
52
54
 
53
55
  def add_relation(parent_id:, child_id:)
54
56
  validate! parent_id, child_id
55
-
57
+
56
58
  clear_cache
57
59
  parent_to_children[parent_id] << child_id
58
60
  child_to_parents[child_id] << parent_id
59
-
61
+
60
62
  nil
61
63
  end
62
64
 
@@ -66,7 +68,7 @@ class HierarchicalGraph
66
68
  clear_cache
67
69
  parent_to_children[parent_id].delete child_id
68
70
  child_to_parents[child_id].delete parent_id
69
-
71
+
70
72
  nil
71
73
  end
72
74
 
@@ -45,6 +45,6 @@ class HierarchicalGraph
45
45
  private
46
46
 
47
47
  attr_reader :graph
48
-
48
+
49
49
  end
50
50
  end
@@ -1,3 +1,3 @@
1
1
  class HierarchicalGraph
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
@@ -10,7 +10,7 @@ describe HierarchicalGraph do
10
10
 
11
11
  it 'Add node' do
12
12
  graph = HierarchicalGraph.new
13
-
13
+
14
14
  node = graph.add_node 1
15
15
 
16
16
  graph[1].must_equal node
@@ -20,7 +20,7 @@ describe HierarchicalGraph do
20
20
  it 'Remove node' do
21
21
  graph = HierarchicalGraph.new
22
22
  graph.add_node 1
23
-
23
+
24
24
  graph.remove_node 1
25
25
 
26
26
  graph[1].must_be_nil
@@ -29,7 +29,7 @@ describe HierarchicalGraph do
29
29
 
30
30
  it 'Remove invalid node' do
31
31
  graph = HierarchicalGraph.new
32
-
32
+
33
33
  error = proc { graph.remove_node 1 }.must_raise RuntimeError
34
34
  error.message.must_equal "Invalid nodes: 1"
35
35
  end
@@ -38,7 +38,7 @@ describe HierarchicalGraph do
38
38
  graph = HierarchicalGraph.new
39
39
  graph.add_node 1
40
40
  graph.add_node 2
41
-
41
+
42
42
  graph.add_relation parent_id: 1, child_id: 2
43
43
 
44
44
  graph.parents_of(1).must_be_empty
@@ -50,7 +50,7 @@ describe HierarchicalGraph do
50
50
 
51
51
  it 'Add relation with invalid nodes' do
52
52
  graph = HierarchicalGraph.new
53
-
53
+
54
54
  error = proc { graph.add_relation parent_id: 1, child_id: 2 }.must_raise RuntimeError
55
55
  error.message.must_equal "Invalid nodes: 1, 2"
56
56
  end
@@ -70,7 +70,7 @@ describe HierarchicalGraph do
70
70
 
71
71
  it 'Remove relation in complex graph' do
72
72
  graph = HierarchicalGraph.new
73
-
73
+
74
74
  graph.add_node 0
75
75
  graph.add_node 1
76
76
  graph.add_node 2
@@ -223,6 +223,18 @@ describe HierarchicalGraph do
223
223
  graph.tsort.map(&:id).must_equal [2, 4, 3, 1]
224
224
  end
225
225
 
226
+ it 'Marshal' do
227
+ graph = HierarchicalGraph.new
228
+ graph.add_node 1
229
+ graph.add_node 2
230
+ graph.add_relation parent_id: 1, child_id: 2
231
+
232
+ dump = Marshal.dump graph
233
+ loaded = Marshal.load dump
234
+
235
+ loaded.to_s.must_equal graph.to_s
236
+ end
237
+
226
238
  it 'To string' do
227
239
  graph = HierarchicalGraph.new
228
240
  graph.add_node 1
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hierarchical_graph
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-12 00:00:00.000000000 Z
11
+ date: 2020-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake