patchwork 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1 -0
- data/LICENSE +21 -0
- data/README.md +3 -0
- data/lib/patchwork.rb +17 -0
- data/lib/patchwork/link.rb +24 -0
- data/lib/patchwork/node.rb +40 -0
- data/lib/patchwork/traversals.rb +15 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ce8b8e9ce287b24c7b92df42f5e0ce751ee4a535
|
4
|
+
data.tar.gz: ee5ae6a3eeccbbe23346fc615e80e2de40c8932d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70566db1272c669fc122ac69a8eca6aead53c61999458420778e010308d7ce0a038794ba4ec1f6690899a876450aa2d2d1a2d0df7d0521e404d732a92b589737
|
7
|
+
data.tar.gz: 88fb87a2ad46fa0d97192dcfd6cc1161bc11e0404dfef87f7ca09a80161990fe8d435cbb1cc6de78587bcab270c3dcdf71564e5dcc3bde166de5d8b380ef8b22
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# CHANGELOG
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) Marlon Landaverde <milanlandaverde@gmail.com> (mml.sexy)
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
data/lib/patchwork.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require_relative './patchwork/node'
|
2
|
+
require_relative './patchwork/link'
|
3
|
+
require_relative './patchwork/traversals'
|
4
|
+
|
5
|
+
class Patchwork
|
6
|
+
attr_reader :nodes
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@nodes = []
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_node(data)
|
13
|
+
new_node = PatchworkInternal::Node.new(data)
|
14
|
+
@nodes << new_node
|
15
|
+
new_node
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module PatchworkInternal
|
4
|
+
class Link
|
5
|
+
attr_reader :id, :node_start, :node_end
|
6
|
+
attr_accessor :cost, :directed
|
7
|
+
|
8
|
+
def initialize(node_a, node_b, directed, cost)
|
9
|
+
@node_start = node_a
|
10
|
+
@node_end = node_b
|
11
|
+
@directed = directed
|
12
|
+
@cost = cost
|
13
|
+
@id = SecureRandom.uuid
|
14
|
+
end
|
15
|
+
|
16
|
+
def links_to(node)
|
17
|
+
if @directed == true
|
18
|
+
node == @node_end
|
19
|
+
else
|
20
|
+
node == @node_start || node == @node_end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
require_relative './link'
|
3
|
+
require_relative './traversals'
|
4
|
+
|
5
|
+
module PatchworkInternal
|
6
|
+
class Node
|
7
|
+
attr_reader :id
|
8
|
+
attr_reader :data
|
9
|
+
attr_reader :links
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
@data = data
|
13
|
+
@links = []
|
14
|
+
@id = SecureRandom.uuid
|
15
|
+
end
|
16
|
+
|
17
|
+
def link_to(node, directed = false, cost = nil)
|
18
|
+
return false if neighbor?(node)
|
19
|
+
@links << Link.new(self, node, directed, cost)
|
20
|
+
node.link_to(self) unless directed
|
21
|
+
true
|
22
|
+
end
|
23
|
+
|
24
|
+
def linked_nodes
|
25
|
+
@links.map(&:node_end)
|
26
|
+
end
|
27
|
+
|
28
|
+
def neighbor?(node)
|
29
|
+
@links.any? { |link| link.links_to(node) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def visit
|
33
|
+
yield self
|
34
|
+
end
|
35
|
+
|
36
|
+
def depth_first(&block)
|
37
|
+
Traversals.depth_first(self, &block)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module PatchworkInternal
|
2
|
+
class Traversals
|
3
|
+
def self.depth_first(start_node, &block)
|
4
|
+
visited_nodes_cache = {}
|
5
|
+
nodes_to_visit = [start_node]
|
6
|
+
until nodes_to_visit.empty?
|
7
|
+
next_node = nodes_to_visit.pop
|
8
|
+
next if visited_nodes_cache[next_node.id]
|
9
|
+
visited_nodes_cache[next_node.id] = true
|
10
|
+
next_node.visit(&block)
|
11
|
+
nodes_to_visit.concat(next_node.linked_nodes)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: patchwork
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marlon Landaverde
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-11-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: In-memory graph for Ruby
|
14
|
+
email: milanlandaverde@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- CHANGELOG.md
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- lib/patchwork.rb
|
23
|
+
- lib/patchwork/link.rb
|
24
|
+
- lib/patchwork/node.rb
|
25
|
+
- lib/patchwork/traversals.rb
|
26
|
+
homepage: http://rubygems.org/gems/patchwork
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.5.1
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: Patchwork
|
50
|
+
test_files: []
|