-
Notifications
You must be signed in to change notification settings - Fork 2
/
queue.sls
71 lines (61 loc) · 1.83 KB
/
queue.sls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!r6rs
;; Copyright 2009 Derick Eddington. My MIT-style license is in the file named
;; LICENSE from the original collection this file is distributed with.
(library (xitomatl queue)
(export
queue?
make-empty-queue
enqueue!
dequeue!
queue-length
queue-empty?
queue->list
queue->list/reset
check-queue)
(import
(rnrs)
(rnrs mutable-pairs)
(only (xitomatl define) define/AV))
(define-record-type queue
(fields (mutable head) (mutable end)))
(define (make-empty-queue)
(make-queue '() '()))
(define (enqueue! q e)
(let ((el (cons e '())))
(let ((qe (queue-end q)))
(unless (null? qe) (set-cdr! qe el))
(queue-end-set! q el)
(when (null? (queue-head q)) (queue-head-set! q el)))))
(define/AV (dequeue! q)
(let ((h (queue-head q)))
(when (null? h) (AV "empty queue"))
(let ((rest (cdr h)))
(queue-head-set! q rest)
(when (null? rest) (queue-end-set! q '())))
(car h)))
(define (queue-length q)
(length (queue-head q)))
(define (queue-empty? q)
(= 0 (queue-length q)))
(define (queue->list q)
(apply list (queue-head q)))
(define (queue->list/reset q)
(let ((h (queue-head q)))
(queue-head-set! q '())
(queue-end-set! q '())
h))
(define/AV (check-queue q)
(let ((head (queue-head q))
(end (queue-end q)))
(if (null? head)
(unless (null? end)
(AV "head is null but end is not"))
(unless (eq? end (list-tail head (- (length head) 1)))
(AV "last pair of head is not end")))
(if (null? end)
(unless (null? head)
(AV "end is null but head is not"))
; We know head is not null, therefore previous if block
; will have checked that last pair of head is end
)))
)