Mercurial > emacs
annotate lisp/emacs-lisp/ring.el @ 3726:33e82e88a210
* xdisp.c (display_text_line): Use temporary variable.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Mon, 14 Jun 1993 15:06:46 +0000 |
parents | e01048f32511 |
children | 95b9760d19e3 |
rev | line source |
---|---|
2424
dbdccee84df3
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
905
diff
changeset
|
1 ;;; ring.el --- handle rings of items |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
3 ;; Copyright (C) 1992 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
811
diff
changeset
|
4 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
5 ;; Maintainer: FSF |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
6 ;; Keywords: extensions |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
7 |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
8 ;; This file is part of GNU Emacs. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
9 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
11 ;; it under the terms of the GNU General Public License as published by |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
12 ;; the Free Software Foundation; either version 2, or (at your option) |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
13 ;; any later version. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
14 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
15 ;; GNU Emacs is distributed in the hope that it will be useful, |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
18 ;; GNU General Public License for more details. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
19 |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
20 ;; You should have received a copy of the GNU General Public License |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
23 |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
24 ;;; Commentary: |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
25 |
124 | 26 ;;; This code defines a ring data structure. A ring is a |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
27 ;;; (hd-index length . vector) |
124 | 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 ;;; | |
2574
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
31 ;;; In ring-ref, 0 is the index of the newest element. Higher indexes |
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
32 ;;; correspond to older elements until they wrap. |
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
33 ;;; |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
34 ;;; hd-index = index of the newest item on the ring. |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
35 ;;; length = number of ring items. |
124 | 36 ;;; |
37 ;;; These functions are used by the input history mechanism, but they can | |
38 ;;; be used for other purposes as well. | |
39 | |
811
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
40 ;;; Code: |
e694e0879463
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
658
diff
changeset
|
41 |
905 | 42 ;;;###autoload |
124 | 43 (defun ring-p (x) |
2574
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
44 "Returns t if X is a ring; nil otherwise." |
124 | 45 (and (consp x) (integerp (car x)) |
46 (consp (cdr x)) (integerp (car (cdr x))) | |
47 (vectorp (cdr (cdr x))))) | |
48 | |
905 | 49 ;;;###autoload |
124 | 50 (defun make-ring (size) |
2574
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
51 "Make a ring that can contain SIZE elements." |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
52 (cons 0 (cons 0 (make-vector size nil)))) |
124 | 53 |
54 (defun ring-plus1 (index veclen) | |
55 "INDEX+1, with wraparound" | |
56 (let ((new-index (+ index 1))) | |
57 (if (= new-index veclen) 0 new-index))) | |
58 | |
59 (defun ring-minus1 (index veclen) | |
60 "INDEX-1, with wraparound" | |
61 (- (if (= 0 index) veclen index) 1)) | |
62 | |
63 (defun ring-length (ring) | |
2574
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
64 "Number of elements in the ring." |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
65 (car (cdr ring))) |
124 | 66 |
67 (defun ring-empty-p (ring) | |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
68 (= 0 (car (cdr ring)))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
69 |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
70 (defun ring-index (index head ringlen veclen) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
71 (setq index (ring-mod index ringlen)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
72 (ring-mod (1- (+ head (- ringlen index))) veclen)) |
124 | 73 |
74 (defun ring-insert (ring item) | |
75 "Insert a new item onto the ring. If the ring is full, dump the oldest | |
76 item to make room." | |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
77 (let* ((vec (cdr (cdr ring))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
78 (veclen (length vec)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
79 (hd (car ring)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
80 (ln (car (cdr ring)))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
81 (prog1 |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
82 (aset vec (ring-mod (+ hd ln) veclen) item) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
83 (if (= ln veclen) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
84 (setcar ring (ring-plus1 hd veclen)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
85 (setcar (cdr ring) (1+ ln)))))) |
124 | 86 |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
87 (defun ring-remove (ring &optional index) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
88 "Remove an item from the RING. Return the removed item. |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
89 If optional INDEX is nil, remove the oldest item. If it's |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
90 numeric, remove the element indexed." |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
91 (if (ring-empty-p ring) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
92 (error "Ring empty") |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
93 (let* ((hd (car ring)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
94 (ln (car (cdr ring))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
95 (vec (cdr (cdr ring))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
96 (veclen (length vec)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
97 (tl (ring-mod (1- (+ hd ln)) veclen)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
98 oldelt) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
99 (if (null index) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
100 (setq index (1- ln))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
101 (setq index (ring-index index hd ln veclen)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
102 (setq oldelt (aref vec index)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
103 (while (/= index tl) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
104 (aset vec index (aref vec (ring-plus1 index veclen))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
105 (setq index (ring-plus1 index veclen))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
106 (aset vec tl nil) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
107 (setcar (cdr ring) (1- ln)) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
108 oldelt))) |
124 | 109 |
905 | 110 (defun ring-mod (n m) |
245 | 111 "Returns N mod M. M is positive. |
112 Answer is guaranteed to be non-negative, and less than m." | |
124 | 113 (let ((n (% n m))) |
114 (if (>= n 0) n | |
115 (+ n | |
116 (if (>= m 0) m (- m)))))) ; (abs m) | |
117 | |
118 (defun ring-ref (ring index) | |
2574
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
119 "Returns RING's INDEX element. |
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
120 INDEX need not be <= the ring length, the appropriate modulo operation |
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
121 will be performed. Element 0 is the most recently inserted; higher indices |
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
122 correspond to older elements until they wrap." |
2582
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
123 (if (ring-empty-p ring) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
124 (error "indexed empty ring") |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
125 (let* ((hd (car ring)) (ln (car (cdr ring))) (vec (cdr (cdr ring)))) |
e01048f32511
Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2574
diff
changeset
|
126 (aref vec (ring-index index hd ln (length vec)))))) |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
127 |
2574
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
128 (provide 'ring) |
c782b69b60a4
Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2424
diff
changeset
|
129 |
658
7cbd4fcd8b0f
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
245
diff
changeset
|
130 ;;; ring.el ends here |