Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,7 +1,8 @@
class PriorityQueueNaive
def initialize
@q = Hash.new { |h, k| h[k] = []}
@priorities = []
def initialize(data=nil)
@q = Hash.new {|h, k| h[k] = []}
data.each {|priority, item| @q[priority] << item} if data
@priorities = @q.keys.sort
end
def push(priority, item)
@ -20,7 +21,7 @@ class PriorityQueueNaive
end
def peek
if not empty?
unless empty?
@q[@priorities[0]][0]
end
end
@ -29,6 +30,25 @@ class PriorityQueueNaive
@priorities.empty?
end
def each
@q.each do |priority, items|
items.each {|item| yield priority, item}
end
end
def dup
@q.each_with_object(self.class.new) do |(priority, items), obj|
items.each {|item| obj.push(priority, item)}
end
end
def merge(other)
raise TypeError unless self.class == other.class
pq = dup
other.each {|priority, item| pq.push(priority, item)}
pq # return a new object
end
def inspect
@q.inspect
end
@ -49,3 +69,14 @@ test.each {|pr, str| pq.push(pr, str) }
until pq.empty?
puts pq.pop
end
puts
test2 = test.shift(3)
p pq1 = PriorityQueueNaive.new(test)
p pq2 = PriorityQueueNaive.new(test2)
p pq3 = pq1.merge(pq2)
puts "peek : #{pq3.peek}"
until pq3.empty?
puts pq3.pop
end
puts "peek : #{pq3.peek}"