]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/ring.el
(eldoc-function-argstring-from-docstring): Add search that finds arglist
[gnu-emacs] / 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 until they wrap.
34 ;;
35 ;; hd-index = index of the newest item on the ring.
36 ;; length = number of ring items.
37 ;;
38 ;; These functions are used by the input history mechanism, but they can
39 ;; be used for other purposes as well.
40
41 ;;; Code:
42
43 ;;;###autoload
44 (defun ring-p (x)
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)))))
49
50 ;;;###autoload
51 (defun make-ring (size)
52 "Make a ring that can contain SIZE elements."
53 (cons 0 (cons 0 (make-vector size nil))))
54
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)))
58 (veclen (length vec))
59 (hd (car ring))
60 (ln (car (cdr ring))))
61 (setq ln (min veclen (1+ ln))
62 hd (ring-minus1 hd veclen))
63 (aset vec hd item)
64 (setcar ring hd)
65 (setcar (cdr ring) ln)))
66
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)))
71
72 (defun ring-minus1 (index veclen)
73 "INDEX-1, with wraparound"
74 (- (if (= 0 index) veclen index) 1))
75
76 (defun ring-length (ring)
77 "Number of elements in the ring."
78 (car (cdr ring)))
79
80 (defun ring-empty-p (ring)
81 (= 0 (car (cdr ring))))
82
83 (defun ring-index (index head ringlen veclen)
84 (setq index (mod index ringlen))
85 (mod (1- (+ head (- ringlen index))) veclen))
86
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)))
91 (veclen (length vec))
92 (hd (car ring))
93 (ln (car (cdr ring))))
94 (prog1
95 (aset vec (mod (+ hd ln) veclen) item)
96 (if (= ln veclen)
97 (setcar ring (ring-plus1 hd veclen))
98 (setcar (cdr ring) (1+ ln))))))
99
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)
105 (error "Ring empty")
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))
111 oldelt)
112 (if (null index)
113 (setq index (1- ln)))
114 (setq index (ring-index index hd ln veclen))
115 (setq oldelt (aref vec index))
116 (while (/= index tl)
117 (aset vec index (aref vec (ring-plus1 index veclen)))
118 (setq index (ring-plus1 index veclen)))
119 (aset vec tl nil)
120 (setcar (cdr ring) (1- ln))
121 oldelt)))
122
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))))))
132
133 (provide 'ring)
134
135 ;;; ring.el ends here