Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -9,123 +9,123 @@ typedef struct { q_elem_t *buf; int n, alloc; } pri_queue_t, *pri_queue;
|
|||
/* first element in array not used to simplify indices */
|
||||
pri_queue priq_new(int size)
|
||||
{
|
||||
if (size < 4) size = 4;
|
||||
if (size < 4) size = 4;
|
||||
|
||||
pri_queue q = malloc(sizeof(pri_queue_t));
|
||||
q->buf = malloc(sizeof(q_elem_t) * size);
|
||||
q->alloc = size;
|
||||
q->n = 1;
|
||||
pri_queue q = malloc(sizeof(pri_queue_t));
|
||||
q->buf = malloc(sizeof(q_elem_t) * size);
|
||||
q->alloc = size;
|
||||
q->n = 1;
|
||||
|
||||
return q;
|
||||
return q;
|
||||
}
|
||||
|
||||
void priq_push(pri_queue q, void *data, int pri)
|
||||
{
|
||||
q_elem_t *b;
|
||||
int n, m;
|
||||
q_elem_t *b;
|
||||
int n, m;
|
||||
|
||||
if (q->n >= q->alloc) {
|
||||
q->alloc *= 2;
|
||||
b = q->buf = realloc(q->buf, sizeof(q_elem_t) * q->alloc);
|
||||
} else
|
||||
b = q->buf;
|
||||
if (q->n >= q->alloc) {
|
||||
q->alloc *= 2;
|
||||
b = q->buf = realloc(q->buf, sizeof(q_elem_t) * q->alloc);
|
||||
} else
|
||||
b = q->buf;
|
||||
|
||||
n = q->n++;
|
||||
/* append at end, then up heap */
|
||||
while ((m = n / 2) && pri < b[m].pri) {
|
||||
b[n] = b[m];
|
||||
n = m;
|
||||
}
|
||||
b[n].data = data;
|
||||
b[n].pri = pri;
|
||||
n = q->n++;
|
||||
/* append at end, then up heap */
|
||||
while ((m = n / 2) && pri < b[m].pri) {
|
||||
b[n] = b[m];
|
||||
n = m;
|
||||
}
|
||||
b[n].data = data;
|
||||
b[n].pri = pri;
|
||||
}
|
||||
|
||||
/* remove top item. returns 0 if empty. *pri can be null. */
|
||||
void * priq_pop(pri_queue q, int *pri)
|
||||
{
|
||||
void *out;
|
||||
if (q->n == 1) return 0;
|
||||
void *out;
|
||||
if (q->n == 1) return 0;
|
||||
|
||||
q_elem_t *b = q->buf;
|
||||
q_elem_t *b = q->buf;
|
||||
|
||||
out = b[1].data;
|
||||
if (pri) *pri = b[1].pri;
|
||||
out = b[1].data;
|
||||
if (pri) *pri = b[1].pri;
|
||||
|
||||
/* pull last item to top, then down heap. */
|
||||
--q->n;
|
||||
/* pull last item to top, then down heap. */
|
||||
--q->n;
|
||||
|
||||
int n = 1, m;
|
||||
while ((m = n * 2) < q->n) {
|
||||
if (m + 1 < q->n && b[m].pri > b[m + 1].pri) m++;
|
||||
int n = 1, m;
|
||||
while ((m = n * 2) < q->n) {
|
||||
if (m + 1 < q->n && b[m].pri > b[m + 1].pri) m++;
|
||||
|
||||
if (b[q->n].pri <= b[m].pri) break;
|
||||
b[n] = b[m];
|
||||
n = m;
|
||||
}
|
||||
if (b[q->n].pri <= b[m].pri) break;
|
||||
b[n] = b[m];
|
||||
n = m;
|
||||
}
|
||||
|
||||
b[n] = b[q->n];
|
||||
if (q->n < q->alloc / 2 && q->n >= 16)
|
||||
q->buf = realloc(q->buf, (q->alloc /= 2) * sizeof(b[0]));
|
||||
b[n] = b[q->n];
|
||||
if (q->n < q->alloc / 2 && q->n >= 16)
|
||||
q->buf = realloc(q->buf, (q->alloc /= 2) * sizeof(b[0]));
|
||||
|
||||
return out;
|
||||
return out;
|
||||
}
|
||||
|
||||
/* get the top element without removing it from queue */
|
||||
void* priq_top(pri_queue q, int *pri)
|
||||
{
|
||||
if (q->n == 1) return 0;
|
||||
if (pri) *pri = q->buf[1].pri;
|
||||
return q->buf[1].data;
|
||||
if (q->n == 1) return 0;
|
||||
if (pri) *pri = q->buf[1].pri;
|
||||
return q->buf[1].data;
|
||||
}
|
||||
|
||||
/* this is O(n log n), but probably not the best */
|
||||
void priq_combine(pri_queue q, pri_queue q2)
|
||||
{
|
||||
int i;
|
||||
q_elem_t *e = q2->buf + 1;
|
||||
int i;
|
||||
q_elem_t *e = q2->buf + 1;
|
||||
|
||||
for (i = q2->n - 1; i >= 1; i--, e++)
|
||||
priq_push(q, e->data, e->pri);
|
||||
priq_purge(q2);
|
||||
for (i = q2->n - 1; i >= 1; i--, e++)
|
||||
priq_push(q, e->data, e->pri);
|
||||
priq_purge(q2);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, p;
|
||||
const char *c, *tasks[] ={
|
||||
"Clear drains", "Feed cat", "Make tea", "Solve RC tasks", "Tax return" };
|
||||
int pri[] = { 3, 4, 5, 1, 2 };
|
||||
int i, p;
|
||||
const char *c, *tasks[] ={
|
||||
"Clear drains", "Feed cat", "Make tea", "Solve RC tasks", "Tax return" };
|
||||
int pri[] = { 3, 4, 5, 1, 2 };
|
||||
|
||||
/* make two queues */
|
||||
pri_queue q = priq_new(0), q2 = priq_new(0);
|
||||
/* make two queues */
|
||||
pri_queue q = priq_new(0), q2 = priq_new(0);
|
||||
|
||||
/* push all 5 tasks into q */
|
||||
for (i = 0; i < 5; i++)
|
||||
priq_push(q, tasks[i], pri[i]);
|
||||
/* push all 5 tasks into q */
|
||||
for (i = 0; i < 5; i++)
|
||||
priq_push(q, tasks[i], pri[i]);
|
||||
|
||||
/* pop them and print one by one */
|
||||
while ((c = priq_pop(q, &p)))
|
||||
printf("%d: %s\n", p, c);
|
||||
/* pop them and print one by one */
|
||||
while ((c = priq_pop(q, &p)))
|
||||
printf("%d: %s\n", p, c);
|
||||
|
||||
/* put a million random tasks in each queue */
|
||||
for (i = 0; i < 1 << 20; i++) {
|
||||
p = rand() / ( RAND_MAX / 5 );
|
||||
priq_push(q, tasks[p], pri[p]);
|
||||
/* put a million random tasks in each queue */
|
||||
for (i = 0; i < 1 << 20; i++) {
|
||||
p = rand() / ( RAND_MAX / 5 );
|
||||
priq_push(q, tasks[p], pri[p]);
|
||||
|
||||
p = rand() / ( RAND_MAX / 5 );
|
||||
priq_push(q2, tasks[p], pri[p]);
|
||||
}
|
||||
p = rand() / ( RAND_MAX / 5 );
|
||||
priq_push(q2, tasks[p], pri[p]);
|
||||
}
|
||||
|
||||
printf("\nq has %d items, q2 has %d items\n", priq_size(q), priq_size(q2));
|
||||
printf("\nq has %d items, q2 has %d items\n", priq_size(q), priq_size(q2));
|
||||
|
||||
/* merge q2 into q; q2 is empty */
|
||||
priq_combine(q, q2);
|
||||
printf("After merge, q has %d items, q2 has %d items\n",
|
||||
priq_size(q), priq_size(q2));
|
||||
/* merge q2 into q; q2 is empty */
|
||||
priq_combine(q, q2);
|
||||
printf("After merge, q has %d items, q2 has %d items\n",
|
||||
priq_size(q), priq_size(q2));
|
||||
|
||||
/* pop q until it's empty */
|
||||
for (i = 0; (c = priq_pop(q, 0)); i++);
|
||||
printf("Popped %d items out of q\n", i);
|
||||
/* pop q until it's empty */
|
||||
for (i = 0; (c = priq_pop(q, 0)); i++);
|
||||
printf("Popped %d items out of q\n", i);
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,87 +2,87 @@ MODULE PQueues;
|
|||
IMPORT StdLog,Boxes;
|
||||
|
||||
TYPE
|
||||
Rank* = POINTER TO RECORD
|
||||
p-: LONGINT; (* Priority *)
|
||||
value-: Boxes.Object
|
||||
END;
|
||||
|
||||
PQueue* = POINTER TO RECORD
|
||||
a: POINTER TO ARRAY OF Rank;
|
||||
size-: LONGINT;
|
||||
END;
|
||||
|
||||
PROCEDURE NewRank*(p: LONGINT; v: Boxes.Object): Rank;
|
||||
VAR
|
||||
r: Rank;
|
||||
BEGIN
|
||||
NEW(r);r.p := p;r.value := v;
|
||||
RETURN r
|
||||
END NewRank;
|
||||
|
||||
PROCEDURE NewPQueue*(cap: LONGINT): PQueue;
|
||||
VAR
|
||||
pq: PQueue;
|
||||
BEGIN
|
||||
NEW(pq);pq.size := 0;
|
||||
NEW(pq.a,cap);pq.a[0] := NewRank(MIN(INTEGER),NIL);
|
||||
RETURN pq
|
||||
END NewPQueue;
|
||||
|
||||
PROCEDURE (pq: PQueue) Push*(r:Rank), NEW;
|
||||
VAR
|
||||
i: LONGINT;
|
||||
BEGIN
|
||||
INC(pq.size);
|
||||
i := pq.size;
|
||||
WHILE r.p < pq.a[i DIV 2].p DO
|
||||
pq.a[i] := pq.a[i DIV 2];i := i DIV 2
|
||||
END;
|
||||
pq.a[i] := r
|
||||
END Push;
|
||||
|
||||
PROCEDURE (pq: PQueue) Pop*(): Rank,NEW;
|
||||
VAR
|
||||
r,y: Rank;
|
||||
i,j: LONGINT;
|
||||
ok: BOOLEAN;
|
||||
BEGIN
|
||||
r := pq.a[1]; (* Priority object *)
|
||||
y := pq.a[pq.size]; DEC(pq.size); i := 1; ok := FALSE;
|
||||
WHILE (i <= pq.size DIV 2) & ~ok DO
|
||||
j := i + 1;
|
||||
IF (j < pq.size) & (pq.a[i].p > pq.a[j + 1].p) THEN INC(j) END;
|
||||
IF y.p > pq.a[j].p THEN pq.a[i] := pq.a[j]; i := j ELSE ok := TRUE END
|
||||
END;
|
||||
pq.a[i] := y;
|
||||
RETURN r
|
||||
END Pop;
|
||||
|
||||
PROCEDURE (pq: PQueue) IsEmpty*(): BOOLEAN,NEW;
|
||||
BEGIN
|
||||
RETURN pq.size = 0
|
||||
END IsEmpty;
|
||||
|
||||
PROCEDURE Test*;
|
||||
VAR
|
||||
pq: PQueue;
|
||||
r: Rank;
|
||||
PROCEDURE ShowRank(r:Rank);
|
||||
BEGIN
|
||||
StdLog.Int(r.p);StdLog.String(":> ");StdLog.String(r.value.AsString());StdLog.Ln;
|
||||
END ShowRank;
|
||||
BEGIN
|
||||
pq := NewPQueue(128);
|
||||
pq.Push(NewRank(3,Boxes.NewString("Clear drains")));
|
||||
pq.Push(NewRank(4,Boxes.NewString("Feed cat")));
|
||||
pq.Push(NewRank(5,Boxes.NewString("Make tea")));
|
||||
pq.Push(NewRank(1,Boxes.NewString("Solve RC tasks")));
|
||||
pq.Push(NewRank(2,Boxes.NewString("Tax return")));
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
END Test;
|
||||
|
||||
Rank* = POINTER TO RECORD
|
||||
p-: LONGINT; (* Priority *)
|
||||
value-: Boxes.Object
|
||||
END;
|
||||
|
||||
PQueue* = POINTER TO RECORD
|
||||
a: POINTER TO ARRAY OF Rank;
|
||||
size-: LONGINT;
|
||||
END;
|
||||
|
||||
PROCEDURE NewRank*(p: LONGINT; v: Boxes.Object): Rank;
|
||||
VAR
|
||||
r: Rank;
|
||||
BEGIN
|
||||
NEW(r);r.p := p;r.value := v;
|
||||
RETURN r
|
||||
END NewRank;
|
||||
|
||||
PROCEDURE NewPQueue*(cap: LONGINT): PQueue;
|
||||
VAR
|
||||
pq: PQueue;
|
||||
BEGIN
|
||||
NEW(pq);pq.size := 0;
|
||||
NEW(pq.a,cap);pq.a[0] := NewRank(MIN(INTEGER),NIL);
|
||||
RETURN pq
|
||||
END NewPQueue;
|
||||
|
||||
PROCEDURE (pq: PQueue) Push*(r:Rank), NEW;
|
||||
VAR
|
||||
i: LONGINT;
|
||||
BEGIN
|
||||
INC(pq.size);
|
||||
i := pq.size;
|
||||
WHILE r.p < pq.a[i DIV 2].p DO
|
||||
pq.a[i] := pq.a[i DIV 2];i := i DIV 2
|
||||
END;
|
||||
pq.a[i] := r
|
||||
END Push;
|
||||
|
||||
PROCEDURE (pq: PQueue) Pop*(): Rank,NEW;
|
||||
VAR
|
||||
r,y: Rank;
|
||||
i,j: LONGINT;
|
||||
ok: BOOLEAN;
|
||||
BEGIN
|
||||
r := pq.a[1]; (* Priority object *)
|
||||
y := pq.a[pq.size]; DEC(pq.size); i := 1; ok := FALSE;
|
||||
WHILE (i <= pq.size DIV 2) & ~ok DO
|
||||
j := i + 1;
|
||||
IF (j < pq.size) & (pq.a[i].p > pq.a[j + 1].p) THEN INC(j) END;
|
||||
IF y.p > pq.a[j].p THEN pq.a[i] := pq.a[j]; i := j ELSE ok := TRUE END
|
||||
END;
|
||||
pq.a[i] := y;
|
||||
RETURN r
|
||||
END Pop;
|
||||
|
||||
PROCEDURE (pq: PQueue) IsEmpty*(): BOOLEAN,NEW;
|
||||
BEGIN
|
||||
RETURN pq.size = 0
|
||||
END IsEmpty;
|
||||
|
||||
PROCEDURE Test*;
|
||||
VAR
|
||||
pq: PQueue;
|
||||
r: Rank;
|
||||
PROCEDURE ShowRank(r:Rank);
|
||||
BEGIN
|
||||
StdLog.Int(r.p);StdLog.String(":> ");StdLog.String(r.value.AsString());StdLog.Ln;
|
||||
END ShowRank;
|
||||
BEGIN
|
||||
pq := NewPQueue(128);
|
||||
pq.Push(NewRank(3,Boxes.NewString("Clear drains")));
|
||||
pq.Push(NewRank(4,Boxes.NewString("Feed cat")));
|
||||
pq.Push(NewRank(5,Boxes.NewString("Make tea")));
|
||||
pq.Push(NewRank(1,Boxes.NewString("Solve RC tasks")));
|
||||
pq.Push(NewRank(2,Boxes.NewString("Tax return")));
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
ShowRank(pq.Pop());
|
||||
END Test;
|
||||
|
||||
END PQueues.
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
DEFINITION PQueues;
|
||||
|
||||
IMPORT Boxes;
|
||||
IMPORT Boxes;
|
||||
|
||||
TYPE
|
||||
PQueue = POINTER TO RECORD
|
||||
size-: LONGINT;
|
||||
(pq: PQueue) IsEmpty (): BOOLEAN, NEW;
|
||||
(pq: PQueue) Pop (): Rank, NEW;
|
||||
(pq: PQueue) Push (r: Rank), NEW
|
||||
END;
|
||||
TYPE
|
||||
PQueue = POINTER TO RECORD
|
||||
size-: LONGINT;
|
||||
(pq: PQueue) IsEmpty (): BOOLEAN, NEW;
|
||||
(pq: PQueue) Pop (): Rank, NEW;
|
||||
(pq: PQueue) Push (r: Rank), NEW
|
||||
END;
|
||||
|
||||
Rank = POINTER TO RECORD
|
||||
p-: LONGINT;
|
||||
value-: Boxes.Object
|
||||
END;
|
||||
Rank = POINTER TO RECORD
|
||||
p-: LONGINT;
|
||||
value-: Boxes.Object
|
||||
END;
|
||||
|
||||
PROCEDURE NewPQueue (cap: LONGINT): PQueue;
|
||||
PROCEDURE NewRank (p: LONGINT; v: Boxes.Object): Rank;
|
||||
PROCEDURE Test;
|
||||
PROCEDURE NewPQueue (cap: LONGINT): PQueue;
|
||||
PROCEDURE NewRank (p: LONGINT; v: Boxes.Object): Rank;
|
||||
PROCEDURE Test;
|
||||
|
||||
END PQueues.
|
||||
|
|
|
|||
|
|
@ -7,22 +7,22 @@ create() -> gb_trees:empty().
|
|||
insert( Element, Priority, Queue ) -> gb_trees:enter( Priority, Element, Queue ).
|
||||
|
||||
peek( Queue ) ->
|
||||
{_Priority, Element, _New_queue} = gb_trees:take_smallest( Queue ),
|
||||
Element.
|
||||
{_Priority, Element, _New_queue} = gb_trees:take_smallest( Queue ),
|
||||
Element.
|
||||
|
||||
task() ->
|
||||
Items = [{3, "Clear drains"}, {4, "Feed cat"}, {5, "Make tea"}, {1, "Solve RC tasks"}, {2, "Tax return"}],
|
||||
Queue = lists:foldl( fun({Priority, Element}, Acc) -> insert( Element, Priority, Acc ) end, create(), Items ),
|
||||
io:fwrite( "peek priority: ~p~n", [peek( Queue )] ),
|
||||
lists:foldl( fun(_N, Q) -> write_top( Q ) end, Queue, lists:seq(1, erlang:length(Items)) ).
|
||||
Items = [{3, "Clear drains"}, {4, "Feed cat"}, {5, "Make tea"}, {1, "Solve RC tasks"}, {2, "Tax return"}],
|
||||
Queue = lists:foldl( fun({Priority, Element}, Acc) -> insert( Element, Priority, Acc ) end, create(), Items ),
|
||||
io:fwrite( "peek priority: ~p~n", [peek( Queue )] ),
|
||||
lists:foldl( fun(_N, Q) -> write_top( Q ) end, Queue, lists:seq(1, erlang:length(Items)) ).
|
||||
|
||||
top( Queue ) ->
|
||||
{_Priority, Element, New_queue} = gb_trees:take_smallest( Queue ),
|
||||
{Element, New_queue}.
|
||||
{_Priority, Element, New_queue} = gb_trees:take_smallest( Queue ),
|
||||
{Element, New_queue}.
|
||||
|
||||
|
||||
|
||||
write_top( Q ) ->
|
||||
{Element, New_queue} = top( Q ),
|
||||
io:fwrite( "top priority: ~p~n", [Element] ),
|
||||
New_queue.
|
||||
{Element, New_queue} = top( Q ),
|
||||
io:fwrite( "top priority: ~p~n", [Element] ),
|
||||
New_queue.
|
||||
|
|
|
|||
18
Task/Priority-queue/Groovy/priority-queue.groovy
Normal file
18
Task/Priority-queue/Groovy/priority-queue.groovy
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import groovy.transform.Canonical
|
||||
|
||||
@Canonical
|
||||
class Task implements Comparable<Task> {
|
||||
int priority
|
||||
String name
|
||||
int compareTo(Task o) { priority <=> o?.priority }
|
||||
}
|
||||
|
||||
new PriorityQueue<Task>().with {
|
||||
add new Task(priority: 3, name: 'Clear drains')
|
||||
add new Task(priority: 4, name: 'Feed cat')
|
||||
add new Task(priority: 5, name: 'Make tea')
|
||||
add new Task(priority: 1, name: 'Solve RC tasks')
|
||||
add new Task(priority: 2, name: 'Tax return')
|
||||
|
||||
while (!empty) { println remove() }
|
||||
}
|
||||
|
|
@ -1,27 +1,27 @@
|
|||
data MinHeap a = Nil | MinHeap { v::a, cnt::Int, l::MinHeap a, r::MinHeap a }
|
||||
deriving (Show, Eq)
|
||||
deriving (Show, Eq)
|
||||
|
||||
hPush :: (Ord a) => a -> MinHeap a -> MinHeap a
|
||||
hPush x Nil = MinHeap {v = x, cnt = 1, l = Nil, r = Nil}
|
||||
hPush x h = if x < vv -- insert element, try to keep the tree balanced
|
||||
then if hLength (l h) <= hLength (r h)
|
||||
then MinHeap { v=x, cnt=cc, l=hPush vv ll, r=rr }
|
||||
else MinHeap { v=x, cnt=cc, l=ll, r=hPush vv rr }
|
||||
else if hLength (l h) <= hLength (r h)
|
||||
then MinHeap { v=vv, cnt=cc, l=hPush x ll, r=rr }
|
||||
else MinHeap { v=vv, cnt=cc, l=ll, r=hPush x rr }
|
||||
where (vv, cc, ll, rr) = (v h, 1 + cnt h, l h, r h)
|
||||
then if hLength (l h) <= hLength (r h)
|
||||
then MinHeap { v=x, cnt=cc, l=hPush vv ll, r=rr }
|
||||
else MinHeap { v=x, cnt=cc, l=ll, r=hPush vv rr }
|
||||
else if hLength (l h) <= hLength (r h)
|
||||
then MinHeap { v=vv, cnt=cc, l=hPush x ll, r=rr }
|
||||
else MinHeap { v=vv, cnt=cc, l=ll, r=hPush x rr }
|
||||
where (vv, cc, ll, rr) = (v h, 1 + cnt h, l h, r h)
|
||||
|
||||
hPop :: (Ord a) => MinHeap a -> (a, MinHeap a)
|
||||
hPop h = (v h, pq) where -- just pop, heed not the tree balance
|
||||
pq | l h == Nil = r h
|
||||
| r h == Nil = l h
|
||||
| v (l h) <= v (r h) = let (vv,hh) = hPop (l h) in
|
||||
MinHeap {v = vv, cnt = hLength hh + hLength (r h),
|
||||
l = hh, r = r h}
|
||||
| otherwise = let (vv,hh) = hPop (r h) in
|
||||
MinHeap {v = vv, cnt = hLength hh + hLength (l h),
|
||||
l = l h, r = hh}
|
||||
pq | l h == Nil = r h
|
||||
| r h == Nil = l h
|
||||
| v (l h) <= v (r h) = let (vv,hh) = hPop (l h) in
|
||||
MinHeap {v = vv, cnt = hLength hh + hLength (r h),
|
||||
l = hh, r = r h}
|
||||
| otherwise = let (vv,hh) = hPop (r h) in
|
||||
MinHeap {v = vv, cnt = hLength hh + hLength (l h),
|
||||
l = l h, r = hh}
|
||||
|
||||
hLength :: (Ord a) => MinHeap a -> Int
|
||||
hLength Nil = 0
|
||||
|
|
@ -36,8 +36,8 @@ hToList = unfoldr f where
|
|||
f h = Just $ hPop h
|
||||
|
||||
main = mapM_ print $ hToList $ hFromList [
|
||||
(3, "Clear drains"),
|
||||
(4, "Feed cat"),
|
||||
(5, "Make tea"),
|
||||
(1, "Solve RC tasks"),
|
||||
(2, "Tax return")]
|
||||
(3, "Clear drains"),
|
||||
(4, "Feed cat"),
|
||||
(5, "Make tea"),
|
||||
(1, "Solve RC tasks"),
|
||||
(2, "Tax return")]
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import Utils # For Closure class
|
||||
import Utils # For Closure class
|
||||
import Collections # For Heap (dense priority queue) class
|
||||
|
||||
procedure main()
|
||||
|
|
|
|||
35
Task/Priority-queue/Mercury/priority-queue.mercury
Normal file
35
Task/Priority-queue/Mercury/priority-queue.mercury
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
:- module test_pqueue.
|
||||
|
||||
:- interface.
|
||||
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
|
||||
:- import_module int.
|
||||
:- import_module list.
|
||||
:- import_module pqueue.
|
||||
:- import_module string.
|
||||
|
||||
:- pred build_pqueue(pqueue(int,string)::in, pqueue(int,string)::out) is det.
|
||||
build_pqueue(!PQ) :-
|
||||
pqueue.insert(3, "Clear drains", !PQ),
|
||||
pqueue.insert(4, "Feed cat", !PQ),
|
||||
pqueue.insert(5, "Make tea", !PQ),
|
||||
pqueue.insert(1, "Solve RC tasks", !PQ),
|
||||
pqueue.insert(2, "Tax return", !PQ).
|
||||
|
||||
:- pred display_pqueue(pqueue(int, string)::in, io::di, io::uo) is det.
|
||||
display_pqueue(PQ, !IO) :-
|
||||
( pqueue.remove(K, V, PQ, PQO) ->
|
||||
io.format("Key = %d, Value = %s\n", [i(K), s(V)], !IO),
|
||||
display_pqueue(PQO, !IO)
|
||||
;
|
||||
true
|
||||
).
|
||||
|
||||
main(!IO) :-
|
||||
build_pqueue(pqueue.init, PQO),
|
||||
display_pqueue(PQO, !IO).
|
||||
|
|
@ -6,9 +6,9 @@ my $h = new Heap::Priority;
|
|||
|
||||
$h->highest_first(); # higher or lower number is more important
|
||||
$h->add(@$_) for ["Clear drains", 3],
|
||||
["Feed cat", 4],
|
||||
["Make tea", 5],
|
||||
["Solve RC tasks", 1],
|
||||
["Tax return", 2];
|
||||
["Feed cat", 4],
|
||||
["Make tea", 5],
|
||||
["Solve RC tasks", 1],
|
||||
["Tax return", 2];
|
||||
|
||||
say while ($_ = $h->pop);
|
||||
|
|
|
|||
|
|
@ -1,28 +1,28 @@
|
|||
priority-queue :-
|
||||
TL0 = [3-'Clear drains',
|
||||
4-'Feed cat'],
|
||||
TL0 = [3-'Clear drains',
|
||||
4-'Feed cat'],
|
||||
|
||||
% we can create a priority queue from a list
|
||||
list_to_heap(TL0, Heap0),
|
||||
|
||||
% alternatively we can start from an empty queue
|
||||
% get from empty_heap/1.
|
||||
|
||||
% now we add the other elements
|
||||
add_to_heap(Heap0, 5, 'Make tea', Heap1),
|
||||
add_to_heap(Heap1, 1, 'Solve RC tasks', Heap2),
|
||||
add_to_heap(Heap2, 2, 'Tax return', Heap3),
|
||||
% we can create a priority queue from a list
|
||||
list_to_heap(TL0, Heap0),
|
||||
|
||||
% we list the content of the heap:
|
||||
heap_to_list(Heap3, TL1),
|
||||
writeln('Content of the queue'), maplist(writeln, TL1),
|
||||
nl,
|
||||
% alternatively we can start from an empty queue
|
||||
% get from empty_heap/1.
|
||||
|
||||
% now we retrieve the minimum-priority pair
|
||||
get_from_heap(Heap3, Priority, Key, Heap4),
|
||||
format('Retrieve top of the queue : Priority ~w, Element ~w~n', [Priority, Key]),
|
||||
nl,
|
||||
% now we add the other elements
|
||||
add_to_heap(Heap0, 5, 'Make tea', Heap1),
|
||||
add_to_heap(Heap1, 1, 'Solve RC tasks', Heap2),
|
||||
add_to_heap(Heap2, 2, 'Tax return', Heap3),
|
||||
|
||||
% we list the content of the heap:
|
||||
heap_to_list(Heap4, TL2),
|
||||
writeln('Content of the queue'), maplist(writeln, TL2).
|
||||
% we list the content of the heap:
|
||||
heap_to_list(Heap3, TL1),
|
||||
writeln('Content of the queue'), maplist(writeln, TL1),
|
||||
nl,
|
||||
|
||||
% now we retrieve the minimum-priority pair
|
||||
get_from_heap(Heap3, Priority, Key, Heap4),
|
||||
format('Retrieve top of the queue : Priority ~w, Element ~w~n', [Priority, Key]),
|
||||
nl,
|
||||
|
||||
% we list the content of the heap:
|
||||
heap_to_list(Heap4, TL2),
|
||||
writeln('Content of the queue'), maplist(writeln, TL2).
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
>>> import queue
|
||||
>>> pq = queue.PriorityQueue()
|
||||
>>> for item in ((3, "Clear drains"), (4, "Feed cat"), (5, "Make tea"), (1, "Solve RC tasks"), (2, "Tax return")):
|
||||
pq.put(item)
|
||||
pq.put(item)
|
||||
|
||||
|
||||
|
||||
>>> while not pq.empty():
|
||||
print(pq.get_nowait())
|
||||
print(pq.get_nowait())
|
||||
|
||||
|
||||
|
||||
(1, 'Solve RC tasks')
|
||||
(2, 'Tax return')
|
||||
(3, 'Clear drains')
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@
|
|||
>>> items = [(3, "Clear drains"), (4, "Feed cat"), (5, "Make tea"), (1, "Solve RC tasks"), (2, "Tax return")]
|
||||
>>> heapify(items)
|
||||
>>> while items:
|
||||
print(heappop(items))
|
||||
print(heappop(items))
|
||||
|
||||
|
||||
|
||||
(1, 'Solve RC tasks')
|
||||
(2, 'Tax return')
|
||||
(3, 'Clear drains')
|
||||
|
|
|
|||
31
Task/Priority-queue/REXX/priority-queue.rexx
Normal file
31
Task/Priority-queue/REXX/priority-queue.rexx
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
/*REXX pgm implements a priority queue; with insert/show/delete top task*/
|
||||
numeric digits 100; #=0; @.= /*big #, 0 tasks, null priority queue*/
|
||||
say '══════ inserting tasks.'; call .ins 3 'Clear drains'
|
||||
call .ins 4 'Feed cat'
|
||||
call .ins 5 'Make tea'
|
||||
call .ins 1 'Solve RC tasks'
|
||||
call .ins 2 'Tax return'
|
||||
call .ins 6 'Relax'
|
||||
call .ins 6 'Enjoy'
|
||||
say '══════ showing tasks.'; call .show
|
||||
say '══════ deletes top task.'; do # /*number of tasks. */
|
||||
say .del() /*delete top task. */
|
||||
end /* [↑] do top first.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────.INS subroutine─────────────────────*/
|
||||
.ins: procedure expose @. #; #=#+1; @.#=arg(1); return # /*entry,P,task.*/
|
||||
/*──────────────────────────────────.DEL subroutine─────────────────────*/
|
||||
.del: procedure expose @. #; parse arg p; if p=='' then p=.top()
|
||||
x=@.p; @.p= /*delete the top task entry. */
|
||||
return x /*return task that was deleted. */
|
||||
/*──────────────────────────────────.SHOW subroutine────────────────────*/
|
||||
.show: procedure expose @. #
|
||||
do j=1 for #; _=@.j; if _=='' then iterate; say _
|
||||
end /*j*/ /* [↑] show whole list or just 1.*/
|
||||
return
|
||||
/*──────────────────────────────────.TOP subroutine─────────────────────*/
|
||||
.top: procedure expose @. #; top=; top#=
|
||||
do j=1 for #; _=word(@.j,1); if _=='' then iterate
|
||||
if top=='' | _>top then do; top=_; top#=j; end
|
||||
end /*j*/
|
||||
return top#
|
||||
|
|
@ -23,7 +23,7 @@ gosub [getQueue]
|
|||
|
||||
what$ = " -------------- Delete Highest Priority ---------------------"
|
||||
mem$ = "DELETE FROM queue WHERE priority = (select max(q.priority) FROM queue as q)"
|
||||
#mem execute(mem$)
|
||||
#mem execute(mem$)
|
||||
|
||||
what$ = " -------------- List Priority Sequence ---------------------"
|
||||
mem$ = "SELECT * FROM queue ORDER BY priority"
|
||||
|
|
@ -33,13 +33,13 @@ end
|
|||
|
||||
[getQueue]
|
||||
print what$
|
||||
#mem execute(mem$)
|
||||
#mem execute(mem$)
|
||||
rows = #mem ROWCOUNT()
|
||||
print "Priority Description"
|
||||
for i = 1 to rows
|
||||
#row = #mem #nextrow()
|
||||
priority = #row priority()
|
||||
descr$ = #row descr$()
|
||||
#row = #mem #nextrow()
|
||||
priority = #row priority()
|
||||
descr$ = #row descr$()
|
||||
print priority;" ";descr$
|
||||
next i
|
||||
RETURN
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue