array_2d 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'http://rubygems.org'
2
+
3
+ group :development do
4
+ gem 'rdoc'
5
+ gem 'bundler'
6
+ gem 'jeweler'
7
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ git (1.2.5)
5
+ jeweler (1.8.4)
6
+ bundler (~> 1.0)
7
+ git (>= 1.2.5)
8
+ rake
9
+ rdoc
10
+ json (1.7.5)
11
+ rake (0.9.2.2)
12
+ rdoc (3.12)
13
+ json (~> 1.4)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler
20
+ jeweler
21
+ rdoc
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Luke Grecki
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,19 @@
1
+ = array_2d
2
+
3
+ Description goes here.
4
+
5
+ == Contributing to array_2d
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Luke Grecki. See LICENSE.txt for
18
+ further details.
19
+
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :development)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ $stderr.puts "Run `bundle install` to install missing gems"
9
+ exit e.status_code
10
+ end
11
+ require 'rake'
12
+
13
+ require 'jeweler'
14
+ Jeweler::Tasks.new do |gem|
15
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
16
+ gem.name = "array_2d"
17
+ gem.homepage = "http://github.com/lukegrecki/array_2d"
18
+ gem.license = "MIT"
19
+ gem.summary = "A lightweight mutabke 2D array class"
20
+ gem.description = "With this class you can create mutable 2D arrays and change subarrays."
21
+ gem.email = "lukegrecki@gmail.com"
22
+ gem.authors = ["Luke Grecki"]
23
+ # dependencies defined in Gemfile
24
+ end
25
+ Jeweler::RubygemsDotOrgTasks.new
26
+
27
+ require 'rake/testtask'
28
+ Rake::TestTask.new(:test) do |test|
29
+ test.libs << 'lib' << 'test'
30
+ test.pattern = 'test/**/test_*.rb'
31
+ test.verbose = true
32
+ end
33
+
34
+ task :default => :test
35
+
36
+ require 'rdoc/task'
37
+ Rake::RDocTask.new do |rdoc|
38
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
39
+
40
+ rdoc.rdoc_dir = 'rdoc'
41
+ rdoc.title = "array_2d #{version}"
42
+ rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('lib/**/*.rb')
44
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/array_2d.gemspec ADDED
@@ -0,0 +1,56 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "array_2d"
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Luke Grecki"]
12
+ s.date = "2012-10-08"
13
+ s.description = "With this class you can create mutable 2D arrays and change subarrays."
14
+ s.email = "lukegrecki@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "array_2d.gemspec",
28
+ "lib/array_2d.rb",
29
+ "test/helper.rb",
30
+ "test/test_array_2d.rb"
31
+ ]
32
+ s.homepage = "http://github.com/lukegrecki/array_2d"
33
+ s.licenses = ["MIT"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = "1.8.10"
36
+ s.summary = "A lightweight mutabke 2D array class"
37
+
38
+ if s.respond_to? :specification_version then
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
42
+ s.add_development_dependency(%q<rdoc>, [">= 0"])
43
+ s.add_development_dependency(%q<bundler>, [">= 0"])
44
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
45
+ else
46
+ s.add_dependency(%q<rdoc>, [">= 0"])
47
+ s.add_dependency(%q<bundler>, [">= 0"])
48
+ s.add_dependency(%q<jeweler>, [">= 0"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<rdoc>, [">= 0"])
52
+ s.add_dependency(%q<bundler>, [">= 0"])
53
+ s.add_dependency(%q<jeweler>, [">= 0"])
54
+ end
55
+ end
56
+
data/lib/array_2d.rb ADDED
@@ -0,0 +1,109 @@
1
+ class Array2D
2
+ include Enumerable
3
+ attr_accessor :state
4
+
5
+ def initialize(rows, columns, value=nil)
6
+ @state = Array.new(rows) { Array.new(columns) { value } }
7
+ end
8
+
9
+ def each(&block)
10
+ @state.each do |row|
11
+ row.each do |e|
12
+ yield e
13
+ end
14
+ end
15
+ end
16
+
17
+ def each_with_index(&block)
18
+ @state.each_with_index do |row, row_index|
19
+ row.each_with_index do |e, column_index|
20
+ yield e, [row_index, column_index]
21
+ end
22
+ end
23
+ end
24
+
25
+ def to_s
26
+ @state.to_s
27
+ end
28
+
29
+ def ==(o)
30
+ o.class == self.class && o.state == state
31
+ end
32
+
33
+ def size
34
+ [row_size, column_size]
35
+ end
36
+
37
+ def row_size
38
+ @state.size
39
+ end
40
+
41
+ def column_size
42
+ @state[0].size
43
+ end
44
+
45
+ def [](x, y)
46
+ case x
47
+ when Integer
48
+ case y
49
+ when Integer
50
+ @state[x][y]
51
+ when Range
52
+ subarray = Array.new(y.to_a.size)
53
+ y.each {|yi| subarray[yi - y.first] = @state[x][yi]}
54
+ subarray
55
+ end
56
+ when Range
57
+ case y
58
+ when Integer
59
+ subarray = Array.new(x.to_a.size)
60
+ x.each {|xi| subarray[xi - x.first] = @state[xi][y]}
61
+ subarray
62
+ when Range
63
+ subarray = Array2D.new(x.to_a.size, y.to_a.size)
64
+ x.each do |xi|
65
+ y.each do |yi|
66
+ subarray.state[xi - x.first][yi - y.first] = @state[xi][yi]
67
+ end
68
+ end
69
+ subarray
70
+ end
71
+ end
72
+ end
73
+
74
+ def []=(x, y, value)
75
+ case x
76
+ when Integer
77
+ case y
78
+ when Integer
79
+ @state[x][y] = value
80
+ when Range
81
+ if value.is_a?(Array) && y.to_a.size == value.size
82
+ y.each {|yi| @state[x][yi] = value[yi - y.first]}
83
+ else
84
+ y.each {|yi| @state[x][yi] = value}
85
+ end
86
+ end
87
+ when Range
88
+ case y
89
+ when Integer
90
+ if value.is_a?(Array) && x.to_a.size == value.size
91
+ x.each {|xi| @state[xi][y] = value[xi - x.first]}
92
+ else
93
+ x.each {|xi| @state[xi][y] = value}
94
+ end
95
+ when Range
96
+ x.each do |xi|
97
+ y.each do |yi|
98
+ if value.is_a?(Array2D) && [x.to_a.size, y.to_a.size] == value.size
99
+ @state[xi][yi] = value[xi - x.first, yi - y.first]
100
+ else
101
+ @state[xi][yi] = value
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
108
+
109
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ begin
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts "Run `bundle install` to install missing gems"
7
+ exit e.status_code
8
+ end
9
+ require 'test/unit'
10
+
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ require 'array_2d'
@@ -0,0 +1,79 @@
1
+ require 'helper'
2
+
3
+ class Array2DTest < Test::Unit::TestCase
4
+ def setup
5
+ @array2d = Array2D.new(3, 5, 0)
6
+ end
7
+
8
+ def test_initialize
9
+ assert_instance_of Array2D, Array2D.new(3, 5)
10
+ assert_instance_of Array2D, Array2D.new(3, 5, 0)
11
+ end
12
+
13
+ def test_each
14
+ @array2d.each {|e| assert_equal(0, e)}
15
+ end
16
+
17
+ def test_each_with_index
18
+ @array2d.each_with_index do |e, index|
19
+ assert_equal 0, e
20
+ assert_instance_of Array, index
21
+ assert_equal 2, index.size
22
+ end
23
+ end
24
+
25
+ def test_to_s
26
+ array2d = Array2D.new(2, 2, 0)
27
+ assert_equal '[[0, 0], [0, 0]]', array2d.to_s
28
+ end
29
+
30
+ def test_equality
31
+ @array2d[0..1, 0..1] = [[0, 1], [2, 3]]
32
+ new_array2d = Array2D.new(3, 5, 0)
33
+ new_array2d[0..1, 0..1] = [[0, 1], [2, 3]]
34
+ assert @array2d == new_array2d
35
+ end
36
+
37
+ def test_size
38
+ assert_equal [3, 5], @array2d.size
39
+ end
40
+
41
+ def test_row_size
42
+ assert_equal 3, @array2d.row_size
43
+ end
44
+
45
+ def test_column_size
46
+ assert_equal 5, @array2d.column_size
47
+ end
48
+
49
+ def test_bracket_with_integers
50
+ @array2d[1, 2] = 4
51
+ assert_equal 4, @array2d[1, 2]
52
+ end
53
+
54
+ def test_bracket_with_ranges
55
+ array2d = Array2D.new(3, 3)
56
+ subarray = Array2D.new(2, 2)
57
+ subarray.state = [[0, 1], [2, 3]]
58
+ array2d[0...2, 1...3] = subarray
59
+ assert_equal subarray, array2d[0...2, 1...3]
60
+
61
+ array2d[0...2, 1...3] = 5
62
+ subarray = Array2D.new(2, 2, 5)
63
+ assert_equal subarray, array2d[0...2, 1...3]
64
+ end
65
+
66
+ def test_bracket_with_integers_and_ranges
67
+ @array2d[0..1, 0] = [0, 1]
68
+ assert_equal [0, 1], @array2d[0..1, 0]
69
+
70
+ @array2d[0, 0..1] = [2, 3]
71
+ assert_equal [2, 3], @array2d[0, 0..1]
72
+
73
+ @array2d[0...3, 0] = 5
74
+ assert_equal [5, 5, 5], @array2d[0...3, 0]
75
+
76
+ @array2d[0, 0...5] = 7
77
+ assert_equal [7, 7, 7, 7, 7], @array2d[0, 0...5]
78
+ end
79
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: array_2d
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Luke Grecki
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-08 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: &80743030 !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: *80743030
25
+ - !ruby/object:Gem::Dependency
26
+ name: bundler
27
+ requirement: &80742780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *80742780
36
+ - !ruby/object:Gem::Dependency
37
+ name: jeweler
38
+ requirement: &80742520 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *80742520
47
+ description: With this class you can create mutable 2D arrays and change subarrays.
48
+ email: lukegrecki@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files:
52
+ - LICENSE.txt
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - Gemfile
57
+ - Gemfile.lock
58
+ - LICENSE.txt
59
+ - README.rdoc
60
+ - Rakefile
61
+ - VERSION
62
+ - array_2d.gemspec
63
+ - lib/array_2d.rb
64
+ - test/helper.rb
65
+ - test/test_array_2d.rb
66
+ homepage: http://github.com/lukegrecki/array_2d
67
+ licenses:
68
+ - MIT
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ segments:
80
+ - 0
81
+ hash: -786817233
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.10
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: A lightweight mutabke 2D array class
94
+ test_files: []