range-bisect 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # encoding: utf-8
2
+ source 'https://rubygems.org'
3
+
4
+ # Specify your gem's dependencies in range-bisect.gemspec
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Ryan Biesemeyer
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,84 @@
1
+ # Range::Bisect
2
+
3
+ Given a `Range` of `Integer`s, find by bisection the Integers who satisfy
4
+ a given block.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'range-bisect'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install range-bisect
19
+
20
+ ## Usage
21
+
22
+ Usage is best-served by an example.
23
+
24
+ Suppose a limited external service allows you to specify a range of integers,
25
+ and will tell you whether or not that range contains one or more things that
26
+ you are looking for.
27
+
28
+ ```ruby
29
+ # Here is a haystack implementation.
30
+ # We can only use its interface.
31
+ class Haystack
32
+ # @param range [Range<Integer>]
33
+ # @return [Boolean]
34
+ def has_needles?(range)
35
+ # ...
36
+ end
37
+ end
38
+
39
+ # suppose we have an instance of that, called haystack
40
+ haystack
41
+ # and we know that somewhere between 0 and 100,
42
+ # it includes one or more needles
43
+ haystack.has_needles?(0...100) #=> true
44
+
45
+ # but we can only know whether or not a range has needles,
46
+ # not how many there are or where they are.
47
+ haystack.has_needle?(0..0) #=> false
48
+ haystack.has_needle?(1..1) #=> false
49
+ haystack.has_needle?(2..2) #=> false
50
+ haystack.has_needle?(3..3) #=> false
51
+ # ...
52
+ haystack.has_needle?(99..99) #=> false
53
+
54
+ # Let's try the same thing with iteration
55
+ (0..100).to_a.select do |idx|
56
+ haystack.has_needle?(idx..idx)
57
+ end
58
+ # => [17, 47, 99]
59
+
60
+ # Note that we still had to query every single item.
61
+ # Imagine a haystack with a billion items; whether
62
+ # it has one or a billion needles, you must query it
63
+ # a billion times.
64
+
65
+ # Bisect improves the best-case scenario using a
66
+ # divide-and-conquer method. It splits the search range
67
+ # recursively, continuing to search into sub-ranges that
68
+ # return true for the given block.
69
+ search_range = 0...100
70
+ search_range.extend(Range::Bisect)
71
+ search_range.bisect do |sub_range|
72
+ haystack.has_needle?(sub_range)
73
+ end
74
+ # => [17, 47, 99]
75
+
76
+ ```
77
+
78
+ ## Contributing
79
+
80
+ 1. Fork it
81
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
82
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
83
+ 4. Push to the branch (`git push origin my-new-feature`)
84
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+ require 'bundler/gem_tasks'
3
+
4
+ require 'rspec/core'
5
+ require 'rspec/core/rake_task'
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = FileList['spec/**/*_spec.rb']
8
+ spec.verbose = true
9
+ end
@@ -0,0 +1,3 @@
1
+ # encoding: utf-8
2
+
3
+ require 'range/bisect'
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require 'range/bisect/version'
3
+
4
+ module Range::Bisect
5
+ # Extends Range::Bisect into a duplicate of integer_range,
6
+ # then calls #bisect.
7
+ #
8
+ # @param integer_range [Range<Integer>]
9
+ # @yieldparam [Range<Integer>]
10
+ # @yieldreturn [Boolean]
11
+ def self.bisect(integer_range, &block)
12
+ source = integer_range.dup
13
+ source.extend(self) unless source.kind_of?(Range::Bisect)
14
+ source.bisect(&block)
15
+ end
16
+
17
+ # By bisection, find all of the elements in this range
18
+ # that cause the block to return true. It is especially
19
+ # important to note that the given block must operate with
20
+ # the whole range, not simply its beginning or end.
21
+ #
22
+ # @yieldparam [Range<Integer>]
23
+ # @yieldreturn [Boolean]
24
+ def bisect(&block)
25
+ ensure_integer_range!
26
+
27
+ return [] unless yield(self)
28
+
29
+ if first(2).size == 1 # determine if single-element without #to_a
30
+ return [first]
31
+ else
32
+ bisection.map do |range|
33
+ Range::Bisect.bisect(range, &block)
34
+ end.flatten.sort
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ # Ensure that self is a [Range<Integer>]
41
+ # @raise [ArgumentError] if self is not [Range<Integer>].
42
+ def ensure_integer_range!
43
+ unless self.kind_of?(Range) &&
44
+ self.first.kind_of?(Integer) &&
45
+ self.last.kind_of?(Integer)
46
+ raise ArgumentError, "Not a Range<Integer>: #{self}"
47
+ end
48
+ end
49
+
50
+ # splits self in two
51
+ # @return [Array<Range<Integer>>]
52
+ def bisection
53
+ size = self.end - self.begin
54
+ midpoint = self.begin + (size / 2)
55
+
56
+ first_half = Range.new(self.begin, midpoint, false)
57
+ last_half = Range.new(midpoint.next, self.end, exclude_end?)
58
+
59
+ [first_half, last_half]
60
+ end
61
+ end
@@ -0,0 +1,4 @@
1
+ # encoding: utf-8
2
+ module Range::Bisect
3
+ VERSION = '0.0.1'
4
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'range/bisect/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'range-bisect'
8
+ spec.version = Range::Bisect::VERSION
9
+ spec.authors = ['Ryan Biesemeyer']
10
+ spec.email = ['ryan@yaauie.com']
11
+ spec.description = 'Bisect a range, finding elements that match the block'
12
+ spec.summary = 'Adds Range::Bisect, which can be used to ' +
13
+ 'find by bisection'
14
+ spec.homepage = 'https://github.com/yaauie/range-bisect'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split($RS)
18
+ spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(/^spec\//)
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ end
@@ -0,0 +1,16 @@
1
+ # encoding: utf-8
2
+
3
+ require 'range/bisect'
4
+
5
+ describe Range::Bisect do
6
+ let(:needles) { [1, 3, 9, 5, 11] }
7
+ let(:needle_finder) do
8
+ proc { |haystack| haystack.any? { |elem| needles.include?(elem) } }
9
+ end
10
+ let(:haystack) do
11
+ (1...12)
12
+ end
13
+ subject { Range::Bisect.bisect(haystack, &needle_finder) }
14
+
15
+ it { should == needles.sort }
16
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: range-bisect
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ryan Biesemeyer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-06-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
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: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
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
+ description: Bisect a range, finding elements that match the block
47
+ email:
48
+ - ryan@yaauie.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/range-bisect.rb
59
+ - lib/range/bisect.rb
60
+ - lib/range/bisect/version.rb
61
+ - range-bisect.gemspec
62
+ - spec/range/bisect_spec.rb
63
+ homepage: https://github.com/yaauie/range-bisect
64
+ licenses:
65
+ - MIT
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.24
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Adds Range::Bisect, which can be used to find by bisection
88
+ test_files:
89
+ - spec/range/bisect_spec.rb
90
+ has_rdoc: