queue.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ;;; queue.el --- Queue data structure -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 1991-1995, 2008-2009, 2012 Free Software Foundation, Inc
  3. ;; Author: Inge Wallin <inge@lysator.liu.se>
  4. ;; Toby Cubitt <toby-predictive@dr-qubit.org>
  5. ;; Maintainer: Toby Cubitt <toby-predictive@dr-qubit.org>
  6. ;; Version: 0.1
  7. ;; Keywords: extensions, data structures, queue
  8. ;; URL: http://www.dr-qubit.org/emacs.php
  9. ;; Repository: http://www.dr-qubit.org/git/predictive.git
  10. ;; This file is part of Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify it under
  13. ;; the terms of the GNU General Public License as published by the Free
  14. ;; Software Foundation, either version 3 of the License, or (at your option)
  15. ;; any later version.
  16. ;;
  17. ;; GNU Emacs is distributed in the hope that it will be useful, but WITHOUT
  18. ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  20. ;; more details.
  21. ;;
  22. ;; You should have received a copy of the GNU General Public License along
  23. ;; with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;;
  26. ;; These queues can be used both as a first-in last-out (FILO) and as a
  27. ;; first-in first-out (FIFO) stack, i.e. elements can be added to the front or
  28. ;; back of the queue, and can be removed from the front. (This type of data
  29. ;; structure is sometimes called an "output-restricted deque".)
  30. ;;
  31. ;; You create a queue using `make-queue', add an element to the end of the
  32. ;; queue using `queue-enqueue', and push an element onto the front of the
  33. ;; queue using `queue-prepend'. To remove the first element from a queue, use
  34. ;; `queue-dequeue'. A number of other queue convenience functions are also
  35. ;; provided, all starting with the prefix `queue-'. Functions with prefix
  36. ;; `queue--' are for internal use only, and should never be used outside this
  37. ;; package.
  38. ;;; Code:
  39. (eval-when-compile (require 'cl))
  40. (defstruct (queue
  41. ;; A tagged list is the pre-defstruct representation.
  42. ;; (:type list)
  43. :named
  44. (:constructor nil)
  45. (:constructor queue-create ())
  46. (:copier nil))
  47. head tail)
  48. ;;;###autoload
  49. (defalias 'make-queue 'queue-create
  50. "Create an empty queue data structure.")
  51. (defun queue-enqueue (queue element)
  52. "Append an ELEMENT to the end of the QUEUE."
  53. (if (queue-head queue)
  54. (setcdr (queue-tail queue)
  55. (setf (queue-tail queue) (cons element nil)))
  56. (setf (queue-head queue)
  57. (setf (queue-tail queue) (cons element nil)))))
  58. (defalias 'queue-append 'queue-enqueue)
  59. (defun queue-prepend (queue element)
  60. "Prepend an ELEMENT to the front of the QUEUE."
  61. (if (queue-head queue)
  62. (push element (queue-head queue))
  63. (setf (queue-head queue)
  64. (setf (queue-tail queue) (cons element nil)))))
  65. (defun queue-dequeue (queue)
  66. "Remove the first element of QUEUE and return it.
  67. Returns nil if the queue is empty."
  68. (unless (cdr (queue-head queue)) (setf (queue-tail queue) nil))
  69. (pop (queue-head queue)))
  70. (defmacro queue-empty (queue)
  71. "Return t if QUEUE is empty, otherwise return nil."
  72. (null (queue-head queue)))
  73. (defmacro queue-first (queue)
  74. "Return the first element of QUEUE or nil if it is empty,
  75. without removing it from the QUEUE."
  76. (car (queue-head queue)))
  77. (defun queue-nth (queue n)
  78. "Return the nth element of a queue, without removing it.
  79. If the length of the queue is less than N, return nil. The first
  80. element in the queue has index 0."
  81. (nth n (queue-head queue)))
  82. (defun queue-last (queue)
  83. "Return the last element of QUEUE, without removing it.
  84. Returns nil if the QUEUE is empty."
  85. (car (queue-tail queue)))
  86. (defun queue-all (queue)
  87. "Return a list of all elements of QUEUE or nil if it is empty.
  88. The oldest element in the queue is the first in the list."
  89. (queue-head queue))
  90. (defun queue-copy (queue)
  91. "Return a copy of QUEUE.
  92. The new queue contains the elements of QUEUE in the same
  93. order. The elements themselves are *not* copied."
  94. (let ((q (queue-create))
  95. (list (queue-head queue)))
  96. (when (queue-head queue)
  97. (setf (queue-head q) (cons (car (queue-head queue)) nil)
  98. (queue-tail q) (queue-head q))
  99. (while (setq list (cdr list))
  100. (setf (queue-tail q)
  101. (setcdr (queue-tail q) (cons (car list) nil)))))
  102. q))
  103. (defun queue-length (queue)
  104. "Return the number of elements in QUEUE."
  105. (length (queue-head queue)))
  106. (defun queue-clear (queue)
  107. "Remove all elements from QUEUE."
  108. (setf (queue-head queue) nil
  109. (queue-tail queue) nil))
  110. (provide 'queue)
  111. ;;; queue.el ends here