array_2d_simple 0.2.0

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.
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+
19
+ .rbenv-gemsets
20
+ .rbenv-version
21
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rspec'
4
+ gem 'factory_girl'
5
+ gem 'guard-rspec'
6
+ gem 'rb-fsevent', '~> 0.9.1'
7
+
8
+ # Specify your gem's dependencies in array_2d_simple.gemspec
9
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,24 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Rails example
10
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
11
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
12
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
13
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
14
+ watch('config/routes.rb') { "spec/routing" }
15
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
16
+
17
+ # Capybara features specs
18
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
19
+
20
+ # Turnip features and steps
21
+ watch(%r{^spec/acceptance/(.+)\.feature$})
22
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
23
+ end
24
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 George
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # Array2dSimple
2
+
3
+ This gem allows you to create a 2D Array in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'array_2d_simple'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install array_2d_simple
18
+
19
+ ## Usage
20
+
21
+ There are 2 ways of setting up the 2D Array:
22
+
23
+ 1. By iterating with #set_by_rc
24
+ 2. By passing a block to #set_each
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'array_2d_simple/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "array_2d_simple"
8
+ gem.version = Array2dSimple::VERSION
9
+ gem.authors = ["George Ulmer"]
10
+ gem.email = ["george@boost-media.com"]
11
+ gem.description = %q{Implementation of 2D array}
12
+ gem.summary = %q{Implementation of 2D array}
13
+ gem.homepage = "https://github.com/georgeu2000/array_2d_simple"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.add_development_dependency "rspec"
21
+ end
@@ -0,0 +1,111 @@
1
+ require "array_2d_simple/version"
2
+
3
+ module Array2dSimple
4
+ class Array2d
5
+ attr_reader :width, :height
6
+
7
+ def initialize params
8
+ raise 'width must be set' unless params[:width]
9
+ raise 'height must be set' unless params[:height]
10
+
11
+ params.each do |key,value|
12
+ instance_variable_set("@#{key}",value)
13
+ end
14
+ @elements = Array.new(@height){ Array.new(@width) }
15
+ end
16
+
17
+ def to_s
18
+ "#{ self.class }: #{ @width }x#{ @height }"
19
+ end
20
+
21
+ def get_by_rc r,c
22
+ raise 'x out of range' if c >= @width
23
+ raise 'y out of range' if r >= @height
24
+ @elements[r][c]
25
+ end
26
+
27
+ def set_by_rc r,c, value
28
+ raise 'x out of range' if c >= @width
29
+ raise 'y out of range' if r >= @height
30
+ @elements[r][c] = value
31
+ end
32
+
33
+ def get_by_xy x,y
34
+ get_by_rc y,x
35
+ end
36
+
37
+ def [] x,y
38
+ get_by_rc y,x
39
+ end
40
+
41
+ def []= x,y, value
42
+ @elements[y][x] = value
43
+ end
44
+
45
+ def output
46
+ size = format2(max.to_f).size
47
+ output = ''
48
+ (0..@height - 1).each do |r|
49
+ (0..@width - 1).each do |c|
50
+ output += "%#{ size + 4 }s" % format2(get_by_rc(r,c).to_f) if get_by_rc(r,c).respond_to? :to_f
51
+ end
52
+ output += "\n"
53
+ end
54
+ output
55
+ end
56
+
57
+ def max
58
+ max = -999999
59
+ (0..@height - 1).each do |r|
60
+ (0..@width - 1).each do |c|
61
+ value = get_by_rc(r,c).to_f if get_by_rc(r,c).respond_to?(:to_f)
62
+ max = [max, value].max if value and ! value.to_f.nan?
63
+ end
64
+ end
65
+ max
66
+ end
67
+
68
+ def min
69
+ min = 999999
70
+ (0..@height - 1).each do |r|
71
+ (0..@width - 1).each do |c|
72
+ value = get_by_rc(r,c).to_f if value.respond_to?(:to_f)
73
+ min = [min, value].min if value and ! value.to_f.nan?
74
+ end
75
+ end
76
+ min
77
+ end
78
+
79
+ def project &proc
80
+ new_array = self.class.new( width: @width, height: @height )
81
+ (0..@height - 1).each do |r|
82
+ (0..@width - 1).each do |c|
83
+ new_array.set_by_rc(r,c, proc.call(get_by_rc(r,c)))
84
+ end
85
+ end
86
+ new_array
87
+ end
88
+
89
+ def set_each &proc
90
+ (0..@height - 1).each do |r|
91
+ (0..@width - 1).each do |c|
92
+ @elements[r][c] = proc.call(r,c)
93
+ end
94
+ end
95
+ end
96
+
97
+ def each &proc
98
+ (0..@height - 1).each do |r|
99
+ (0..@width - 1).each do |c|
100
+ @elements[r][c] = proc.call(@elements[r][c]) if @elements[r][c].class == Float
101
+ end
102
+ end
103
+ end
104
+
105
+ # Helper methods
106
+ def format2 value
107
+ "%0.2f" % value.round(2).to_s if value.finite?
108
+ end
109
+
110
+ end
111
+ end
@@ -0,0 +1,21 @@
1
+ require 'delegate'
2
+
3
+ module Array2dSimple
4
+ class NormalizedArray2d < SimpleDelegator
5
+ def [] x, y
6
+ normalized_by_rc y,x
7
+ end
8
+
9
+ def normalized_by_rc r,c
10
+ value = get_by_rc(r,c)
11
+ # value = max if value == Float::INFINITY
12
+ # value = min if value == -Float::INFINITY # Does this work?
13
+ if value and value.respond_to?(:to_f)
14
+ return (value.to_f - min) / (max - min).to_f
15
+ else
16
+ return 0.0
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Array2dSimple
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'array_2d_simple/array_2d'
2
+ require 'array_2d_simple/normalized_array_2d'
@@ -0,0 +1,78 @@
1
+ require 'spec_helper.rb'
2
+
3
+ module Array2dSimple
4
+ describe Array2d do
5
+ let(:array_2d) { Array2d.new(width:8, height: 2) }
6
+
7
+ it 'creates' do
8
+ array_2d.class.should eq Array2d
9
+ end
10
+
11
+ it 'creates different size' do
12
+ big_array = Array2d.new(width: 30, height: 100)
13
+ big_array.to_s.should include '30x100'
14
+ end
15
+
16
+ context 'shared' do
17
+ before do
18
+ proc = Proc.new { |r, c| r * 8 + c + 1}
19
+ array_2d.set_each &proc
20
+ end
21
+
22
+ it 'fills Array2d' do
23
+ array_2d.get_by_rc(0,0).should eq 1
24
+ array_2d.get_by_rc(0,7).should eq 8
25
+ array_2d.get_by_rc(1,1).should eq 10
26
+ array_2d.get_by_rc(1,3).should eq 12
27
+ array_2d.get_by_rc(1,7).should eq 16
28
+ end
29
+
30
+ it 'gets by rc' do
31
+ array_2d.get_by_rc(1,4).should eq 13
32
+ end
33
+
34
+ it 'gets_by_r_c' do
35
+ array_2d.get_by_rc(1,5).should eq 14
36
+ end
37
+
38
+ it 'gets_by_x_y' do
39
+ array_2d.get_by_xy(5,1).should eq 14
40
+ end
41
+
42
+ it 'gets [x,y]' do
43
+ array_2d[5,1].should eq 14
44
+ end
45
+
46
+ it 'sets [x,y]' do
47
+ array_2d[2,1] = 88
48
+ array_2d[2,1].should eq 88
49
+ end
50
+
51
+ it 'prints Array2d' do
52
+ array_2d.output.class.should eq String
53
+ end
54
+
55
+ it 'gets max' do
56
+ array_2d.max.should eq 16
57
+ end
58
+
59
+ it 'gets min' do
60
+ array_2d.min.should eq 1
61
+ end
62
+
63
+ it 'projects values to new Array2d' do
64
+ proc = Proc.new { |i| i * 4 }
65
+ projection = array_2d.project &proc
66
+ projection.class.should eq array_2d.class
67
+ projection.to_s.should eq array_2d.to_s
68
+ projection[3,1].should eq array_2d[3,1] * 4
69
+ end
70
+
71
+ it 'raises x out of range exception' do
72
+ expect { array_2d[8,0] }.to raise_error 'x out of range'
73
+ expect { array_2d[0,2] }.to raise_error 'y out of range'
74
+ end
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,11 @@
1
+ FactoryGirl.define do
2
+
3
+ factory :array_2d_simple do
4
+ height 2
5
+ width 8
6
+
7
+ initialize_with { new(attributes) }
8
+ end
9
+
10
+
11
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper.rb'
2
+
3
+ module Array2dSimple
4
+ describe NormalizedArray2d do
5
+ let(:array_2d) { Array2d.new(width:8, height:2) }
6
+
7
+ it 'gets normalized value' do
8
+ array_2d.set_each { |r, c| r * 8 + c + 1}
9
+ normalized_array = NormalizedArray2d.new(array_2d)
10
+ normalized_array[0,0].should eq 0.0
11
+ normalized_array[3,0].should eq 0.2
12
+ normalized_array[7,1].should eq 1.0
13
+ end
14
+
15
+ end
16
+
17
+ end
@@ -0,0 +1,9 @@
1
+ # require 'simplecov'
2
+ # SimpleCov.start if ENV['CODE_COVERAGE']
3
+ require './lib/array_2d_simple.rb'
4
+
5
+ RSpec.configure do |c|
6
+ c.filter_run focus: true
7
+ c.run_all_when_everything_filtered = true
8
+ c.filter_run_excluding slow: true unless ENV['ALL']
9
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_2d_simple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - George Ulmer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Implementation of 2D array
31
+ email:
32
+ - george@boost-media.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - .rspec
39
+ - Gemfile
40
+ - Guardfile
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - array_2d_simple.gemspec
45
+ - lib/array_2d_simple.rb
46
+ - lib/array_2d_simple/array_2d.rb
47
+ - lib/array_2d_simple/normalized_array_2d.rb
48
+ - lib/array_2d_simple/version.rb
49
+ - spec/array_2d_simple_spec.rb
50
+ - spec/factories/array_2d_simples.rb
51
+ - spec/normalized_array_2d_spec.rb
52
+ - spec/spec_helper.rb
53
+ homepage: https://github.com/georgeu2000/array_2d_simple
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.23
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Implementation of 2D array
77
+ test_files:
78
+ - spec/array_2d_simple_spec.rb
79
+ - spec/factories/array_2d_simples.rb
80
+ - spec/normalized_array_2d_spec.rb
81
+ - spec/spec_helper.rb