1 ;;; ring.el --- handle rings of items
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
6 ;; Keywords: extensions
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; This code defines a ring data structure. A ring is a
28 ;; (hd-index length . vector)
29 ;; list. You can insert to, remove from, and rotate a ring. When the ring
30 ;; fills up, insertions cause the oldest elts to be quietly dropped.
32 ;; In ring-ref, 0 is the index of the newest element. Higher indexes
33 ;; correspond to older elements until they wrap.
35 ;; hd-index = index of the newest item on the ring.
36 ;; length = number of ring items.
38 ;; These functions are used by the input history mechanism, but they can
39 ;; be used for other purposes as well.
45 "Returns t if X is a ring; nil otherwise."
46 (and (consp x
) (integerp (car x
))
47 (consp (cdr x
)) (integerp (car (cdr x
)))
48 (vectorp (cdr (cdr x
)))))
51 (defun make-ring (size)
52 "Make a ring that can contain SIZE elements."
53 (cons 0 (cons 0 (make-vector size nil
))))
55 (defun ring-insert-at-beginning (ring item
)
56 "Add to RING the item ITEM. Add it at the front (the early end)."
57 (let* ((vec (cdr (cdr ring
)))
60 (ln (car (cdr ring
))))
61 (setq ln
(min veclen
(1+ ln
))
62 hd
(ring-minus1 hd veclen
))
65 (setcar (cdr ring
) ln
)))
67 (defun ring-plus1 (index veclen
)
68 "INDEX+1, with wraparound"
69 (let ((new-index (+ index
1)))
70 (if (= new-index veclen
) 0 new-index
)))
72 (defun ring-minus1 (index veclen
)
73 "INDEX-1, with wraparound"
74 (- (if (= 0 index
) veclen index
) 1))
76 (defun ring-length (ring)
77 "Number of elements in the ring."
80 (defun ring-empty-p (ring)
81 (= 0 (car (cdr ring
))))
83 (defun ring-index (index head ringlen veclen
)
84 (setq index
(mod index ringlen
))
85 (mod (1- (+ head
(- ringlen index
))) veclen
))
87 (defun ring-insert (ring item
)
88 "Insert onto ring RING the item ITEM, as the newest (last) item.
89 If the ring is full, dump the oldest item to make room."
90 (let* ((vec (cdr (cdr ring
)))
93 (ln (car (cdr ring
))))
95 (aset vec
(mod (+ hd ln
) veclen
) item
)
97 (setcar ring
(ring-plus1 hd veclen
))
98 (setcar (cdr ring
) (1+ ln
))))))
100 (defun ring-remove (ring &optional index
)
101 "Remove an item from the RING. Return the removed item.
102 If optional INDEX is nil, remove the oldest item. If it's
103 numeric, remove the element indexed."
104 (if (ring-empty-p ring
)
106 (let* ((hd (car ring
))
107 (ln (car (cdr ring
)))
108 (vec (cdr (cdr ring
)))
109 (veclen (length vec
))
110 (tl (mod (1- (+ hd ln
)) veclen
))
113 (setq index
(1- ln
)))
114 (setq index
(ring-index index hd ln veclen
))
115 (setq oldelt
(aref vec index
))
117 (aset vec index
(aref vec
(ring-plus1 index veclen
)))
118 (setq index
(ring-plus1 index veclen
)))
120 (setcar (cdr ring
) (1- ln
))
123 (defun ring-ref (ring index
)
124 "Returns RING's INDEX element.
125 INDEX need not be <= the ring length, the appropriate modulo operation
126 will be performed. Element 0 is the most recently inserted; higher indices
127 correspond to older elements until they wrap."
128 (if (ring-empty-p ring
)
129 (error "indexed empty ring")
130 (let* ((hd (car ring
)) (ln (car (cdr ring
))) (vec (cdr (cdr ring
))))
131 (aref vec
(ring-index index hd ln
(length vec
))))))
135 ;;; ring.el ends here