Rewritten. A poor choice of representation made the old code excessively
[emacs.git] / lisp / emacs-lisp / ring.el
blob28f568d17fd873c64f5f6682a54cd34638a8d42f
1 ;;; ring.el --- handle rings of items
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
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)
13 ;; any later version.
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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
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.
30 ;;;
31 ;;; In ring-ref, 0 is the index of the newest element. Higher indexes
32 ;;; correspond to older elements until they wrap.
33 ;;;
34 ;;; hd-index = index of the newest item on the ring.
35 ;;; length = number of ring items.
36 ;;;
37 ;;; These functions are used by the input history mechanism, but they can
38 ;;; be used for other purposes as well.
40 ;;; Code:
42 ;;;###autoload
43 (defun ring-p (x)
44 "Returns t if X is a ring; nil otherwise."
45 (and (consp x) (integerp (car x))
46 (consp (cdr x)) (integerp (car (cdr x)))
47 (vectorp (cdr (cdr x)))))
49 ;;;###autoload
50 (defun make-ring (size)
51 "Make a ring that can contain SIZE elements."
52 (cons 0 (cons 0 (make-vector size nil))))
54 (defun ring-plus1 (index veclen)
55 "INDEX+1, with wraparound"
56 (let ((new-index (+ index 1)))
57 (if (= new-index veclen) 0 new-index)))
59 (defun ring-minus1 (index veclen)
60 "INDEX-1, with wraparound"
61 (- (if (= 0 index) veclen index) 1))
63 (defun ring-length (ring)
64 "Number of elements in the ring."
65 (car (cdr ring)))
67 (defun ring-empty-p (ring)
68 (= 0 (car (cdr ring))))
70 (defun ring-index (index head ringlen veclen)
71 (setq index (ring-mod index ringlen))
72 (ring-mod (1- (+ head (- ringlen index))) veclen))
74 (defun ring-insert (ring item)
75 "Insert a new item onto the ring. If the ring is full, dump the oldest
76 item to make room."
77 (let* ((vec (cdr (cdr ring)))
78 (veclen (length vec))
79 (hd (car ring))
80 (ln (car (cdr ring))))
81 (prog1
82 (aset vec (ring-mod (+ hd ln) veclen) item)
83 (if (= ln veclen)
84 (setcar ring (ring-plus1 hd veclen))
85 (setcar (cdr ring) (1+ ln))))))
87 (defun ring-remove (ring &optional index)
88 "Remove an item from the RING. Return the removed item.
89 If optional INDEX is nil, remove the oldest item. If it's
90 numeric, remove the element indexed."
91 (if (ring-empty-p ring)
92 (error "Ring empty")
93 (let* ((hd (car ring))
94 (ln (car (cdr ring)))
95 (vec (cdr (cdr ring)))
96 (veclen (length vec))
97 (tl (ring-mod (1- (+ hd ln)) veclen))
98 oldelt)
99 (if (null index)
100 (setq index (1- ln)))
101 (setq index (ring-index index hd ln veclen))
102 (setq oldelt (aref vec index))
103 (while (/= index tl)
104 (aset vec index (aref vec (ring-plus1 index veclen)))
105 (setq index (ring-plus1 index veclen)))
106 (aset vec tl nil)
107 (setcar (cdr ring) (1- ln))
108 oldelt)))
110 (defun ring-mod (n m)
111 "Returns N mod M. M is positive.
112 Answer is guaranteed to be non-negative, and less than m."
113 (let ((n (% n m)))
114 (if (>= n 0) n
115 (+ n
116 (if (>= m 0) m (- m)))))) ; (abs m)
118 (defun ring-ref (ring index)
119 "Returns RING's INDEX element.
120 INDEX need not be <= the ring length, the appropriate modulo operation
121 will be performed. Element 0 is the most recently inserted; higher indices
122 correspond to older elements until they wrap."
123 (if (ring-empty-p ring)
124 (error "indexed empty ring")
125 (let* ((hd (car ring)) (ln (car (cdr ring))) (vec (cdr (cdr ring))))
126 (aref vec (ring-index index hd ln (length vec))))))
128 (provide 'ring)
130 ;;; ring.el ends here