Sha256: 47a8a49ab6780117d1f27ed1e76a0509d2886b92624607698360953128ff1d9e

Contents?: true

Size: 1.26 KB

Versions: 8

Compression:

Stored size: 1.26 KB

Contents

# A MinMaxStackQueue is a queue that allows for Min and Max
# to be found in constant time, unlike a queue or
# array that generally takes linear time.  It does this
# because it's based on a MinMaxStack, which has this
# ability.
module Zadt
  class MinMaxStackQueue
    def initialize
      @in = MinMaxStack.new
      @out = MinMaxStack.new
    end

    def self.help
      Zadt::ADT::show_minmaxstackqueue_help_message
    end

    def help
      MinMaxStackQueue.help
    end

    def show
      @out.show.reverse + @in.show
    end

    def enqueue(val)
      @in.push(val)
    end

    def dequeue
      if @out.empty?
        @in.length.times do
          @out.push(@in.pop)
        end
      end
      @out.pop
    end

    def peek
      if @out.empty?
        @in.length.times do
          @out.push(@in.pop)
        end
      end
      @out.peek
    end

    def min
      if @in.empty?
        @out.min
      elsif @out.empty?
        @in.min
      else
        [@in.min, @out.min].min
      end
    end

    def max
      if @in.empty?
        @out.max
      elsif @out.empty?
        @in.max
      else
        [@in.max, @out.max].max
      end
    end

    def length
      @in.length + @out.length
    end

    def empty?
      @in.empty? && @out.empty?
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
zadt-1.1.6 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb
zadt-1.1.5 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb
zadt-1.1.4 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb
zadt-1.1.3 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb
zadt-1.1.2 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb
zadt-1.1.1 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb
zadt-1.1.0 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb
zadt-0.1.9 lib/zadt/AbstractDataTypes/StackQueue/MinMaxStackQueue.rb