grid_struct 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 8123cc6cecee6f31395c03f4f6bb71d097ac4fd1
4
- data.tar.gz: 8a620e7b2af1da9daa5bdb595c388c535a14c16d
3
+ metadata.gz: 257074659fe7fc009a0eb3a0f8b5000223595a37
4
+ data.tar.gz: d2d08a8e99c244ec3a4c89c6b4b92038151cd7a6
5
5
  SHA512:
6
- metadata.gz: 64a7448a8fb6b4273d5dcf61d7625f523c5db7b9b97cc26cf66b8580a4df4c2cc2d7a28f4e785c82edd811ed305106be57c2768b1da0a909925dd600dfdae75a
7
- data.tar.gz: 286fe7c82704872ecc924f2c414f5b0089b607623e4c2fecb79ee200ac547f3f7f7dfd3cde17e5c5755a9f5bd541f66f6df665f12d0b9288cc7fa7b34f811141
6
+ metadata.gz: 50ee3b942ab3be9bc95ca14ebecbedbd6aebd2fc36275db315b1d4ace8709e8c7e51a7572a85c3c5093faa6bcfafd801c8ed4e40bf5d7a128ee82f1fe1824f45
7
+ data.tar.gz: a186f4b1c87b8995004636566a7df145fe987e56b26fed4d87adbbb6cb01690913f0f34bcf536a68afc04c28d4886508178535e69f62295db7a0f053b7f2d359
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ cache: bundler
3
+
4
+ rvm:
5
+ - 1.9.3
6
+ - 2.0.0
7
+ - 2.1.5
8
+
9
+ script: 'bundle exec rake'
10
+
11
+ notifications:
12
+ email:
13
+ recipients:
14
+ - samuel@molinari.me
15
+ on_failure: change
16
+ on_success: never
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # GridStruct
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/grid_struct.svg)](http://badge.fury.io/rb/grid_struct)
4
+ [![Build Status](https://travis-ci.org/samuelmolinari/grid_struct.svg?branch=master)](https://travis-ci.org/samuelmolinari/grid_struct)
5
+ [![Code Climate](https://codeclimate.com/github/samuelmolinari/grid_struct/badges/gpa.svg)](https://codeclimate.com/github/samuelmolinari/grid_struct)
6
+ [![Test Coverage](https://codeclimate.com/github/samuelmolinari/grid_struct/badges/coverage.svg)](https://codeclimate.com/github/samuelmolinari/grid_struct)
7
+
3
8
  Manipulate grid like structure in Ruby.
4
9
 
5
10
  ## Installation
data/grid_struct.gemspec CHANGED
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "rspec"
24
24
  spec.add_development_dependency "guard"
25
25
  spec.add_development_dependency "guard-rspec"
26
+ spec.add_development_dependency "codeclimate-test-reporter"
26
27
  end
data/lib/grid_struct.rb CHANGED
@@ -26,24 +26,15 @@ class GridStruct
26
26
  return self
27
27
  end
28
28
 
29
- def map_row!(n)
30
- @columns.times.each do |column|
31
- index = get_index(n,column)
32
- value = @store[index]
33
- @store[index] = yield(value,column)
34
- end
35
- return self
29
+ def map_row!(n, &block)
30
+ return map_line!(n, @columns, true, &block)
36
31
  end
37
32
 
38
- def map_column!(n)
39
- @rows.times.each do |row|
40
- index = get_index(row,n)
41
- value = @store[index]
42
- @store[index] = yield(value,row)
43
- end
44
- return self
33
+ def map_column!(n, &block)
34
+ return map_line!(n, @rows, false, &block)
45
35
  end
46
36
 
37
+
47
38
  def each
48
39
  @store.fill(nil,@store.size...size).each.with_index do |value, index|
49
40
  row_column = get_row_column_at(index)
@@ -79,25 +70,7 @@ class GridStruct
79
70
  sub_row = index / sub_columns
80
71
  sub_column = index - (sub_row * sub_columns)
81
72
 
82
- indexes = []
83
- rows.times.each do |r|
84
- start_index = (r * @columns) +
85
- (sub_row * rows * @columns) +
86
- (sub_column * columns)
87
- end_index = start_index + columns
88
-
89
- start_row = get_row_column_at(start_index)[:row]
90
- end_row = get_row_column_at(end_index)[:row]
91
-
92
- if start_row != end_row
93
- extras = [0,(end_index - @columns)].max % @columns
94
- end_index -= extras
95
- end
96
-
97
- final_row_indexes = (start_index...end_index).to_a
98
- final_row_indexes.fill(nil,final_row_indexes.size...columns)
99
- indexes += final_row_indexes
100
- end
73
+ indexes = retrieve_sliced_indexes(rows,columns,sub_row,sub_column)
101
74
 
102
75
  return GridStruct::Selector.new(self, *indexes).dimensions(rows,columns)
103
76
  end
@@ -139,8 +112,50 @@ class GridStruct
139
112
  @store == other.instance_variable_get(:@store)
140
113
  end
141
114
 
115
+ def within_bounds?(row,column)
116
+ row_within_bounds?(row) && column_within_bounds?(column)
117
+ end
118
+
142
119
  protected
143
120
 
121
+ def fit_selection_within_same_row(start_index, end_index)
122
+ start_row = get_row_column_at(start_index)[:row]
123
+ end_row = get_row_column_at(end_index)[:row]
124
+
125
+ if start_row != end_row
126
+ extras = [0,(end_index - @columns)].max % @columns
127
+ end_index -= extras
128
+ end
129
+
130
+ return start_index...end_index
131
+ end
132
+
133
+ def retrieve_sliced_indexes(rows, columns, sub_row, sub_column)
134
+ rows.times.inject([]) do |memo, r|
135
+ start_index = (r * @columns) +
136
+ (sub_row * rows * @columns) +
137
+ (sub_column * columns)
138
+ end_index = start_index + columns
139
+
140
+ final_row_indexes = fit_selection_within_same_row(start_index, end_index).to_a
141
+ final_row_indexes.fill(nil,final_row_indexes.size...columns)
142
+
143
+ memo += final_row_indexes
144
+ end
145
+ end
146
+
147
+ def line_within_bounds?(n, axis_size)
148
+ n >= 0 && n < axis_size
149
+ end
150
+
151
+ def row_within_bounds?(row)
152
+ line_within_bounds?(row, @rows)
153
+ end
154
+
155
+ def column_within_bounds?(column)
156
+ line_within_bounds?(column, @columns)
157
+ end
158
+
144
159
  def get_index(row,column)
145
160
  (row * @columns) + column
146
161
  end
@@ -156,31 +171,36 @@ class GridStruct
156
171
  column = coordinates[:column]
157
172
  index = get_index(row,column)
158
173
 
159
- if !diagonal_indexes.include?(index) &&
160
- row >= 0 &&
161
- column >= 0 &&
162
- row < @rows &&
163
- column < @columns
164
-
174
+ if !diagonal_indexes.include?(index) && within_bounds?(row, column)
165
175
  diagonal_indexes.method(action).call(get_index(row,column))
166
176
 
167
- diagonal_builder({ row: row - (row_direction * 1),
168
- column: column - (column_direction * 1) },
169
- row_direction,
170
- column_direction,
171
- diagonal_indexes,
172
- :unshift)
173
-
174
- diagonal_builder({ row: row + (row_direction * 1),
175
- column: column + (column_direction * 1) },
176
- row_direction,
177
- column_direction,
178
- diagonal_indexes,
179
- :push)
180
-
177
+ fetch_diagonal_index(row, column, row_direction, column_direction, diagonal_indexes, -1)
178
+ fetch_diagonal_index(row, column, row_direction, column_direction, diagonal_indexes, 1)
181
179
  end
182
180
 
183
181
  return diagonal_indexes
184
182
  end
185
183
 
184
+ def fetch_diagonal_index(row, column, row_direction, column_direction, diagonal_indexes, sign)
185
+ row = row + sign * row_direction
186
+ column = column + sign * column_direction
187
+ action = sign > 0 ? :push : :unshift
188
+
189
+ diagonal_builder({ row: row,
190
+ column: column },
191
+ row_direction,
192
+ column_direction,
193
+ diagonal_indexes,
194
+ action)
195
+ end
196
+
197
+ def map_line!(n, other_axis_size, is_row)
198
+ other_axis_size.times.each do |other_axis|
199
+ index = is_row ? get_index(n,other_axis) : get_index(other_axis,n)
200
+ value = @store[index]
201
+ @store[index] = yield(value,other_axis)
202
+ end
203
+ return self
204
+ end
205
+
186
206
  end
@@ -1,3 +1,3 @@
1
1
  class GridStruct
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,6 @@
1
+ require 'codeclimate-test-reporter'
2
+ CodeClimate::TestReporter.start
3
+
1
4
  require 'grid_struct'
2
5
 
3
6
  RSpec.configure do |config|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grid_struct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Molinari
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-17 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - '>='
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: codeclimate-test-reporter
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
83
97
  description: Grid data structure
84
98
  email:
85
99
  - samuel@molinari.me
@@ -89,6 +103,7 @@ extra_rdoc_files: []
89
103
  files:
90
104
  - .gitignore
91
105
  - .rspec
106
+ - .travis.yml
92
107
  - Gemfile
93
108
  - Guardfile
94
109
  - LICENSE.txt