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

@ -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