Add 2011 to FSF/AIST copyright years.
[emacs.git] / lisp / emacs-lisp / ring.el
blob91ea5a93aabc24430d9ddf2b4a10cf0fe4db7396
1 ;;; ring.el --- handle rings of items
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This code defines a ring data structure. A ring is a
27 ;; (hd-index length . vector)
28 ;; list. You can insert to, remove from, and rotate a ring. When the ring
29 ;; fills up, insertions cause the oldest elts to be quietly dropped.
31 ;; In ring-ref, 0 is the index of the newest element. Higher indexes
32 ;; correspond to older elements; when the index equals the ring length,
33 ;; it wraps to the newest element again.
35 ;; hd-index = vector index of the oldest ring item.
36 ;; Newer items follow this item; at the end of the vector,
37 ;; they wrap around to the start of the vector.
38 ;; length = number of items currently in the ring.
39 ;; This never exceeds the length of the vector itself.
41 ;; These functions are used by the input history mechanism, but they can
42 ;; be used for other purposes as well.
44 ;;; Code:
46 ;;; User Functions:
48 ;;;###autoload
49 (defun ring-p (x)
50 "Return t if X is a ring; nil otherwise."
51 (and (consp x) (integerp (car x))
52 (consp (cdr x)) (integerp (cadr x))
53 (vectorp (cddr x))))
55 ;;;###autoload
56 (defun make-ring (size)
57 "Make a ring that can contain SIZE elements."
58 (cons 0 (cons 0 (make-vector size nil))))
60 (defun ring-insert-at-beginning (ring item)
61 "Add to RING the item ITEM, at the front, as the oldest item."
62 (let* ((vec (cddr ring))
63 (veclen (length vec))
64 (hd (car ring))
65 (ln (cadr ring)))
66 (setq ln (min veclen (1+ ln))
67 hd (ring-minus1 hd veclen))
68 (aset vec hd item)
69 (setcar ring hd)
70 (setcar (cdr ring) ln)))
72 (defun ring-plus1 (index veclen)
73 "Return INDEX+1, with wraparound."
74 (let ((new-index (1+ index)))
75 (if (= new-index veclen) 0 new-index)))
77 (defun ring-minus1 (index veclen)
78 "Return INDEX-1, with wraparound."
79 (- (if (zerop index) veclen index) 1))
81 (defun ring-length (ring)
82 "Return the number of elements in the RING."
83 (cadr ring))
85 (defun ring-index (index head ringlen veclen)
86 "Convert nominal ring index INDEX to an internal index.
87 The internal index refers to the items ordered from newest to oldest.
88 HEAD is the index of the oldest element in the ring.
89 RINGLEN is the number of elements currently in the ring.
90 VECLEN is the size of the vector in the ring."
91 (setq index (mod index ringlen))
92 (mod (1- (+ head (- ringlen index))) veclen))
94 (defun ring-empty-p (ring)
95 "Return t if RING is empty; nil otherwise."
96 (zerop (cadr ring)))
98 (defun ring-size (ring)
99 "Return the size of RING, the maximum number of elements it can contain."
100 (length (cddr ring)))
102 (defun ring-copy (ring)
103 "Return a copy of RING."
104 (let ((vec (cddr ring))
105 (hd (car ring))
106 (ln (cadr ring)))
107 (cons hd (cons ln (copy-sequence vec)))))
109 (defun ring-insert (ring item)
110 "Insert onto ring RING the item ITEM, as the newest (last) item.
111 If the ring is full, dump the oldest item to make room."
112 (let* ((vec (cddr ring))
113 (veclen (length vec))
114 (hd (car ring))
115 (ln (cadr ring)))
116 (prog1
117 (aset vec (mod (+ hd ln) veclen) item)
118 (if (= ln veclen)
119 (setcar ring (ring-plus1 hd veclen))
120 (setcar (cdr ring) (1+ ln))))))
122 (defun ring-remove (ring &optional index)
123 "Remove an item from the RING. Return the removed item.
124 If optional INDEX is nil, remove the oldest item. If it's
125 numeric, remove the element indexed."
126 (if (ring-empty-p ring)
127 (error "Ring empty")
128 (let* ((hd (car ring))
129 (ln (cadr ring))
130 (vec (cddr ring))
131 (veclen (length vec))
132 (tl (mod (1- (+ hd ln)) veclen))
133 oldelt)
134 (when (null index)
135 (setq index (1- ln)))
136 (setq index (ring-index index hd ln veclen))
137 (setq oldelt (aref vec index))
138 (while (/= index tl)
139 (aset vec index (aref vec (ring-plus1 index veclen)))
140 (setq index (ring-plus1 index veclen)))
141 (aset vec tl nil)
142 (setcar (cdr ring) (1- ln))
143 oldelt)))
145 (defun ring-ref (ring index)
146 "Return RING's INDEX element.
147 INDEX = 0 is the most recently inserted; higher indices
148 correspond to older elements.
149 INDEX need not be <= the ring length; the appropriate modulo operation
150 will be performed."
151 (if (ring-empty-p ring)
152 (error "Accessing an empty ring")
153 (let ((hd (car ring))
154 (ln (cadr ring))
155 (vec (cddr ring)))
156 (aref vec (ring-index index hd ln (length vec))))))
158 (defun ring-elements (ring)
159 "Return a list of the elements of RING, in order, newest first."
160 (let ((start (car ring))
161 (size (ring-size ring))
162 (vect (cddr ring))
163 lst)
164 (dotimes (var (cadr ring) lst)
165 (push (aref vect (mod (+ start var) size)) lst))))
167 (defun ring-member (ring item)
168 "Return index of ITEM if on RING, else nil.
169 Comparison is done via `equal'. The index is 0-based."
170 (catch 'found
171 (dotimes (ind (ring-length ring) nil)
172 (when (equal item (ring-ref ring ind))
173 (throw 'found ind)))))
175 (defun ring-next (ring item)
176 "Return the next item in the RING, after ITEM.
177 Raise error if ITEM is not in the RING."
178 (let ((curr-index (ring-member ring item)))
179 (unless curr-index (error "Item is not in the ring: `%s'" item))
180 (ring-ref ring (ring-plus1 curr-index (ring-length ring)))))
182 (defun ring-previous (ring item)
183 "Return the previous item in the RING, before ITEM.
184 Raise error if ITEM is not in the RING."
185 (let ((curr-index (ring-member ring item)))
186 (unless curr-index (error "Item is not in the ring: `%s'" item))
187 (ring-ref ring (ring-minus1 curr-index (ring-length ring)))))
189 (defun ring-insert+extend (ring item &optional grow-p)
190 "Like `ring-insert', but if GROW-P is non-nil, then enlarge ring.
191 Insert onto ring RING the item ITEM, as the newest (last) item.
192 If the ring is full, behavior depends on GROW-P:
193 If GROW-P is non-nil, enlarge the ring to accommodate the new item.
194 If GROW-P is nil, dump the oldest item to make room for the new."
195 (let* ((vec (cddr ring))
196 (veclen (length vec))
197 (hd (car ring))
198 (ringlen (ring-length ring)))
199 (prog1
200 (cond ((and grow-p (= ringlen veclen)) ; Full ring. Enlarge it.
201 (setq veclen (1+ veclen))
202 (setcdr ring (cons (setq ringlen (1+ ringlen))
203 (setq vec (vconcat vec (vector item)))))
204 (setcar ring hd))
205 (t (aset vec (mod (+ hd ringlen) veclen) item)))
206 (if (= ringlen veclen)
207 (setcar ring (ring-plus1 hd veclen))
208 (setcar (cdr ring) (1+ ringlen))))))
210 (defun ring-remove+insert+extend (ring item &optional grow-p)
211 "`ring-remove' ITEM from RING, then `ring-insert+extend' it.
212 This ensures that there is only one ITEM on RING.
214 If the RING is full, behavior depends on GROW-P:
215 If GROW-P is non-nil, enlarge the ring to accommodate the new ITEM.
216 If GROW-P is nil, dump the oldest item to make room for the new."
217 (let (ind)
218 (while (setq ind (ring-member ring item))
219 (ring-remove ring ind)))
220 (ring-insert+extend ring item grow-p))
222 (defun ring-convert-sequence-to-ring (seq)
223 "Convert sequence SEQ to a ring. Return the ring.
224 If SEQ is already a ring, return it."
225 (if (ring-p seq)
227 (let* ((size (length seq))
228 (ring (make-ring size)))
229 (dotimes (count size)
230 (when (or (ring-empty-p ring)
231 (not (equal (ring-ref ring 0) (elt seq count))))
232 (ring-insert-at-beginning ring (elt seq count))))
233 ring)))
235 ;;; provide ourself:
237 (provide 'ring)
239 ;; arch-tag: e707682b-ed69-47c9-b20f-cf2c68cc92d2
240 ;;; ring.el ends here