ruby_array_find_consecutive 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +8 -0
- data/benchmark +45 -0
- data/ext/ruby_array_find_consecutive/extconf.rb +2 -0
- data/ext/ruby_array_find_consecutive/ruby_array_find_consecutive.c +59 -0
- data/lib/.gitkeep +0 -0
- data/ruby_array_find_consecutive.gemspec +18 -0
- data/spec/ruby_array_consecutive_spec.rb +34 -0
- data/spec/spec_helper.rb +17 -0
- metadata +108 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 richardcalahan
|
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,52 @@
|
|
1
|
+
## Ruby C extension for Array#find_consecutive
|
2
|
+
|
3
|
+
### Description
|
4
|
+
|
5
|
+
`Array#find_consecutive` is a Ruby C extension. It returns an array of non-overlapping arrays of consecutive numbers for `self` for a given interval. If no interval argument is provided the default is 1. That's a mouthful, so perhaps an example will help.
|
6
|
+
|
7
|
+
### Usage
|
8
|
+
|
9
|
+
```
|
10
|
+
|
11
|
+
[1, 2, 3, 4, 5].find_consecutive
|
12
|
+
# => [[1, 2, 3, 4, 5]]
|
13
|
+
|
14
|
+
[1, 2, 3, 4, 5, 10, 4, 5, 6, 7, 8].find_consecutive
|
15
|
+
# => [[1, 2, 3, 4, 5], [4, 5, 6, 7, 8]]
|
16
|
+
|
17
|
+
[1, 2, 3, 4, 6, 8, 10, 12, 14].find_consecutive(2)
|
18
|
+
# => [[4, 6, 8, 10, 12, 14]]
|
19
|
+
|
20
|
+
[1, 2, 3.5, 5, 6.5, 8, 9, 10, 11.5, 13].find_consecutive(1.5)
|
21
|
+
# => [[2, 3.5, 5, 6.5, 8], [10, 11.5, 13]]
|
22
|
+
|
23
|
+
```
|
24
|
+
|
25
|
+
### And...it's fast!
|
26
|
+
|
27
|
+
The benchmark suite interates an array of length > 2,000,000 using the C extension and a pure Ruby implementation.
|
28
|
+
|
29
|
+
```
|
30
|
+
|
31
|
+
$ ./benchmark
|
32
|
+
user system total real
|
33
|
+
find_consecutive C (integer interval) 0.460000 0.000000 0.460000 ( 0.459470)
|
34
|
+
find_consecutive Ruby (integer interval) 1.030000 0.000000 1.030000 ( 1.030171)
|
35
|
+
find_consecutive C (float interval) 0.390000 0.000000 0.390000 ( 0.394497)
|
36
|
+
find_consecutive Ruby (float interval) 1.500000 0.010000 1.510000 ( 1.506115)
|
37
|
+
|
38
|
+
```
|
39
|
+
|
40
|
+
### Installation
|
41
|
+
|
42
|
+
Add this line to your application's Gemfile:
|
43
|
+
|
44
|
+
gem 'ruby_array_find_consecutive'
|
45
|
+
|
46
|
+
And then execute:
|
47
|
+
|
48
|
+
$ bundle
|
49
|
+
|
50
|
+
Or install it yourself as:
|
51
|
+
|
52
|
+
$ gem install ruby_array_find_consecutive
|
data/Rakefile
ADDED
data/benchmark
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
require 'ruby_array_consecutive'
|
5
|
+
|
6
|
+
# A pure ruby implementation of Array#consecutive
|
7
|
+
class Array
|
8
|
+
def r_consecutive step=1
|
9
|
+
r_ary, c_ary = [[], []]
|
10
|
+
self.each_with_index do |v, i|
|
11
|
+
n = self[i+1]
|
12
|
+
c_ary << v if c_ary.length == 0
|
13
|
+
if v + step == n
|
14
|
+
c_ary << n
|
15
|
+
else
|
16
|
+
r_ary << c_ary if c_ary.length > 1
|
17
|
+
c_ary = []
|
18
|
+
end
|
19
|
+
end
|
20
|
+
return r_ary
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
array = (1..1000000).map { rand(1..100) }
|
25
|
+
array += [5, 6, 7, 8, 9]
|
26
|
+
array += [1, 2.5, 4, 5.5, 7, 8.5]
|
27
|
+
array += (1..1000000).map { rand(1..100) }
|
28
|
+
|
29
|
+
Benchmark.bmbm do |x|
|
30
|
+
x.report 'consecutive C (integer interval)' do
|
31
|
+
array.consecutive
|
32
|
+
end
|
33
|
+
|
34
|
+
x.report 'consecutive Ruby (integer interval)' do
|
35
|
+
array.r_consecutive
|
36
|
+
end
|
37
|
+
|
38
|
+
x.report 'consecutive C (float interval)' do
|
39
|
+
array.consecutive(1.5)
|
40
|
+
end
|
41
|
+
|
42
|
+
x.report 'consecutive Ruby (float interval)' do
|
43
|
+
array.r_consecutive(1.5)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#include <stdio.h>
|
2
|
+
#include "ruby.h"
|
3
|
+
|
4
|
+
static VALUE
|
5
|
+
rb_arr_find_consecutive ( int argc, VALUE *argv, VALUE self )
|
6
|
+
{
|
7
|
+
if ( argc > 1 ) {
|
8
|
+
rb_raise(rb_eArgError, "wrong number of arguments (%i for 0..1)", argc);
|
9
|
+
}
|
10
|
+
|
11
|
+
double step;
|
12
|
+
|
13
|
+
if ( argc == 1 && TYPE(argv[0]) == T_FLOAT ) {
|
14
|
+
step = NUM2DBL(argv[0]);
|
15
|
+
} else if ( argc == 1 && TYPE(argv[0]) == T_FIXNUM ) {
|
16
|
+
step = NUM2INT(argv[0]);
|
17
|
+
} else {
|
18
|
+
step = 1;
|
19
|
+
}
|
20
|
+
|
21
|
+
VALUE r_ary = rb_ary_new();
|
22
|
+
VALUE c_ary = rb_ary_new();
|
23
|
+
|
24
|
+
long int max = RARRAY_LEN(self);
|
25
|
+
double c, n;
|
26
|
+
int i;
|
27
|
+
for ( i = 0; i < max; i++ ) {
|
28
|
+
VALUE ce = rb_ary_entry(self, i);
|
29
|
+
VALUE ne = rb_ary_entry(self, i + 1);
|
30
|
+
|
31
|
+
c = TYPE(ce) == T_FLOAT ? NUM2DBL(ce) : FIX2INT(ce);
|
32
|
+
|
33
|
+
if ( TYPE(ne) == T_FLOAT ) {
|
34
|
+
n = NUM2DBL(ne);
|
35
|
+
} else if ( TYPE(ne) == T_FIXNUM ) {
|
36
|
+
n = FIX2INT(ne);
|
37
|
+
}
|
38
|
+
|
39
|
+
if ( RARRAY_LEN(c_ary) == 0 ) {
|
40
|
+
VALUE c_ary_e = TYPE(ce) == T_FLOAT ? rb_float_new(c) : INT2FIX(c);
|
41
|
+
rb_ary_push(c_ary, c_ary_e);
|
42
|
+
}
|
43
|
+
|
44
|
+
if ( c + step == n ) {
|
45
|
+
VALUE c_ary_e = TYPE(ne) == T_FLOAT ? rb_float_new(n) : INT2FIX(n);
|
46
|
+
rb_ary_push(c_ary, c_ary_e);
|
47
|
+
} else {
|
48
|
+
if ( RARRAY_LEN(c_ary) > 1 ) rb_ary_push(r_ary, c_ary);
|
49
|
+
c_ary = rb_ary_new();
|
50
|
+
}
|
51
|
+
}
|
52
|
+
|
53
|
+
return r_ary;
|
54
|
+
}
|
55
|
+
|
56
|
+
void Init_ruby_array_find_consecutive ( void )
|
57
|
+
{
|
58
|
+
rb_define_method(rb_cArray, "find_consecutive", rb_arr_find_consecutive, -1);
|
59
|
+
}
|
data/lib/.gitkeep
ADDED
File without changes
|
@@ -0,0 +1,18 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = 'ruby_array_find_consecutive'
|
3
|
+
gem.version = '0.0.1'
|
4
|
+
gem.authors = ['Richard Calahan']
|
5
|
+
gem.email = ['richard@calahan.me']
|
6
|
+
gem.description = 'Provides a C level extension to Ruby, Array#find_consecutive'
|
7
|
+
gem.summary = 'Array#find_consecutive implementation'
|
8
|
+
gem.homepage = ''
|
9
|
+
gem.files = `git ls-files`.split($/)
|
10
|
+
gem.extensions = ['ext/ruby_array_find_consecutive/extconf.rb']
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.require_paths = ['lib']
|
14
|
+
gem.add_development_dependency 'rake'
|
15
|
+
gem.add_development_dependency 'rake-compiler'
|
16
|
+
gem.add_development_dependency 'rspec'
|
17
|
+
gem.test_files = Dir.glob "spec/**/*_spec.rb"
|
18
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
$LOAD_PATH << File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
require 'ruby_array_find_consecutive'
|
5
|
+
|
6
|
+
describe Array do
|
7
|
+
it 'finds 1 array of consecutive integers incrementing by 1' do
|
8
|
+
[1, 2, 3].find_consecutive.length.should == 1
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'finds 2 arrays of consecutive integers incrementing by 1' do
|
12
|
+
[1, 2, 3, 5, 4, 5, 6].find_consecutive.length.should == 2
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'finds 1 array of consecutive integers incrementing by 2' do
|
16
|
+
[1, 3, 5, 7, 9].find_consecutive(2).length.should == 1
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'finds 2 arrays of consecutive integers incrementing by 2' do
|
20
|
+
[1, 3, 5, 7, 9, 3, 5, 7, 9, 11].find_consecutive(2).length.should == 2
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'finds 1 array of consecutive integers incrementing by 1.5' do
|
24
|
+
[0, 1.5, 3, 4.5, 6].find_consecutive(1.5).length.should == 1
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'finds 2 arrays of consecutive integers incrementing by 1.5' do
|
28
|
+
[0, 1.5, 3, 4.5, 6, 3, 4.5].find_consecutive(1.5).length.should == 2
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'finds 0 arrays of consecutive integers incrementing 1' do
|
32
|
+
[0, 1.5, 3, 4.5, 6, 3, 4.5].find_consecutive(1).length.should == 0
|
33
|
+
end
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
# config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
# config.run_all_when_everything_filtered = true
|
10
|
+
# config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby_array_find_consecutive
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Richard Calahan
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-21 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
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
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake-compiler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: Provides a C level extension to Ruby, Array#find_consecutive
|
63
|
+
email:
|
64
|
+
- richard@calahan.me
|
65
|
+
executables: []
|
66
|
+
extensions:
|
67
|
+
- ext/ruby_array_find_consecutive/extconf.rb
|
68
|
+
extra_rdoc_files: []
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- .rspec
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- benchmark
|
77
|
+
- ext/ruby_array_find_consecutive/extconf.rb
|
78
|
+
- ext/ruby_array_find_consecutive/ruby_array_find_consecutive.c
|
79
|
+
- lib/.gitkeep
|
80
|
+
- ruby_array_find_consecutive.gemspec
|
81
|
+
- spec/ruby_array_consecutive_spec.rb
|
82
|
+
- spec/spec_helper.rb
|
83
|
+
homepage: ''
|
84
|
+
licenses: []
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
none: false
|
91
|
+
requirements:
|
92
|
+
- - ! '>='
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
requirements: []
|
102
|
+
rubyforge_project:
|
103
|
+
rubygems_version: 1.8.24
|
104
|
+
signing_key:
|
105
|
+
specification_version: 3
|
106
|
+
summary: Array#find_consecutive implementation
|
107
|
+
test_files:
|
108
|
+
- spec/ruby_array_consecutive_spec.rb
|