Class: Laziest::MinMax

Inherits:
Promise
  • Object
show all
Defined in:
lib/laziest/minmax.rb

Overview

Base class. Conceptually a max, but we can override it with min/max classes… but only for naturally-ordered elements! If we allow arbitrary comparators, we have no way of optimizing attempted comparisons against the max value itself.

Direct Known Subclasses

Max, Min

Defined Under Namespace

Classes: Max, Min

Instance Method Summary collapse

Constructor Details

#initialize(enumerator, gtop) ⇒ MinMax

Returns a new instance of MinMax.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/laziest/minmax.rb', line 7

def initialize enumerator, gtop
  @enumerator = enumerator
  # "Greater-Than" operation
  @gtop = gtop
  begin
    @max = @enumerator.next
  rescue ::StopIteration
    @is_nil = true
  end

  super() do
    ::Kernel.loop do
      value = @enumerator.next
      if value.public_send @gtop, @max
        @max = value
      end
    end
    @max
  end
end

Instance Method Details

#__force_until__Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/laziest/minmax.rb', line 28

def __force_until__
  if @result.equal?(NOT_SET) && @error.equal?(NOT_SET)
    @mutex.synchronize do
      if @result.equal?(NOT_SET) && @error.equal?(NOT_SET) 
        unless yield
          ::Kernel.loop do
            # StopIteration can stop the loop here
            value = @enumerator.next
            if value.public_send @gtop, @max
              @max = value
              break if yield
            end
          end
        end
      end
    end
  end
  ::Kernel.raise(@error) unless @error.equal?(NOT_SET)
end