(debug): Fix arg to backtrace-debug for debug-on-entry.
[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
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
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.
39
40 ;;; Code:
41
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)))))
48
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))))
53
54 (defun ring-insert-at-beginning (ring item)
55 "Add to RING the item ITEM. Add it at the front (the early end)."
56 (let* ((vec (cdr (cdr ring)))
57 (veclen (length vec))
58 (hd (car ring))
59 (ln (car (cdr ring))))
60 (setq ln (min veclen (1+ ln))
61 hd (ring-minus1 hd veclen))
62 (aset vec hd item)
63 (setcar ring hd)
64 (setcar (cdr ring) ln)))
65
66 (defun ring-plus1 (index veclen)
67 "INDEX+1, with wraparound"
68 (let ((new-index (+ index 1)))
69 (if (= new-index veclen) 0 new-index)))
70
71 (defun ring-minus1 (index veclen)
72 "INDEX-1, with wraparound"
73 (- (if (= 0 index) veclen index) 1))
74
75 (defun ring-length (ring)
76 "Number of elements in the ring."
77 (car (cdr ring)))
78
79 (defun ring-empty-p (ring)
80 (= 0 (car (cdr ring))))
81
82 (defun ring-index (index head ringlen veclen)
83 (setq index (mod index ringlen))
84 (mod (1- (+ head (- ringlen index))) veclen))
85
86 (defun ring-insert (ring item)
87 "Insert onto ring RING the item ITEM, as the newest (last) item.
88 If the ring is full, dump the oldest item to make room."
89 (let* ((vec (cdr (cdr ring)))
90 (veclen (length vec))
91 (hd (car ring))
92 (ln (car (cdr ring))))
93 (prog1
94 (aset vec (mod (+ hd ln) veclen) item)
95 (if (= ln veclen)
96 (setcar ring (ring-plus1 hd veclen))
97 (setcar (cdr ring) (1+ ln))))))
98
99 (defun ring-remove (ring &optional index)
100 "Remove an item from the RING. Return the removed item.
101 If optional INDEX is nil, remove the oldest item. If it's
102 numeric, remove the element indexed."
103 (if (ring-empty-p ring)
104 (error "Ring empty")
105 (let* ((hd (car ring))
106 (ln (car (cdr ring)))
107 (vec (cdr (cdr ring)))
108 (veclen (length vec))
109 (tl (mod (1- (+ hd ln)) veclen))
110 oldelt)
111 (if (null index)
112 (setq index (1- ln)))
113 (setq index (ring-index index hd ln veclen))
114 (setq oldelt (aref vec index))
115 (while (/= index tl)
116 (aset vec index (aref vec (ring-plus1 index veclen)))
117 (setq index (ring-plus1 index veclen)))
118 (aset vec tl nil)
119 (setcar (cdr ring) (1- ln))
120 oldelt)))
121
122 (defun ring-ref (ring index)
123 "Returns RING's INDEX element.
124 INDEX need not be <= the ring length, the appropriate modulo operation
125 will be performed. Element 0 is the most recently inserted; higher indices
126 correspond to older elements until they wrap."
127 (if (ring-empty-p ring)
128 (error "indexed empty ring")
129 (let* ((hd (car ring)) (ln (car (cdr ring))) (vec (cdr (cdr ring))))
130 (aref vec (ring-index index hd ln (length vec))))))
131
132 (provide 'ring)
133
134 ;;; ring.el ends here