Typo fixes in elisp manual
[emacs.git] / doc / lispref / sequences.texi
blob920399586c54df9f58fbe05896b22091f79fb9c1
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2016 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
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.
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.
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.
25   The following diagram shows the relationship between these types:
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
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
59 @node Sequence Functions
60 @section Sequences
62   This section describes functions that accept any kind of sequence.
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
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 @anchor{Definition of 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.
82 @xref{Definition of safe-length}, for the related function @code{safe-length}.
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
108 @noindent
109 See also @code{string-bytes}, in @ref{Text Representations}.
111 If you need to compute the width of a string on display, you should use
112 @code{string-width} (@pxref{Size of Displayed Text}), not @code{length},
113 since @code{length} only counts the number of characters, but does not
114 account for the display width of each character.
116 @defun elt sequence index
117 @anchor{Definition of elt}
118 @cindex elements of sequences
119 This function returns the element of @var{sequence} indexed by
120 @var{index}.  Legitimate values of @var{index} are integers ranging
121 from 0 up to one less than the length of @var{sequence}.  If
122 @var{sequence} is a list, out-of-range values behave as for
123 @code{nth}.  @xref{Definition of nth}.  Otherwise, out-of-range values
124 trigger an @code{args-out-of-range} error.
126 @example
127 @group
128 (elt [1 2 3 4] 2)
129      @result{} 3
130 @end group
131 @group
132 (elt '(1 2 3 4) 2)
133      @result{} 3
134 @end group
135 @group
136 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
137 (string (elt "1234" 2))
138      @result{} "3"
139 @end group
140 @group
141 (elt [1 2 3 4] 4)
142      @error{} Args out of range: [1 2 3 4], 4
143 @end group
144 @group
145 (elt [1 2 3 4] -1)
146      @error{} Args out of range: [1 2 3 4], -1
147 @end group
148 @end example
150 This function generalizes @code{aref} (@pxref{Array Functions}) and
151 @code{nth} (@pxref{Definition of nth}).
152 @end defun
154 @defun copy-sequence sequence
155 @cindex copying sequences
156 This function returns a copy of @var{sequence}.  The copy is the same
157 type of object as the original sequence, and it has the same elements
158 in the same order.
160 Storing a new element into the copy does not affect the original
161 @var{sequence}, and vice versa.  However, the elements of the new
162 sequence are not copies; they are identical (@code{eq}) to the elements
163 of the original.  Therefore, changes made within these elements, as
164 found via the copied sequence, are also visible in the original
165 sequence.
167 If the sequence is a string with text properties, the property list in
168 the copy is itself a copy, not shared with the original's property
169 list.  However, the actual values of the properties are shared.
170 @xref{Text Properties}.
172 This function does not work for dotted lists.  Trying to copy a
173 circular list may cause an infinite loop.
175 See also @code{append} in @ref{Building Lists}, @code{concat} in
176 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
177 for other ways to copy sequences.
179 @example
180 @group
181 (setq bar '(1 2))
182      @result{} (1 2)
183 @end group
184 @group
185 (setq x (vector 'foo bar))
186      @result{} [foo (1 2)]
187 @end group
188 @group
189 (setq y (copy-sequence x))
190      @result{} [foo (1 2)]
191 @end group
193 @group
194 (eq x y)
195      @result{} nil
196 @end group
197 @group
198 (equal x y)
199      @result{} t
200 @end group
201 @group
202 (eq (elt x 1) (elt y 1))
203      @result{} t
204 @end group
206 @group
207 ;; @r{Replacing an element of one sequence.}
208 (aset x 0 'quux)
209 x @result{} [quux (1 2)]
210 y @result{} [foo (1 2)]
211 @end group
213 @group
214 ;; @r{Modifying the inside of a shared element.}
215 (setcar (aref x 1) 69)
216 x @result{} [quux (69 2)]
217 y @result{} [foo (69 2)]
218 @end group
219 @end example
220 @end defun
222 @defun reverse sequence
223 @cindex string reverse
224 @cindex list reverse
225 @cindex vector reverse
226 @cindex sequence reverse
227 This function creates a new sequence whose elements are the elements
228 of @var{sequence}, but in reverse order.  The original argument @var{sequence}
229 is @emph{not} altered.  Note that char-tables cannot be reversed.
231 @example
232 @group
233 (setq x '(1 2 3 4))
234      @result{} (1 2 3 4)
235 @end group
236 @group
237 (reverse x)
238      @result{} (4 3 2 1)
240      @result{} (1 2 3 4)
241 @end group
242 @group
243 (setq x [1 2 3 4])
244      @result{} [1 2 3 4]
245 @end group
246 @group
247 (reverse x)
248      @result{} [4 3 2 1]
250      @result{} [1 2 3 4]
251 @end group
252 @group
253 (setq x "xyzzy")
254      @result{} "xyzzy"
255 @end group
256 @group
257 (reverse x)
258      @result{} "yzzyx"
260      @result{} "xyzzy"
261 @end group
262 @end example
263 @end defun
265 @defun nreverse sequence
266 @cindex reversing a string
267 @cindex reversing a list
268 @cindex reversing a vector
269   This function reverses the order of the elements of @var{sequence}.
270 Unlike @code{reverse} the original @var{sequence} may be modified.
272   For example:
274 @example
275 @group
276 (setq x '(a b c))
277      @result{} (a b c)
278 @end group
279 @group
281      @result{} (a b c)
282 (nreverse x)
283      @result{} (c b a)
284 @end group
285 @group
286 ;; @r{The cons cell that was first is now last.}
288      @result{} (a)
289 @end group
290 @end example
292   To avoid confusion, we usually store the result of @code{nreverse}
293 back in the same variable which held the original list:
295 @example
296 (setq x (nreverse x))
297 @end example
299   Here is the @code{nreverse} of our favorite example, @code{(a b c)},
300 presented graphically:
302 @smallexample
303 @group
304 @r{Original list head:}                       @r{Reversed list:}
305  -------------        -------------        ------------
306 | car  | cdr  |      | car  | cdr  |      | car | cdr  |
307 |   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
308 |      |      |   |  |      |   |  |   |  |     |   |  |
309  -------------    |   --------- | -    |   -------- | -
310                   |             |      |            |
311                    -------------        ------------
312 @end group
313 @end smallexample
315   For the vector, it is even simpler because you don't need setq:
317 @example
318 (setq x [1 2 3 4])
319      @result{} [1 2 3 4]
320 (nreverse x)
321      @result{} [4 3 2 1]
323      @result{} [4 3 2 1]
324 @end example
326 Note that unlike @code{reverse}, this function doesn't work with strings.
327 Although you can alter string data by using @code{aset}, it is strongly
328 encouraged to treat strings as immutable.
330 @end defun
332 @defun sort sequence predicate
333 @cindex stable sort
334 @cindex sorting lists
335 @cindex sorting vectors
336 This function sorts @var{sequence} stably.  Note that this function doesn't work
337 for all sequences; it may be used only for lists and vectors.  If @var{sequence}
338 is a list, it is modified destructively.  This functions returns the sorted
339 @var{sequence} and compares elements using @var{predicate}.  A stable sort is
340 one in which elements with equal sort keys maintain their relative order before
341 and after the sort.  Stability is important when successive sorts are used to
342 order elements according to different criteria.
344 The argument @var{predicate} must be a function that accepts two
345 arguments.  It is called with two elements of @var{sequence}.  To get an
346 increasing order sort, the @var{predicate} should return non-@code{nil} if the
347 first element is ``less'' than the second, or @code{nil} if not.
349 The comparison function @var{predicate} must give reliable results for
350 any given pair of arguments, at least within a single call to
351 @code{sort}.  It must be @dfn{antisymmetric}; that is, if @var{a} is
352 less than @var{b}, @var{b} must not be less than @var{a}.  It must be
353 @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
354 is less than @var{c}, then @var{a} must be less than @var{c}.  If you
355 use a comparison function which does not meet these requirements, the
356 result of @code{sort} is unpredictable.
358 The destructive aspect of @code{sort} for lists is that it rearranges the
359 cons cells forming @var{sequence} by changing @sc{cdr}s.  A nondestructive
360 sort function would create new cons cells to store the elements in their
361 sorted order.  If you wish to make a sorted copy without destroying the
362 original, copy it first with @code{copy-sequence} and then sort.
364 Sorting does not change the @sc{car}s of the cons cells in @var{sequence};
365 the cons cell that originally contained the element @code{a} in
366 @var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
367 appears in a different position in the list due to the change of
368 @sc{cdr}s.  For example:
370 @example
371 @group
372 (setq nums '(1 3 2 6 5 4 0))
373      @result{} (1 3 2 6 5 4 0)
374 @end group
375 @group
376 (sort nums '<)
377      @result{} (0 1 2 3 4 5 6)
378 @end group
379 @group
380 nums
381      @result{} (1 2 3 4 5 6)
382 @end group
383 @end example
385 @noindent
386 @strong{Warning}: Note that the list in @code{nums} no longer contains
387 0; this is the same cons cell that it was before, but it is no longer
388 the first one in the list.  Don't assume a variable that formerly held
389 the argument now holds the entire sorted list!  Instead, save the result
390 of @code{sort} and use that.  Most often we store the result back into
391 the variable that held the original list:
393 @example
394 (setq nums (sort nums '<))
395 @end example
397 For the better understanding of what stable sort is, consider the following
398 vector example.  After sorting, all items whose @code{car} is 8 are grouped
399 at the beginning of @code{vector}, but their relative order is preserved.
400 All items whose @code{car} is 9 are grouped at the end of @code{vector},
401 but their relative order is also preserved:
403 @example
404 @group
405 (setq
406   vector
407   (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
408           '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")))
409      @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz")
410          (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")]
411 @end group
412 @group
413 (sort vector (lambda (x y) (< (car x) (car y))))
414      @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
415          (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
416 @end group
417 @end example
419 @xref{Sorting}, for more functions that perform sorting.
420 See @code{documentation} in @ref{Accessing Documentation}, for a
421 useful example of @code{sort}.
422 @end defun
424 @cindex sequence functions in seq
425 @cindex seq library
426   The @file{seq.el} library provides the following additional sequence
427 manipulation macros and functions, prefixed with @code{seq-}.  To use
428 them, you must first load the @file{seq} library.
430   All functions defined in this library are free of side-effects;
431 i.e., they do not modify any sequence (list, vector, or string) that
432 you pass as an argument.  Unless otherwise stated, the result is a
433 sequence of the same type as the input.  For those functions that take
434 a predicate, this should be a function of one argument.
436   The @file{seq.el} library can be extended to work with additional
437 types of sequential data-structures.  For that purpose, all functions
438 are defined using @code{cl-defgeneric}.  @xref{Generic Functions}, for
439 more details about using @code{cl-defgeneric} for adding extensions.
441 @defun seq-elt sequence index
442   This function returns the element of @var{sequence} at the specified
443 @var{index}, which is an integer whose valid value range is zero to
444 one less than the length of @var{sequence}.  For out-of-range values
445 on built-in sequence types, @code{seq-elt} behaves like @code{elt}.
446 For the details, see @ref{Definition of elt}.
448 @example
449 @group
450 (seq-elt [1 2 3 4] 2)
451 @result{} 3
452 @end group
453 @end example
455   @code{seq-elt} returns places settable using @code{setf}
456 (@pxref{Setting Generalized Variables}).
458 @example
459 @group
460 (setq vec [1 2 3 4])
461 (setf (seq-elt vec 2) 5)
463 @result{} [1 2 5 4]
464 @end group
465 @end example
466 @end defun
468 @defun seq-length sequence
469   This function returns the number of elements in @var{sequence}.  For
470 built-in sequence types, @code{seq-length} behaves like @code{length}.
471 @xref{Definition of length}.
472 @end defun
474 @defun seqp sequence
475   This function returns non-@code{nil} if @var{sequence} is a sequence
476 (a list or array), or any additional type of sequence defined via
477 @file{seq.el} generic functions.
479 @example
480 @group
481 (seqp [1 2])
482 @result{} t
483 @end group
484 @group
485 (seqp 2)
486 @result{} nil
487 @end group
488 @end example
489 @end defun
491 @defun seq-drop sequence n
492   This function returns all but the first @var{n} (an integer)
493 elements of @var{sequence}.  If @var{n} is negative or zero,
494 the result is @var{sequence}.
496 @example
497 @group
498 (seq-drop [1 2 3 4 5 6] 3)
499 @result{} [4 5 6]
500 @end group
501 @group
502 (seq-drop "hello world" -4)
503 @result{} "hello world"
504 @end group
505 @end example
506 @end defun
508 @defun seq-take sequence n
509   This function returns the first @var{n} (an integer) elements of
510 @var{sequence}.  If @var{n} is negative or zero, the result
511 is @code{nil}.
513 @example
514 @group
515 (seq-take '(1 2 3 4) 3)
516 @result{} (1 2 3)
517 @end group
518 @group
519 (seq-take [1 2 3 4] 0)
520 @result{} []
521 @end group
522 @end example
523 @end defun
525 @defun seq-take-while predicate sequence
526   This function returns the members of @var{sequence} in order,
527 stopping before the first one for which @var{predicate} returns @code{nil}.
529 @example
530 @group
531 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
532 @result{} (1 2 3)
533 @end group
534 @group
535 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
536 @result{} []
537 @end group
538 @end example
539 @end defun
541 @defun seq-drop-while predicate sequence
542   This function returns the members of @var{sequence} in order,
543 starting from the first one for which @var{predicate} returns @code{nil}.
545 @example
546 @group
547 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
548 @result{} (-1 -2)
549 @end group
550 @group
551 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
552 @result{} [1 4 6]
553 @end group
554 @end example
555 @end defun
557 @defun seq-do function sequence
558   This function applies @var{function} to each element of
559 @var{sequence} in turn (presumably for side effects), and returns
560 @var{sequence}.
561 @end defun
563 @defun seq-map function sequence
564   This function returns the result of applying @var{function} to each
565 element of @var{sequence}.  The returned value is a list.
567 @example
568 @group
569 (seq-map #'1+ '(2 4 6))
570 @result{} (3 5 7)
571 @end group
572 @group
573 (seq-map #'symbol-name [foo bar])
574 @result{} ("foo" "bar")
575 @end group
576 @end example
577 @end defun
579 @defun seq-mapn function &rest sequences
580   This function returns the result of applying @var{function} to each
581 element of @var{sequences}.  The arity (@pxref{What Is a Function,
582 sub-arity}) of @var{function} must match the number of sequences.
583 Mapping stops at the end of the shortest sequence, and the returned
584 value is a list.
586 @example
587 @group
588 (seq-mapn #'+ '(2 4 6) '(20 40 60))
589 @result{} (22 44 66)
590 @end group
591 @group
592 (seq-mapn #'concat '("moskito" "bite") ["bee" "sting"])
593 @result{} ("moskitobee" "bitesting")
594 @end group
595 @end example
596 @end defun
598 @defun seq-filter predicate sequence
599 @cindex filtering sequences
600   This function returns a list of all the elements in @var{sequence}
601 for which @var{predicate} returns non-@code{nil}.
603 @example
604 @group
605 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
606 @result{} (1 3 5)
607 @end group
608 @group
609 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
610 @result{} nil
611 @end group
612 @end example
613 @end defun
615 @defun seq-remove predicate sequence
616 @cindex removing from sequences
617   This function returns a list of all the elements in @var{sequence}
618 for which @var{predicate} returns @code{nil}.
620 @example
621 @group
622 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
623 @result{} (-1 -3)
624 @end group
625 @group
626 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
627 @result{} nil
628 @end group
629 @end example
630 @end defun
632 @defun seq-reduce function sequence initial-value
633 @cindex reducing sequences
634   This function returns the result of calling @var{function} with
635 @var{initial-value} and the first element of @var{sequence}, then calling
636 @var{function} with that result and the second element of @var{sequence},
637 then with that result and the third element of @var{sequence}, etc.
638 @var{function} should be a function of two arguments.  If
639 @var{sequence} is empty, this returns @var{initial-value} without
640 calling @var{function}.
642 @example
643 @group
644 (seq-reduce #'+ [1 2 3 4] 0)
645 @result{} 10
646 @end group
647 @group
648 (seq-reduce #'+ '(1 2 3 4) 5)
649 @result{} 15
650 @end group
651 @group
652 (seq-reduce #'+ '() 3)
653 @result{} 3
654 @end group
655 @end example
656 @end defun
658 @defun seq-some predicate sequence
659   This function returns the first non-@code{nil} value returned by
660 applying @var{predicate} to each element of @var{sequence} in turn.
662 @example
663 @group
664 (seq-some #'numberp ["abc" 1 nil])
665 @result{} t
666 @end group
667 @group
668 (seq-some #'numberp ["abc" "def"])
669 @result{} nil
670 @end group
671 @group
672 (seq-some #'null ["abc" 1 nil])
673 @result{} t
674 @end group
675 @group
676 (seq-some #'1+ [2 4 6])
677 @result{} 3
678 @end group
679 @end example
680 @end defun
682 @defun seq-find predicate sequence &optional default
683   This function returns the first element in @var{sequence} for which
684 @var{predicate} returns non-@code{nil}.  If no element matches
685 @var{predicate}, the function returns @var{default}.
687 Note that this function has an ambiguity if the found element is
688 identical to @var{default}, as in that case it cannot be known whether
689 an element was found or not.
691 @example
692 @group
693 (seq-find #'numberp ["abc" 1 nil])
694 @result{} 1
695 @end group
696 @group
697 (seq-find #'numberp ["abc" "def"])
698 @result{} nil
699 @end group
700 @end example
701 @end defun
703 @defun seq-every-p predicate sequence
704   This function returns non-@code{nil} if applying @var{predicate}
705 to every element of @var{sequence} returns non-@code{nil}.
707 @example
708 @group
709 (seq-every-p #'numberp [2 4 6])
710 @result{} t
711 @end group
712 @group
713 (seq-some #'numberp [2 4 "6"])
714 @result{} nil
715 @end group
716 @end example
717 @end defun
719 @defun seq-empty-p sequence
720   This function returns non-@code{nil} if @var{sequence} is empty.
722 @example
723 @group
724 (seq-empty-p "not empty")
725 @result{} nil
726 @end group
727 @group
728 (seq-empty-p "")
729 @result{} t
730 @end group
731 @end example
732 @end defun
734 @defun seq-count predicate sequence
735   This function returns the number of elements in @var{sequence} for which
736 @var{predicate} returns non-@code{nil}.
738 @example
739 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
740 @result{} 2
741 @end example
742 @end defun
744 @cindex sorting sequences
745 @defun seq-sort function sequence
746   This function returns a copy of @var{sequence} that is sorted
747 according to @var{function}, a function of two arguments that returns
748 non-@code{nil} if the first argument should sort before the second.
749 @end defun
751 @defun seq-contains sequence elt &optional function
752   This function returns the first element in @var{sequence} that is equal to
753 @var{elt}.  If the optional argument @var{function} is non-@code{nil},
754 it is a function of two arguments to use instead of the default @code{equal}.
756 @example
757 @group
758 (seq-contains '(symbol1 symbol2) 'symbol1)
759 @result{} symbol1
760 @end group
761 @group
762 (seq-contains '(symbol1 symbol2) 'symbol3)
763 @result{} nil
764 @end group
765 @end example
767 @end defun
769 @defun seq-position sequence elt &optional function
770   This function returns the index of the first element in
771 @var{sequence} that is equal to @var{elt}.  If the optional argument
772 @var{function} is non-@code{nil}, it is a function of two arguments to
773 use instead of the default @code{equal}.
775 @example
776 @group
777 (seq-position '(a b c) 'b)
778 @result{} 1
779 @end group
780 @group
781 (seq-position '(a b c) 'd)
782 @result{} nil
783 @end group
784 @end example
785 @end defun
788 @defun seq-uniq sequence &optional function
789   This function returns a list of the elements of @var{sequence} with
790 duplicates removed.  If the optional argument @var{function} is non-@code{nil},
791 it is a function of two arguments to use instead of the default @code{equal}.
793 @example
794 @group
795 (seq-uniq '(1 2 2 1 3))
796 @result{} (1 2 3)
797 @end group
798 @group
799 (seq-uniq '(1 2 2.0 1.0) #'=)
800 @result{} [3 4]
801 @end group
802 @end example
803 @end defun
805 @defun seq-subseq sequence start &optional end
806   This function returns a subset of @var{sequence} from @var{start}
807 to @var{end}, both integers (@var{end} defaults to the last element).
808 If @var{start} or @var{end} is negative, it counts from the end of
809 @var{sequence}.
811 @example
812 @group
813 (seq-subseq '(1 2 3 4 5) 1)
814 @result{} (2 3 4 5)
815 @end group
816 @group
817 (seq-subseq '[1 2 3 4 5] 1 3)
818 @result{} [2 3]
819 @end group
820 @group
821 (seq-subseq '[1 2 3 4 5] -3 -1)
822 @result{} [3 4]
823 @end group
824 @end example
825 @end defun
827 @defun seq-concatenate type &rest sequences
828   This function returns a sequence of type @var{type} made of the
829 concatenation of @var{sequences}.  @var{type} may be: @code{vector},
830 @code{list} or @code{string}.
832 @example
833 @group
834 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
835 @result{} (1 2 3 4 5 6)
836 @end group
837 @group
838 (seq-concatenate 'string "Hello " "world")
839 @result{} "Hello world"
840 @end group
841 @end example
842 @end defun
844 @defun seq-mapcat function sequence &optional type
845   This function returns the result of applying @code{seq-concatenate}
846 to the result of applying @var{function} to each element of
847 @var{sequence}.  The result is a sequence of type @var{type}, or a
848 list if @var{type} is @code{nil}.
850 @example
851 @group
852 (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
853 @result{} (1 2 3 4 5 6)
854 @end group
855 @end example
856 @end defun
858 @defun seq-partition sequence n
859   This function returns a list of the elements of @var{sequence}
860 grouped into sub-sequences of length @var{n}.  The last sequence may
861 contain less elements than @var{n}.  @var{n} must be an integer.  If
862 @var{n} is a negative integer or 0, the return value is @code{nil}.
864 @example
865 @group
866 (seq-partition '(0 1 2 3 4 5 6 7) 3)
867 @result{} ((0 1 2) (3 4 5) (6 7))
868 @end group
869 @end example
870 @end defun
872 @defun seq-intersection sequence1 sequence2 &optional function
873   This function returns a list of the elements that appear both in
874 @var{sequence1} and @var{sequence2}.  If the optional argument
875 @var{function} is non-@code{nil}, it is a function of two arguments to
876 use to compare elements instead of the default @code{equal}.
878 @example
879 @group
880 (seq-intersection [2 3 4 5] [1 3 5 6 7])
881 @result{} (3 5)
882 @end group
883 @end example
884 @end defun
887 @defun seq-difference sequence1 sequence2 &optional function
888   This function returns a list of the elements that appear in
889 @var{sequence1} but not in @var{sequence2}.  If the optional argument
890 @var{function} is non-@code{nil}, it is a function of two arguments to
891 use to compare elements instead of the default @code{equal}.
893 @example
894 @group
895 (seq-difference '(2 3 4 5) [1 3 5 6 7])
896 @result{} (2 4)
897 @end group
898 @end example
899 @end defun
901 @defun seq-group-by function sequence
902   This function separates the elements of @var{sequence} into an alist
903 whose keys are the result of applying @var{function} to each element
904 of @var{sequence}.  Keys are compared using @code{equal}.
906 @example
907 @group
908 (seq-group-by #'integerp '(1 2.1 3 2 3.2))
909 @result{} ((t 1 3 2) (nil 2.1 3.2))
910 @end group
911 @group
912 (seq-group-by #'car '((a 1) (b 2) (a 3) (c 4)))
913 @result{} ((b (b 2)) (a (a 1) (a 3)) (c (c 4)))
914 @end group
915 @end example
916 @end defun
918 @defun seq-into sequence type
919   This function converts the sequence @var{sequence} into a sequence
920 of type @var{type}.  @var{type} can be one of the following symbols:
921 @code{vector}, @code{string} or @code{list}.
923 @example
924 @group
925 (seq-into [1 2 3] 'list)
926 @result{} (1 2 3)
927 @end group
928 @group
929 (seq-into nil 'vector)
930 @result{} []
931 @end group
932 @group
933 (seq-into "hello" 'vector)
934 @result{} [104 101 108 108 111]
935 @end group
936 @end example
937 @end defun
939 @defun seq-min sequence
940   This function returns the smallest element of @var{sequence}.  The
941 elements of @var{sequence} must be numbers or markers
942 (@pxref{Markers}).
944 @example
945 @group
946 (seq-min [3 1 2])
947 @result{} 1
948 @end group
949 @group
950 (seq-min "Hello")
951 @result{} 72
952 @end group
953 @end example
954 @end defun
956 @defun seq-max sequence
957   This function returns the largest element of @var{sequence}.  The
958 elements of @var{sequence} must be numbers or markers.
960 @example
961 @group
962 (seq-max [1 3 2])
963 @result{} 3
964 @end group
965 @group
966 (seq-max "Hello")
967 @result{} 111
968 @end group
969 @end example
970 @end defun
972 @defmac seq-doseq (var sequence) body@dots{}
973 @cindex sequence iteration
974   This macro is like @code{dolist} (@pxref{Iteration, dolist}), except
975 that @var{sequence} can be a list, vector or string.  This is
976 primarily useful for side-effects.
977 @end defmac
979 @defmac seq-let arguments sequence body@dots{}
980 @cindex sequence destructuring
981   This macro binds the variables defined in @var{arguments} to the
982 elements of @var{sequence}.  @var{arguments} can themselves include
983 sequences, allowing for nested destructuring.
985 The @var{arguments} sequence can also include the @code{&rest} marker
986 followed by a variable name to be bound to the rest of
987 @code{sequence}.
989 @example
990 @group
991 (seq-let [first second] [1 2 3 4]
992   (list first second))
993 @result{} (1 2)
994 @end group
995 @group
996 (seq-let (_ a _ b) '(1 2 3 4)
997   (list a b))
998 @result{} (2 4)
999 @end group
1000 @group
1001 (seq-let [a [b [c]]] [1 [2 [3]]]
1002   (list a b c))
1003 @result{} (1 2 3)
1004 @end group
1005 @group
1006 (seq-let [a b &rest others] [1 2 3 4]
1007   others)
1008 @end group
1009 @result{} [3 4]
1010 @end example
1011 @end defmac
1014 @node Arrays
1015 @section Arrays
1016 @cindex array
1018   An @dfn{array} object has slots that hold a number of other Lisp
1019 objects, called the elements of the array.  Any element of an array
1020 may be accessed in constant time.  In contrast, the time to access an
1021 element of a list is proportional to the position of that element in
1022 the list.
1024   Emacs defines four types of array, all one-dimensional:
1025 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
1026 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
1027 @dfn{char-tables} (@pxref{Char-Table Type}).  Vectors and char-tables
1028 can hold elements of any type, but strings can only hold characters,
1029 and bool-vectors can only hold @code{t} and @code{nil}.
1031   All four kinds of array share these characteristics:
1033 @itemize @bullet
1034 @item
1035 The first element of an array has index zero, the second element has
1036 index 1, and so on.  This is called @dfn{zero-origin} indexing.  For
1037 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
1039 @item
1040 The length of the array is fixed once you create it; you cannot
1041 change the length of an existing array.
1043 @item
1044 For purposes of evaluation, the array is a constant---i.e.,
1045 it evaluates to itself.
1047 @item
1048 The elements of an array may be referenced or changed with the functions
1049 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
1050 @end itemize
1052     When you create an array, other than a char-table, you must specify
1053 its length.  You cannot specify the length of a char-table, because that
1054 is determined by the range of character codes.
1056   In principle, if you want an array of text characters, you could use
1057 either a string or a vector.  In practice, we always choose strings for
1058 such applications, for four reasons:
1060 @itemize @bullet
1061 @item
1062 They occupy one-fourth the space of a vector of the same elements.
1064 @item
1065 Strings are printed in a way that shows the contents more clearly
1066 as text.
1068 @item
1069 Strings can hold text properties.  @xref{Text Properties}.
1071 @item
1072 Many of the specialized editing and I/O facilities of Emacs accept only
1073 strings.  For example, you cannot insert a vector of characters into a
1074 buffer the way you can insert a string.  @xref{Strings and Characters}.
1075 @end itemize
1077   By contrast, for an array of keyboard input characters (such as a key
1078 sequence), a vector may be necessary, because many keyboard input
1079 characters are outside the range that will fit in a string.  @xref{Key
1080 Sequence Input}.
1082 @node Array Functions
1083 @section Functions that Operate on Arrays
1085   In this section, we describe the functions that accept all types of
1086 arrays.
1088 @defun arrayp object
1089 This function returns @code{t} if @var{object} is an array (i.e., a
1090 vector, a string, a bool-vector or a char-table).
1092 @example
1093 @group
1094 (arrayp [a])
1095      @result{} t
1096 (arrayp "asdf")
1097      @result{} t
1098 (arrayp (syntax-table))    ;; @r{A char-table.}
1099      @result{} t
1100 @end group
1101 @end example
1102 @end defun
1104 @defun aref array index
1105 @cindex array elements
1106 This function returns the @var{index}th element of @var{array}.  The
1107 first element is at index zero.
1109 @example
1110 @group
1111 (setq primes [2 3 5 7 11 13])
1112      @result{} [2 3 5 7 11 13]
1113 (aref primes 4)
1114      @result{} 11
1115 @end group
1116 @group
1117 (aref "abcdefg" 1)
1118      @result{} 98           ; @r{@samp{b} is @acronym{ASCII} code 98.}
1119 @end group
1120 @end example
1122 See also the function @code{elt}, in @ref{Sequence Functions}.
1123 @end defun
1125 @defun aset array index object
1126 This function sets the @var{index}th element of @var{array} to be
1127 @var{object}.  It returns @var{object}.
1129 @example
1130 @group
1131 (setq w [foo bar baz])
1132      @result{} [foo bar baz]
1133 (aset w 0 'fu)
1134      @result{} fu
1136      @result{} [fu bar baz]
1137 @end group
1139 @group
1140 (setq x "asdfasfd")
1141      @result{} "asdfasfd"
1142 (aset x 3 ?Z)
1143      @result{} 90
1145      @result{} "asdZasfd"
1146 @end group
1147 @end example
1149 If @var{array} is a string and @var{object} is not a character, a
1150 @code{wrong-type-argument} error results.  The function converts a
1151 unibyte string to multibyte if necessary to insert a character.
1152 @end defun
1154 @defun fillarray array object
1155 This function fills the array @var{array} with @var{object}, so that
1156 each element of @var{array} is @var{object}.  It returns @var{array}.
1158 @example
1159 @group
1160 (setq a [a b c d e f g])
1161      @result{} [a b c d e f g]
1162 (fillarray a 0)
1163      @result{} [0 0 0 0 0 0 0]
1165      @result{} [0 0 0 0 0 0 0]
1166 @end group
1167 @group
1168 (setq s "When in the course")
1169      @result{} "When in the course"
1170 (fillarray s ?-)
1171      @result{} "------------------"
1172 @end group
1173 @end example
1175 If @var{array} is a string and @var{object} is not a character, a
1176 @code{wrong-type-argument} error results.
1177 @end defun
1179 The general sequence functions @code{copy-sequence} and @code{length}
1180 are often useful for objects known to be arrays.  @xref{Sequence Functions}.
1182 @node Vectors
1183 @section Vectors
1184 @cindex vector (type)
1186   A @dfn{vector} is a general-purpose array whose elements can be any
1187 Lisp objects.  (By contrast, the elements of a string can only be
1188 characters.  @xref{Strings and Characters}.)  Vectors are used in
1189 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
1190 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
1191 representation of a byte-compiled function (@pxref{Byte Compilation}),
1192 and more.
1194   Like other arrays, vectors use zero-origin indexing: the first
1195 element has index 0.
1197   Vectors are printed with square brackets surrounding the elements.
1198 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
1199 @code{a} is printed as @code{[a b a]}.  You can write vectors in the
1200 same way in Lisp input.
1202   A vector, like a string or a number, is considered a constant for
1203 evaluation: the result of evaluating it is the same vector.  This does
1204 not evaluate or even examine the elements of the vector.
1205 @xref{Self-Evaluating Forms}.
1207   Here are examples illustrating these principles:
1209 @example
1210 @group
1211 (setq avector [1 two '(three) "four" [five]])
1212      @result{} [1 two (quote (three)) "four" [five]]
1213 (eval avector)
1214      @result{} [1 two (quote (three)) "four" [five]]
1215 (eq avector (eval avector))
1216      @result{} t
1217 @end group
1218 @end example
1220 @node Vector Functions
1221 @section Functions for Vectors
1223   Here are some functions that relate to vectors:
1225 @defun vectorp object
1226 This function returns @code{t} if @var{object} is a vector.
1228 @example
1229 @group
1230 (vectorp [a])
1231      @result{} t
1232 (vectorp "asdf")
1233      @result{} nil
1234 @end group
1235 @end example
1236 @end defun
1238 @defun vector &rest objects
1239 This function creates and returns a vector whose elements are the
1240 arguments, @var{objects}.
1242 @example
1243 @group
1244 (vector 'foo 23 [bar baz] "rats")
1245      @result{} [foo 23 [bar baz] "rats"]
1246 (vector)
1247      @result{} []
1248 @end group
1249 @end example
1250 @end defun
1252 @defun make-vector length object
1253 This function returns a new vector consisting of @var{length} elements,
1254 each initialized to @var{object}.
1256 @example
1257 @group
1258 (setq sleepy (make-vector 9 'Z))
1259      @result{} [Z Z Z Z Z Z Z Z Z]
1260 @end group
1261 @end example
1262 @end defun
1264 @defun vconcat &rest sequences
1265 @cindex copying vectors
1266 This function returns a new vector containing all the elements of
1267 @var{sequences}.  The arguments @var{sequences} may be true lists,
1268 vectors, strings or bool-vectors.  If no @var{sequences} are given,
1269 the empty vector is returned.
1271 The value is either the empty vector, or is a newly constructed
1272 nonempty vector that is not @code{eq} to any existing vector.
1274 @example
1275 @group
1276 (setq a (vconcat '(A B C) '(D E F)))
1277      @result{} [A B C D E F]
1278 (eq a (vconcat a))
1279      @result{} nil
1280 @end group
1281 @group
1282 (vconcat)
1283      @result{} []
1284 (vconcat [A B C] "aa" '(foo (6 7)))
1285      @result{} [A B C 97 97 foo (6 7)]
1286 @end group
1287 @end example
1289 The @code{vconcat} function also allows byte-code function objects as
1290 arguments.  This is a special feature to make it easy to access the entire
1291 contents of a byte-code function object.  @xref{Byte-Code Objects}.
1293 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
1294 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
1295 in @ref{Building Lists}.
1296 @end defun
1298   The @code{append} function also provides a way to convert a vector into a
1299 list with the same elements:
1301 @example
1302 @group
1303 (setq avector [1 two (quote (three)) "four" [five]])
1304      @result{} [1 two (quote (three)) "four" [five]]
1305 (append avector nil)
1306      @result{} (1 two (quote (three)) "four" [five])
1307 @end group
1308 @end example
1310 @node Char-Tables
1311 @section Char-Tables
1312 @cindex char-tables
1313 @cindex extra slots of char-table
1315   A char-table is much like a vector, except that it is indexed by
1316 character codes.  Any valid character code, without modifiers, can be
1317 used as an index in a char-table.  You can access a char-table's
1318 elements with @code{aref} and @code{aset}, as with any array.  In
1319 addition, a char-table can have @dfn{extra slots} to hold additional
1320 data not associated with particular character codes.  Like vectors,
1321 char-tables are constants when evaluated, and can hold elements of any
1322 type.
1324 @cindex subtype of char-table
1325   Each char-table has a @dfn{subtype}, a symbol, which serves two
1326 purposes:
1328 @itemize @bullet
1329 @item
1330 The subtype provides an easy way to tell what the char-table is for.
1331 For instance, display tables are char-tables with @code{display-table}
1332 as the subtype, and syntax tables are char-tables with
1333 @code{syntax-table} as the subtype.  The subtype can be queried using
1334 the function @code{char-table-subtype}, described below.
1336 @item
1337 The subtype controls the number of @dfn{extra slots} in the
1338 char-table.  This number is specified by the subtype's
1339 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1340 Properties}), whose value should be an integer between 0 and 10.  If
1341 the subtype has no such symbol property, the char-table has no extra
1342 slots.
1343 @end itemize
1345 @cindex parent of char-table
1346   A char-table can have a @dfn{parent}, which is another char-table.  If
1347 it does, then whenever the char-table specifies @code{nil} for a
1348 particular character @var{c}, it inherits the value specified in the
1349 parent.  In other words, @code{(aref @var{char-table} @var{c})} returns
1350 the value from the parent of @var{char-table} if @var{char-table} itself
1351 specifies @code{nil}.
1353 @cindex default value of char-table
1354   A char-table can also have a @dfn{default value}.  If so, then
1355 @code{(aref @var{char-table} @var{c})} returns the default value
1356 whenever the char-table does not specify any other non-@code{nil} value.
1358 @defun make-char-table subtype &optional init
1359 Return a newly-created char-table, with subtype @var{subtype} (a
1360 symbol).  Each element is initialized to @var{init}, which defaults to
1361 @code{nil}.  You cannot alter the subtype of a char-table after the
1362 char-table is created.
1364 There is no argument to specify the length of the char-table, because
1365 all char-tables have room for any valid character code as an index.
1367 If @var{subtype} has the @code{char-table-extra-slots} symbol
1368 property, that specifies the number of extra slots in the char-table.
1369 This should be an integer between 0 and 10; otherwise,
1370 @code{make-char-table} raises an error.  If @var{subtype} has no
1371 @code{char-table-extra-slots} symbol property (@pxref{Property
1372 Lists}), the char-table has no extra slots.
1373 @end defun
1375 @defun char-table-p object
1376 This function returns @code{t} if @var{object} is a char-table, and
1377 @code{nil} otherwise.
1378 @end defun
1380 @defun char-table-subtype char-table
1381 This function returns the subtype symbol of @var{char-table}.
1382 @end defun
1384 There is no special function to access default values in a char-table.
1385 To do that, use @code{char-table-range} (see below).
1387 @defun char-table-parent char-table
1388 This function returns the parent of @var{char-table}.  The parent is
1389 always either @code{nil} or another char-table.
1390 @end defun
1392 @defun set-char-table-parent char-table new-parent
1393 This function sets the parent of @var{char-table} to @var{new-parent}.
1394 @end defun
1396 @defun char-table-extra-slot char-table n
1397 This function returns the contents of extra slot @var{n} (zero based)
1398 of @var{char-table}.  The number of extra slots in a char-table is
1399 determined by its subtype.
1400 @end defun
1402 @defun set-char-table-extra-slot char-table n value
1403 This function stores @var{value} in extra slot @var{n} (zero based) of
1404 @var{char-table}.
1405 @end defun
1407   A char-table can specify an element value for a single character code;
1408 it can also specify a value for an entire character set.
1410 @defun char-table-range char-table range
1411 This returns the value specified in @var{char-table} for a range of
1412 characters @var{range}.  Here are the possibilities for @var{range}:
1414 @table @asis
1415 @item @code{nil}
1416 Refers to the default value.
1418 @item @var{char}
1419 Refers to the element for character @var{char}
1420 (supposing @var{char} is a valid character code).
1422 @item @code{(@var{from} . @var{to})}
1423 A cons cell refers to all the characters in the inclusive range
1424 @samp{[@var{from}..@var{to}]}.
1425 @end table
1426 @end defun
1428 @defun set-char-table-range char-table range value
1429 This function sets the value in @var{char-table} for a range of
1430 characters @var{range}.  Here are the possibilities for @var{range}:
1432 @table @asis
1433 @item @code{nil}
1434 Refers to the default value.
1436 @item @code{t}
1437 Refers to the whole range of character codes.
1439 @item @var{char}
1440 Refers to the element for character @var{char}
1441 (supposing @var{char} is a valid character code).
1443 @item @code{(@var{from} . @var{to})}
1444 A cons cell refers to all the characters in the inclusive range
1445 @samp{[@var{from}..@var{to}]}.
1446 @end table
1447 @end defun
1449 @defun map-char-table function char-table
1450 This function calls its argument @var{function} for each element of
1451 @var{char-table} that has a non-@code{nil} value.  The call to
1452 @var{function} is with two arguments, a key and a value.  The key
1453 is a possible @var{range} argument for @code{char-table-range}---either
1454 a valid character or a cons cell @code{(@var{from} . @var{to})},
1455 specifying a range of characters that share the same value.  The value is
1456 what @code{(char-table-range @var{char-table} @var{key})} returns.
1458 Overall, the key-value pairs passed to @var{function} describe all the
1459 values stored in @var{char-table}.
1461 The return value is always @code{nil}; to make calls to
1462 @code{map-char-table} useful, @var{function} should have side effects.
1463 For example, here is how to examine the elements of the syntax table:
1465 @example
1466 (let (accumulator)
1467    (map-char-table
1468     #'(lambda (key value)
1469         (setq accumulator
1470               (cons (list
1471                      (if (consp key)
1472                          (list (car key) (cdr key))
1473                        key)
1474                      value)
1475                     accumulator)))
1476     (syntax-table))
1477    accumulator)
1478 @result{}
1479 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1480  ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1481  ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1482 @end example
1483 @end defun
1485 @node Bool-Vectors
1486 @section Bool-vectors
1487 @cindex Bool-vectors
1489   A bool-vector is much like a vector, except that it stores only the
1490 values @code{t} and @code{nil}.  If you try to store any non-@code{nil}
1491 value into an element of the bool-vector, the effect is to store
1492 @code{t} there.  As with all arrays, bool-vector indices start from 0,
1493 and the length cannot be changed once the bool-vector is created.
1494 Bool-vectors are constants when evaluated.
1496   Several functions work specifically with bool-vectors; aside
1497 from that, you manipulate them with same functions used for other kinds
1498 of arrays.
1500 @defun make-bool-vector length initial
1501 Return a new bool-vector of @var{length} elements,
1502 each one initialized to @var{initial}.
1503 @end defun
1505 @defun bool-vector &rest objects
1506 This function creates and returns a bool-vector whose elements are the
1507 arguments, @var{objects}.
1508 @end defun
1510 @defun bool-vector-p object
1511 This returns @code{t} if @var{object} is a bool-vector,
1512 and @code{nil} otherwise.
1513 @end defun
1515 There are also some bool-vector set operation functions, described below:
1517 @defun bool-vector-exclusive-or a b &optional c
1518 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1519 If optional argument @var{c} is given, the result of this operation is
1520 stored into @var{c}.  All arguments should be bool vectors of the same length.
1521 @end defun
1523 @defun bool-vector-union a b &optional c
1524 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}.  If
1525 optional argument @var{c} is given, the result of this operation is
1526 stored into @var{c}.  All arguments should be bool vectors of the same length.
1527 @end defun
1529 @defun bool-vector-intersection a b &optional c
1530 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}.  If
1531 optional argument @var{c} is given, the result of this operation is
1532 stored into @var{c}.  All arguments should be bool vectors of the same length.
1533 @end defun
1535 @defun bool-vector-set-difference a b &optional c
1536 Return @dfn{set difference} of bool vectors @var{a} and @var{b}.  If
1537 optional argument @var{c} is given, the result of this operation is
1538 stored into @var{c}.  All arguments should be bool vectors of the same length.
1539 @end defun
1541 @defun bool-vector-not a &optional b
1542 Return @dfn{set complement} of bool vector @var{a}.  If optional
1543 argument @var{b} is given, the result of this operation is stored into
1544 @var{b}.  All arguments should be bool vectors of the same length.
1545 @end defun
1547 @defun bool-vector-subsetp a b
1548 Return @code{t} if every @code{t} value in @var{a} is also t in
1549 @var{b}, @code{nil} otherwise.  All arguments should be bool vectors of the
1550 same length.
1551 @end defun
1553 @defun bool-vector-count-consecutive a b i
1554 Return the number of consecutive elements in @var{a} equal @var{b}
1555 starting at @var{i}.  @code{a} is a bool vector, @var{b} is @code{t}
1556 or @code{nil}, and @var{i} is an index into @code{a}.
1557 @end defun
1559 @defun bool-vector-count-population a
1560 Return the number of elements that are @code{t} in bool vector @var{a}.
1561 @end defun
1563   The printed form represents up to 8 boolean values as a single
1564 character:
1566 @example
1567 @group
1568 (bool-vector t nil t nil)
1569      @result{} #&4"^E"
1570 (bool-vector)
1571      @result{} #&0""
1572 @end group
1573 @end example
1575 You can use @code{vconcat} to print a bool-vector like other vectors:
1577 @example
1578 @group
1579 (vconcat (bool-vector nil t nil t))
1580      @result{} [nil t nil t]
1581 @end group
1582 @end example
1584   Here is another example of creating, examining, and updating a
1585 bool-vector:
1587 @example
1588 (setq bv (make-bool-vector 5 t))
1589      @result{} #&5"^_"
1590 (aref bv 1)
1591      @result{} t
1592 (aset bv 3 nil)
1593      @result{} nil
1595      @result{} #&5"^W"
1596 @end example
1598 @noindent
1599 These results make sense because the binary codes for control-_ and
1600 control-W are 11111 and 10111, respectively.
1602 @node Rings
1603 @section Managing a Fixed-Size Ring of Objects
1605 @cindex ring data structure
1606   A @dfn{ring} is a fixed-size data structure that supports insertion,
1607 deletion, rotation, and modulo-indexed reference and traversal.  An
1608 efficient ring data structure is implemented by the @code{ring}
1609 package.  It provides the functions listed in this section.
1611   Note that several rings in Emacs, like the kill ring and the
1612 mark ring, are actually implemented as simple lists, @emph{not} using
1613 the @code{ring} package; thus the following functions won't work on
1614 them.
1616 @defun make-ring size
1617 This returns a new ring capable of holding @var{size} objects.
1618 @var{size} should be an integer.
1619 @end defun
1621 @defun ring-p object
1622 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1623 @end defun
1625 @defun ring-size ring
1626 This returns the maximum capacity of the @var{ring}.
1627 @end defun
1629 @defun ring-length ring
1630 This returns the number of objects that @var{ring} currently contains.
1631 The value will never exceed that returned by @code{ring-size}.
1632 @end defun
1634 @defun ring-elements ring
1635 This returns a list of the objects in @var{ring}, in order, newest first.
1636 @end defun
1638 @defun ring-copy ring
1639 This returns a new ring which is a copy of @var{ring}.
1640 The new ring contains the same (@code{eq}) objects as @var{ring}.
1641 @end defun
1643 @defun ring-empty-p ring
1644 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1645 @end defun
1647   The newest element in the ring always has index 0.  Higher indices
1648 correspond to older elements.  Indices are computed modulo the ring
1649 length.  Index @minus{}1 corresponds to the oldest element, @minus{}2
1650 to the next-oldest, and so forth.
1652 @defun ring-ref ring index
1653 This returns the object in @var{ring} found at index @var{index}.
1654 @var{index} may be negative or greater than the ring length.  If
1655 @var{ring} is empty, @code{ring-ref} signals an error.
1656 @end defun
1658 @defun ring-insert ring object
1659 This inserts @var{object} into @var{ring}, making it the newest
1660 element, and returns @var{object}.
1662 If the ring is full, insertion removes the oldest element to
1663 make room for the new element.
1664 @end defun
1666 @defun ring-remove ring &optional index
1667 Remove an object from @var{ring}, and return that object.  The
1668 argument @var{index} specifies which item to remove; if it is
1669 @code{nil}, that means to remove the oldest item.  If @var{ring} is
1670 empty, @code{ring-remove} signals an error.
1671 @end defun
1673 @defun ring-insert-at-beginning ring object
1674 This inserts @var{object} into @var{ring}, treating it as the oldest
1675 element.  The return value is not significant.
1677 If the ring is full, this function removes the newest element to make
1678 room for the inserted element.
1679 @end defun
1681 @cindex fifo data structure
1682   If you are careful not to exceed the ring size, you can
1683 use the ring as a first-in-first-out queue.  For example:
1685 @lisp
1686 (let ((fifo (make-ring 5)))
1687   (mapc (lambda (obj) (ring-insert fifo obj))
1688         '(0 one "two"))
1689   (list (ring-remove fifo) t
1690         (ring-remove fifo) t
1691         (ring-remove fifo)))
1692      @result{} (0 t one t "two")
1693 @end lisp