graph_lib 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/Gemfile +5 -0
- data/Gemfile.lock +26 -0
- data/README.md +44 -0
- data/lib/graph.rb +61 -0
- data/lib/vertex.rb +31 -0
- data/spec/graph.json +9 -0
- data/spec/graph_spec.rb +85 -0
- data/spec/spec_helper.rb +100 -0
- data/spec/vertex_spec.rb +36 -0
- metadata +53 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3f1d7845f4d37b26dce97a69825c98a40d4f28cb474cddb41c7f5e2988c6e89e
|
4
|
+
data.tar.gz: c128b41df038ab5d31ac705f24f72d803993d7c888ca1cd79cfac1d2cc299d8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7b3c97475e9d041685b8be31885d67cbf87c7c66180bfa24869fe074938f0528f6da6ff6b77675cd0d670a260ee8d2c8ead0d8ebddfb7ac92242a050bb1fb6b2
|
7
|
+
data.tar.gz: 1b94d3c4217dbd319c87c6d7aa2a507f4c822adc6464a04afdfb6859569d4fb755ef46700d33a381b3a523f97f02bb5c059c1af8433c0e236fac2935512484ce
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
GEM
|
2
|
+
remote: https://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.3)
|
5
|
+
rspec (3.8.0)
|
6
|
+
rspec-core (~> 3.8.0)
|
7
|
+
rspec-expectations (~> 3.8.0)
|
8
|
+
rspec-mocks (~> 3.8.0)
|
9
|
+
rspec-core (3.8.0)
|
10
|
+
rspec-support (~> 3.8.0)
|
11
|
+
rspec-expectations (3.8.2)
|
12
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
13
|
+
rspec-support (~> 3.8.0)
|
14
|
+
rspec-mocks (3.8.0)
|
15
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
16
|
+
rspec-support (~> 3.8.0)
|
17
|
+
rspec-support (3.8.0)
|
18
|
+
|
19
|
+
PLATFORMS
|
20
|
+
ruby
|
21
|
+
|
22
|
+
DEPENDENCIES
|
23
|
+
rspec
|
24
|
+
|
25
|
+
BUNDLED WITH
|
26
|
+
1.17.1
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# GraphLib
|
2
|
+
|
3
|
+
A very minimal graph organization library in Ruby.
|
4
|
+
|
5
|
+
Allows you to add vertices and edges and know about parents and
|
6
|
+
children.
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
require './graph.rb'
|
10
|
+
|
11
|
+
g = Graph.new
|
12
|
+
g.add_vertex("a")
|
13
|
+
g.add_vertex("b")
|
14
|
+
g.add_vertex("c")
|
15
|
+
g.add_vertex("d")
|
16
|
+
|
17
|
+
g.add_edge("a", "b")
|
18
|
+
g.add_edge("a", "c")
|
19
|
+
g.add_edge("b", "d")
|
20
|
+
g.add_edge("c", "d")
|
21
|
+
|
22
|
+
g.children_for("a")
|
23
|
+
g.parents_for("d")
|
24
|
+
```
|
25
|
+
|
26
|
+
If you try to add an edge between nodes that don't exist, you get an
|
27
|
+
error...
|
28
|
+
|
29
|
+
You can also generate a graph directly from a json document like this:
|
30
|
+
|
31
|
+
```json
|
32
|
+
{
|
33
|
+
"vertices": ["a", "b", "c", "d"],
|
34
|
+
"edges": [
|
35
|
+
["a", "b"],
|
36
|
+
["a", "c"],
|
37
|
+
["b", "d"],
|
38
|
+
["c", "d"]
|
39
|
+
]
|
40
|
+
}
|
41
|
+
```
|
42
|
+
|
43
|
+
Do this by either calling `Graph.from_hash(graph_hash)` or
|
44
|
+
`Graph.from_file('graph.json')`.
|
data/lib/graph.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'vertex'
|
3
|
+
|
4
|
+
class Graph
|
5
|
+
def initialize
|
6
|
+
@vertices = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def add_vertex(name)
|
10
|
+
vertex = Vertex.new(name)
|
11
|
+
@vertices << vertex
|
12
|
+
vertex
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_edge(from, to)
|
16
|
+
names = @vertices.map(&:name)
|
17
|
+
#return false unless names.include?(from) && names.include?(to)
|
18
|
+
@vertices[names.index(from)].add_child(to)
|
19
|
+
@vertices[names.index(to)].add_parent(from)
|
20
|
+
nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def children_for(vertex)
|
24
|
+
names = @vertices.map(&:name)
|
25
|
+
children_names = @vertices[names.index(vertex)].children
|
26
|
+
children = children_names.collect do |child|
|
27
|
+
@vertices[names.index(child)]
|
28
|
+
end
|
29
|
+
children
|
30
|
+
end
|
31
|
+
|
32
|
+
def parents_for(vertex)
|
33
|
+
names = @vertices.map(&:name)
|
34
|
+
parent_names = @vertices[names.index(vertex)].parents
|
35
|
+
parents = parent_names.collect do |parent|
|
36
|
+
@vertices[names.index(parent)]
|
37
|
+
end
|
38
|
+
parents
|
39
|
+
end
|
40
|
+
|
41
|
+
def get_vertex(name)
|
42
|
+
names = @vertices.map(&:name)
|
43
|
+
@vertices[names.index(name)]
|
44
|
+
end
|
45
|
+
|
46
|
+
def self.from_hash(hash)
|
47
|
+
g = Graph.new
|
48
|
+
hash['vertices'].each do |vertex|
|
49
|
+
g.add_vertex(vertex)
|
50
|
+
end
|
51
|
+
|
52
|
+
hash['edges'].each do |from, to|
|
53
|
+
g.add_edge(from, to)
|
54
|
+
end
|
55
|
+
return g
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.from_file(filename)
|
59
|
+
self.from_hash(JSON.parse(File.read(filename)))
|
60
|
+
end
|
61
|
+
end
|
data/lib/vertex.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
class Vertex
|
2
|
+
def initialize(name)
|
3
|
+
@name = name
|
4
|
+
@children = []
|
5
|
+
@parents = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def add_child(name)
|
9
|
+
@children << name
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_parent(name)
|
13
|
+
@parents << name
|
14
|
+
end
|
15
|
+
|
16
|
+
def name
|
17
|
+
@name
|
18
|
+
end
|
19
|
+
|
20
|
+
def children
|
21
|
+
@children
|
22
|
+
end
|
23
|
+
|
24
|
+
def parents
|
25
|
+
@parents
|
26
|
+
end
|
27
|
+
|
28
|
+
def root?
|
29
|
+
@parents.empty?
|
30
|
+
end
|
31
|
+
end
|
data/spec/graph.json
ADDED
data/spec/graph_spec.rb
ADDED
@@ -0,0 +1,85 @@
|
|
1
|
+
require './lib/graph.rb'
|
2
|
+
|
3
|
+
RSpec.describe Graph do
|
4
|
+
before do
|
5
|
+
@graph = Graph.new
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#add_vertex' do
|
9
|
+
it 'creates and adds a new vertex' do
|
10
|
+
expect(Vertex).to receive(:new).with('a')
|
11
|
+
@graph.add_vertex('a')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#get_vertex' do
|
16
|
+
it 'returns the vertex by that name' do
|
17
|
+
@graph.add_vertex('a')
|
18
|
+
@graph.add_vertex('b')
|
19
|
+
@graph.add_vertex('c')
|
20
|
+
@graph.add_edge('a', 'b')
|
21
|
+
@graph.add_edge('b', 'c')
|
22
|
+
|
23
|
+
b = @graph.get_vertex('b')
|
24
|
+
expect(b.name).to eq 'b'
|
25
|
+
expect(b.parents.first).to eq 'a'
|
26
|
+
expect(b.children.first).to eq 'c'
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe '#add_edge' do
|
31
|
+
it 'creates a new edge between two existing vertices' do
|
32
|
+
a = @graph.add_vertex('a')
|
33
|
+
b = @graph.add_vertex('b')
|
34
|
+
expect(a).to receive(:add_child).with('b')
|
35
|
+
expect(b).to receive(:add_parent).with('a')
|
36
|
+
@graph.add_edge('a', 'b')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '#children_for' do
|
41
|
+
it 'returns an array of all children of a node' do
|
42
|
+
a = @graph.add_vertex('a')
|
43
|
+
b = @graph.add_vertex('b')
|
44
|
+
c = @graph.add_vertex('c')
|
45
|
+
@graph.add_edge('a', 'b')
|
46
|
+
@graph.add_edge('a', 'c')
|
47
|
+
expect(@graph.children_for('a')).to match_array [b, c]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe '#parents_for' do
|
52
|
+
it 'returns an array of all parents of a node' do
|
53
|
+
a = @graph.add_vertex('a')
|
54
|
+
b = @graph.add_vertex('b')
|
55
|
+
@graph.add_edge('a', 'b')
|
56
|
+
expect(@graph.parents_for('b')).to match_array [a]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
describe '.from_hash' do
|
61
|
+
it 'creates a graph according to a hash spec' do
|
62
|
+
hash = {
|
63
|
+
'vertices' => ['a', 'b', 'c', 'd'],
|
64
|
+
'edges' => [
|
65
|
+
['a', 'b'],
|
66
|
+
['a', 'c'],
|
67
|
+
['b', 'd'],
|
68
|
+
['c', 'd']
|
69
|
+
]
|
70
|
+
}
|
71
|
+
|
72
|
+
graph = Graph.from_hash(hash)
|
73
|
+
expect(graph.children_for('a').map(&:name)).to match_array ['b', 'c']
|
74
|
+
expect(graph.parents_for('d').map(&:name)).to match_array ['b', 'c']
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '.from_file' do
|
79
|
+
it 'creates a graph according to a hash spec' do
|
80
|
+
graph = Graph.from_file('./spec/graph.json')
|
81
|
+
expect(graph.children_for('a').map(&:name)).to match_array ['b', 'c']
|
82
|
+
expect(graph.parents_for('d').map(&:name)).to match_array ['b', 'c']
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
16
|
+
RSpec.configure do |config|
|
17
|
+
# rspec-expectations config goes here. You can use an alternate
|
18
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
19
|
+
# assertions if you prefer.
|
20
|
+
config.expect_with :rspec do |expectations|
|
21
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
22
|
+
# and `failure_message` of custom matchers include text for helper methods
|
23
|
+
# defined using `chain`, e.g.:
|
24
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
25
|
+
# # => "be bigger than 2 and smaller than 4"
|
26
|
+
# ...rather than:
|
27
|
+
# # => "be bigger than 2"
|
28
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
29
|
+
end
|
30
|
+
|
31
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
32
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
33
|
+
config.mock_with :rspec do |mocks|
|
34
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
35
|
+
# a real object. This is generally recommended, and will default to
|
36
|
+
# `true` in RSpec 4.
|
37
|
+
mocks.verify_partial_doubles = true
|
38
|
+
end
|
39
|
+
|
40
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
41
|
+
# have no way to turn it off -- the option exists only for backwards
|
42
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
43
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
44
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
45
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
46
|
+
|
47
|
+
# The settings below are suggested to provide a good initial experience
|
48
|
+
# with RSpec, but feel free to customize to your heart's content.
|
49
|
+
=begin
|
50
|
+
# This allows you to limit a spec run to individual examples or groups
|
51
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
52
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
53
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
54
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
55
|
+
config.filter_run_when_matching :focus
|
56
|
+
|
57
|
+
# Allows RSpec to persist some state between runs in order to support
|
58
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
59
|
+
# you configure your source control system to ignore this file.
|
60
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
61
|
+
|
62
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
63
|
+
# recommended. For more details, see:
|
64
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
65
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
66
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
67
|
+
config.disable_monkey_patching!
|
68
|
+
|
69
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
70
|
+
# be too noisy due to issues in dependencies.
|
71
|
+
config.warnings = true
|
72
|
+
|
73
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
74
|
+
# file, and it's useful to allow more verbose output when running an
|
75
|
+
# individual spec file.
|
76
|
+
if config.files_to_run.one?
|
77
|
+
# Use the documentation formatter for detailed output,
|
78
|
+
# unless a formatter has already been configured
|
79
|
+
# (e.g. via a command-line flag).
|
80
|
+
config.default_formatter = "doc"
|
81
|
+
end
|
82
|
+
|
83
|
+
# Print the 10 slowest examples and example groups at the
|
84
|
+
# end of the spec run, to help surface which specs are running
|
85
|
+
# particularly slow.
|
86
|
+
config.profile_examples = 10
|
87
|
+
|
88
|
+
# Run specs in random order to surface order dependencies. If you find an
|
89
|
+
# order dependency and want to debug it, you can fix the order by providing
|
90
|
+
# the seed, which is printed after each run.
|
91
|
+
# --seed 1234
|
92
|
+
config.order = :random
|
93
|
+
|
94
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
95
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
96
|
+
# test failures related to randomization by passing the same `--seed` value
|
97
|
+
# as the one that triggered the failure.
|
98
|
+
Kernel.srand config.seed
|
99
|
+
=end
|
100
|
+
end
|
data/spec/vertex_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require './lib/vertex.rb'
|
2
|
+
|
3
|
+
RSpec.describe Vertex do
|
4
|
+
before do
|
5
|
+
@vertex = Vertex.new('some node')
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '#add_child' do
|
9
|
+
it 'adds a child to the children array' do
|
10
|
+
@vertex.add_child('a')
|
11
|
+
expect(@vertex.children).to eq ['a']
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe '#add_parent' do
|
16
|
+
it 'adds a parent to the parents array' do
|
17
|
+
@vertex.add_parent('a')
|
18
|
+
expect(@vertex.parents).to eq ['a']
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe '#root?' do
|
23
|
+
context 'when no parents present' do
|
24
|
+
it 'returns true' do
|
25
|
+
expect(@vertex.root?).to eq true
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context 'when parents present' do
|
30
|
+
it 'returns false' do
|
31
|
+
@vertex.add_parent('a')
|
32
|
+
expect(@vertex.root?).to eq false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graph_lib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Peikert
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description:
|
14
|
+
email: t.s.peikert+yappl@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".rspec"
|
20
|
+
- Gemfile
|
21
|
+
- Gemfile.lock
|
22
|
+
- README.md
|
23
|
+
- lib/graph.rb
|
24
|
+
- lib/vertex.rb
|
25
|
+
- spec/graph.json
|
26
|
+
- spec/graph_spec.rb
|
27
|
+
- spec/spec_helper.rb
|
28
|
+
- spec/vertex_spec.rb
|
29
|
+
homepage: https://github.com/TPei/graph_lib
|
30
|
+
licenses:
|
31
|
+
- MIT
|
32
|
+
metadata: {}
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 2.7.6
|
50
|
+
signing_key:
|
51
|
+
specification_version: 4
|
52
|
+
summary: ruby gem for a very minimal graph implementation
|
53
|
+
test_files: []
|