GraphUtils 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/graphutils.rb +109 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a38723353cc27a96475a0774c91d1dd01f080dca
4
+ data.tar.gz: 7a51cbe06406eb18d6e7e477e1556d8187a14cf7
5
+ SHA512:
6
+ metadata.gz: 70f0c67605219be63d07c0050e8fbb2b32e0a3d3b482302411eb8cc0346338b7625e7bc3a6704c40b656bda5d38a70366168289bc5a12f2bb76021d6a0d47248
7
+ data.tar.gz: 152a033e2b1b963e12c71c329f0c83ed6785ebc15f46a2cb2c2c3073bdcee421fbeef8ad0dd13982aa0b13ee0b93c20b2d82bc6ea3b1cf639863e1ca4ae68fac
data/lib/graphutils.rb ADDED
@@ -0,0 +1,109 @@
1
+ # row, column accessor methods for 2-dimensonal array structures
2
+
3
+ # These methods are helper methods meant to make traversing 2d-Array structures much easier to manipulate. Keep in mind that the coordinate-techniques here are 0-indexed -- for example, the first item in a 2d array will have the coordinates of [0,0]. Another important thing to consider is that coordinate mapping with 2d array structures works differently than traditional xy_lookups. For easier comprehension, consider your x to refer to the row and your y to refer to the column, as such:
4
+
5
+ # [row, column]
6
+
7
+ # Included in these methods are enumerators that can perform iterations over entire N-d Array structures. We can call these 'deep' enumerators.
8
+
9
+ module GraphUtils
10
+
11
+ def self.find_rc(two_dimensional_array, target)
12
+ column = nil
13
+ row = nil
14
+ two_dimensional_array.each do |y|
15
+ if y.include?(target)
16
+ row = two_dimensional_array.index(y)
17
+ y.each do |x|
18
+ if x == target
19
+ column = y.index(x)
20
+ break
21
+ end
22
+ end
23
+ end
24
+ end
25
+ [row, column]
26
+ end
27
+
28
+ def self.rc_lookup(two_dimensional_array, coordinates)
29
+ row = coordinates[0]
30
+ column = coordinates[1]
31
+ two_dimensional_array[row][column]
32
+ end
33
+
34
+ def self.find_column(board, column_index)
35
+ column = []
36
+ row_index = 0
37
+ until row_index == board.length
38
+ column << board[row_index][column_index]
39
+ row_index += 1
40
+ end
41
+ column
42
+ end
43
+
44
+ def self.surrounding_coordinates(two_dimensional_array, coordinates, options = {} )
45
+ raise ArgumentError.new("Must include [row,column] coordinates") unless coordinates.is_a?(Array)
46
+ range = options[:range] || :inclusive
47
+ row = coordinates[0]
48
+ column = coordinates[1]
49
+
50
+ coordinates = [
51
+ [(row - 1), (column )], # up
52
+ [(row - 1), (column - 1)], # upleft
53
+ [(row ), (column - 1)], # left
54
+ [(row + 1), (column - 1)], # downleft
55
+ [(row + 1), (column )], # down
56
+ [(row + 1), (column + 1)], # downright
57
+ [(row ), (column + 1)], # right
58
+ [(row - 1), (column + 1)], # upright
59
+ ]
60
+
61
+ coordinates.unshift([row, column]) if range == :inclusive
62
+
63
+ values = []
64
+ coordinates.each do |xyvalues|
65
+ unless xyvalues.any? { |n| n < 0 || n > two_dimensional_array.length-1 }
66
+ values << xyvalues
67
+ end
68
+ end
69
+ values
70
+ end
71
+
72
+ def self.surrounding_coordinates_to_values(two_dimensional_array, coordinates, options = {})
73
+ coordinates = GraphUtils.surrounding_coordinates(two_dimensional_array, coordinates, options)
74
+
75
+ values = []
76
+ coordinates.each do |xyvalues|
77
+ unless xyvalues.any? { |n| n < 0 || n > two_dimensional_array.length-1 }
78
+ values << rc_lookup(two_dimensional_array, xyvalues)
79
+ end
80
+ end
81
+ values
82
+ end
83
+
84
+ def self.deepmap!(array, &block)
85
+ array.map! do |elem|
86
+ elem.is_a?(Array) ? deepmap!(elem, &block) : yield(elem)
87
+ end
88
+ end
89
+
90
+ def self.deepmap(array, &block)
91
+ array.map do |elem|
92
+ elem.is_a?(Array) ? deepmap(elem, &block) : yield(elem)
93
+ end
94
+ end
95
+
96
+ def self.deepeach(array, &block)
97
+ array.each do |elem|
98
+ elem.is_a?(Array) ? deepeach(elem, &block) : yield(elem)
99
+ end
100
+ end
101
+
102
+ def self.deepselect!(array, &block)
103
+ array.select! do |elem|
104
+ elem.is_a?(Array) ? deepselect!(elem, &block) : yield(elem)
105
+ end
106
+ array
107
+ end
108
+
109
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: GraphUtils
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Marshall Hattersley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-15 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby helper method for 2d-array coordinate lookups
14
+ email: mwhatters@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/graphutils.rb
20
+ homepage: http://rubygems.org/gems/graphutils
21
+ licenses:
22
+ - MIT
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.2.2
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Graphing Utilities
44
+ test_files: []