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