annotate lisp/utils/ring.el @ 2:ac2d302a0011 r19-15b2

Import from CVS: tag r19-15b2
author cvs
date Mon, 13 Aug 2007 08:46:35 +0200
parents 376386a54a3c
children 2d532a89d707
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; ring.el --- handle rings of items
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Maintainer: FSF
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;; Keywords: extensions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
8 ;; This file is part of XEmacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
10 ;; XEmacs is free software; you can redistribute it and/or modify it
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
11 ;; under the terms of the GNU General Public License as published by
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
15 ;; XEmacs is distributed in the hope that it will be useful, but
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
18 ;; General Public License for more details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;; You should have received a copy of the GNU General Public License
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
23 ;; 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
25 ;;; Synched up with: FSF 19.34.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
29 ;; This code defines a ring data structure. A ring is a
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
30 ;; (hd-index length . vector)
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
31 ;; list. You can insert to, remove from, and rotate a ring. When the ring
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
32 ;; fills up, insertions cause the oldest elts to be quietly dropped.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
33 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
34 ;; In ring-ref, 0 is the index of the newest element. Higher indexes
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
35 ;; correspond to older elements until they wrap.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
36 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
37 ;; hd-index = index of the newest item on the ring.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
38 ;; length = number of ring items.
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
39 ;;
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
40 ;; These functions are used by the input history mechanism, but they can
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
41 ;; be used for other purposes as well.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
45 ;; XEmacs change
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (defun ringp (x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 "Returns t if X is a ring; nil otherwise."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (and (consp x) (integerp (car x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (consp (cdr x)) (integerp (car (cdr x)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (vectorp (cdr (cdr x)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
53 ;; XEmacs change
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (define-obsolete-function-alias 'ring-p 'ringp)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 ;;;###autoload
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 (defun make-ring (size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 "Make a ring that can contain SIZE elements."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (cons 0 (cons 0 (make-vector size nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (defun ring-insert-at-beginning (ring item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 "Add to RING the item ITEM. Add it at the front (the early end)."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (let* ((vec (cdr (cdr ring)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (veclen (length vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (hd (car ring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (ln (car (cdr ring))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (setq ln (min veclen (1+ ln))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 hd (ring-minus1 hd veclen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (aset vec hd item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (setcar ring hd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (setcar (cdr ring) ln)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (defun ring-plus1 (index veclen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 "INDEX+1, with wraparound"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (let ((new-index (+ index 1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (if (= new-index veclen) 0 new-index)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (defun ring-minus1 (index veclen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 "INDEX-1, with wraparound"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (- (if (= 0 index) veclen index) 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (defun ring-length (ring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 "Number of elements in the ring."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (car (cdr ring)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (defun ring-empty-p (ring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (= 0 (car (cdr ring))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (defun ring-index (index head ringlen veclen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (setq index (mod index ringlen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (mod (1- (+ head (- ringlen index))) veclen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (defun ring-insert (ring item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 "Insert onto ring RING the item ITEM, as the newest (last) item.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 If the ring is full, dump the oldest item to make room."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (let* ((vec (cdr (cdr ring)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (veclen (length vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (hd (car ring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (ln (car (cdr ring))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (prog1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (aset vec (mod (+ hd ln) veclen) item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (if (= ln veclen)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (setcar ring (ring-plus1 hd veclen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (setcar (cdr ring) (1+ ln))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (defun ring-remove (ring &optional index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 "Remove an item from the RING. Return the removed item.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 If optional INDEX is nil, remove the oldest item. If it's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 numeric, remove the element indexed."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (if (ring-empty-p ring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (error "Ring empty")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (let* ((hd (car ring))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (ln (car (cdr ring)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (vec (cdr (cdr ring)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (veclen (length vec))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (tl (mod (1- (+ hd ln)) veclen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 oldelt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (if (null index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (setq index (1- ln)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (setq index (ring-index index hd ln veclen))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (setq oldelt (aref vec index))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (while (/= index tl)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (aset vec index (aref vec (ring-plus1 index veclen)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (setq index (ring-plus1 index veclen)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (aset vec tl nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (setcar (cdr ring) (1- ln))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 oldelt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (defun ring-ref (ring index)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 "Returns RING's INDEX element.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 INDEX need not be <= the ring length, the appropriate modulo operation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 will be performed. Element 0 is the most recently inserted; higher indices
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 correspond to older elements until they wrap."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (if (ring-empty-p ring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (error "indexed empty ring")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (let* ((hd (car ring)) (ln (car (cdr ring))) (vec (cdr (cdr ring))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (aref vec (ring-index index hd ln (length vec))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (provide 'ring)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 ;;; ring.el ends here