# File lib/extlib/lazy_array.rb, line 171 171: def <<(entry) 172: if loaded? 173: lazy_load 174: @array << entry 175: else 176: @tail << entry 177: end 178: self 179: end
# File lib/extlib/lazy_array.rb, line 317 317: def ==(other) 318: if equal?(other) 319: return true 320: end 321: 322: unless other.respond_to?(:to_ary) 323: return false 324: end 325: 326: # if necessary, convert to something that can be compared 327: other = other.to_ary unless other.respond_to?(:[]) 328: 329: cmp?(other, :==) 330: end
# File lib/extlib/lazy_array.rb, line 102 102: def [](*args) 103: index, length = extract_slice_arguments(*args) 104: 105: if length == 1 && args.size == 1 && args.first.kind_of?(Integer) 106: return at(index) 107: end 108: 109: if index >= 0 && lazy_possible?(@head, index + length) 110: @head[*args] 111: elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length) 112: @tail[*args] 113: else 114: lazy_load 115: @array[*args] 116: end 117: end
# File lib/extlib/lazy_array.rb, line 134 134: def []=(*args) 135: index, length = extract_slice_arguments(*args[0..2]) 136: 137: if index >= 0 && lazy_possible?(@head, index + length) 138: @head.[]=(*args) 139: elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length) 140: @tail.[]=(*args) 141: else 142: lazy_load 143: @array.[]=(*args) 144: end 145: end
# File lib/extlib/lazy_array.rb, line 94 94: def any?(&block) 95: (lazy_possible?(@tail) && @tail.any?(&block)) || 96: (lazy_possible?(@head) && @head.any?(&block)) || begin 97: lazy_load 98: @array.any?(&block) 99: end 100: end
# File lib/extlib/lazy_array.rb, line 26 26: def at(index) 27: if index >= 0 && lazy_possible?(@head, index + 1) 28: @head.at(index) 29: elsif index < 0 && lazy_possible?(@tail, index.abs) 30: @tail.at(index) 31: else 32: lazy_load 33: @array.at(index) 34: end 35: end
# File lib/extlib/lazy_array.rb, line 270 270: def clear 271: mark_loaded 272: @array.clear 273: self 274: end
# File lib/extlib/lazy_array.rb, line 181 181: def concat(other) 182: if loaded? 183: lazy_load 184: @array.concat(other) 185: else 186: @tail.concat(other) 187: end 188: self 189: end
# File lib/extlib/lazy_array.rb, line 241 241: def delete_at(index) 242: if index >= 0 && lazy_possible?(@head, index + 1) 243: @head.delete_at(index) 244: elsif index < 0 && lazy_possible?(@tail, index.abs) 245: @tail.delete_at(index) 246: else 247: lazy_load 248: @array.delete_at(index) 249: end 250: end
# File lib/extlib/lazy_array.rb, line 252 252: def delete_if(&block) 253: if loaded? 254: lazy_load 255: @array.delete_if(&block) 256: else 257: @reapers << block 258: @head.delete_if(&block) 259: @tail.delete_if(&block) 260: end 261: self 262: end
# File lib/extlib/lazy_array.rb, line 86 86: def empty? 87: (@tail.nil? || @tail.empty?) && 88: (@head.nil? || @head.empty?) && begin 89: lazy_load 90: @array.empty? 91: end 92: end
# File lib/extlib/lazy_array.rb, line 332 332: def eql?(other) 333: if equal?(other) 334: return true 335: end 336: 337: unless other.class.equal?(self.class) 338: return false 339: end 340: 341: cmp?(other, :eql?) 342: end
# File lib/extlib/lazy_array.rb, line 37 37: def fetch(*args, &block) 38: index = args.first 39: 40: if index >= 0 && lazy_possible?(@head, index + 1) 41: @head.fetch(*args, &block) 42: elsif index < 0 && lazy_possible?(@tail, index.abs) 43: @tail.fetch(*args, &block) 44: else 45: lazy_load 46: @array.fetch(*args, &block) 47: end 48: end
# File lib/extlib/lazy_array.rb, line 8 8: def first(*args) 9: if lazy_possible?(@head, *args) 10: @head.first(*args) 11: else 12: lazy_load 13: @array.first(*args) 14: end 15: end
# File lib/extlib/lazy_array.rb, line 302 302: def freeze 303: if loaded? 304: @array.freeze 305: else 306: @head.freeze 307: @tail.freeze 308: end 309: @frozen = true 310: self 311: end
# File lib/extlib/lazy_array.rb, line 313 313: def frozen? 314: @frozen == true 315: end
# File lib/extlib/lazy_array.rb, line 78 78: def include?(entry) 79: (lazy_possible?(@tail) && @tail.include?(entry)) || 80: (lazy_possible?(@head) && @head.include?(entry)) || begin 81: lazy_load 82: @array.include?(entry) 83: end 84: end
# File lib/extlib/lazy_array.rb, line 71 71: def index(entry) 72: (lazy_possible?(@head) && @head.index(entry)) || begin 73: lazy_load 74: @array.index(entry) 75: end 76: end
# File lib/extlib/lazy_array.rb, line 211 211: def insert(index, *entries) 212: if index >= 0 && lazy_possible?(@head, index) 213: @head.insert(index, *entries) 214: elsif index < 0 && lazy_possible?(@tail, index.abs - 1) 215: @tail.insert(index, *entries) 216: else 217: lazy_load 218: @array.insert(index, *entries) 219: end 220: self 221: end
# File lib/extlib/lazy_array.rb, line 292 292: def kind_of?(klass) 293: super || @array.kind_of?(klass) 294: end
# File lib/extlib/lazy_array.rb, line 17 17: def last(*args) 18: if lazy_possible?(@tail, *args) 19: @tail.last(*args) 20: else 21: lazy_load 22: @array.last(*args) 23: end 24: end
# File lib/extlib/lazy_array.rb, line 344 344: def lazy_possible?(list, need_length = 1) 345: !loaded? && need_length <= list.size 346: end
# File lib/extlib/lazy_array.rb, line 283 283: def load_with(&block) 284: @load_with_proc = block 285: self 286: end
# File lib/extlib/lazy_array.rb, line 288 288: def loaded? 289: @loaded == true 290: end
# File lib/extlib/lazy_array.rb, line 223 223: def pop(*args) 224: if lazy_possible?(@tail, *args) 225: @tail.pop(*args) 226: else 227: lazy_load 228: @array.pop(*args) 229: end 230: end
# File lib/extlib/lazy_array.rb, line 191 191: def push(*entries) 192: if loaded? 193: lazy_load 194: @array.push(*entries) 195: else 196: @tail.push(*entries) 197: end 198: self 199: end
# File lib/extlib/lazy_array.rb, line 264 264: def replace(other) 265: mark_loaded 266: @array.replace(other) 267: self 268: end
# File lib/extlib/lazy_array.rb, line 298 298: def respond_to?(method, include_private = false) 299: super || @array.respond_to?(method) 300: end
# File lib/extlib/lazy_array.rb, line 149 149: def reverse 150: dup.reverse! 151: end
# File lib/extlib/lazy_array.rb, line 153 153: def reverse! 154: # reverse without kicking if possible 155: if loaded? 156: @array = @array.reverse 157: else 158: @head, @tail = @tail.reverse, @head.reverse 159: 160: proc = @load_with_proc 161: 162: @load_with_proc = lambda do |v| 163: proc.call(v) 164: v.instance_variable_get(:@array).reverse! 165: end 166: end 167: 168: self 169: end
# File lib/extlib/lazy_array.rb, line 232 232: def shift(*args) 233: if lazy_possible?(@head, *args) 234: @head.shift(*args) 235: else 236: lazy_load 237: @array.shift(*args) 238: end 239: end
# File lib/extlib/lazy_array.rb, line 121 121: def slice!(*args) 122: index, length = extract_slice_arguments(*args) 123: 124: if index >= 0 && lazy_possible?(@head, index + length) 125: @head.slice!(*args) 126: elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length) 127: @tail.slice!(*args) 128: else 129: lazy_load 130: @array.slice!(*args) 131: end 132: end
# File lib/extlib/lazy_array.rb, line 276 276: def to_a 277: lazy_load 278: @array.to_a 279: end
# File lib/extlib/lazy_array.rb, line 201 201: def unshift(*entries) 202: if loaded? 203: lazy_load 204: @array.unshift(*entries) 205: else 206: @head.unshift(*entries) 207: end 208: self 209: end
# File lib/extlib/lazy_array.rb, line 50 50: def values_at(*args) 51: accumulator = [] 52: 53: lazy_possible = args.all? do |arg| 54: index, length = extract_slice_arguments(arg) 55: 56: if index >= 0 && lazy_possible?(@head, index + length) 57: accumulator.concat(head.values_at(*arg)) 58: elsif index < 0 && lazy_possible?(@tail, index.abs) 59: accumulator.concat(tail.values_at(*arg)) 60: end 61: end 62: 63: if lazy_possible 64: accumulator 65: else 66: lazy_load 67: @array.values_at(*args) 68: end 69: end
# File lib/extlib/lazy_array.rb, line 432 432: def cmp?(other, operator) 433: unless loaded? 434: # compare the head against the beginning of other. start at index 435: # 0 and incrementally compare each entry. if other is a LazyArray 436: # this has a lesser likelyhood of triggering a lazy load 437: 0.upto(@head.size - 1) do |i| 438: return false unless @head[i].__send__(operator, other[i]) 439: end 440: 441: # compare the tail against the end of other. start at index 442: # -1 and decrementally compare each entry. if other is a LazyArray 443: # this has a lesser likelyhood of triggering a lazy load 444: 1.downto(@tail.size * 1) do |i| 445: return false unless @tail[i].__send__(operator, other[i]) 446: end 447: 448: lazy_load 449: end 450: 451: @array.send(operator, other.to_ary) 452: end
# File lib/extlib/lazy_array.rb, line 410 410: def each 411: lazy_load 412: if block_given? 413: @array.each { |entry| yield entry } 414: self 415: else 416: @array.each 417: end 418: end
Extract arguments for # an # and return index and length
@param [Integer, Array(Integer), Range] *args the index,
index and length, or range indicating first and last position
@return [Integer] the index @return [Integer,NilClass] the length, if any
@api private
# File lib/extlib/lazy_array.rb, line 391 391: def extract_slice_arguments(*args) 392: first_arg, second_arg = args 393: 394: if args.size == 2 && first_arg.kind_of?(Integer) && second_arg.kind_of?(Integer) 395: return first_arg, second_arg 396: elsif args.size == 1 397: if first_arg.kind_of?(Integer) 398: return first_arg, 1 399: elsif first_arg.kind_of?(Range) 400: index = first_arg.first 401: length = first_arg.last - index 402: length += 1 unless first_arg.exclude_end? 403: return index, length 404: end 405: end 406: 407: raise ArgumentError, "arguments may be 1 or 2 Integers, or 1 Range object, was: #{args.inspect}", caller(1) 408: end
# File lib/extlib/lazy_array.rb, line 360 360: def initialize_copy(original) 361: @head = @head.try_dup 362: @tail = @tail.try_dup 363: @array = @array.try_dup 364: end
# File lib/extlib/lazy_array.rb, line 366 366: def lazy_load 367: return if loaded? 368: mark_loaded 369: @load_with_proc[self] 370: @array.unshift(*@head) 371: @array.concat(@tail) 372: @head = @tail = nil 373: @reapers.each { |r| @array.delete_if(&r) } if @reapers 374: @array.freeze if frozen? 375: end
# File lib/extlib/lazy_array.rb, line 377 377: def mark_loaded 378: @loaded = true 379: end
delegate any not-explicitly-handled methods to @array, if possible. this is handy for handling methods mixed-into Array like group_by
# File lib/extlib/lazy_array.rb, line 422 422: def method_missing(method, *args, &block) 423: if @array.respond_to?(method) 424: lazy_load 425: results = @array.send(method, *args, &block) 426: results.equal?(@array) ? self : results 427: else 428: super 429: end 430: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.