Sha256: c77660e8547632eec5c38c9ced7efae7bedfad4abcbbf0a6c7cf24f0ac75adfb

Contents?: true

Size: 841 Bytes

Versions: 47

Compression:

Stored size: 841 Bytes

Contents

# A Poor mans Priority Queue. (Very inefficent but minimal implemention).
class PoorPriorityQueue < Hash
  def push(object, priority)
    self[object] = priority
  end

  def min
    return nil if self.empty?
    min_k = self.keys.first
    min_p = self[min_k]
    self.each do | k, p |
      min_k, min_p = k, p if p < min_p
    end
    [min_k, min_p]
  end

  def min_key
    min[0] rescue nil
  end
  
  def min_priority
    min[1] rescue nil
  end

  def delete_min
    return nil if self.empty?
    min_k, min_p = *min
    self.delete(min_k)
    [min_k, min_p]
  end

  def delete_min_return_key
    delete_min[0] rescue nil
  end
  
  def delete_min_return_priority
    delete_min[1] rescue nil
  end

  def delete(object)
    return nil unless self.has_key?(object) 
    result = [object, self[object]]
    super
    result
  end
end

Version data entries

47 entries across 47 versions & 10 rubygems

Version Path
auser-poolparty-1.3.0 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.1 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.10 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.11 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.12 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.13 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.14 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.15 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.16 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.17 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.2 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.3 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.4 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.5 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.6 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.7 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
auser-poolparty-1.3.8 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
fairchild-poolparty-1.3.17 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
fairchild-poolparty-1.3.5 vendor/gems/gratr/lib/priority-queue/lib/priority_queue/poor_priority_queue.rb
gratr19-0.4.4.1 lib/priority-queue/lib/priority_queue/poor_priority_queue.rb