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