Sha256: 4099e13e42af624a6f493072fe0a222a9967a3d25de7be35142e479e0608b172

Contents?: true

Size: 873 Bytes

Versions: 16

Compression:

Stored size: 873 Bytes

Contents


# Keeps a list of items sorted. Elements must be comparable
class SortedList
  attr_reader :list
  def initialize(initial_list=nil)
    @list = LinkedList.new
    unless initial_list.nil?
      initial_list.each do |item|
        add item
      end
    end
  end
  
  def add(item)
    added = false
    @list.each_element do |node|
      if node.obj > item
        added = true
        @list.place item, :before, node
        break
      end
    end
    @list << item unless added
    item
  end
  alias :<< :add
  
  def shift
    @list.shift
  end
  
  def empty?
    @list.empty?
  end
  
  def contains?(item)
    @list.each do |obj|
      if obj == item
        return true
      end
    end
    false
  end
  
  def find(item)
    @list.each do |obj|
      if obj == item
        return obj
      end
    end
    nil
  end
  
  def size
    @list.size
  end
  
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
gamebox-0.5.5 lib/gamebox/lib/sorted_list.rb
gamebox-0.5.4 lib/gamebox/lib/sorted_list.rb
gamebox-0.5.2 lib/gamebox/lib/sorted_list.rb
gamebox-0.5.1 lib/gamebox/lib/sorted_list.rb
gamebox-0.5.0 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.1 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.0 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.0.rc11 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.0.rc5 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.0.rc4 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.0.rc3 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.0.rc2 lib/gamebox/lib/sorted_list.rb
gamebox-0.4.0.rc1 lib/gamebox/lib/sorted_list.rb
gamebox-0.3.4 lib/gamebox/lib/sorted_list.rb
gamebox-0.3.3 lib/gamebox/lib/sorted_list.rb
gamebox-0.3.2 lib/gamebox/lib/sorted_list.rb