Sha256: c8fb178b1d1501ab3a775b500a2046961a61976cbb25995e3fee756d416af437

Contents?: true

Size: 828 Bytes

Versions: 5

Compression:

Stored size: 828 Bytes

Contents

module God
    
    class Timeline < Array
      def initialize(max_size)
        super()
        @max_size = max_size
      end
      
      # Push a value onto the Timeline
      # 
      # Implementation explanation:
      # A performance optimization appears here to speed up the push time.
      # In essence, the code does this:
      #
      #   def push(val)
      #     super(val)
      #     shift if size > @max_size
      #   end
      #
      # But that's super slow due to the shift, so we resort to reverse! and pop
      # which gives us a 2x speedup with 100 elements and a 6x speedup with 1000
      def push(val)
        if (size + 1) > @max_size
          reverse!
          pop
          reverse!
        end
        super(val)
      end
      
      def <<(val)
        push(val)
      end
    end
    
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
god-0.4.1 lib/god/timeline.rb
god-0.4.3 lib/god/timeline.rb
god-0.4.0 lib/god/timeline.rb
god-0.5.0 lib/god/timeline.rb
god-0.6.0 lib/god/timeline.rb