Klimi's new dotfiles with stow.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.6 KiB

  1. ;;; queue.el --- Queue data structure -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 1991-1995, 2008-2009, 2012, 2017 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.2
  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. (defmacro queue--when-generators (then)
  41. "Evaluate THEN if `generator' library is available."
  42. (declare (debug t))
  43. (if (require 'generator nil 'noerror) then))
  44. (defstruct (queue
  45. ;; A tagged list is the pre-defstruct representation.
  46. ;; (:type list)
  47. :named
  48. (:constructor nil)
  49. (:constructor queue-create ())
  50. (:copier nil))
  51. head tail)
  52. ;;;###autoload
  53. (defalias 'make-queue 'queue-create
  54. "Create an empty queue data structure.")
  55. (defun queue-enqueue (queue element)
  56. "Append an ELEMENT to the end of the QUEUE."
  57. (if (queue-head queue)
  58. (setcdr (queue-tail queue)
  59. (setf (queue-tail queue) (cons element nil)))
  60. (setf (queue-head queue)
  61. (setf (queue-tail queue) (cons element nil)))))
  62. (defalias 'queue-append 'queue-enqueue)
  63. (defun queue-prepend (queue element)
  64. "Prepend an ELEMENT to the front of the QUEUE."
  65. (if (queue-head queue)
  66. (push element (queue-head queue))
  67. (setf (queue-head queue)
  68. (setf (queue-tail queue) (cons element nil)))))
  69. (defun queue-dequeue (queue)
  70. "Remove the first element of QUEUE and return it.
  71. Returns nil if the queue is empty."
  72. (unless (cdr (queue-head queue)) (setf (queue-tail queue) nil))
  73. (pop (queue-head queue)))
  74. (defun queue-empty (queue)
  75. "Return t if QUEUE is empty, otherwise return nil."
  76. (null (queue-head queue)))
  77. (defun queue-first (queue)
  78. "Return the first element of QUEUE or nil if it is empty,
  79. without removing it from the QUEUE."
  80. (car (queue-head queue)))
  81. (defun queue-nth (queue n)
  82. "Return the nth element of a queue, without removing it.
  83. If the length of the queue is less than N, return nil. The first
  84. element in the queue has index 0."
  85. (nth n (queue-head queue)))
  86. (defun queue-last (queue)
  87. "Return the last element of QUEUE, without removing it.
  88. Returns nil if the QUEUE is empty."
  89. (car (queue-tail queue)))
  90. (defun queue-all (queue)
  91. "Return a list of all elements of QUEUE or nil if it is empty.
  92. The oldest element in the queue is the first in the list."
  93. (queue-head queue))
  94. (defun queue-copy (queue)
  95. "Return a copy of QUEUE.
  96. The new queue contains the elements of QUEUE in the same
  97. order. The elements themselves are *not* copied."
  98. (let ((q (queue-create))
  99. (list (queue-head queue)))
  100. (when (queue-head queue)
  101. (setf (queue-head q) (cons (car (queue-head queue)) nil)
  102. (queue-tail q) (queue-head q))
  103. (while (setq list (cdr list))
  104. (setf (queue-tail q)
  105. (setcdr (queue-tail q) (cons (car list) nil)))))
  106. q))
  107. (defun queue-length (queue)
  108. "Return the number of elements in QUEUE."
  109. (length (queue-head queue)))
  110. (defun queue-clear (queue)
  111. "Remove all elements from QUEUE."
  112. (setf (queue-head queue) nil
  113. (queue-tail queue) nil))
  114. (queue--when-generators
  115. (iter-defun queue-iter (queue)
  116. "Return a queue iterator object.
  117. Calling `iter-next' on this object will retrieve the next element
  118. from the queue. The queue itself is not modified."
  119. (let ((list (queue-head queue)))
  120. (while list (iter-yield (pop list))))))
  121. ;;;; ChangeLog:
  122. ;; 2017-08-16 Toby S. Cubitt <tsc25@cantab.net>
  123. ;;
  124. ;; Upgrade data structure packages to latest versions.
  125. ;;
  126. ;; 2014-05-15 Toby S. Cubitt <tsc25@cantab.net>
  127. ;;
  128. ;; queue.el: fix buggy queue-first and queue-empty definitions.
  129. ;;
  130. ;; 2012-04-30 Toby S. Cubitt <tsc25@cantab.net>
  131. ;;
  132. ;; Minor fixes to commentaries, package headers, and whitespace
  133. ;;
  134. ;; * queue.el: fix description of data structure in Commentary; add
  135. ;; Maintainer
  136. ;; header.
  137. ;;
  138. ;; * queue.el, heap.el, tNFA.el, trie.el, dict-tree.el: trivial whitespace
  139. ;; fixes.
  140. ;;
  141. ;; 2012-04-29 Toby S. Cubitt <tsc25@cantab.net>
  142. ;;
  143. ;; Add queue.el
  144. ;;
  145. (provide 'queue)
  146. ;;; queue.el ends here