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

@ -0,0 +1,60 @@
using System;
namespace PriorityQueue
{
class Program
{
static void Main(string[] args)
{
PriorityQueue PQ = new PriorityQueue();
PQ.push(3, "Clear drains");
PQ.push(4, "Feed cat");
PQ.push(5, "Make tea");
PQ.push(1, "Solve RC tasks");
PQ.push(2, "Tax return");
while (!PQ.Empty)
{
var Val = PQ.pop();
Console.WriteLine(Val[0] + " : " + Val[1]);
}
Console.ReadKey();
}
}
class PriorityQueue
{
private System.Collections.SortedList PseudoQueue;
public bool Empty
{
get
{
return PseudoQueue.Count == 0;
}
}
public PriorityQueue()
{
PseudoQueue = new System.Collections.SortedList();
}
public void push(object Priority, object Value)
{
PseudoQueue.Add(Priority, Value);
}
public object[] pop()
{
object[] ReturnValue = { null, null };
if (PseudoQueue.Count > 0)
{
ReturnValue[0] = PseudoQueue.GetKey(0);
ReturnValue[1] = PseudoQueue.GetByIndex(0);
PseudoQueue.RemoveAt(0);
}
return ReturnValue;
}
}
}

View file

@ -0,0 +1,19 @@
user=> (use 'clojure.data.priority-map)
; priority-map can be used as a priority queue
user=> (def p (priority-map "Clear drains" 3, "Feed cat" 4, "Make tea" 5, "Solve RC tasks" 1))
#'user/p
user=> p
{"Solve RC tasks" 1, "Clear drains" 3, "Feed cat" 4, "Make tea" 5}
; You can use assoc or conj to add items
user=> (assoc p "Tax return" 2)
{"Solve RC tasks" 1, "Tax return" 2, "Clear drains" 3, "Feed cat" 4, "Make tea" 5}
; peek to get first item, pop to give you back the priority-map with the first item removed
user=> (peek p)
["Solve RC tasks" 1]
; Merge priority-maps together
user=> (into p [["Wax Car" 4]["Paint Fence" 1]["Sand Floor" 3]])
{"Solve RC tasks" 1, "Paint Fence" 1, "Clear drains" 3, "Sand Floor" 3, "Wax Car" 4, "Feed cat" 4, "Make tea" 5}

View file

@ -0,0 +1,50 @@
PriorityQueue = {
__index = {
put = function(self, p, v)
local q = self[p]
if not q then
q = {first = 1, last = 0}
self[p] = q
end
q.last = q.last + 1
q[q.last] = v
end,
pop = function(self)
for p, q in pairs(self) do
if q.first <= q.last then
local v = q[q.first]
q[q.first] = nil
q.first = q.first + 1
return p, v
else
self[p] = nil
end
end
end
},
__call = function(cls)
return setmetatable({}, cls)
end
}
setmetatable(PriorityQueue, PriorityQueue)
-- Usage:
pq = PriorityQueue()
tasks = {
{3, 'Clear drains'},
{4, 'Feed cat'},
{5, 'Make tea'},
{1, 'Solve RC tasks'},
{2, 'Tax return'}
}
for _, task in ipairs(tasks) do
print(string.format("Putting: %d - %s", unpack(task)))
pq:put(unpack(task))
end
for prio, task in pq.pop, pq do
print(string.format("Popped: %d - %s", prio, task))
end

View file

@ -0,0 +1,40 @@
-- Use socket.gettime() for benchmark measurements
-- since it has millisecond precision on most systems
local socket = require("socket")
n = 10000000 -- number of tasks added (10^7)
m = 1000 -- number different priorities
local pq = PriorityQueue()
print(string.format("Adding %d tasks with random priority 1-%d ...", n, m))
start = socket.gettime()
for i = 1, n do
pq:put(math.random(m), i)
end
print(string.format("Elapsed: %.3f ms.", (socket.gettime() - start) * 1000))
print("Retrieving all tasks in order...")
start = socket.gettime()
local pp = 0
local pv = 0
for i = 1, n do
local p, task = pq:pop()
-- check that tasks are popped in ascending priority
assert(p >= pp)
if pp == p then
-- check that tasks within one priority maintain the insertion order
assert(task > pt)
end
pp = p
pt = task
end
print(string.format("Elapsed: %.3f ms.", (socket.gettime() - start) * 1000))

View file

@ -14,12 +14,12 @@ CFComparisonResult PQCompare(const void *ptr1, const void *ptr2, void *unused) {
int priority;
NSString *name;
}
- (id)initWithPriority:(int)p andName:(NSString *)n;
- (instancetype)initWithPriority:(int)p andName:(NSString *)n;
- (NSComparisonResult)compare:(Task *)other;
@end
@implementation Task
- (id)initWithPriority:(int)p andName:(NSString *)n {
- (instancetype)initWithPriority:(int)p andName:(NSString *)n {
if ((self = [super init])) {
priority = p;
name = [n copy];

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}"