Sha256: 8a5706c9e39ce42e9a5057db8a76ffba1aa65863229a780eba9dfee428a0c8a7

Contents?: true

Size: 1 KB

Versions: 2

Compression:

Stored size: 1 KB

Contents

module RubyPx
  class Dataset
    class Data

      CHUNK_SIZE = 5_000
      attr_accessor :current_chunk_index

      def initialize
        @current_chunk_index = 0
      end

      def at index
        chunk_index = index/CHUNK_SIZE
        index_inside_chunk = index%CHUNK_SIZE

        get_chunk(chunk_index)[index_inside_chunk]
      end

      def concat array
        current_chunk.concat(array)
        if current_chunk.size > CHUNK_SIZE
          excess = current_chunk.pop(current_chunk.size-CHUNK_SIZE)
          self.current_chunk_index += 1
          concat(excess)
        end
      end

      def indexes_count
        self.current_chunk_index+1
      end

      private


      def current_chunk
        current = instance_variable_get("@chunk_#{self.current_chunk_index}")
        return current if current

        instance_variable_set("@chunk_#{self.current_chunk_index}", [])
      end

      def get_chunk chunk_index
        instance_variable_get("@chunk_#{chunk_index}")
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ruby_px-0.8.0 lib/ruby_px/dataset/data.rb
ruby_px-0.7.0 lib/ruby_px/dataset/data.rb