annotate lisp/utils/ring.el @ 0:376386a54a3c r19-14

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