Class: Charty::RangeIndex
Instance Attribute Summary
Attributes inherited from Index
Instance Method Summary collapse
- #[](i) ⇒ Object
-
#initialize(values, name: nil) ⇒ RangeIndex
constructor
A new instance of RangeIndex.
- #length ⇒ Object
- #loc(key) ⇒ Object
- #union(other) ⇒ Object
Methods inherited from Index
Constructor Details
#initialize(values, name: nil) ⇒ RangeIndex
Returns a new instance of RangeIndex.
57 58 59 60 61 62 63 |
# 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 |
Instance Method Details
#[](i) ⇒ Object
69 70 71 72 73 74 75 76 |
# 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 ⇒ Object
65 66 67 |
# File 'lib/charty/index.rb', line 65 def length size end |
#loc(key) ⇒ Object
78 79 80 81 82 83 84 85 |
# 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) ⇒ Object
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# 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 |