1 ;;; fifo.el --- FIFO queue. |
|
2 |
|
3 ;; Copyright (c) 2015 Chris Done. |
|
4 |
|
5 ;; This file is free software; you can redistribute it and/or modify |
|
6 ;; it under the terms of the GNU General Public License as published by |
|
7 ;; the Free Software Foundation; either version 3, or (at your option) |
|
8 ;; any later version. |
|
9 |
|
10 ;; This file is distributed in the hope that it will be useful, |
|
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 ;; GNU General Public License for more details. |
|
14 |
|
15 ;; You should have received a copy of the GNU General Public License |
|
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 |
|
18 ;;; Code: |
|
19 |
|
20 (require 'cl-lib) |
|
21 |
|
22 (defun fifo-make () |
|
23 "Make a fifo queue." |
|
24 (cons 0 nil)) |
|
25 |
|
26 (defun fifo-push (q a) |
|
27 "Push a new item onto the queue." |
|
28 (cl-assert (consp q) nil "Must be a queue.") |
|
29 (setcar q (1+ (car q))) |
|
30 (let ((next q) |
|
31 (continue t)) |
|
32 (while (not (null (cdr next))) |
|
33 (setq next (cdr next))) |
|
34 (setcdr next (cons a nil))) |
|
35 q) |
|
36 |
|
37 (defun fifo-pop (q) |
|
38 "Pop the next item on the queue." |
|
39 (cl-assert (consp q) nil "Must be a queue.") |
|
40 (cl-assert (consp (cdr q)) nil "No items to pop from queue.") |
|
41 (setcar q (1- (car q))) |
|
42 (let ((a (car (cdr q)))) |
|
43 (setcdr q (cdr (cdr q))) |
|
44 a)) |
|
45 |
|
46 (defun fifo-size (q) |
|
47 "Get the size of the queue." |
|
48 (cl-assert (consp q) nil "Must be a queue.") |
|
49 (car q)) |
|
50 |
|
51 (defun fifo-null-p (q) |
|
52 "Is the queue empty?" |
|
53 (= (fifo-size q) 0)) |
|
54 |
|
55 (provide 'fifo) |
|