Class: Semian::Simple::SlidingWindow
- Inherits:
-
Object
- Object
- Semian::Simple::SlidingWindow
- Extended by:
- Forwardable
- Defined in:
- lib/semian/simple_sliding_window.rb
Overview
:nodoc:
Instance Attribute Summary collapse
-
#max_size ⇒ Object
readonly
Returns the value of attribute max_size.
Instance Method Summary collapse
- #clear ⇒ Object
- #destroy ⇒ Object
-
#initialize(max_size:) ⇒ SlidingWindow
constructor
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].
- #push(value) ⇒ Object (also: #<<)
- #resize_to(size) ⇒ Object
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.
13 14 15 16 |
# File 'lib/semian/simple_sliding_window.rb', line 13 def initialize(max_size:) @max_size = max_size @window = [] end |
Instance Attribute Details
#max_size ⇒ Object (readonly)
Returns the value of attribute max_size.
7 8 9 |
# File 'lib/semian/simple_sliding_window.rb', line 7 def max_size @max_size end |
Instance Method Details
#clear ⇒ Object
33 34 35 36 |
# File 'lib/semian/simple_sliding_window.rb', line 33 def clear @window.clear self end |
#destroy ⇒ Object
38 39 40 |
# File 'lib/semian/simple_sliding_window.rb', line 38 def destroy clear end |
#push(value) ⇒ Object Also known as: <<
25 26 27 28 29 |
# File 'lib/semian/simple_sliding_window.rb', line 25 def push(value) @window.shift while @window.size >= @max_size @window << value self end |
#resize_to(size) ⇒ Object
18 19 20 21 22 23 |
# File 'lib/semian/simple_sliding_window.rb', line 18 def resize_to(size) raise ArgumentError.new('size must be larger than 0') if size < 1 @max_size = size @window.shift while @window.size > @max_size self end |