annotate lisp/emacs-lisp/ring.el @ 85511:f873840f9fea

* emulation/edt-mapper.el (function-key-map): (edt-map-key): Make it a function instead of using fset. Inline edt-gnu-map-key and edt-lucid-map-key. Use featurep 'xemacs. (edt-gnu-map-key, edt-lucid-map-key): Remove. (edt-x-emacs-p): Remove. (edt-emacs-variant, edt-window-system, edt-xserver): Use featurep 'xemacs. * net/eudc.el: Use (featurep 'xemacs) instead of the string test. Replace eudc-xemacs-p with its definition. (eudc-xemacs-p, eudc-emacs-p, eudc-xemacs-mule-p) (eudc-emacs-mule-p): Remove. (eudc-install-menu, eudc-mode): Replace eudc-emacs-p and eudc-xemacs-p with feature tests. * net/eudc-bob.el (eudc-bob-generic-menu, eudc-bob-mail-keymap) (eudc-bob-url-keymap, eudc-bob-sound-keymap) (eudc-bob-generic-keymap, eudc-bob-popup-menu) (eudc-bob-toggle-inline-display): * net/eudc-hotlist.el (eudc-hotlist-emacs-menu): Replace eudc-emacs-p and eudc-xemacs-p with feature tests. * net/eudcb-ph.el (eudc-ph-open-session): Replace eudc-xemacs-mule-p with its former definition. * progmodes/octave-mod.el (octave-xemacs-p): Remove. (octave-abbrev-start): Replace octave-xemacs-p with (featurep 'xemacs). * progmodes/vera-mode.el (vera-xemacs): Remove. (vera-mode-syntax-table): Replace vera-xemacs with (featurep 'xemacs). * progmodes/vhdl-mode.el (vhdl-xemacs): Remove. (vhdl-doc-mode, vhdl-doc-variable, vhdl-compile-init) (vhdl-speedbar-initialize, vhdl-ps-print-init) (vhdl-forward-comment, vhdl-mode-map-init, vhdl-show-messages) (vhdl-emacs-22, vhdl-emacs-21): Replace vhdl-xemacs with (featurep 'xemacs). * progmodes/antlr-mode.el (cond-emacs-xemacs-macfn, defunx) (save-buffer-state-x): * obsolete/fast-lock.el (fast-lock-verbose): * emulation/viper-init.el (viper-xemacs-p) (viper-cond-compile-for-xemacs-or-emacs): * emacs-lisp/checkdoc.el (checkdoc-minor-mode-map): * ps-print.el (case-fold-search): * ediff-hook.el (ediff-cond-compile-for-xemacs-or-emacs): * calculator.el (calculator-help): Use featurep 'xemacs.
author Dan Nicolaescu <dann@ics.uci.edu>
date Sun, 21 Oct 2007 17:22:04 +0000
parents e1264e487a97
children e6b555f6c76b a0e466c4d599
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
74466
1d4b1a32fd66 Update copyright years.
Glenn Morris <rgm@gnu.org>
parents: 68648
diff changeset
3 ;; Copyright (C) 1992, 2001, 2002, 2003, 2004, 2005,
75346
7a3f13e2dd57 Add 2007 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 74466
diff changeset
4 ;; 2006, 2007 Free Software Foundation, Inc.
846
20674ae6bf52 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 811
diff changeset
5
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
6 ;; Maintainer: FSF
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
7 ;; Keywords: extensions
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
8
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
9 ;; This file is part of GNU Emacs.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
10
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
11 ;; 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
12 ;; it under the terms of the GNU General Public License as published by
78217
935157c0b596 Switch license to GPLv3 or later.
Glenn Morris <rgm@gnu.org>
parents: 75346
diff changeset
13 ;; the Free Software Foundation; either version 3, or (at your option)
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
14 ;; any later version.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
15
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
16 ;; 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
17 ;; 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
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
19 ;; GNU General Public License for more details.
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
20
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
21 ;; You should have received a copy of the GNU General Public License
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
64085
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 63727
diff changeset
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18a818a2ee7c Update FSF's address.
Lute Kamstra <lute@gnu.org>
parents: 63727
diff changeset
24 ;; Boston, MA 02110-1301, USA.
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
25
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
26 ;;; Commentary:
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
27
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
28 ;; This code defines a ring data structure. A ring is a
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
29 ;; (hd-index length . vector)
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
30 ;; list. You can insert to, remove from, and rotate a ring. When the ring
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
31 ;; fills up, insertions cause the oldest elts to be quietly dropped.
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
32 ;;
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
33 ;; In ring-ref, 0 is the index of the newest element. Higher indexes
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
34 ;; correspond to older elements; when the index equals the ring length,
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
35 ;; it wraps to the newest element again.
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
36 ;;
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
37 ;; hd-index = vector index of the oldest ring item.
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
38 ;; Newer items follow this item; at the end of the vector,
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
39 ;; they wrap around to the start of the vector.
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
40 ;; length = number of items currently in the ring.
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
41 ;; This never exceeds the length of the vector itself.
14169
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
42 ;;
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
43 ;; These functions are used by the input history mechanism, but they can
83f275dcd93a Update FSF's address.
Erik Naggum <erik@naggum.no>
parents: 8031
diff changeset
44 ;; be used for other purposes as well.
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
45
811
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
46 ;;; Code:
e694e0879463 *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 658
diff changeset
47
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
48 ;;; User Functions:
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
49
905
48e4034a2176 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 846
diff changeset
50 ;;;###autoload
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
51 (defun ring-p (x)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
52 "Return t if X is a ring; nil otherwise."
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
53 (and (consp x) (integerp (car x))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
54 (consp (cdr x)) (integerp (car (cdr x)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
55 (vectorp (cdr (cdr x)))))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
56
905
48e4034a2176 entered into RCS
Richard M. Stallman <rms@gnu.org>
parents: 846
diff changeset
57 ;;;###autoload
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
58 (defun make-ring (size)
2574
c782b69b60a4 Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2424
diff changeset
59 "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
60 (cons 0 (cons 0 (make-vector size nil))))
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
61
8031
4b45aa6d5d76 (ring-insert-at-beginning): New function.
Richard M. Stallman <rms@gnu.org>
parents: 4516
diff changeset
62 (defun ring-insert-at-beginning (ring item)
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
63 "Add to RING the item ITEM. Add it at the front, as the oldest item."
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
64 (let* ((vec (cdr (cdr ring)))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
65 (veclen (length vec))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
66 (hd (car ring))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
67 (ln (car (cdr ring))))
8031
4b45aa6d5d76 (ring-insert-at-beginning): New function.
Richard M. Stallman <rms@gnu.org>
parents: 4516
diff changeset
68 (setq ln (min veclen (1+ ln))
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
69 hd (ring-minus1 hd veclen))
8031
4b45aa6d5d76 (ring-insert-at-beginning): New function.
Richard M. Stallman <rms@gnu.org>
parents: 4516
diff changeset
70 (aset vec hd item)
4b45aa6d5d76 (ring-insert-at-beginning): New function.
Richard M. Stallman <rms@gnu.org>
parents: 4516
diff changeset
71 (setcar ring hd)
4b45aa6d5d76 (ring-insert-at-beginning): New function.
Richard M. Stallman <rms@gnu.org>
parents: 4516
diff changeset
72 (setcar (cdr ring) ln)))
4b45aa6d5d76 (ring-insert-at-beginning): New function.
Richard M. Stallman <rms@gnu.org>
parents: 4516
diff changeset
73
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
74 (defun ring-plus1 (index veclen)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
75 "Return INDEX+1, with wraparound."
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
76 (let ((new-index (+ index 1)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
77 (if (= new-index veclen) 0 new-index)))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
78
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
79 (defun ring-minus1 (index veclen)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
80 "Return INDEX-1, with wraparound."
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
81 (- (if (= 0 index) veclen index) 1))
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
82
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
83 (defun ring-length (ring)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
84 "Return the 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
85 (car (cdr ring)))
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
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-index (index head ringlen veclen)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
88 "Convert nominal ring index INDEX to an internal index.
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
89 The internal index refers to the items ordered from newest to oldest.
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
90 HEAD is the index of the oldest element in the ring.
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
91 RINGLEN is the number of elements currently in the ring.
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
92 VECLEN is the size of the vector in the ring."
4516
95b9760d19e3 (ring-mod): Remove, since floor and mod
Paul Eggert <eggert@twinsun.com>
parents: 2582
diff changeset
93 (setq index (mod index ringlen))
95b9760d19e3 (ring-mod): Remove, since floor and mod
Paul Eggert <eggert@twinsun.com>
parents: 2582
diff changeset
94 (mod (1- (+ head (- ringlen index))) veclen))
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
95
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
96 (defun ring-empty-p (ring)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
97 "Return t if RING is empty; nil otherwise."
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
98 (zerop (car (cdr ring))))
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
99
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
100 (defun ring-size (ring)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
101 "Return the size of RING, the maximum number of elements it can contain."
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
102 (length (cdr (cdr ring))))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
103
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
104 (defun ring-copy (ring)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
105 "Return a copy of RING."
25468
637af9b4fdbb Fix previous change.
Richard M. Stallman <rms@gnu.org>
parents: 25467
diff changeset
106 (let* ((vec (cdr (cdr ring)))
637af9b4fdbb Fix previous change.
Richard M. Stallman <rms@gnu.org>
parents: 25467
diff changeset
107 (hd (car ring))
637af9b4fdbb Fix previous change.
Richard M. Stallman <rms@gnu.org>
parents: 25467
diff changeset
108 (ln (car (cdr ring))))
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
109 (cons hd (cons ln (copy-sequence vec)))))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
110
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
111 (defun ring-insert (ring item)
8031
4b45aa6d5d76 (ring-insert-at-beginning): New function.
Richard M. Stallman <rms@gnu.org>
parents: 4516
diff changeset
112 "Insert onto ring RING the item ITEM, as the newest (last) item.
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
113 If the ring is full, dump the oldest item to make room."
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
114 (let* ((vec (cdr (cdr ring)))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
115 (veclen (length vec))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
116 (hd (car ring))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
117 (ln (car (cdr 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
118 (prog1
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
119 (aset vec (mod (+ hd ln) veclen) item)
2582
e01048f32511 Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2574
diff changeset
120 (if (= ln veclen)
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
121 (setcar ring (ring-plus1 hd veclen))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
122 (setcar (cdr ring) (1+ ln))))))
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
123
2582
e01048f32511 Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2574
diff changeset
124 (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
125 "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
126 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
127 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
128 (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
129 (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
130 (let* ((hd (car ring))
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
131 (ln (car (cdr ring)))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
132 (vec (cdr (cdr ring)))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
133 (veclen (length vec))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
134 (tl (mod (1- (+ hd ln)) veclen))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
135 oldelt)
2582
e01048f32511 Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2574
diff changeset
136 (if (null index)
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
137 (setq index (1- ln)))
2582
e01048f32511 Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2574
diff changeset
138 (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
139 (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
140 (while (/= index tl)
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
141 (aset vec index (aref vec (ring-plus1 index veclen)))
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
142 (setq index (ring-plus1 index veclen)))
2582
e01048f32511 Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2574
diff changeset
143 (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
144 (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
145 oldelt)))
124
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
146
27691d738b4f Initial revision
David Lawrence <tale@gnu.org>
parents:
diff changeset
147 (defun ring-ref (ring index)
51873
000e2717e1a5 2003-07-11 John Paul Wallington <jpw@gnu.org>
John Paul Wallington <jpw@pobox.com>
parents: 29055
diff changeset
148 "Return RING's INDEX element.
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
149 INDEX = 0 is the most recently inserted; higher indices
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
150 correspond to older elements.
25468
637af9b4fdbb Fix previous change.
Richard M. Stallman <rms@gnu.org>
parents: 25467
diff changeset
151 INDEX need not be <= the ring length; the appropriate modulo operation
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
152 will be performed."
2582
e01048f32511 Rewritten. A poor choice of representation made the old code excessively
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2574
diff changeset
153 (if (ring-empty-p ring)
17212
de55dc47bae6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 14169
diff changeset
154 (error "Accessing an empty 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
155 (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
156 (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
157
29055
661c97b907a8 (ring-elements): New function.
Dave Love <fx@gnu.org>
parents: 25468
diff changeset
158 (defun ring-elements (ring)
63727
fe0423976eb0 (ring-elements): Make it return a list of the elements of RING in
Luc Teirlinck <teirllm@auburn.edu>
parents: 52401
diff changeset
159 "Return a list of the elements of RING, in order, newest first."
fe0423976eb0 (ring-elements): Make it return a list of the elements of RING in
Luc Teirlinck <teirllm@auburn.edu>
parents: 52401
diff changeset
160 (let ((start (car ring))
fe0423976eb0 (ring-elements): Make it return a list of the elements of RING in
Luc Teirlinck <teirllm@auburn.edu>
parents: 52401
diff changeset
161 (size (ring-size ring))
fe0423976eb0 (ring-elements): Make it return a list of the elements of RING in
Luc Teirlinck <teirllm@auburn.edu>
parents: 52401
diff changeset
162 (vect (cddr ring))
fe0423976eb0 (ring-elements): Make it return a list of the elements of RING in
Luc Teirlinck <teirllm@auburn.edu>
parents: 52401
diff changeset
163 lst)
fe0423976eb0 (ring-elements): Make it return a list of the elements of RING in
Luc Teirlinck <teirllm@auburn.edu>
parents: 52401
diff changeset
164 (dotimes (var (cadr ring) lst)
fe0423976eb0 (ring-elements): Make it return a list of the elements of RING in
Luc Teirlinck <teirllm@auburn.edu>
parents: 52401
diff changeset
165 (push (aref vect (mod (+ start var) size)) lst))))
29055
661c97b907a8 (ring-elements): New function.
Dave Love <fx@gnu.org>
parents: 25468
diff changeset
166
85305
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
167 (defun ring-member (ring item)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
168 "Return index of ITEM if on RING, else nil. Comparison via `equal'.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
169 The index is 0-based."
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
170 (let ((ind 0)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
171 (len (1- (ring-length ring)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
172 (memberp nil))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
173 (while (and (<= ind len)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
174 (not (setq memberp (equal item (ring-ref ring ind)))))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
175 (setq ind (1+ ind)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
176 (and memberp ind)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
177
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
178 (defun ring-next (ring item)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
179 "Return the next item in the RING, after ITEM.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
180 Raise error if ITEM is not in the RING."
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
181 (let ((curr-index (ring-member ring item)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
182 (unless curr-index (error "Item is not in the ring: `%s'" item))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
183 (ring-ref ring (ring-plus1 curr-index (ring-length ring)))))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
184
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
185 (defun ring-previous (ring item)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
186 "Return the previous item in the RING, before ITEM.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
187 Raise error if ITEM is not in the RING."
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
188 (let ((curr-index (ring-member ring item)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
189 (unless curr-index (error "Item is not in the ring: `%s'" item))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
190 (ring-ref ring (ring-minus1 curr-index (ring-length ring)))))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
191
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
192 (defun ring-insert+extend (ring item &optional grow-p)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
193 "Like ring-insert, but if GROW-P is non-nil, then enlarge ring.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
194 Insert onto ring RING the item ITEM, as the newest (last) item.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
195 If the ring is full, behavior depends on GROW-P:
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
196 If GROW-P is non-nil, enlarge the ring to accommodate the new item.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
197 If GROW-P is nil, dump the oldest item to make room for the new."
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
198 (let* ((vec (cdr (cdr ring)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
199 (veclen (length vec))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
200 (hd (car ring))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
201 (ringlen (ring-length ring)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
202 (prog1
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
203 (cond ((and grow-p (= ringlen veclen)) ; Full ring. Enlarge it.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
204 (setq veclen (1+ veclen))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
205 (setcdr ring (cons (setq ringlen (1+ ringlen))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
206 (setq vec (vconcat vec (vector item)))))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
207 (setcar ring hd))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
208 (t (aset vec (mod (+ hd ringlen) veclen) item)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
209 (if (= ringlen veclen)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
210 (setcar ring (ring-plus1 hd veclen))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
211 (setcar (cdr ring) (1+ ringlen))))))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
212
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
213 (defun ring-remove+insert+extend (ring item &optional grow-p)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
214 "`ring-remove' ITEM from RING, then `ring-insert+extend' it.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
215 This ensures that there is only one ITEM on RING.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
216
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
217 If the RING is full, behavior depends on GROW-P:
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
218 If GROW-P is non-nil, enlarge the ring to accommodate the new ITEM.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
219 If GROW-P is nil, dump the oldest item to make room for the new."
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
220 (let (ind)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
221 (while (setq ind (ring-member ring item)) (ring-remove ring ind)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
222 (ring-insert+extend ring item grow-p))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
223
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
224 (defun ring-convert-sequence-to-ring (seq)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
225 "Convert sequence SEQ to a ring. Return the ring.
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
226 If SEQ is already a ring, return it."
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
227 (if (ring-p seq)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
228 seq
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
229 (let* ((size (length seq))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
230 (ring (make-ring size))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
231 (count 0))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
232 (while (< count size)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
233 (if (or (ring-empty-p ring)
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
234 (not (equal (ring-ref ring 0) (elt seq count))))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
235 (ring-insert-at-beginning ring (elt seq count)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
236 (setq count (1+ count)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
237 ring)))
e1264e487a97 (ring-convert-sequence-to-ring)
Richard M. Stallman <rms@gnu.org>
parents: 78217
diff changeset
238
25467
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
239 ;;; provide ourself:
cf992000b9c6 Many doc fixes.
Richard M. Stallman <rms@gnu.org>
parents: 17212
diff changeset
240
2574
c782b69b60a4 Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2424
diff changeset
241 (provide 'ring)
c782b69b60a4 Added and fixed documentation.
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 2424
diff changeset
242
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51873
diff changeset
243 ;;; arch-tag: e707682b-ed69-47c9-b20f-cf2c68cc92d2
658
7cbd4fcd8b0f *** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents: 245
diff changeset
244 ;;; ring.el ends here