]> code.delx.au - gnu-emacs/blob - doc/lispref/sequences.texi
Document define-minor-mode's new :variable keyword in the lispref.
[gnu-emacs] / doc / lispref / sequences.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2012
4 @c Free Software Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @setfilename ../../info/sequences
7 @node Sequences Arrays Vectors, Hash Tables, Lists, Top
8 @chapter Sequences, Arrays, and Vectors
9 @cindex sequence
10
11 The @dfn{sequence} type is the union of two other Lisp types: lists
12 and arrays. In other words, any list is a sequence, and any array is
13 a sequence. The common property that all sequences have is that each
14 is an ordered collection of elements.
15
16 An @dfn{array} is a fixed-length object with a slot for each of its
17 elements. All the elements are accessible in constant time. The four
18 types of arrays are strings, vectors, char-tables and bool-vectors.
19
20 A list is a sequence of elements, but it is not a single primitive
21 object; it is made of cons cells, one cell per element. Finding the
22 @var{n}th element requires looking through @var{n} cons cells, so
23 elements farther from the beginning of the list take longer to access.
24 But it is possible to add elements to the list, or remove elements.
25
26 The following diagram shows the relationship between these types:
27
28 @example
29 @group
30 _____________________________________________
31 | |
32 | Sequence |
33 | ______ ________________________________ |
34 | | | | | |
35 | | List | | Array | |
36 | | | | ________ ________ | |
37 | |______| | | | | | | |
38 | | | Vector | | String | | |
39 | | |________| |________| | |
40 | | ____________ _____________ | |
41 | | | | | | | |
42 | | | Char-table | | Bool-vector | | |
43 | | |____________| |_____________| | |
44 | |________________________________| |
45 |_____________________________________________|
46 @end group
47 @end example
48
49 @menu
50 * Sequence Functions:: Functions that accept any kind of sequence.
51 * Arrays:: Characteristics of arrays in Emacs Lisp.
52 * Array Functions:: Functions specifically for arrays.
53 * Vectors:: Special characteristics of Emacs Lisp vectors.
54 * Vector Functions:: Functions specifically for vectors.
55 * Char-Tables:: How to work with char-tables.
56 * Bool-Vectors:: How to work with bool-vectors.
57 * Rings:: Managing a fixed-size ring of objects.
58 @end menu
59
60 @node Sequence Functions
61 @section Sequences
62
63 This section describes functions that accept any kind of sequence.
64
65 @defun sequencep object
66 This function returns @code{t} if @var{object} is a list, vector,
67 string, bool-vector, or char-table, @code{nil} otherwise.
68 @end defun
69
70 @defun length sequence
71 @cindex string length
72 @cindex list length
73 @cindex vector length
74 @cindex sequence length
75 @cindex char-table length
76 This function returns the number of elements in @var{sequence}. If
77 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
78 signaled. Circular lists may cause an infinite loop. For a
79 char-table, the value returned is always one more than the maximum
80 Emacs character code.
81
82 @xref{Definition of safe-length}, for the related function @code{safe-length}.
83
84 @example
85 @group
86 (length '(1 2 3))
87 @result{} 3
88 @end group
89 @group
90 (length ())
91 @result{} 0
92 @end group
93 @group
94 (length "foobar")
95 @result{} 6
96 @end group
97 @group
98 (length [1 2 3])
99 @result{} 3
100 @end group
101 @group
102 (length (make-bool-vector 5 nil))
103 @result{} 5
104 @end group
105 @end example
106 @end defun
107
108 @noindent
109 See also @code{string-bytes}, in @ref{Text Representations}.
110
111 @defun elt sequence index
112 @cindex elements of sequences
113 This function returns the element of @var{sequence} indexed by
114 @var{index}. Legitimate values of @var{index} are integers ranging
115 from 0 up to one less than the length of @var{sequence}. If
116 @var{sequence} is a list, out-of-range values behave as for
117 @code{nth}. @xref{Definition of nth}. Otherwise, out-of-range values
118 trigger an @code{args-out-of-range} error.
119
120 @example
121 @group
122 (elt [1 2 3 4] 2)
123 @result{} 3
124 @end group
125 @group
126 (elt '(1 2 3 4) 2)
127 @result{} 3
128 @end group
129 @group
130 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
131 (string (elt "1234" 2))
132 @result{} "3"
133 @end group
134 @group
135 (elt [1 2 3 4] 4)
136 @error{} Args out of range: [1 2 3 4], 4
137 @end group
138 @group
139 (elt [1 2 3 4] -1)
140 @error{} Args out of range: [1 2 3 4], -1
141 @end group
142 @end example
143
144 This function generalizes @code{aref} (@pxref{Array Functions}) and
145 @code{nth} (@pxref{Definition of nth}).
146 @end defun
147
148 @defun copy-sequence sequence
149 @cindex copying sequences
150 This function returns a copy of @var{sequence}. The copy is the same
151 type of object as the original sequence, and it has the same elements
152 in the same order.
153
154 Storing a new element into the copy does not affect the original
155 @var{sequence}, and vice versa. However, the elements of the new
156 sequence are not copies; they are identical (@code{eq}) to the elements
157 of the original. Therefore, changes made within these elements, as
158 found via the copied sequence, are also visible in the original
159 sequence.
160
161 If the sequence is a string with text properties, the property list in
162 the copy is itself a copy, not shared with the original's property
163 list. However, the actual values of the properties are shared.
164 @xref{Text Properties}.
165
166 This function does not work for dotted lists. Trying to copy a
167 circular list may cause an infinite loop.
168
169 See also @code{append} in @ref{Building Lists}, @code{concat} in
170 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
171 for other ways to copy sequences.
172
173 @example
174 @group
175 (setq bar '(1 2))
176 @result{} (1 2)
177 @end group
178 @group
179 (setq x (vector 'foo bar))
180 @result{} [foo (1 2)]
181 @end group
182 @group
183 (setq y (copy-sequence x))
184 @result{} [foo (1 2)]
185 @end group
186
187 @group
188 (eq x y)
189 @result{} nil
190 @end group
191 @group
192 (equal x y)
193 @result{} t
194 @end group
195 @group
196 (eq (elt x 1) (elt y 1))
197 @result{} t
198 @end group
199
200 @group
201 ;; @r{Replacing an element of one sequence.}
202 (aset x 0 'quux)
203 x @result{} [quux (1 2)]
204 y @result{} [foo (1 2)]
205 @end group
206
207 @group
208 ;; @r{Modifying the inside of a shared element.}
209 (setcar (aref x 1) 69)
210 x @result{} [quux (69 2)]
211 y @result{} [foo (69 2)]
212 @end group
213 @end example
214 @end defun
215
216 @node Arrays
217 @section Arrays
218 @cindex array
219
220 An @dfn{array} object has slots that hold a number of other Lisp
221 objects, called the elements of the array. Any element of an array
222 may be accessed in constant time. In contrast, the time to access an
223 element of a list is proportional to the position of that element in
224 the list.
225
226 Emacs defines four types of array, all one-dimensional:
227 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
228 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
229 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
230 can hold elements of any type, but strings can only hold characters,
231 and bool-vectors can only hold @code{t} and @code{nil}.
232
233 All four kinds of array share these characteristics:
234
235 @itemize @bullet
236 @item
237 The first element of an array has index zero, the second element has
238 index 1, and so on. This is called @dfn{zero-origin} indexing. For
239 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
240
241 @item
242 The length of the array is fixed once you create it; you cannot
243 change the length of an existing array.
244
245 @item
246 For purposes of evaluation, the array is a constant---in other words,
247 it evaluates to itself.
248
249 @item
250 The elements of an array may be referenced or changed with the functions
251 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
252 @end itemize
253
254 When you create an array, other than a char-table, you must specify
255 its length. You cannot specify the length of a char-table, because that
256 is determined by the range of character codes.
257
258 In principle, if you want an array of text characters, you could use
259 either a string or a vector. In practice, we always choose strings for
260 such applications, for four reasons:
261
262 @itemize @bullet
263 @item
264 They occupy one-fourth the space of a vector of the same elements.
265
266 @item
267 Strings are printed in a way that shows the contents more clearly
268 as text.
269
270 @item
271 Strings can hold text properties. @xref{Text Properties}.
272
273 @item
274 Many of the specialized editing and I/O facilities of Emacs accept only
275 strings. For example, you cannot insert a vector of characters into a
276 buffer the way you can insert a string. @xref{Strings and Characters}.
277 @end itemize
278
279 By contrast, for an array of keyboard input characters (such as a key
280 sequence), a vector may be necessary, because many keyboard input
281 characters are outside the range that will fit in a string. @xref{Key
282 Sequence Input}.
283
284 @node Array Functions
285 @section Functions that Operate on Arrays
286
287 In this section, we describe the functions that accept all types of
288 arrays.
289
290 @defun arrayp object
291 This function returns @code{t} if @var{object} is an array (i.e., a
292 vector, a string, a bool-vector or a char-table).
293
294 @example
295 @group
296 (arrayp [a])
297 @result{} t
298 (arrayp "asdf")
299 @result{} t
300 (arrayp (syntax-table)) ;; @r{A char-table.}
301 @result{} t
302 @end group
303 @end example
304 @end defun
305
306 @defun aref array index
307 @cindex array elements
308 This function returns the @var{index}th element of @var{array}. The
309 first element is at index zero.
310
311 @example
312 @group
313 (setq primes [2 3 5 7 11 13])
314 @result{} [2 3 5 7 11 13]
315 (aref primes 4)
316 @result{} 11
317 @end group
318 @group
319 (aref "abcdefg" 1)
320 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
321 @end group
322 @end example
323
324 See also the function @code{elt}, in @ref{Sequence Functions}.
325 @end defun
326
327 @defun aset array index object
328 This function sets the @var{index}th element of @var{array} to be
329 @var{object}. It returns @var{object}.
330
331 @example
332 @group
333 (setq w [foo bar baz])
334 @result{} [foo bar baz]
335 (aset w 0 'fu)
336 @result{} fu
337 w
338 @result{} [fu bar baz]
339 @end group
340
341 @group
342 (setq x "asdfasfd")
343 @result{} "asdfasfd"
344 (aset x 3 ?Z)
345 @result{} 90
346 x
347 @result{} "asdZasfd"
348 @end group
349 @end example
350
351 If @var{array} is a string and @var{object} is not a character, a
352 @code{wrong-type-argument} error results. The function converts a
353 unibyte string to multibyte if necessary to insert a character.
354 @end defun
355
356 @defun fillarray array object
357 This function fills the array @var{array} with @var{object}, so that
358 each element of @var{array} is @var{object}. It returns @var{array}.
359
360 @example
361 @group
362 (setq a [a b c d e f g])
363 @result{} [a b c d e f g]
364 (fillarray a 0)
365 @result{} [0 0 0 0 0 0 0]
366 a
367 @result{} [0 0 0 0 0 0 0]
368 @end group
369 @group
370 (setq s "When in the course")
371 @result{} "When in the course"
372 (fillarray s ?-)
373 @result{} "------------------"
374 @end group
375 @end example
376
377 If @var{array} is a string and @var{object} is not a character, a
378 @code{wrong-type-argument} error results.
379 @end defun
380
381 The general sequence functions @code{copy-sequence} and @code{length}
382 are often useful for objects known to be arrays. @xref{Sequence Functions}.
383
384 @node Vectors
385 @section Vectors
386 @cindex vector (type)
387
388 A @dfn{vector} is a general-purpose array whose elements can be any
389 Lisp objects. (By contrast, the elements of a string can only be
390 characters. @xref{Strings and Characters}.) Vectors are used in
391 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
392 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
393 representation of a byte-compiled function (@pxref{Byte Compilation}),
394 and more.
395
396 Like other arrays, vectors use zero-origin indexing: the first
397 element has index 0.
398
399 Vectors are printed with square brackets surrounding the elements.
400 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
401 @code{a} is printed as @code{[a b a]}. You can write vectors in the
402 same way in Lisp input.
403
404 A vector, like a string or a number, is considered a constant for
405 evaluation: the result of evaluating it is the same vector. This does
406 not evaluate or even examine the elements of the vector.
407 @xref{Self-Evaluating Forms}.
408
409 Here are examples illustrating these principles:
410
411 @example
412 @group
413 (setq avector [1 two '(three) "four" [five]])
414 @result{} [1 two (quote (three)) "four" [five]]
415 (eval avector)
416 @result{} [1 two (quote (three)) "four" [five]]
417 (eq avector (eval avector))
418 @result{} t
419 @end group
420 @end example
421
422 @node Vector Functions
423 @section Functions for Vectors
424
425 Here are some functions that relate to vectors:
426
427 @defun vectorp object
428 This function returns @code{t} if @var{object} is a vector.
429
430 @example
431 @group
432 (vectorp [a])
433 @result{} t
434 (vectorp "asdf")
435 @result{} nil
436 @end group
437 @end example
438 @end defun
439
440 @defun vector &rest objects
441 This function creates and returns a vector whose elements are the
442 arguments, @var{objects}.
443
444 @example
445 @group
446 (vector 'foo 23 [bar baz] "rats")
447 @result{} [foo 23 [bar baz] "rats"]
448 (vector)
449 @result{} []
450 @end group
451 @end example
452 @end defun
453
454 @defun make-vector length object
455 This function returns a new vector consisting of @var{length} elements,
456 each initialized to @var{object}.
457
458 @example
459 @group
460 (setq sleepy (make-vector 9 'Z))
461 @result{} [Z Z Z Z Z Z Z Z Z]
462 @end group
463 @end example
464 @end defun
465
466 @defun vconcat &rest sequences
467 @cindex copying vectors
468 This function returns a new vector containing all the elements of
469 @var{sequences}. The arguments @var{sequences} may be true lists,
470 vectors, strings or bool-vectors. If no @var{sequences} are given, an
471 empty vector is returned.
472
473 The value is a newly constructed vector that is not @code{eq} to any
474 existing vector.
475
476 @example
477 @group
478 (setq a (vconcat '(A B C) '(D E F)))
479 @result{} [A B C D E F]
480 (eq a (vconcat a))
481 @result{} nil
482 @end group
483 @group
484 (vconcat)
485 @result{} []
486 (vconcat [A B C] "aa" '(foo (6 7)))
487 @result{} [A B C 97 97 foo (6 7)]
488 @end group
489 @end example
490
491 The @code{vconcat} function also allows byte-code function objects as
492 arguments. This is a special feature to make it easy to access the entire
493 contents of a byte-code function object. @xref{Byte-Code Objects}.
494
495 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
496 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
497 in @ref{Building Lists}.
498 @end defun
499
500 The @code{append} function also provides a way to convert a vector into a
501 list with the same elements:
502
503 @example
504 @group
505 (setq avector [1 two (quote (three)) "four" [five]])
506 @result{} [1 two (quote (three)) "four" [five]]
507 (append avector nil)
508 @result{} (1 two (quote (three)) "four" [five])
509 @end group
510 @end example
511
512 @node Char-Tables
513 @section Char-Tables
514 @cindex char-tables
515 @cindex extra slots of char-table
516
517 A char-table is much like a vector, except that it is indexed by
518 character codes. Any valid character code, without modifiers, can be
519 used as an index in a char-table. You can access a char-table's
520 elements with @code{aref} and @code{aset}, as with any array. In
521 addition, a char-table can have @dfn{extra slots} to hold additional
522 data not associated with particular character codes. Like vectors,
523 char-tables are constants when evaluated, and can hold elements of any
524 type.
525
526 @cindex subtype of char-table
527 Each char-table has a @dfn{subtype}, a symbol, which serves two
528 purposes:
529
530 @itemize @bullet
531 @item
532 The subtype provides an easy way to tell what the char-table is for.
533 For instance, display tables are char-tables with @code{display-table}
534 as the subtype, and syntax tables are char-tables with
535 @code{syntax-table} as the subtype. The subtype can be queried using
536 the function @code{char-table-subtype}, described below.
537
538 @item
539 The subtype controls the number of @dfn{extra slots} in the
540 char-table. This number is specified by the subtype's
541 @code{char-table-extra-slots} symbol property, which should be an
542 integer between 0 and 10. If the subtype has no such symbol property,
543 the char-table has no extra slots. @xref{Property Lists}, for
544 information about symbol properties.
545 @end itemize
546
547 @cindex parent of char-table
548 A char-table can have a @dfn{parent}, which is another char-table. If
549 it does, then whenever the char-table specifies @code{nil} for a
550 particular character @var{c}, it inherits the value specified in the
551 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
552 the value from the parent of @var{char-table} if @var{char-table} itself
553 specifies @code{nil}.
554
555 @cindex default value of char-table
556 A char-table can also have a @dfn{default value}. If so, then
557 @code{(aref @var{char-table} @var{c})} returns the default value
558 whenever the char-table does not specify any other non-@code{nil} value.
559
560 @defun make-char-table subtype &optional init
561 Return a newly-created char-table, with subtype @var{subtype} (a
562 symbol). Each element is initialized to @var{init}, which defaults to
563 @code{nil}. You cannot alter the subtype of a char-table after the
564 char-table is created.
565
566 There is no argument to specify the length of the char-table, because
567 all char-tables have room for any valid character code as an index.
568
569 If @var{subtype} has the @code{char-table-extra-slots} symbol
570 property, that specifies the number of extra slots in the char-table.
571 This should be an integer between 0 and 10; otherwise,
572 @code{make-char-table} raises an error. If @var{subtype} has no
573 @code{char-table-extra-slots} symbol property (@pxref{Property
574 Lists}), the char-table has no extra slots.
575 @end defun
576
577 @defun char-table-p object
578 This function returns @code{t} if @var{object} is a char-table, and
579 @code{nil} otherwise.
580 @end defun
581
582 @defun char-table-subtype char-table
583 This function returns the subtype symbol of @var{char-table}.
584 @end defun
585
586 There is no special function to access default values in a char-table.
587 To do that, use @code{char-table-range} (see below).
588
589 @defun char-table-parent char-table
590 This function returns the parent of @var{char-table}. The parent is
591 always either @code{nil} or another char-table.
592 @end defun
593
594 @defun set-char-table-parent char-table new-parent
595 This function sets the parent of @var{char-table} to @var{new-parent}.
596 @end defun
597
598 @defun char-table-extra-slot char-table n
599 This function returns the contents of extra slot @var{n} of
600 @var{char-table}. The number of extra slots in a char-table is
601 determined by its subtype.
602 @end defun
603
604 @defun set-char-table-extra-slot char-table n value
605 This function stores @var{value} in extra slot @var{n} of
606 @var{char-table}.
607 @end defun
608
609 A char-table can specify an element value for a single character code;
610 it can also specify a value for an entire character set.
611
612 @defun char-table-range char-table range
613 This returns the value specified in @var{char-table} for a range of
614 characters @var{range}. Here are the possibilities for @var{range}:
615
616 @table @asis
617 @item @code{nil}
618 Refers to the default value.
619
620 @item @var{char}
621 Refers to the element for character @var{char}
622 (supposing @var{char} is a valid character code).
623
624 @item @code{(@var{from} . @var{to})}
625 A cons cell refers to all the characters in the inclusive range
626 @samp{[@var{from}..@var{to}]}.
627 @end table
628 @end defun
629
630 @defun set-char-table-range char-table range value
631 This function sets the value in @var{char-table} for a range of
632 characters @var{range}. Here are the possibilities for @var{range}:
633
634 @table @asis
635 @item @code{nil}
636 Refers to the default value.
637
638 @item @code{t}
639 Refers to the whole range of character codes.
640
641 @item @var{char}
642 Refers to the element for character @var{char}
643 (supposing @var{char} is a valid character code).
644
645 @item @code{(@var{from} . @var{to})}
646 A cons cell refers to all the characters in the inclusive range
647 @samp{[@var{from}..@var{to}]}.
648 @end table
649 @end defun
650
651 @defun map-char-table function char-table
652 This function calls its argument @var{function} for each element of
653 @var{char-table} that has a non-@code{nil} value. The call to
654 @var{function} is with two arguments, a key and a value. The key
655 is a possible @var{range} argument for @code{char-table-range}---either
656 a valid character or a cons cell @code{(@var{from} . @var{to})},
657 specifying a range of characters that share the same value. The value is
658 what @code{(char-table-range @var{char-table} @var{key})} returns.
659
660 Overall, the key-value pairs passed to @var{function} describe all the
661 values stored in @var{char-table}.
662
663 The return value is always @code{nil}; to make calls to
664 @code{map-char-table} useful, @var{function} should have side effects.
665 For example, here is how to examine the elements of the syntax table:
666
667 @example
668 (let (accumulator)
669 (map-char-table
670 #'(lambda (key value)
671 (setq accumulator
672 (cons (list
673 (if (consp key)
674 (list (car key) (cdr key))
675 key)
676 value)
677 accumulator)))
678 (syntax-table))
679 accumulator)
680 @result{}
681 (((2597602 4194303) (2)) ((2597523 2597601) (3))
682 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
683 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
684 @end example
685 @end defun
686
687 @node Bool-Vectors
688 @section Bool-vectors
689 @cindex Bool-vectors
690
691 A bool-vector is much like a vector, except that it stores only the
692 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
693 value into an element of the bool-vector, the effect is to store
694 @code{t} there. As with all arrays, bool-vector indices start from 0,
695 and the length cannot be changed once the bool-vector is created.
696 Bool-vectors are constants when evaluated.
697
698 There are two special functions for working with bool-vectors; aside
699 from that, you manipulate them with same functions used for other kinds
700 of arrays.
701
702 @defun make-bool-vector length initial
703 Return a new bool-vector of @var{length} elements,
704 each one initialized to @var{initial}.
705 @end defun
706
707 @defun bool-vector-p object
708 This returns @code{t} if @var{object} is a bool-vector,
709 and @code{nil} otherwise.
710 @end defun
711
712 Here is an example of creating, examining, and updating a
713 bool-vector. Note that the printed form represents up to 8 boolean
714 values as a single character.
715
716 @example
717 (setq bv (make-bool-vector 5 t))
718 @result{} #&5"^_"
719 (aref bv 1)
720 @result{} t
721 (aset bv 3 nil)
722 @result{} nil
723 bv
724 @result{} #&5"^W"
725 @end example
726
727 @noindent
728 These results make sense because the binary codes for control-_ and
729 control-W are 11111 and 10111, respectively.
730
731 @node Rings
732 @section Managing a Fixed-Size Ring of Objects
733
734 @cindex ring data structure
735 A @dfn{ring} is a fixed-size data structure that supports insertion,
736 deletion, rotation, and modulo-indexed reference and traversal. An
737 efficient ring data structure is implemented by the @code{ring}
738 package. It provides the functions listed in this section.
739
740 Note that several ``rings'' in Emacs, like the kill ring and the
741 mark ring, are actually implemented as simple lists, @emph{not} using
742 the @code{ring} package; thus the following functions won't work on
743 them.
744
745 @defun make-ring size
746 This returns a new ring capable of holding @var{size} objects.
747 @var{size} should be an integer.
748 @end defun
749
750 @defun ring-p object
751 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
752 @end defun
753
754 @defun ring-size ring
755 This returns the maximum capacity of the @var{ring}.
756 @end defun
757
758 @defun ring-length ring
759 This returns the number of objects that @var{ring} currently contains.
760 The value will never exceed that returned by @code{ring-size}.
761 @end defun
762
763 @defun ring-elements ring
764 This returns a list of the objects in @var{ring}, in order, newest first.
765 @end defun
766
767 @defun ring-copy ring
768 This returns a new ring which is a copy of @var{ring}.
769 The new ring contains the same (@code{eq}) objects as @var{ring}.
770 @end defun
771
772 @defun ring-empty-p ring
773 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
774 @end defun
775
776 The newest element in the ring always has index 0. Higher indices
777 correspond to older elements. Indices are computed modulo the ring
778 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
779 to the next-oldest, and so forth.
780
781 @defun ring-ref ring index
782 This returns the object in @var{ring} found at index @var{index}.
783 @var{index} may be negative or greater than the ring length. If
784 @var{ring} is empty, @code{ring-ref} signals an error.
785 @end defun
786
787 @defun ring-insert ring object
788 This inserts @var{object} into @var{ring}, making it the newest
789 element, and returns @var{object}.
790
791 If the ring is full, insertion removes the oldest element to
792 make room for the new element.
793 @end defun
794
795 @defun ring-remove ring &optional index
796 Remove an object from @var{ring}, and return that object. The
797 argument @var{index} specifies which item to remove; if it is
798 @code{nil}, that means to remove the oldest item. If @var{ring} is
799 empty, @code{ring-remove} signals an error.
800 @end defun
801
802 @defun ring-insert-at-beginning ring object
803 This inserts @var{object} into @var{ring}, treating it as the oldest
804 element. The return value is not significant.
805
806 If the ring is full, this function removes the newest element to make
807 room for the inserted element.
808 @end defun
809
810 @cindex fifo data structure
811 If you are careful not to exceed the ring size, you can
812 use the ring as a first-in-first-out queue. For example:
813
814 @lisp
815 (let ((fifo (make-ring 5)))
816 (mapc (lambda (obj) (ring-insert fifo obj))
817 '(0 one "two"))
818 (list (ring-remove fifo) t
819 (ring-remove fifo) t
820 (ring-remove fifo)))
821 @result{} (0 t one t "two")
822 @end lisp