summaryrefslogtreecommitdiff
path: root/phpinspect-queue.el
blob: af0a57eb96059257288e16231363154c1448f702 (plain)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
;;; phpinspect-queue.el --- PHP parsing and completion package  -*- lexical-binding: t; -*-

;; Copyright (C) 2021-2025  Free Software Foundation, Inc

;; Author: Hugo Thunnissen <devel@hugot.nl>
;; Keywords: php, languages, tools, convenience
;; Version: 3.0.1

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; Code:


(cl-defstruct (phpinspect-queue
               (:constructor phpinspect-make-queue-generated))
  (-first nil
         :type phpinspect-queue-item
         :documentation
         "The first item in the queue")
  (-last nil
        :type phpinspect-queue-item
        :documentation
        "The last item in the queue")
  (subscription  nil
                 :type function
                :documentation
                "A function that should be called when items are
                enqueued."))

(cl-defstruct (phpinspect-queue-item
               (:constructor phpinspect-make-queue-item))
  (next nil
        :type phpinspect-queue-item
        :documentation
        "The next item in the queue")
  (value nil
         :type any
         :documentation
         "The value stored in the queue")
  (previous nil
            :type phpinspect-queue-item
            :documentation
            "The previous item in the queue"))

(define-inline phpinspect-make-queue (&optional subscription)
  (inline-quote
   (progn
     (phpinspect-make-queue-generated :subscription ,subscription))))

(define-inline phpinspect-queue-first (queue)
  (inline-quote (phpinspect-queue--first ,queue)))

(define-inline phpinspect-queue-last (queue)
  (inline-letevals (queue)
    (inline-quote
     (or (phpinspect-queue--last ,queue) (phpinspect-queue--first ,queue)))))

(defun phpinspect-queue-enqueue (queue value &optional no-notify)
  "Add VALUE to the end of the queue that ITEM is part of."
  (let ((last (phpinspect-queue-last queue))
        (new-item (phpinspect-make-queue-item :value value)))
    (if (not last)
        (setf (phpinspect-queue--first queue) new-item)
      (setf (phpinspect-queue-item-next last) new-item)
      (setf (phpinspect-queue-item-previous new-item) last))
    (setf (phpinspect-queue--last queue) new-item))

  (when (and (not no-notify) (phpinspect-queue-subscription queue))
    (funcall (phpinspect-queue-subscription queue))))

(defun phpinspect-queue-dequeue (queue)
  "Remove the value at the front of the queue that ITEM is part of and return it."
  (let* ((first (phpinspect-queue-first queue))
         next value)
    (when first
      (setq next (phpinspect-queue-item-next first))
      (setq value (phpinspect-queue-item-value first)))
    (if next
        (setf (phpinspect-queue-item-previous next) nil)
      (setf (phpinspect-queue--last queue) nil))
    (setf (phpinspect-queue--first queue) next)
    value))

(defmacro phpinspect-doqueue (place-and-queue &rest body)
  "Loop over queue defined in PLACE-AND-QUEUE executing BODY.

PLACE-AND-QUEUE is a two-member list. The first item should be
the place that the current value in the queue should be assigned
to upon each iteration. The second item should be a queue-item
belonging to the queue that must be iterated over.

BODY can be any form."
  (declare (indent defun))
  (let ((item-sym (gensym))
        (place (car place-and-queue))
        (queue (cadr place-and-queue)))
    `(let* ((,item-sym (phpinspect-queue-first ,queue)))
       (while ,item-sym
         (let ((,place (phpinspect-queue-item-value ,item-sym)))
           ,@body
           (setq ,item-sym (phpinspect-queue-item-next ,item-sym)))))))

(defun phpinspect-queue-find (queue value comparison-func)
  "Find VALUE in the queue that ITEM is part of using COMPARISON-FUNC."
  (catch 'found
    (phpinspect-doqueue (current-value queue)
      (when (funcall comparison-func current-value value)
        (throw 'found current-value)))))

(defun phpinspect-queue-enqueue-noduplicate (queue value comparison-func)
  (unless (phpinspect-queue-find queue value comparison-func)
    (phpinspect-queue-enqueue queue value)))

(provide 'phpinspect-queue)
;;; phpinspect-queue.el ends here