Sha256: c26bb3526348fd6bd6fae3f212ce15a8e517cbc36c2a8ad3309e73d1691b0eab

Contents?: true

Size: 998 Bytes

Versions: 1

Compression:

Stored size: 998 Bytes

Contents

module Hamster

  class Stack

    def initialize(list = List.new)
      @list = list
    end

    # Returns <tt>true</tt> if the stack contains no items.
    def empty?
      @list.empty?
    end

    # Returns the number of items on the stack.
    def size
      @list.size
    end

    # Returns the item at the top of the stack.
    def top
      @list.car
    end

    # Returns a copy of <tt>self</tt> with the given item as the new top
    def push(item)
      self.class.new(@list.cons(item))
    end

    # Returns a copy of <tt>self</tt> without the top item.
    def pop
      copy = @list.cdr
      if !copy.equal?(@list)
        self.class.new(copy)
      else
        self
      end
    end

    # Returns <tt>true</tt> if . <tt>eql?</tt> is synonymous with <tt>==</tt>
    def eql?(other)
      other.is_a?(self.class) && @list.eql?(other.instance_eval{@list})
    end
    alias :== :eql?

    # Returns <tt>self</tt>
    def dup
      self
    end
    alias :clone :dup

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hamster-0.1.8 lib/hamster/stack.rb