Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/bsearch20.rb
Instance Method Summary collapse
-
#bsearch_first(range = 0...self.length, &block) ⇒ Object
(also: #bsearch)
This method searches the FIRST occurrence which satisfies a condition given by a block in binary fashion and return the index of the first occurrence.
-
#bsearch_last(range = 0...self.length, &block) ⇒ Object
This method searches the LAST occurrence which satisfies a condition given by a block in binary fashion and return the index of the last occurrence.
-
#bsearch_lower_boundary(range = 0...self.length, &block) ⇒ Object
Return the lower boundary.
-
#bsearch_range(range = 0...self.length, &block) ⇒ Object
Return the search result as a Range object.
-
#bsearch_upper_boundary(range = 0...self.length, &block) ⇒ Object
Return the upper boundary.
Instance Method Details
#bsearch_first(range = 0...self.length, &block) ⇒ Object Also known as: bsearch
This method searches the FIRST occurrence which satisfies a condition given by a block in binary fashion and return the index of the first occurrence. Return nil if not found.
66 67 68 69 70 71 72 73 |
# File 'lib/bsearch20.rb', line 66 def bsearch_first(range = 0...self.length, &block) boundary = bsearch_lower_boundary(range, &block) if boundary >= self.length || yield(self[boundary]) != 0 return nil else return boundary end end |
#bsearch_last(range = 0...self.length, &block) ⇒ Object
This method searches the LAST occurrence which satisfies a condition given by a block in binary fashion and return the index of the last occurrence. Return nil if not found.
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/bsearch20.rb', line 99 def bsearch_last(range = 0...self.length, &block) # `- 1' for canceling `lower + 1' in bsearch_upper_boundary. boundary = bsearch_upper_boundary(range, &block) - 1 if (boundary <= -1 || yield(self[boundary]) != 0) return nil else return boundary end end |
#bsearch_lower_boundary(range = 0...self.length, &block) ⇒ Object
Return the lower boundary. (inside)
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/bsearch20.rb', line 47 def bsearch_lower_boundary(range = 0...self.length, &block) lower = range.first() -1 upper = if range.exclude_end? then range.last else range.last + 1 end while lower + 1 != upper mid = ((lower + upper) / 2).to_i # for working with mathn.rb (Rational) if yield(self[mid]) < 0 lower = mid else upper = mid end end return upper end |
#bsearch_range(range = 0...self.length, &block) ⇒ Object
Return the search result as a Range object.
113 114 115 116 117 |
# File 'lib/bsearch20.rb', line 113 def bsearch_range(range = 0...self.length, &block) lower = bsearch_lower_boundary(range, &block) upper = bsearch_upper_boundary(range, &block) return lower...upper end |
#bsearch_upper_boundary(range = 0...self.length, &block) ⇒ Object
Return the upper boundary. (outside)
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/bsearch20.rb', line 80 def bsearch_upper_boundary(range = 0...self.length, &block) lower = range.first() -1 upper = if range.exclude_end? then range.last else range.last + 1 end while lower + 1 != upper mid = ((lower + upper) / 2).to_i # for working with mathn.rb (Rational) if yield(self[mid]) <= 0 lower = mid else upper = mid end end return lower + 1 # outside of the matching range. end |