backports 2.7.1 → 2.8.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.
@@ -1,5 +1,11 @@
1
1
  = Backports --- History
2
2
 
3
+ == Version 2.8.0 - February 3rd, 2013
4
+
5
+ * Added some features of 2.0.0 (must be required explicitly until official release):
6
+ * Array#bsearch
7
+ * Range#bsearch
8
+
3
9
  == Version 2.7.0 - January 14th, 2013
4
10
 
5
11
  * Added some features of 2.0.0 (must be required explicitly until official release):
@@ -195,9 +195,14 @@ To include all Ruby backports but not those of Rails, <tt>require "backports/1.9
195
195
 
196
196
  Some features of Ruby 2.0.0 have been backported:
197
197
 
198
+ * Array
199
+ * +bsearch+
198
200
  * Enumerable
199
201
  * +lazy+
200
202
  * Enumerator::Lazy
203
+ * all methods
204
+ * Range
205
+ * +bsearch+
201
206
 
202
207
  *Note*: Ruby 2.0.0 will not included by default until 2.0.0 is released (and specs are thus finalized). To play around with these today, <tt>require "backports/2.0"</tt>.
203
208
 
@@ -0,0 +1,30 @@
1
+ class Array
2
+ def bsearch
3
+ return to_enum __method__ unless block_given?
4
+ from = 0
5
+ to = size - 1
6
+ satisfied = nil
7
+ while from <= to do
8
+ midpoint = (from + to).div(2)
9
+ result = yield(cur = self[midpoint])
10
+ case result
11
+ when Numeric
12
+ return cur if result == 0
13
+ result = result < 0
14
+ when true
15
+ satisfied = cur
16
+ when nil, false
17
+ # nothing to do
18
+ else
19
+ raise TypeError, "wrong argument type #{result.class} (must be numeric, true, false or nil)"
20
+ end
21
+
22
+ if result
23
+ to = midpoint - 1
24
+ else
25
+ from = midpoint + 1
26
+ end
27
+ end
28
+ satisfied
29
+ end unless method_defined? :bsearch
30
+ end
@@ -0,0 +1,49 @@
1
+ class Range
2
+ def bsearch
3
+ return to_enum __method__ unless block_given?
4
+ from = self.begin
5
+ to = self.end
6
+ unless from.is_a?(Numeric) && to.is_a?(Numeric)
7
+ raise TypeError, "can't do binary search for #{from.class}"
8
+ end
9
+
10
+ midpoint = nil
11
+ if from.is_a?(Integer) && to.is_a?(Integer)
12
+ convert = ->{ midpoint }
13
+ else
14
+ map = ->(pk, unpk, nb) do
15
+ result, = [nb.abs].pack(pk).unpack(unpk)
16
+ nb < 0 ? -result : result
17
+ end.curry
18
+ i2f = map['q', 'D']
19
+ f2i = map['D', 'q']
20
+ from = f2i[from.to_f]
21
+ to = f2i[to.to_f]
22
+ convert = -> { i2f[midpoint] }
23
+ end
24
+ to -= 1 if exclude_end?
25
+ satisfied = nil
26
+ while from <= to do
27
+ midpoint = (from + to).div(2)
28
+ result = yield(cur = convert.call)
29
+ case result
30
+ when Numeric
31
+ return cur if result == 0
32
+ result = result < 0
33
+ when true
34
+ satisfied = cur
35
+ when nil, false
36
+ # nothing to do
37
+ else
38
+ raise TypeError, "wrong argument type #{result.class} (must be numeric, true, false or nil)"
39
+ end
40
+
41
+ if result
42
+ to = midpoint - 1
43
+ else
44
+ from = midpoint + 1
45
+ end
46
+ end
47
+ satisfied
48
+ end unless method_defined? :bsearch
49
+ end
@@ -1,3 +1,3 @@
1
1
  module Backports
2
- VERSION = "2.7.1"
2
+ VERSION = "2.8.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backports
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.7.1
4
+ version: 2.8.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-21 00:00:00.000000000 Z
12
+ date: 2013-02-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Essential backports that enable some of the really nice features of ruby
15
15
  1.8.7, ruby 1.9 and rails from ruby 1.8.6 and earlier.
@@ -102,8 +102,10 @@ files:
102
102
  - lib/backports/1.9.3/string.rb
103
103
  - lib/backports/1.9.rb
104
104
  - lib/backports/2.0.0.rb
105
+ - lib/backports/2.0.0/array.rb
105
106
  - lib/backports/2.0.0/enumerable.rb
106
107
  - lib/backports/2.0.0/enumerator/lazy.rb
108
+ - lib/backports/2.0.0/range.rb
107
109
  - lib/backports/2.0.rb
108
110
  - lib/backports/basic_object.rb
109
111
  - lib/backports/force/array_map.rb