Module: Parslet::Source::RangeSearch

Defined in:
lib/parslet/source/line_cache.rb

Overview

Mixin for arrays that implicitly give a number of ranges, where one range begins where the other one ends.

Example: 

  [10, 20, 30]
  # would describe [0, 10], (10, 20], (20, 30]

Instance Method Summary collapse

Instance Method Details

#find_mid(left, right) ⇒ Object



67
68
69
70
71
# File 'lib/parslet/source/line_cache.rb', line 67

def find_mid(left, right)
  # NOTE: Jonathan Hinkle reported that when mathn is required, just
  # dividing and relying on the integer truncation is not enough.
  left + ((right - left) / 2).floor
end

#lbound(bound) ⇒ Object

Scans the array for the first number that is > than bound. Returns the index of that number.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/parslet/source/line_cache.rb', line 76

def lbound(bound)
  return nil if empty?
  return nil unless last > bound

  left = 0
  right = size - 1 

  loop do
    mid = find_mid(left, right)

    if self[mid] > bound
      right = mid
    else
      # assert: self[mid] <= bound
      left = mid+1
    end

    if right <= left
      return right
    end
  end
end