Doc fixes.
[bpt/emacs.git] / lisp / emacs-lisp / ring.el
1 ;;; ring.el --- handle rings of items
2
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: extensions
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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.
24
25 ;;; Commentary:
26
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.
31 ;;
32 ;; In ring-ref, 0 is the index of the newest element. Higher indexes
33 ;; correspond to older elements; when the index equals the ring length,
34 ;; it wraps to the newest element again.
35 ;;
36 ;; hd-index = vector index of the oldest ring item.
37 ;; Newer items follow this item; at the end of the vector,
38 ;; they wrap around to the start of the vector.
39 ;; length = number of items currently in the ring.
40 ;; This never exceeds the length of the vector itself.
41 ;;
42 ;; These functions are used by the input history mechanism, but they can
43 ;; be used for other purposes as well.
44
45 ;;; Code:
46
47 ;;;###autoload
48 (defun ring-p (x)
49 "Returns t if X is a ring; nil otherwise."
50 (and (consp x) (integerp (car x))
51 (consp (cdr x)) (integerp (car (cdr x)))
52 (vectorp (cdr (cdr x)))))
53
54 ;;;###autoload
55 (defun make-ring (size)
56 "Make a ring that can contain SIZE elements."
57 (cons 0 (cons 0 (make-vector size nil))))
58
59 (defun ring-insert-at-beginning (ring item)
60 "Add to RING the item ITEM. Add it at the front, as the oldest item."
61 (let* ((vec (cdr (cdr ring)))
62 (veclen (length vec))
63 (hd (car ring))
64 (ln (car (cdr ring))))
65 (setq ln (min veclen (1+ ln))
66 hd (ring-minus1 hd veclen))
67 (aset vec hd item)
68 (setcar ring hd)
69 (setcar (cdr ring) ln)))
70
71 (defun ring-plus1 (index veclen)
72 "INDEX+1, with wraparound."
73 (let ((new-index (+ index 1)))
74 (if (= new-index veclen) 0 new-index)))
75
76 (defun ring-minus1 (index veclen)
77 "INDEX-1, with wraparound."
78 (- (if (= 0 index) veclen index) 1))
79
80 (defun ring-length (ring)
81 "Number of elements in the ring RING."
82 (car (cdr ring)))
83
84 (defun ring-empty-p (ring)
85 (= 0 (car (cdr ring))))
86
87 (defun ring-index (index head ringlen veclen)
88 (setq index (mod index ringlen))
89 (mod (1- (+ head (- ringlen index))) veclen))
90
91 (defun ring-insert (ring item)
92 "Insert onto ring RING the item ITEM, as the newest (last) item.
93 If the ring is full, dump the oldest item to make room."
94 (let* ((vec (cdr (cdr ring)))
95 (veclen (length vec))
96 (hd (car ring))
97 (ln (car (cdr ring))))
98 (prog1
99 (aset vec (mod (+ hd ln) veclen) item)
100 (if (= ln veclen)
101 (setcar ring (ring-plus1 hd veclen))
102 (setcar (cdr ring) (1+ ln))))))
103
104 (defun ring-remove (ring &optional index)
105 "Remove an item from the RING. Return the removed item.
106 If optional INDEX is nil, remove the oldest item. If it's
107 numeric, remove the element indexed."
108 (if (ring-empty-p ring)
109 (error "Ring empty")
110 (let* ((hd (car ring))
111 (ln (car (cdr ring)))
112 (vec (cdr (cdr ring)))
113 (veclen (length vec))
114 (tl (mod (1- (+ hd ln)) veclen))
115 oldelt)
116 (if (null index)
117 (setq index (1- ln)))
118 (setq index (ring-index index hd ln veclen))
119 (setq oldelt (aref vec index))
120 (while (/= index tl)
121 (aset vec index (aref vec (ring-plus1 index veclen)))
122 (setq index (ring-plus1 index veclen)))
123 (aset vec tl nil)
124 (setcar (cdr ring) (1- ln))
125 oldelt)))
126
127 (defun ring-ref (ring index)
128 "Returns RING's INDEX element.
129 INDEX = 0 is the most recently inserted; higher indices
130 correspond to older elements.
131 INDEX need not be <= the ring length, the appropriate modulo operation
132 will be performed."
133 (if (ring-empty-p ring)
134 (error "Accessing an empty ring")
135 (let* ((hd (car ring)) (ln (car (cdr ring))) (vec (cdr (cdr ring))))
136 (aref vec (ring-index index hd ln (length vec))))))
137
138 (provide 'ring)
139
140 ;;; ring.el ends here