Class: LazyArray

### Home Classes Methods

### In Files

- lib/extlib/lazy_array.rb

### Parent

Object

### Methods

- ::new
- #<<
- #==
- #[]
- #[]=
- #any?
- #at
- #clear
- #concat
- #delete_at
- #delete_if
- #empty?
- #eql?
- #fetch
- #first
- #freeze
- #frozen?
- #include?
- #index
- #insert
- #is_a?
- #kind_of?
- #last
- #lazy_possible?
- #load_with
- #loaded?
- #pop
- #push
- #replace
- #respond_to?
- #reverse
- #reverse!
- #shift
- #slice
- #slice!
- #splice
- #to_a
- #to_ary
- #unshift
- #values_at

### Included Modules

- Enumerable

### Files

- LICENSE
- README.rdoc

### Class/Module Index

Quicksearch

- Extlib
- Extlib::Assertions
- Extlib::ByteArray
- Extlib::Hook
- Extlib::Hook::ClassMethods
- Extlib::Inflection
- Extlib::LocalObjectSpace
- Extlib::Logger
- Extlib::Pooling
- Extlib::Pooling::InvalidResourceError
- Extlib::Pooling::Pool
- Extlib::SimpleSet
- Gem
- Gem::SourceIndex
- Gem::Specification
- LazyModule
- LazyModule::ClassMethods
- Array
- Class
- DateTime
- Dictionary
- FalseClass
- Hash
- LazyArray
- Mash
- Module
- NilClass
- Numeric
- Object
- ObjectSpace
- Pathname
- REXMLUtilityNode
- String
- Struct
- Symbol
- Time
- ToHashParser
- TrueClass
- VirtualFile

No matching classes.

# LazyArray

### Attributes

head[R]
tail[R]

### Public Class Methods

new() click to toggle source

    # File lib/extlib/lazy_array.rb, line 350
    def initialize
      @frozen         = false
      @loaded         = false
      @load_with_proc = lambda { |v| v }
      @head           = []
      @tail           = []
      @array          = []
      @reapers        = []
    end

### Public Instance Methods

<<(entry) click to toggle source

    # File lib/extlib/lazy_array.rb, line 171
    def <<(entry)
      if loaded?
        lazy_load
        @array << entry
      else
        @tail << entry
      end
      self
    end

==(other) click to toggle source

    # File lib/extlib/lazy_array.rb, line 317
    def ==(other)
      if equal?(other)
        return true
      end

      unless other.respond_to?(:to_ary)
        return false
      end

      # if necessary, convert to something that can be compared
      other = other.to_ary unless other.respond_to?(:[])

      cmp?(other, :==)
    end

[](*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 102
    def [](*args)
      index, length = extract_slice_arguments(*args)

      if length == 1 && args.size == 1 && args.first.kind_of?(Integer)
        return at(index)
      end

      if index >= 0 && lazy_possible?(@head, index + length)
        @head[*args]
      elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
        @tail[*args]
      else
        lazy_load
        @array[*args]
      end
    end

Also aliased as: slice
[]=(*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 134
    def []=(*args)
      index, length = extract_slice_arguments(*args[0..-2])

      if index >= 0 && lazy_possible?(@head, index + length)
        @head.[]=(*args)
      elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
        @tail.[]=(*args)
      else
        lazy_load
        @array.[]=(*args)
      end
    end

Also aliased as: splice
any?(&block) click to toggle source

    # File lib/extlib/lazy_array.rb, line 94
    def any?(&block)
      (lazy_possible?(@tail) && @tail.any?(&block)) ||
      (lazy_possible?(@head) && @head.any?(&block)) || begin
        lazy_load
        @array.any?(&block)
      end
    end

at(index) click to toggle source

    # File lib/extlib/lazy_array.rb, line 26
    def at(index)
      if index >= 0 && lazy_possible?(@head, index + 1)
        @head.at(index)
      elsif index < 0 && lazy_possible?(@tail, index.abs)
        @tail.at(index)
      else
        lazy_load
        @array.at(index)
      end
    end

clear() click to toggle source

    # File lib/extlib/lazy_array.rb, line 270
    def clear
      mark_loaded
      @array.clear
      self
    end

concat(other) click to toggle source

    # File lib/extlib/lazy_array.rb, line 181
    def concat(other)
      if loaded?
        lazy_load
        @array.concat(other)
      else
        @tail.concat(other)
      end
      self
    end

delete_at(index) click to toggle source

    # File lib/extlib/lazy_array.rb, line 241
    def delete_at(index)
      if index >= 0 && lazy_possible?(@head, index + 1)
        @head.delete_at(index)
      elsif index < 0 && lazy_possible?(@tail, index.abs)
        @tail.delete_at(index)
      else
        lazy_load
        @array.delete_at(index)
      end
    end

delete_if(&block) click to toggle source

    # File lib/extlib/lazy_array.rb, line 252
    def delete_if(&block)
      if loaded?
        lazy_load
        @array.delete_if(&block)
      else
        @reapers << block
        @head.delete_if(&block)
        @tail.delete_if(&block)
      end
      self
    end

empty?() click to toggle source

    # File lib/extlib/lazy_array.rb, line 86
    def empty?
      (@tail.nil? || @tail.empty?) &&
      (@head.nil? || @head.empty?) && begin
        lazy_load
        @array.empty?
      end
    end

eql?(other) click to toggle source

    # File lib/extlib/lazy_array.rb, line 332
    def eql?(other)
      if equal?(other)
        return true
      end

      unless other.class.equal?(self.class)
        return false
      end

      cmp?(other, :eql?)
    end

fetch(*args, &block) click to toggle source

    # File lib/extlib/lazy_array.rb, line 37
    def fetch(*args, &block)
      index = args.first

      if index >= 0 && lazy_possible?(@head, index + 1)
        @head.fetch(*args, &block)
      elsif index < 0 && lazy_possible?(@tail, index.abs)
        @tail.fetch(*args, &block)
      else
        lazy_load
        @array.fetch(*args, &block)
      end
    end

first(*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 8
    def first(*args)
      if lazy_possible?(@head, *args)
        @head.first(*args)
      else
        lazy_load
        @array.first(*args)
      end
    end

freeze() click to toggle source

    # File lib/extlib/lazy_array.rb, line 302
    def freeze
      if loaded?
        @array.freeze
      else
        @head.freeze
        @tail.freeze
      end
      @frozen = true
      self
    end

frozen?() click to toggle source

    # File lib/extlib/lazy_array.rb, line 313
    def frozen?
      @frozen == true
    end

include?(entry) click to toggle source

    # File lib/extlib/lazy_array.rb, line 78
    def include?(entry)
      (lazy_possible?(@tail) && @tail.include?(entry)) ||
      (lazy_possible?(@head) && @head.include?(entry)) || begin
        lazy_load
        @array.include?(entry)
      end
    end

index(entry) click to toggle source

    # File lib/extlib/lazy_array.rb, line 71
    def index(entry)
      (lazy_possible?(@head) && @head.index(entry)) || begin
        lazy_load
        @array.index(entry)
      end
    end

insert(index, *entries) click to toggle source

    # File lib/extlib/lazy_array.rb, line 211
    def insert(index, *entries)
      if index >= 0 && lazy_possible?(@head, index)
        @head.insert(index, *entries)
      elsif index < 0 && lazy_possible?(@tail, index.abs - 1)
        @tail.insert(index, *entries)
      else
        lazy_load
        @array.insert(index, *entries)
      end
      self
    end

is_a?(klass) click to toggle source
Alias for: kind_of?
kind_of?(klass) click to toggle source

    # File lib/extlib/lazy_array.rb, line 292
    def kind_of?(klass)
      super || @array.kind_of?(klass)
    end

Also aliased as: is_a?
last(*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 17
    def last(*args)
      if lazy_possible?(@tail, *args)
        @tail.last(*args)
      else
        lazy_load
        @array.last(*args)
      end
    end

lazy_possible?(list, need_length = 1) click to toggle source

    # File lib/extlib/lazy_array.rb, line 344
    def lazy_possible?(list, need_length = 1)
      !loaded? && need_length <= list.size
    end

load_with(&block) click to toggle source

    # File lib/extlib/lazy_array.rb, line 283
    def load_with(&block)
      @load_with_proc = block
      self
    end

loaded?() click to toggle source

    # File lib/extlib/lazy_array.rb, line 288
    def loaded?
      @loaded == true
    end

pop(*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 223
    def pop(*args)
      if lazy_possible?(@tail, *args)
        @tail.pop(*args)
      else
        lazy_load
        @array.pop(*args)
      end
    end

push(*entries) click to toggle source

    # File lib/extlib/lazy_array.rb, line 191
    def push(*entries)
      if loaded?
        lazy_load
        @array.push(*entries)
      else
        @tail.push(*entries)
      end
      self
    end

replace(other) click to toggle source

    # File lib/extlib/lazy_array.rb, line 264
    def replace(other)
      mark_loaded
      @array.replace(other)
      self
    end

respond_to?(method, include_private = false) click to toggle source

    # File lib/extlib/lazy_array.rb, line 298
    def respond_to?(method, include_private = false)
      super || @array.respond_to?(method)
    end

reverse() click to toggle source

    # File lib/extlib/lazy_array.rb, line 149
    def reverse
      dup.reverse!
    end

reverse!() click to toggle source

    # File lib/extlib/lazy_array.rb, line 153
    def reverse!
      # reverse without kicking if possible
      if loaded?
        @array = @array.reverse
      else
        @head, @tail = @tail.reverse, @head.reverse

        proc = @load_with_proc

        @load_with_proc = lambda do |v|
          proc.call(v)
          v.instance_variable_get(:@array).reverse!
        end
      end

      self
    end

shift(*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 232
    def shift(*args)
      if lazy_possible?(@head, *args)
        @head.shift(*args)
      else
        lazy_load
        @array.shift(*args)
      end
    end

slice(*args) click to toggle source
Alias for: []
slice!(*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 121
    def slice!(*args)
      index, length = extract_slice_arguments(*args)

      if index >= 0 && lazy_possible?(@head, index + length)
        @head.slice!(*args)
      elsif index < 0 && lazy_possible?(@tail, index.abs - 1 + length)
        @tail.slice!(*args)
      else
        lazy_load
        @array.slice!(*args)
      end
    end

splice(*args) click to toggle source
Alias for: []=
to_a() click to toggle source

    # File lib/extlib/lazy_array.rb, line 276
    def to_a
      lazy_load
      @array.to_a
    end

Also aliased as: to_ary
to_ary() click to toggle source
Alias for: to_a
unshift(*entries) click to toggle source

    # File lib/extlib/lazy_array.rb, line 201
    def unshift(*entries)
      if loaded?
        lazy_load
        @array.unshift(*entries)
      else
        @head.unshift(*entries)
      end
      self
    end

values_at(*args) click to toggle source

    # File lib/extlib/lazy_array.rb, line 50
    def values_at(*args)
      accumulator = []

      lazy_possible = args.all? do |arg|
        index, length = extract_slice_arguments(arg)

        if index >= 0 && lazy_possible?(@head, index + length)
          accumulator.concat(head.values_at(*arg))
        elsif index < 0 && lazy_possible?(@tail, index.abs)
          accumulator.concat(tail.values_at(*arg))
        end
      end

      if lazy_possible
        accumulator
      else
        lazy_load
        @array.values_at(*args)
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.