aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEric S. Raymond1993-04-25 06:14:03 +0000
committerEric S. Raymond1993-04-25 06:14:03 +0000
commit41dc743ded9ab4a149804d2f0ce3c6758c121cdb (patch)
treebfe17938dcd707e5147c61b919bc16c9705e00e6
parent42d5c01e21ec50a4413e6b7d266486b78529d807 (diff)
downloademacs-41dc743ded9ab4a149804d2f0ce3c6758c121cdb.tar.gz
emacs-41dc743ded9ab4a149804d2f0ce3c6758c121cdb.zip
Added and fixed documentation.
(ring-rotate): Nuked. It was (a) unused, and (b) totally broken (as in, any attempt to use it died with a type error, and when I patched it to fix that I found its algorithm was broken). (ring-ref): Added doc string.
-rw-r--r--lisp/emacs-lisp/ring.el42
1 files changed, 12 insertions, 30 deletions
diff --git a/lisp/emacs-lisp/ring.el b/lisp/emacs-lisp/ring.el
index 2fc5e22caed..eedc801e16a 100644
--- a/lisp/emacs-lisp/ring.el
+++ b/lisp/emacs-lisp/ring.el
@@ -28,6 +28,9 @@
28;;; list. You can insert to, remove from, and rotate a ring. When the ring 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. 29;;; fills up, insertions cause the oldest elts to be quietly dropped.
30;;; 30;;;
31;;; In ring-ref, 0 is the index of the newest element. Higher indexes
32;;; correspond to older elements until they wrap.
33;;;
31;;; HEAD = index of the newest item on the ring. 34;;; HEAD = index of the newest item on the ring.
32;;; TAIL = index of the oldest item on the ring. 35;;; TAIL = index of the oldest item on the ring.
33;;; 36;;;
@@ -36,18 +39,16 @@
36 39
37;;; Code: 40;;; Code:
38 41
39(provide 'ring)
40
41;;;###autoload 42;;;###autoload
42(defun ring-p (x) 43(defun ring-p (x)
43 "T if X is a ring; NIL otherwise." 44 "Returns t if X is a ring; nil otherwise."
44 (and (consp x) (integerp (car x)) 45 (and (consp x) (integerp (car x))
45 (consp (cdr x)) (integerp (car (cdr x))) 46 (consp (cdr x)) (integerp (car (cdr x)))
46 (vectorp (cdr (cdr x))))) 47 (vectorp (cdr (cdr x)))))
47 48
48;;;###autoload 49;;;###autoload
49(defun make-ring (size) 50(defun make-ring (size)
50 "Make a ring that can contain SIZE elts." 51 "Make a ring that can contain SIZE elements."
51 (cons 1 (cons 0 (make-vector (+ size 1) nil)))) 52 (cons 1 (cons 0 (make-vector (+ size 1) nil))))
52 53
53(defun ring-plus1 (index veclen) 54(defun ring-plus1 (index veclen)
@@ -60,7 +61,7 @@
60 (- (if (= 0 index) veclen index) 1)) 61 (- (if (= 0 index) veclen index) 1))
61 62
62(defun ring-length (ring) 63(defun ring-length (ring)
63 "Number of elts in the ring." 64 "Number of elements in the ring."
64 (let ((hd (car ring)) (tl (car (cdr ring))) (siz (length (cdr (cdr ring))))) 65 (let ((hd (car ring)) (tl (car (cdr ring))) (siz (length (cdr (cdr ring)))))
65 (let ((len (if (<= hd tl) (+ 1 (- tl hd)) (+ 1 tl (- siz hd))))) 66 (let ((len (if (<= hd tl) (+ 1 (- tl hd)) (+ 1 tl (- siz hd)))))
66 (if (= len siz) 0 len)))) 67 (if (= len siz) 0 len))))
@@ -85,31 +86,6 @@ item to make room."
85 (setcar (cdr ring) (ring-minus1 tl (length vec))) 86 (setcar (cdr ring) (ring-minus1 tl (length vec)))
86 (aref vec tl)))) 87 (aref vec tl))))
87 88
88;;; This isn't actually used in this package. I just threw it in in case
89;;; someone else wanted it. If you want rotating-ring behavior on your history
90;;; retrieval (analagous to kill ring behavior), this function is what you
91;;; need. I should write the yank-input and yank-pop-input-or-kill to go with
92;;; this, and not bind it to a key by default, so it would be available to
93;;; people who want to bind it to a key. But who would want it? Blech.
94(defun ring-rotate (ring n)
95 (if (not (= n 0))
96 (if (ring-empty-p ring) ;Is this the right error check?
97 (error "ring empty")
98 (let ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring))))
99 (let ((len (length vec)))
100 (while (> n 0)
101 (setq tl (ring-plus1 tl len))
102 (aset ring tl (aref ring hd))
103 (setq hd (ring-plus1 hd len))
104 (setq n (- n 1)))
105 (while (< n 0)
106 (setq hd (ring-minus1 hd len))
107 (aset vec hd (aref vec tl))
108 (setq tl (ring-minus1 tl len))
109 (setq n (- n 1))))
110 (setcar ring hd)
111 (setcar (cdr ring) tl)))))
112
113(defun ring-mod (n m) 89(defun ring-mod (n m)
114 "Returns N mod M. M is positive. 90 "Returns N mod M. M is positive.
115Answer is guaranteed to be non-negative, and less than m." 91Answer is guaranteed to be non-negative, and less than m."
@@ -119,6 +95,10 @@ Answer is guaranteed to be non-negative, and less than m."
119 (if (>= m 0) m (- m)))))) ; (abs m) 95 (if (>= m 0) m (- m)))))) ; (abs m)
120 96
121(defun ring-ref (ring index) 97(defun ring-ref (ring index)
98 "Returns RING's INDEX element.
99INDEX need not be <= the ring length, the appropriate modulo operation
100will be performed. Element 0 is the most recently inserted; higher indices
101correspond to older elements until they wrap."
122 (let ((numelts (ring-length ring))) 102 (let ((numelts (ring-length ring)))
123 (if (= numelts 0) (error "indexed empty ring") 103 (if (= numelts 0) (error "indexed empty ring")
124 (let* ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring))) 104 (let* ((hd (car ring)) (tl (car (cdr ring))) (vec (cdr (cdr ring)))
@@ -126,4 +106,6 @@ Answer is guaranteed to be non-negative, and less than m."
126 (vec-index (ring-mod (+ index hd) (length vec)))) 106 (vec-index (ring-mod (+ index hd) (length vec))))
127 (aref vec vec-index))))) 107 (aref vec vec-index)))))
128 108
109(provide 'ring)
110
129;;; ring.el ends here 111;;; ring.el ends here