linked-queue.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ; Copyright (c) 1993-2007 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; This file is no longer used.
  3. ; Queues implemented as doubly linked lists (because the thread package needs
  4. ; to delete queue entries quickly).
  5. ; The exported procedures are those of the simpler queue package, with the
  6. ; addition of DELETE-QUEUE-ENTRY!. ENQUEUE! returns a queue-entry which can
  7. ; then be passed to DELETE-QUEUE-ENTRY! to remove the thing from the queue.
  8. (define-record-type q-entry :q-entry
  9. (make-q-entry data prev next)
  10. q-entry?
  11. (data q-entry-data)
  12. (prev q-entry-prev set-q-entry-prev!)
  13. (next q-entry-next set-q-entry-next!))
  14. (define queue? q-entry?)
  15. (define (make-queue)
  16. (let ((e (make-q-entry #f #f #f)))
  17. (set-q-entry-prev! e e)
  18. (set-q-entry-next! e e)
  19. e))
  20. (define (queue-empty? q)
  21. (eq? (q-entry-next q) q))
  22. (define (enqueue! q thing)
  23. (let* ((prev (q-entry-prev q))
  24. (e (make-q-entry thing prev q)))
  25. (set-q-entry-prev! q e)
  26. (set-q-entry-next! prev e)
  27. e))
  28. (define (queue-head q)
  29. (let ((e (q-entry-next q)))
  30. (if (eq? q e) ;(queue-empty? q)
  31. (error "empty queue" q)
  32. (q-entry-data e))))
  33. (define (dequeue! q)
  34. (let ((e (q-entry-next q)))
  35. (cond ((eq? q e) ;(queue-empty? q)
  36. (error "empty queue" q))
  37. (else
  38. (set-q-entry-next! q (q-entry-next e))
  39. (set-q-entry-prev! (q-entry-next q) q)
  40. (q-entry-data e)))))
  41. (define (delete-queue-entry! e)
  42. (let ((next (q-entry-next e))
  43. (prev (q-entry-prev e)))
  44. (set-q-entry-next! prev next)
  45. (set-q-entry-prev! next prev)))
  46. (define (queue->list q)
  47. (do ((e (q-entry-prev q) (q-entry-prev e))
  48. (l '() (cons (q-entry-data e) l)))
  49. ((eq? q e) l)))
  50. (define (queue-length q)
  51. (do ((e (q-entry-prev q) (q-entry-prev e))
  52. (l 0 (+ l 1)))
  53. ((eq? q e) l)))
  54. (define (delete-from-queue! q v)
  55. (let loop ((e (q-entry-next q)))
  56. (cond ((eq? e q))
  57. ((eq? (q-entry-data e) v)
  58. (delete-queue-entry! e))
  59. (else
  60. (loop (q-entry-next e))))))