June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -35,11 +35,14 @@ char *pop (heap_t *h) {
return NULL;
}
char *data = h->nodes[1].data;
h->nodes[1] = h->nodes[h->len];
h->len--;
i = 1;
while (1) {
k = i;
while (i!=h->len+1) {
k = h->len+1;
j = 2 * i;
if (j <= h->len && h->nodes[j].priority < h->nodes[k].priority) {
k = j;
@ -47,13 +50,9 @@ char *pop (heap_t *h) {
if (j + 1 <= h->len && h->nodes[j + 1].priority < h->nodes[k].priority) {
k = j + 1;
}
if (k == i) {
break;
}
h->nodes[i] = h->nodes[k];
i = k;
}
h->nodes[i] = h->nodes[h->len + 1];
return data;
}

View file

@ -1,18 +1,12 @@
import tables
import HeapQueue
var
pq = initTable[int, string]()
var pq = newHeapQueue[(int, string)]()
proc main() =
pq.add(3, "Clear drains")
pq.add(4, "Feed cat")
pq.add(5, "Make tea")
pq.add(1, "Solve RC tasks")
pq.add(2, "Tax return")
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"))
for i in countUp(1,5):
if pq.hasKey(i):
echo i, ": ", pq[i]
pq.del(i)
main()
while pq.len() > 0:
echo pq.pop()

View file

@ -0,0 +1,18 @@
import tables
var
pq = initTable[int, string]()
proc main() =
pq.add(3, "Clear drains")
pq.add(4, "Feed cat")
pq.add(5, "Make tea")
pq.add(1, "Solve RC tasks")
pq.add(2, "Tax return")
for i in countUp(1,5):
if pq.hasKey(i):
echo i, ": ", pq[i]
pq.del(i)
main()

View file

@ -1,5 +1,5 @@
/*REXX program implements a priority queue; with insert/display/delete the top task. */
#=0; @.= /*0 tasks; nullify the priority queue.*/
/*REXX program implements a priority queue with insert/display/delete the top task.*/
#=0; @.= /*0 tasks; nullify the priority queue.*/
say ' inserting tasks.'; call .ins 3 "Clear drains"
call .ins 4 "Feed cat"
call .ins 5 "Make tea"
@ -11,16 +11,12 @@ say '══════ showing tasks.'; call .show
say ' deletes top task.'; say .del() /*delete the top task. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
.del: procedure expose @. #; parse arg p; if p=='' then p=.top(); x=@.p; @.p=; return x
/*delete the top task entry. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
.del: procedure expose @. #; arg p; if p='' then p=.top(); y=@.p; @.p=; return y
.ins: procedure expose @. #; #=#+1; @.#=arg(1); return # /*entry, P, task.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
.show: procedure expose @. #; do j=1 for #; _=@.j; if _=='' then iterate; say _; end
return /* [↑] show whole list or just one. */
.show: procedure expose @. #; do j=1 for #; _=@.j; if _\=='' then say _; end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
.top: procedure expose @. #; top=; top#=
do j=1 for #; _=word(@.j,1); if _=='' then iterate
if top=='' | _>top then do; top=_; top#=j; end
do j=1 for #; _=word(@.j, 1); if _=='' then iterate
if top=='' | _>top then do; top=_; top#=j; end
end /*j*/
return top#

View file

@ -0,0 +1,35 @@
(define-class priority-queue
(instance-variables queue lowest-priority most-urgent) )
(define-method (priority-queue 'initialize limit)
(defun setup (x)
(vector-set! queue x nil)
(if (< x limit)
(setup (+ x 1)) ) )
(setq lowest-priority limit)
(setq most-urgent limit)
(setq queue (make-vector (+ limit 1)))
(setup 0)
self )
(define-method (priority-queue 'push item priority)
(if (and (integerp priority) (>= priority 0) (<= priority lowest-priority))
(progn
(setq most-urgent (min priority most-urgent))
(vector-set! queue priority (nconc (vector-ref queue priority) (cons item nil))) ) ) )
(define-method (priority-queue 'pop)
(defun find-next (q)
(if (or (= q lowest-priority) (not (null (vector-ref queue q))))
q
(find-next (+ q 1)) ) )
(define item (car (vector-ref queue most-urgent)))
(vector-set! queue most-urgent (cdr (vector-ref queue most-urgent)))
(setq most-urgent (find-next most-urgent))
item )
(define-method (priority-queue 'peek)
(car (vector-ref queue most-urgent)) )
(define-method (priority-queue 'emptyp)
(and (= most-urgent lowest-priority) (null (vector-ref queue most-urgent))) )

View file

@ -0,0 +1,3 @@
[13] (pq 'pop)
()

View file

@ -0,0 +1,7 @@
(define pq (priority-queue 'new 5))
(pq 'push "Clear drains" 3)
(pq 'push "Feed cat" 4)
(pq 'push "Make tea" 5)
(pq 'push "Solve RC tasks" 1)
(pq 'push "Tax return" 2)

View file

@ -0,0 +1,6 @@
[1] (pq 'pop)
"Solve RC tasks"
[2] (pq 'pop)
"Tax return"

View file

@ -0,0 +1,3 @@
[3] (pq 'push "Answer emails" 4)
("Feed cat" "Answer emails")

View file

@ -0,0 +1,3 @@
[4] (pq 'push "Weed garden" 17)
()

View file

@ -0,0 +1,3 @@
[5] (pq 'emptyp)
()

View file

@ -0,0 +1,3 @@
[6] (pq 'peek)
"Clear drains"

View file

@ -0,0 +1,8 @@
[7] (pq 'show)
Object is #<Object:PRIORITY-QUEUE #x4e2cba8>, Class is #<Class:PRIORITY-QUEUE #x4e254c8>
Instance variables:
QUEUE = #(() () () ("Clear drains") ("Feed cat" "Answer emails") ("Make tea"))
LOWEST-PRIORITY = 5
MOST-URGENT = 3
#<Object:PRIORITY-QUEUE #x4e2cba8>

View file

@ -0,0 +1,15 @@
[8] (pq 'pop)
"Clear drains"
[9] (pq 'pop)
"Feed cat"
[10] (pq 'pop)
"Answer emails"
[11] (pq 'pop)
"Make tea"
[12] (pq 'emptyp)
#T