diff options
author | David Thompson <dthompson2@worcester.edu> | 2018-11-29 14:27:00 -0500 |
---|---|---|
committer | David Thompson <dthompson2@worcester.edu> | 2018-11-29 14:28:59 -0500 |
commit | 1afab11233de20272bdc029e70a5e1dcfd17218e (patch) | |
tree | 761391e6f8f41a5d35740496323b09ae87bcc662 | |
parent | e6706e271eeb165ef48a3edc637a732028c4058b (diff) |
queue: Add queue-length procedure.
* chickadee/queue.scm (queue-length): New procedure.
-rw-r--r-- | chickadee/queue.scm | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/chickadee/queue.scm b/chickadee/queue.scm index 158ac74..51cec76 100644 --- a/chickadee/queue.scm +++ b/chickadee/queue.scm @@ -1,5 +1,5 @@ ;;; Chickadee Game Toolkit -;;; Copyright © 2017 David Thompson <davet@gnu.org> +;;; Copyright © 2017, 2018 David Thompson <davet@gnu.org> ;;; ;;; Chickadee is free software: you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published @@ -22,6 +22,7 @@ #:use-module (chickadee array-list) #:export (make-queue queue? + queue-length queue-empty? enqueue! dequeue!)) @@ -33,9 +34,7 @@ (output queue-output)) (define (display-queue q port) - (format port "#<queue length: ~d>" - (+ (array-list-size (queue-input q)) - (array-list-size (queue-output q))))) + (format port "#<queue length: ~d>" (queue-length q))) (set-record-type-printer! <queue> display-queue) @@ -43,10 +42,14 @@ "Return a new, empty queue." (%make-queue (make-array-list) (make-array-list))) +(define (queue-length q) + "Return the number of elements in Q." + (+ (array-list-size (queue-input q)) + (array-list-size (queue-output q)))) + (define (queue-empty? q) "Return #t if Q is empty." - (and (array-list-empty? (queue-input q)) - (array-list-empty? (queue-output q)))) + (zero? (queue-length q))) (define (enqueue! q item) "Add ITEM to Q." |