Class: Semian::Simple::SlidingWindow

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/semian/simple_sliding_window.rb

Overview

:nodoc:

Direct Known Subclasses

ThreadSafe::SlidingWindow

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size:) ⇒ SlidingWindow

A sliding window is a structure that stores the most @max_size recent timestamps like this: if @max_size = 4, current time is 10, @window =[5,7,9,10]. Another push of (11) at 11 sec would make @window [7,9,10,11], shifting off 5.



15
16
17
18
# File 'lib/semian/simple_sliding_window.rb', line 15

def initialize(max_size:)
  @max_size = max_size
  @window = []
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



9
10
11
# File 'lib/semian/simple_sliding_window.rb', line 9

def max_size
  @max_size
end

Instance Method Details

#clearObject Also known as: destroy



31
32
33
34
# File 'lib/semian/simple_sliding_window.rb', line 31

def clear
  @window.clear
  self
end

#push(value) ⇒ Object Also known as: <<



24
25
26
27
28
# File 'lib/semian/simple_sliding_window.rb', line 24

def push(value)
  resize_to(@max_size - 1) # make room
  @window << value
  self
end

#reject!(&block) ⇒ Object



20
21
22
# File 'lib/semian/simple_sliding_window.rb', line 20

def reject!(&block)
  @window.reject!(&block)
end