class Charty::RangeIndex

Public Class Methods

new(values, name: nil) click to toggle source
Calls superclass method Charty::Index::new
# File lib/charty/index.rb, line 57
def initialize(values, name: nil)
  if values.is_a?(Range) && values.begin.is_a?(Integer) && values.end.is_a?(Integer)
    super
  else
    raise ArgumentError, "values must be an integer range"
  end
end

Public Instance Methods

[](i) click to toggle source
# File lib/charty/index.rb, line 69
def [](i)
  case i
  when 0 ... length
    values.begin + i
  else
    raise IndexError, "index out of range (#{i} for 0 ... #{length})"
  end
end
length() click to toggle source
# File lib/charty/index.rb, line 65
def length
  size
end
loc(key) click to toggle source
# File lib/charty/index.rb, line 78
def loc(key)
  case key
  when Integer
    if values.cover?(key)
      return key - values.begin
    end
  end
end
union(other) click to toggle source
Calls superclass method Charty::Index#union
# File lib/charty/index.rb, line 87
def union(other)
  case other
  when RangeIndex
    return union(other.values)
  when Range
    if disjoint_range?(values, other)
      return Index.new(values.to_a.union(other.to_a))
    end
    new_beg = [values.begin, other.begin].min
    new_end = [values.end,   other.end  ].max
    new_range = if values.end < new_end
                  if other.exclude_end?
                    new_beg ... new_end
                  else
                    new_beg .. new_end
                  end
                elsif other.end < new_end
                  if values.exclude_end?
                    new_beg ... new_end
                  else
                    new_beg .. new_end
                  end
                else
                  if values.exclude_end? && other.exclude_end?
                    new_beg ... new_end
                  else
                    new_beg .. new_end
                  end
                end
    RangeIndex.new(new_range)
  else
    super
  end
end

Private Instance Methods

disjoint_range?(r1, r2) click to toggle source
# File lib/charty/index.rb, line 122
        def disjoint_range?(r1, r2)
  r1.end < r2.begin || r2.end < r1.begin
end