modalsupport 0.5.2 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.2
1
+ 0.6.0
@@ -0,0 +1,14 @@
1
+ class Array
2
+ # backport of Ruby 1.9.2 Array#rotate
3
+ unless [].respond_to?(:rotate)
4
+ def rotate(n=1)
5
+ n = (n % self.size)
6
+ n>0 ? self[n..-1] + self[0...n] : self
7
+ end
8
+ end
9
+ unless [].respond_to?(:rotate!)
10
+ def rotate!(n=1)
11
+ self.replace(self.rotate(n))
12
+ end
13
+ end
14
+ end
@@ -29,5 +29,5 @@ def ruby_platform_is?(platform)
29
29
  end
30
30
 
31
31
  def if_ruby_platform_is(platform)
32
- yield if platform_is?(platform)
32
+ yield if platform_is?(platform)
33
33
  end
data/lib/modalsupport.rb CHANGED
@@ -5,6 +5,7 @@ require 'modalsupport/string'
5
5
  require 'modalsupport/regexp'
6
6
  require 'modalsupport/enumerable'
7
7
  require 'modalsupport/file'
8
+ require 'modalsupport/array'
8
9
 
9
10
  module ModalSupport
10
11
  end
data/modalsupport.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{modalsupport}
8
- s.version = "0.5.2"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Javier Goizueta"]
12
- s.date = %q{2010-06-10}
12
+ s.date = %q{2011-02-08}
13
13
  s.description = %q{additional support extensions to ActiveSupport and HoboSupport}
14
14
  s.email = %q{jgoizueta@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/modalsupport.rb",
27
+ "lib/modalsupport/array.rb",
27
28
  "lib/modalsupport/enumerable.rb",
28
29
  "lib/modalsupport/file.rb",
29
30
  "lib/modalsupport/full.rb",
@@ -44,6 +45,7 @@ Gem::Specification.new do |s|
44
45
  "test/test_paralell_each.rb",
45
46
  "test/test_product.rb",
46
47
  "test/test_relative_path.rb",
48
+ "test/test_rotate.rb",
47
49
  "test/test_slice.rb",
48
50
  "test/test_state_equivalent.rb",
49
51
  "test/test_unindent.rb"
@@ -51,7 +53,7 @@ Gem::Specification.new do |s|
51
53
  s.homepage = %q{http://github.com/jgoizueta/modalsupport}
52
54
  s.rdoc_options = ["--charset=UTF-8"]
53
55
  s.require_paths = ["lib"]
54
- s.rubygems_version = %q{1.3.6}
56
+ s.rubygems_version = %q{1.3.7}
55
57
  s.summary = %q{simple extensions to core classes}
56
58
  s.test_files = [
57
59
  "test/helper.rb",
@@ -63,16 +65,18 @@ Gem::Specification.new do |s|
63
65
  "test/test_paralell_each.rb",
64
66
  "test/test_product.rb",
65
67
  "test/test_relative_path.rb",
68
+ "test/test_rotate.rb",
66
69
  "test/test_slice.rb",
67
70
  "test/test_state_equivalent.rb",
68
- "test/test_unindent.rb"
71
+ "test/test_unindent.rb",
72
+ "test/tstr.rb"
69
73
  ]
70
74
 
71
75
  if s.respond_to? :specification_version then
72
76
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
73
77
  s.specification_version = 3
74
78
 
75
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
79
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
76
80
  s.add_development_dependency(%q<shoulda>, [">= 0"])
77
81
  else
78
82
  s.add_dependency(%q<shoulda>, [">= 0"])
@@ -0,0 +1,72 @@
1
+ require 'helper'
2
+
3
+ class TestRotate < Test::Unit::TestCase
4
+
5
+ context "Arrays" do
6
+
7
+ should "have a proper rotate method" do
8
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate
9
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate(1)
10
+ assert_equal [3,4,5,6,7,1,2], [1,2,3,4,5,6,7].rotate(2)
11
+ assert_equal [4,5,6,7,1,2,3], [1,2,3,4,5,6,7].rotate(3)
12
+ assert_equal [5,6,7,1,2,3,4], [1,2,3,4,5,6,7].rotate(4)
13
+ assert_equal [6,7,1,2,3,4,5], [1,2,3,4,5,6,7].rotate(5)
14
+ assert_equal [7,1,2,3,4,5,6], [1,2,3,4,5,6,7].rotate(6)
15
+ assert_equal [1,2,3,4,5,6,7], [1,2,3,4,5,6,7].rotate(7)
16
+ assert_equal [1,2,3,4,5,6,7], [1,2,3,4,5,6,7].rotate(0)
17
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate(8)
18
+ assert_equal [7,1,2,3,4,5,6], [1,2,3,4,5,6,7].rotate(-1)
19
+ assert_equal [6,7,1,2,3,4,5], [1,2,3,4,5,6,7].rotate(-2)
20
+ assert_equal [5,6,7,1,2,3,4], [1,2,3,4,5,6,7].rotate(-3)
21
+ assert_equal [4,5,6,7,1,2,3], [1,2,3,4,5,6,7].rotate(-4)
22
+ assert_equal [3,4,5,6,7,1,2], [1,2,3,4,5,6,7].rotate(-5)
23
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate(-6)
24
+ assert_equal [1,2,3,4,5,6,7], [1,2,3,4,5,6,7].rotate(-7)
25
+ assert_equal [7,1,2,3,4,5,6], [1,2,3,4,5,6,7].rotate(-8)
26
+ assert_equal [3,4,5,6,7,1,2], [1,2,3,4,5,6,7].rotate(23)
27
+ assert_equal [6,7,1,2,3,4,5], [1,2,3,4,5,6,7].rotate(-23)
28
+ end
29
+
30
+ should "not be muted by rotate" do
31
+ a = [1,2,3,4,5,6,7]
32
+ a.rotate(3)
33
+ assert_equal [1,2,3,4,5,6,7], a
34
+ a.rotate(-3)
35
+ assert_equal [1,2,3,4,5,6,7], a
36
+ end
37
+
38
+ should "have a proper rotate! method" do
39
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate!
40
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate!(1)
41
+ assert_equal [3,4,5,6,7,1,2], [1,2,3,4,5,6,7].rotate!(2)
42
+ assert_equal [4,5,6,7,1,2,3], [1,2,3,4,5,6,7].rotate!(3)
43
+ assert_equal [5,6,7,1,2,3,4], [1,2,3,4,5,6,7].rotate!(4)
44
+ assert_equal [6,7,1,2,3,4,5], [1,2,3,4,5,6,7].rotate!(5)
45
+ assert_equal [7,1,2,3,4,5,6], [1,2,3,4,5,6,7].rotate!(6)
46
+ assert_equal [1,2,3,4,5,6,7], [1,2,3,4,5,6,7].rotate!(7)
47
+ assert_equal [1,2,3,4,5,6,7], [1,2,3,4,5,6,7].rotate!(0)
48
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate!(8)
49
+ assert_equal [7,1,2,3,4,5,6], [1,2,3,4,5,6,7].rotate!(-1)
50
+ assert_equal [6,7,1,2,3,4,5], [1,2,3,4,5,6,7].rotate!(-2)
51
+ assert_equal [5,6,7,1,2,3,4], [1,2,3,4,5,6,7].rotate!(-3)
52
+ assert_equal [4,5,6,7,1,2,3], [1,2,3,4,5,6,7].rotate!(-4)
53
+ assert_equal [3,4,5,6,7,1,2], [1,2,3,4,5,6,7].rotate!(-5)
54
+ assert_equal [2,3,4,5,6,7,1], [1,2,3,4,5,6,7].rotate!(-6)
55
+ assert_equal [1,2,3,4,5,6,7], [1,2,3,4,5,6,7].rotate!(-7)
56
+ assert_equal [7,1,2,3,4,5,6], [1,2,3,4,5,6,7].rotate!(-8)
57
+ assert_equal [3,4,5,6,7,1,2], [1,2,3,4,5,6,7].rotate!(23)
58
+ assert_equal [6,7,1,2,3,4,5], [1,2,3,4,5,6,7].rotate!(-23)
59
+ end
60
+
61
+ should "be muted by rotate" do
62
+ a = [1,2,3,4,5,6,7]
63
+ a.rotate!(3)
64
+ assert_equal [4,5,6,7,1,2,3], a
65
+ a = [1,2,3,4,5,6,7]
66
+ a.rotate!(-3)
67
+ assert_equal [5,6,7,1,2,3,4], a
68
+ end
69
+
70
+ end
71
+
72
+ end
data/test/tstr.rb ADDED
@@ -0,0 +1,109 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '.', 'lib'))
2
+
3
+ require 'benchmark'
4
+
5
+ class Array
6
+
7
+ # rot1: splicing
8
+ def rot1(n=1)
9
+ self[n..-1] + self[0...n]
10
+ end
11
+ def rot1!(n=1)
12
+ self.replace(self.rot1(n))
13
+ end
14
+
15
+ # rot2: <</shift, no válido para n<0
16
+ def rot2(n=1)
17
+ a = dup
18
+ n.times do a << a.shift end
19
+ a
20
+ end
21
+ def rot2!(n=1)
22
+ n.times do self << self.shift end
23
+ self
24
+ end
25
+
26
+ # rot3: in-place juggling
27
+ def rot3!(n=1)
28
+ m = self.size
29
+ i0 = 0
30
+ v0 = self[i=i0]
31
+ loop do
32
+ j = (i+n)%m # next index
33
+ if j==i0
34
+ self[i] = v0
35
+ break
36
+ else
37
+ self[i] = self[j]
38
+ end
39
+ i = j
40
+ end
41
+ self
42
+ end
43
+ def rot3(n=1)
44
+ self.dup.rot3!(n)
45
+ end
46
+
47
+ end
48
+
49
+ if false
50
+ puts [1,2,3,4,5,6,7].rot3(0).inspect
51
+ puts [1,2,3,4,5,6,7].rot3(1).inspect
52
+ puts [1,2,3,4,5,6,7].rot3(3).inspect
53
+ puts [1,2,3,4,5,6,7].rot3(5).inspect
54
+ puts [1,2,3,4,5,6,7].rot3(6).inspect
55
+
56
+ a=(1..7).to_a
57
+ a.rot3!(3)
58
+ puts a.inspect
59
+
60
+ exit
61
+ end
62
+
63
+ N=100
64
+ M=1000
65
+ M2=15
66
+
67
+ a1 = Array.new(M){rand}
68
+ a2 = Array.new(M){Array.new(rand(M2)){rand}}
69
+ b1 = b2 = nil
70
+
71
+ Benchmark.bm(15) do |bm|
72
+ b1 = a1.dup
73
+ b2 = a2.dup
74
+ bm.report("rot1") {
75
+ N.times{ (1...b1.size).each{|n| b1.rot1(n)} }
76
+ N.times{ (1...b2.size).each{|n| b2.rot1(n)} }
77
+ }
78
+ b1 = a1.dup
79
+ b2 = a2.dup
80
+ bm.report("rot1!") {
81
+ N.times{ (1...b1.size).each{|n| b1.rot1!(n)} }
82
+ N.times{ (1...b2.size).each{|n| b2.rot1!(n)} }
83
+ }
84
+ b1 = a1.dup
85
+ b2 = a2.dup
86
+ bm.report("rot2") {
87
+ N.times{ (1...b1.size).each{|n| b1.rot2(n)} }
88
+ N.times{ (1...b2.size).each{|n| b2.rot2(n)} }
89
+ }
90
+ b1 = a1.dup
91
+ b2 = a2.dup
92
+ bm.report("rot2!") {
93
+ N.times{ (1...b1.size).each{|n| b1.rot2!(n)} }
94
+ N.times{ (1...b2.size).each{|n| b2.rot2!(n)} }
95
+ }
96
+ b1 = a1.dup
97
+ b2 = a2.dup
98
+ bm.report("rot3") {
99
+ N.times{ (1...b1.size).each{|n| b1.rot3(n)} }
100
+ N.times{ (1...b2.size).each{|n| b2.rot3(n)} }
101
+ }
102
+ b1 = a1.dup
103
+ b2 = a2.dup
104
+ bm.report("rot3!") {
105
+ N.times{ (1...b1.size).each{|n| b1.rot3!(n)} }
106
+ N.times{ (1...b2.size).each{|n| b2.rot3!(n)} }
107
+ }
108
+
109
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: modalsupport
3
3
  version: !ruby/object:Gem::Version
4
- hash: 15
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
9
- - 2
10
- version: 0.5.2
8
+ - 6
9
+ - 0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Javier Goizueta
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-06-10 00:00:00 +02:00
18
+ date: 2011-02-08 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -49,6 +49,7 @@ files:
49
49
  - Rakefile
50
50
  - VERSION
51
51
  - lib/modalsupport.rb
52
+ - lib/modalsupport/array.rb
52
53
  - lib/modalsupport/enumerable.rb
53
54
  - lib/modalsupport/file.rb
54
55
  - lib/modalsupport/full.rb
@@ -69,9 +70,11 @@ files:
69
70
  - test/test_paralell_each.rb
70
71
  - test/test_product.rb
71
72
  - test/test_relative_path.rb
73
+ - test/test_rotate.rb
72
74
  - test/test_slice.rb
73
75
  - test/test_state_equivalent.rb
74
76
  - test/test_unindent.rb
77
+ - test/tstr.rb
75
78
  has_rdoc: true
76
79
  homepage: http://github.com/jgoizueta/modalsupport
77
80
  licenses: []
@@ -102,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
105
  requirements: []
103
106
 
104
107
  rubyforge_project:
105
- rubygems_version: 1.3.6
108
+ rubygems_version: 1.3.7
106
109
  signing_key:
107
110
  specification_version: 3
108
111
  summary: simple extensions to core classes
@@ -116,6 +119,8 @@ test_files:
116
119
  - test/test_paralell_each.rb
117
120
  - test/test_product.rb
118
121
  - test/test_relative_path.rb
122
+ - test/test_rotate.rb
119
123
  - test/test_slice.rb
120
124
  - test/test_state_equivalent.rb
121
125
  - test/test_unindent.rb
126
+ - test/tstr.rb