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 +4 -4
- data/lib/hierarchical_graph.rb +7 -5
- data/lib/hierarchical_graph/node.rb +1 -1
- data/lib/hierarchical_graph/version.rb +1 -1
- data/spec/hierarchical_graph_spec.rb +18 -6
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 501ca870e56fee37c942917a8471f238c82cac2c84a9eecb06ae4fb9440f0f9a
|
|
4
|
+
data.tar.gz: b5a59a609d92864e3132bb0f5d86154b1ec082f3697fdf97f07d02c2ebb37302
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 20ab16944cc5455d6f62278e291426d4e10c66f21e4be0cc6c0491cdd540872163a224bdfd9bf8d161f65b59e7a32bec7a8311aa4ff5ad3970248761d035eca4
|
|
7
|
+
data.tar.gz: 43f9f1e3cbe70fe4f2df57f326fa116f44c7c0d928025aae98c4dd00fea12c1be0554cd5d20432e983ea914459ef83a7be9238ad73675bf563b39ae8cea685d1
|
data/lib/hierarchical_graph.rb
CHANGED
|
@@ -8,8 +8,8 @@ class HierarchicalGraph
|
|
|
8
8
|
|
|
9
9
|
def initialize
|
|
10
10
|
@nodes = {}
|
|
11
|
-
@parent_to_children =
|
|
12
|
-
@child_to_parents =
|
|
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
|
|
|
@@ -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.
|
|
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-
|
|
11
|
+
date: 2020-10-27 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rake
|