Debian Codegraph @

src/ring-buffer.lisp

raw
1 ;;;; cl-heredoc - ring-buffer.lisp
2 ;;;; Copyright (C) 2010 Alexander Kahl <[email protected]>
3 ;;;; This file is part of cl-heredoc.
4 ;;;; cl-heredoc is free software; you can redistribute it and/or modify
5 ;;;; it under the terms of the GNU General Public License as published by
6 ;;;; the Free Software Foundation; either version 3 of the License, or
7 ;;;; (at your option) any later version.
8 ;;;;
9 ;;;; cl-heredoc is distributed in the hope that it will be useful,
10 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ;;;; GNU General Public License for more details.
13 ;;;;
14 ;;;; You should have received a copy of the GNU General Public License
15 ;;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 (in-package :cl-heredoc)
18
19 ;;; Operations on ring buffers, as described by Paul Graham in ANSI Common Lisp.
20 (defstruct ring-buffer
21 "Structure defining ring buffers utilizing a simple VECTOR of fixed size and
22 four indices:
23 START: Index of first live value
24 END: Index of last live value
25 USED: Beginning of current match
26 NEW: End of current match"
27 vector (start -1) (used -1) (new -1) (end -1))
28
29 (defun new-ring-buffer (length)
30 "new-ring-buffer length => ring-buffer
31
32 Create a new RING-BUFFER containing a simple character vector of fixed size
33 LENGTH."
34 (make-ring-buffer :vector (make-array length :element-type 'character)))
35
36 (defun rbref (buffer index)
37 "rbref buffer index => character or #\Nul
38
39 Return character stored at INDEX in ring BUFFER."
40 (char (ring-buffer-vector buffer)
41 (mod index (length (ring-buffer-vector buffer)))))
42
43 (defun (setf rbref) (value buffer index)
44 "setf (rbref buffer index) value => value
45
46 SETF for RBREF. If INDEX > LENGTH of BUFFER, start over at the beginning."
47 (setf (char (ring-buffer-vector buffer)
48 (mod index (length (ring-buffer-vector buffer))))
49 value))
50
51 (defun ring-buffer-insert (buffer value)
52 "ring-buffer-insert buffer value => value
53
54 Increment END of BUFFER inserting VALUE at the new index."
55 (setf (rbref buffer (incf (ring-buffer-end buffer)))
56 value))
57
58 (defun ring-buffer-reset (buffer)
59 "ring-buffer-reset buffer => end-index
60
61 Reset match beginning/end indices USED and NEW in BUFFER to START and END."
62 (setf (ring-buffer-used buffer) (ring-buffer-start buffer)
63 (ring-buffer-new buffer) (ring-buffer-end buffer)))
64
65 (defun ring-buffer-pop (buffer)
66 "ring-buffer-pop buffer => character
67
68 Increment START of BUFFER returning VALUE at the new index. Additionally, reset
69 the BUFFER match indices."
70 (prog1
71 (rbref buffer (incf (ring-buffer-start buffer)))
72 (ring-buffer-reset buffer)))
73
74 (defun ring-buffer-next (buffer)
75 "ring-buffer-next buffer => character or nil
76
77 Return next match character incrementing USED in BUFFER or simply NIL if none
78 are left."
79 (when (< (ring-buffer-used buffer) (ring-buffer-new buffer))
80 (rbref buffer (incf (ring-buffer-used buffer)))))
81
82 (defun ring-buffer-clear (buffer)
83 "ring-buffer-clear buffer => -1
84
85 Reset all indices of BUFFER to their initial state."
86 (setf (ring-buffer-start buffer) -1
87 (ring-buffer-used buffer) -1
88 (ring-buffer-new buffer) -1
89 (ring-buffer-end buffer) -1))
90
91 (defun ring-buffer-flush (buffer)
92 "ring-buffer-flush buffer => string
93
94 Flush all unused characters in BUFFER."
95 (with-output-to-string (out)
96 (do ((index (1+ (ring-buffer-used buffer)) (1+ index)))
97 ((> index (ring-buffer-end buffer)))
98 (write-char (rbref buffer index) out))))