Drop face from hi-lock--unused-faces only when used
[emacs.git] / doc / lispref / sequences.texi
blob93e8fa8a5fa6caa156ce42f60cc02ccc88cf55c2
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2017 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 seqr
155 @cindex copying sequences
156 This function returns a copy of @var{seqr}, which should be either a
157 sequence or a record.  The copy is the same type of object as the
158 original, and it has the same elements in the same order.
160 Storing a new element into the copy does not affect the original
161 @var{seqr}, and vice versa.  However, the elements of the copy
162 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 copy, are also visible in the original.
166 If the argument 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}.
171 This function does not work for dotted lists.  Trying to copy a
172 circular list may cause an infinite loop.
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.
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
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
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
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
221 @defun reverse sequence
222 @cindex string reverse
223 @cindex list reverse
224 @cindex vector reverse
225 @cindex sequence reverse
226 This function creates a new sequence whose elements are the elements
227 of @var{sequence}, but in reverse order.  The original argument @var{sequence}
228 is @emph{not} altered.  Note that char-tables cannot be reversed.
230 @example
231 @group
232 (setq x '(1 2 3 4))
233      @result{} (1 2 3 4)
234 @end group
235 @group
236 (reverse x)
237      @result{} (4 3 2 1)
239      @result{} (1 2 3 4)
240 @end group
241 @group
242 (setq x [1 2 3 4])
243      @result{} [1 2 3 4]
244 @end group
245 @group
246 (reverse x)
247      @result{} [4 3 2 1]
249      @result{} [1 2 3 4]
250 @end group
251 @group
252 (setq x "xyzzy")
253      @result{} "xyzzy"
254 @end group
255 @group
256 (reverse x)
257      @result{} "yzzyx"
259      @result{} "xyzzy"
260 @end group
261 @end example
262 @end defun
264 @defun nreverse sequence
265 @cindex reversing a string
266 @cindex reversing a list
267 @cindex reversing a vector
268   This function reverses the order of the elements of @var{sequence}.
269 Unlike @code{reverse} the original @var{sequence} may be modified.
271   For example:
273 @example
274 @group
275 (setq x '(a b c))
276      @result{} (a b c)
277 @end group
278 @group
280      @result{} (a b c)
281 (nreverse x)
282      @result{} (c b a)
283 @end group
284 @group
285 ;; @r{The cons cell that was first is now last.}
287      @result{} (a)
288 @end group
289 @end example
291   To avoid confusion, we usually store the result of @code{nreverse}
292 back in the same variable which held the original list:
294 @example
295 (setq x (nreverse x))
296 @end example
298   Here is the @code{nreverse} of our favorite example, @code{(a b c)},
299 presented graphically:
301 @smallexample
302 @group
303 @r{Original list head:}                       @r{Reversed list:}
304  -------------        -------------        ------------
305 | car  | cdr  |      | car  | cdr  |      | car | cdr  |
306 |   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
307 |      |      |   |  |      |   |  |   |  |     |   |  |
308  -------------    |   --------- | -    |   -------- | -
309                   |             |      |            |
310                    -------------        ------------
311 @end group
312 @end smallexample
314   For the vector, it is even simpler because you don't need setq:
316 @example
317 (setq x [1 2 3 4])
318      @result{} [1 2 3 4]
319 (nreverse x)
320      @result{} [4 3 2 1]
322      @result{} [4 3 2 1]
323 @end example
325 Note that unlike @code{reverse}, this function doesn't work with strings.
326 Although you can alter string data by using @code{aset}, it is strongly
327 encouraged to treat strings as immutable.
329 @end defun
331 @defun sort sequence predicate
332 @cindex stable sort
333 @cindex sorting lists
334 @cindex sorting vectors
335 This function sorts @var{sequence} stably.  Note that this function doesn't work
336 for all sequences; it may be used only for lists and vectors.  If @var{sequence}
337 is a list, it is modified destructively.  This functions returns the sorted
338 @var{sequence} and compares elements using @var{predicate}.  A stable sort is
339 one in which elements with equal sort keys maintain their relative order before
340 and after the sort.  Stability is important when successive sorts are used to
341 order elements according to different criteria.
343 The argument @var{predicate} must be a function that accepts two
344 arguments.  It is called with two elements of @var{sequence}.  To get an
345 increasing order sort, the @var{predicate} should return non-@code{nil} if the
346 first element is ``less'' than the second, or @code{nil} if not.
348 The comparison function @var{predicate} must give reliable results for
349 any given pair of arguments, at least within a single call to
350 @code{sort}.  It must be @dfn{antisymmetric}; that is, if @var{a} is
351 less than @var{b}, @var{b} must not be less than @var{a}.  It must be
352 @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
353 is less than @var{c}, then @var{a} must be less than @var{c}.  If you
354 use a comparison function which does not meet these requirements, the
355 result of @code{sort} is unpredictable.
357 The destructive aspect of @code{sort} for lists is that it rearranges the
358 cons cells forming @var{sequence} by changing @sc{cdr}s.  A nondestructive
359 sort function would create new cons cells to store the elements in their
360 sorted order.  If you wish to make a sorted copy without destroying the
361 original, copy it first with @code{copy-sequence} and then sort.
363 Sorting does not change the @sc{car}s of the cons cells in @var{sequence};
364 the cons cell that originally contained the element @code{a} in
365 @var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
366 appears in a different position in the list due to the change of
367 @sc{cdr}s.  For example:
369 @example
370 @group
371 (setq nums '(1 3 2 6 5 4 0))
372      @result{} (1 3 2 6 5 4 0)
373 @end group
374 @group
375 (sort nums '<)
376      @result{} (0 1 2 3 4 5 6)
377 @end group
378 @group
379 nums
380      @result{} (1 2 3 4 5 6)
381 @end group
382 @end example
384 @noindent
385 @strong{Warning}: Note that the list in @code{nums} no longer contains
386 0; this is the same cons cell that it was before, but it is no longer
387 the first one in the list.  Don't assume a variable that formerly held
388 the argument now holds the entire sorted list!  Instead, save the result
389 of @code{sort} and use that.  Most often we store the result back into
390 the variable that held the original list:
392 @example
393 (setq nums (sort nums '<))
394 @end example
396 For the better understanding of what stable sort is, consider the following
397 vector example.  After sorting, all items whose @code{car} is 8 are grouped
398 at the beginning of @code{vector}, but their relative order is preserved.
399 All items whose @code{car} is 9 are grouped at the end of @code{vector},
400 but their relative order is also preserved:
402 @example
403 @group
404 (setq
405   vector
406   (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
407           '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")))
408      @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz")
409          (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")]
410 @end group
411 @group
412 (sort vector (lambda (x y) (< (car x) (car y))))
413      @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
414          (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
415 @end group
416 @end example
418 @xref{Sorting}, for more functions that perform sorting.
419 See @code{documentation} in @ref{Accessing Documentation}, for a
420 useful example of @code{sort}.
421 @end defun
423 @cindex sequence functions in seq
424 @cindex seq library
425   The @file{seq.el} library provides the following additional sequence
426 manipulation macros and functions, prefixed with @code{seq-}.  To use
427 them, you must first load the @file{seq} library.
429   All functions defined in this library are free of side-effects;
430 i.e., they do not modify any sequence (list, vector, or string) that
431 you pass as an argument.  Unless otherwise stated, the result is a
432 sequence of the same type as the input.  For those functions that take
433 a predicate, this should be a function of one argument.
435   The @file{seq.el} library can be extended to work with additional
436 types of sequential data-structures.  For that purpose, all functions
437 are defined using @code{cl-defgeneric}.  @xref{Generic Functions}, for
438 more details about using @code{cl-defgeneric} for adding extensions.
440 @defun seq-elt sequence index
441   This function returns the element of @var{sequence} at the specified
442 @var{index}, which is an integer whose valid value range is zero to
443 one less than the length of @var{sequence}.  For out-of-range values
444 on built-in sequence types, @code{seq-elt} behaves like @code{elt}.
445 For the details, see @ref{Definition of elt}.
447 @example
448 @group
449 (seq-elt [1 2 3 4] 2)
450 @result{} 3
451 @end group
452 @end example
454   @code{seq-elt} returns places settable using @code{setf}
455 (@pxref{Setting Generalized Variables}).
457 @example
458 @group
459 (setq vec [1 2 3 4])
460 (setf (seq-elt vec 2) 5)
462 @result{} [1 2 5 4]
463 @end group
464 @end example
465 @end defun
467 @defun seq-length sequence
468   This function returns the number of elements in @var{sequence}.  For
469 built-in sequence types, @code{seq-length} behaves like @code{length}.
470 @xref{Definition of length}.
471 @end defun
473 @defun seqp sequence
474   This function returns non-@code{nil} if @var{sequence} is a sequence
475 (a list or array), or any additional type of sequence defined via
476 @file{seq.el} generic functions.
478 @example
479 @group
480 (seqp [1 2])
481 @result{} t
482 @end group
483 @group
484 (seqp 2)
485 @result{} nil
486 @end group
487 @end example
488 @end defun
490 @defun seq-drop sequence n
491   This function returns all but the first @var{n} (an integer)
492 elements of @var{sequence}.  If @var{n} is negative or zero,
493 the result is @var{sequence}.
495 @example
496 @group
497 (seq-drop [1 2 3 4 5 6] 3)
498 @result{} [4 5 6]
499 @end group
500 @group
501 (seq-drop "hello world" -4)
502 @result{} "hello world"
503 @end group
504 @end example
505 @end defun
507 @defun seq-take sequence n
508   This function returns the first @var{n} (an integer) elements of
509 @var{sequence}.  If @var{n} is negative or zero, the result
510 is @code{nil}.
512 @example
513 @group
514 (seq-take '(1 2 3 4) 3)
515 @result{} (1 2 3)
516 @end group
517 @group
518 (seq-take [1 2 3 4] 0)
519 @result{} []
520 @end group
521 @end example
522 @end defun
524 @defun seq-take-while predicate sequence
525   This function returns the members of @var{sequence} in order,
526 stopping before the first one for which @var{predicate} returns @code{nil}.
528 @example
529 @group
530 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
531 @result{} (1 2 3)
532 @end group
533 @group
534 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
535 @result{} []
536 @end group
537 @end example
538 @end defun
540 @defun seq-drop-while predicate sequence
541   This function returns the members of @var{sequence} in order,
542 starting from the first one for which @var{predicate} returns @code{nil}.
544 @example
545 @group
546 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
547 @result{} (-1 -2)
548 @end group
549 @group
550 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
551 @result{} [1 4 6]
552 @end group
553 @end example
554 @end defun
556 @defun seq-do function sequence
557   This function applies @var{function} to each element of
558 @var{sequence} in turn (presumably for side effects), and returns
559 @var{sequence}.
560 @end defun
562 @defun seq-map function sequence
563   This function returns the result of applying @var{function} to each
564 element of @var{sequence}.  The returned value is a list.
566 @example
567 @group
568 (seq-map #'1+ '(2 4 6))
569 @result{} (3 5 7)
570 @end group
571 @group
572 (seq-map #'symbol-name [foo bar])
573 @result{} ("foo" "bar")
574 @end group
575 @end example
576 @end defun
578 @defun seq-map-indexed function sequence
579   This function returns the result of applying @var{function} to each
580 element of @var{sequence} and its index within @var{seq}.  The
581 returned value is a list.
583 @example
584 @group
585 (seq-map-indexed (lambda (elt idx)
586                    (list idx elt))
587                  '(a b c))
588 @result{} ((0 a) (b 1) (c 2))
589 @end group
590 @end example
591 @end defun
593 @defun seq-mapn function &rest sequences
594   This function returns the result of applying @var{function} to each
595 element of @var{sequences}.  The arity (@pxref{What Is a Function,
596 sub-arity}) of @var{function} must match the number of sequences.
597 Mapping stops at the end of the shortest sequence, and the returned
598 value is a list.
600 @example
601 @group
602 (seq-mapn #'+ '(2 4 6) '(20 40 60))
603 @result{} (22 44 66)
604 @end group
605 @group
606 (seq-mapn #'concat '("moskito" "bite") ["bee" "sting"])
607 @result{} ("moskitobee" "bitesting")
608 @end group
609 @end example
610 @end defun
612 @defun seq-filter predicate sequence
613 @cindex filtering sequences
614   This function returns a list of all the elements in @var{sequence}
615 for which @var{predicate} returns non-@code{nil}.
617 @example
618 @group
619 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
620 @result{} (1 3 5)
621 @end group
622 @group
623 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
624 @result{} nil
625 @end group
626 @end example
627 @end defun
629 @defun seq-remove predicate sequence
630 @cindex removing from sequences
631   This function returns a list of all the elements in @var{sequence}
632 for which @var{predicate} returns @code{nil}.
634 @example
635 @group
636 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
637 @result{} (-1 -3)
638 @end group
639 @group
640 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
641 @result{} nil
642 @end group
643 @end example
644 @end defun
646 @defun seq-reduce function sequence initial-value
647 @cindex reducing sequences
648   This function returns the result of calling @var{function} with
649 @var{initial-value} and the first element of @var{sequence}, then calling
650 @var{function} with that result and the second element of @var{sequence},
651 then with that result and the third element of @var{sequence}, etc.
652 @var{function} should be a function of two arguments.  If
653 @var{sequence} is empty, this returns @var{initial-value} without
654 calling @var{function}.
656 @example
657 @group
658 (seq-reduce #'+ [1 2 3 4] 0)
659 @result{} 10
660 @end group
661 @group
662 (seq-reduce #'+ '(1 2 3 4) 5)
663 @result{} 15
664 @end group
665 @group
666 (seq-reduce #'+ '() 3)
667 @result{} 3
668 @end group
669 @end example
670 @end defun
672 @defun seq-some predicate sequence
673   This function returns the first non-@code{nil} value returned by
674 applying @var{predicate} to each element of @var{sequence} in turn.
676 @example
677 @group
678 (seq-some #'numberp ["abc" 1 nil])
679 @result{} t
680 @end group
681 @group
682 (seq-some #'numberp ["abc" "def"])
683 @result{} nil
684 @end group
685 @group
686 (seq-some #'null ["abc" 1 nil])
687 @result{} t
688 @end group
689 @group
690 (seq-some #'1+ [2 4 6])
691 @result{} 3
692 @end group
693 @end example
694 @end defun
696 @defun seq-find predicate sequence &optional default
697   This function returns the first element in @var{sequence} for which
698 @var{predicate} returns non-@code{nil}.  If no element matches
699 @var{predicate}, the function returns @var{default}.
701 Note that this function has an ambiguity if the found element is
702 identical to @var{default}, as in that case it cannot be known whether
703 an element was found or not.
705 @example
706 @group
707 (seq-find #'numberp ["abc" 1 nil])
708 @result{} 1
709 @end group
710 @group
711 (seq-find #'numberp ["abc" "def"])
712 @result{} nil
713 @end group
714 @end example
715 @end defun
717 @defun seq-every-p predicate sequence
718   This function returns non-@code{nil} if applying @var{predicate}
719 to every element of @var{sequence} returns non-@code{nil}.
721 @example
722 @group
723 (seq-every-p #'numberp [2 4 6])
724 @result{} t
725 @end group
726 @group
727 (seq-some #'numberp [2 4 "6"])
728 @result{} nil
729 @end group
730 @end example
731 @end defun
733 @defun seq-empty-p sequence
734   This function returns non-@code{nil} if @var{sequence} is empty.
736 @example
737 @group
738 (seq-empty-p "not empty")
739 @result{} nil
740 @end group
741 @group
742 (seq-empty-p "")
743 @result{} t
744 @end group
745 @end example
746 @end defun
748 @defun seq-count predicate sequence
749   This function returns the number of elements in @var{sequence} for which
750 @var{predicate} returns non-@code{nil}.
752 @example
753 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
754 @result{} 2
755 @end example
756 @end defun
758 @cindex sorting sequences
759 @defun seq-sort function sequence
760   This function returns a copy of @var{sequence} that is sorted
761 according to @var{function}, a function of two arguments that returns
762 non-@code{nil} if the first argument should sort before the second.
763 @end defun
765 @defun seq-sort-by function predicate sequence
766   This function is similar to @code{seq-sort}, but the elements of
767 @var{sequence} are transformed by applying @var{function} on them
768 before being sorted.  @var{function} is a function of one argument.
770 @example
771 (seq-sort-by #'seq-length #'> ["a" "ab" "abc"])
772 @result{} ["abc" "ab" "a"]
773 @end example
774 @end defun
777 @defun seq-contains sequence elt &optional function
778   This function returns the first element in @var{sequence} that is equal to
779 @var{elt}.  If the optional argument @var{function} is non-@code{nil},
780 it is a function of two arguments to use instead of the default @code{equal}.
782 @example
783 @group
784 (seq-contains '(symbol1 symbol2) 'symbol1)
785 @result{} symbol1
786 @end group
787 @group
788 (seq-contains '(symbol1 symbol2) 'symbol3)
789 @result{} nil
790 @end group
791 @end example
793 @end defun
795 @defun seq-position sequence elt &optional function
796   This function returns the index of the first element in
797 @var{sequence} that is equal to @var{elt}.  If the optional argument
798 @var{function} is non-@code{nil}, it is a function of two arguments to
799 use instead of the default @code{equal}.
801 @example
802 @group
803 (seq-position '(a b c) 'b)
804 @result{} 1
805 @end group
806 @group
807 (seq-position '(a b c) 'd)
808 @result{} nil
809 @end group
810 @end example
811 @end defun
814 @defun seq-uniq sequence &optional function
815   This function returns a list of the elements of @var{sequence} with
816 duplicates removed.  If the optional argument @var{function} is non-@code{nil},
817 it is a function of two arguments to use instead of the default @code{equal}.
819 @example
820 @group
821 (seq-uniq '(1 2 2 1 3))
822 @result{} (1 2 3)
823 @end group
824 @group
825 (seq-uniq '(1 2 2.0 1.0) #'=)
826 @result{} [3 4]
827 @end group
828 @end example
829 @end defun
831 @defun seq-subseq sequence start &optional end
832   This function returns a subset of @var{sequence} from @var{start}
833 to @var{end}, both integers (@var{end} defaults to the last element).
834 If @var{start} or @var{end} is negative, it counts from the end of
835 @var{sequence}.
837 @example
838 @group
839 (seq-subseq '(1 2 3 4 5) 1)
840 @result{} (2 3 4 5)
841 @end group
842 @group
843 (seq-subseq '[1 2 3 4 5] 1 3)
844 @result{} [2 3]
845 @end group
846 @group
847 (seq-subseq '[1 2 3 4 5] -3 -1)
848 @result{} [3 4]
849 @end group
850 @end example
851 @end defun
853 @defun seq-concatenate type &rest sequences
854   This function returns a sequence of type @var{type} made of the
855 concatenation of @var{sequences}.  @var{type} may be: @code{vector},
856 @code{list} or @code{string}.
858 @example
859 @group
860 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
861 @result{} (1 2 3 4 5 6)
862 @end group
863 @group
864 (seq-concatenate 'string "Hello " "world")
865 @result{} "Hello world"
866 @end group
867 @end example
868 @end defun
870 @defun seq-mapcat function sequence &optional type
871   This function returns the result of applying @code{seq-concatenate}
872 to the result of applying @var{function} to each element of
873 @var{sequence}.  The result is a sequence of type @var{type}, or a
874 list if @var{type} is @code{nil}.
876 @example
877 @group
878 (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
879 @result{} (1 2 3 4 5 6)
880 @end group
881 @end example
882 @end defun
884 @defun seq-partition sequence n
885   This function returns a list of the elements of @var{sequence}
886 grouped into sub-sequences of length @var{n}.  The last sequence may
887 contain less elements than @var{n}.  @var{n} must be an integer.  If
888 @var{n} is a negative integer or 0, the return value is @code{nil}.
890 @example
891 @group
892 (seq-partition '(0 1 2 3 4 5 6 7) 3)
893 @result{} ((0 1 2) (3 4 5) (6 7))
894 @end group
895 @end example
896 @end defun
898 @defun seq-intersection sequence1 sequence2 &optional function
899   This function returns a list of the elements that appear both in
900 @var{sequence1} and @var{sequence2}.  If the optional argument
901 @var{function} is non-@code{nil}, it is a function of two arguments to
902 use to compare elements instead of the default @code{equal}.
904 @example
905 @group
906 (seq-intersection [2 3 4 5] [1 3 5 6 7])
907 @result{} (3 5)
908 @end group
909 @end example
910 @end defun
913 @defun seq-difference sequence1 sequence2 &optional function
914   This function returns a list of the elements that appear in
915 @var{sequence1} but not in @var{sequence2}.  If the optional argument
916 @var{function} is non-@code{nil}, it is a function of two arguments to
917 use to compare elements instead of the default @code{equal}.
919 @example
920 @group
921 (seq-difference '(2 3 4 5) [1 3 5 6 7])
922 @result{} (2 4)
923 @end group
924 @end example
925 @end defun
927 @defun seq-group-by function sequence
928   This function separates the elements of @var{sequence} into an alist
929 whose keys are the result of applying @var{function} to each element
930 of @var{sequence}.  Keys are compared using @code{equal}.
932 @example
933 @group
934 (seq-group-by #'integerp '(1 2.1 3 2 3.2))
935 @result{} ((t 1 3 2) (nil 2.1 3.2))
936 @end group
937 @group
938 (seq-group-by #'car '((a 1) (b 2) (a 3) (c 4)))
939 @result{} ((b (b 2)) (a (a 1) (a 3)) (c (c 4)))
940 @end group
941 @end example
942 @end defun
944 @defun seq-into sequence type
945   This function converts the sequence @var{sequence} into a sequence
946 of type @var{type}.  @var{type} can be one of the following symbols:
947 @code{vector}, @code{string} or @code{list}.
949 @example
950 @group
951 (seq-into [1 2 3] 'list)
952 @result{} (1 2 3)
953 @end group
954 @group
955 (seq-into nil 'vector)
956 @result{} []
957 @end group
958 @group
959 (seq-into "hello" 'vector)
960 @result{} [104 101 108 108 111]
961 @end group
962 @end example
963 @end defun
965 @defun seq-min sequence
966   This function returns the smallest element of @var{sequence}.  The
967 elements of @var{sequence} must be numbers or markers
968 (@pxref{Markers}).
970 @example
971 @group
972 (seq-min [3 1 2])
973 @result{} 1
974 @end group
975 @group
976 (seq-min "Hello")
977 @result{} 72
978 @end group
979 @end example
980 @end defun
982 @defun seq-max sequence
983   This function returns the largest element of @var{sequence}.  The
984 elements of @var{sequence} must be numbers or markers.
986 @example
987 @group
988 (seq-max [1 3 2])
989 @result{} 3
990 @end group
991 @group
992 (seq-max "Hello")
993 @result{} 111
994 @end group
995 @end example
996 @end defun
998 @defmac seq-doseq (var sequence) body@dots{}
999 @cindex sequence iteration
1000   This macro is like @code{dolist} (@pxref{Iteration, dolist}), except
1001 that @var{sequence} can be a list, vector or string.  This is
1002 primarily useful for side-effects.
1003 @end defmac
1005 @defmac seq-let arguments sequence body@dots{}
1006 @cindex sequence destructuring
1007   This macro binds the variables defined in @var{arguments} to the
1008 elements of @var{sequence}.  @var{arguments} can themselves include
1009 sequences, allowing for nested destructuring.
1011 The @var{arguments} sequence can also include the @code{&rest} marker
1012 followed by a variable name to be bound to the rest of
1013 @code{sequence}.
1015 @example
1016 @group
1017 (seq-let [first second] [1 2 3 4]
1018   (list first second))
1019 @result{} (1 2)
1020 @end group
1021 @group
1022 (seq-let (_ a _ b) '(1 2 3 4)
1023   (list a b))
1024 @result{} (2 4)
1025 @end group
1026 @group
1027 (seq-let [a [b [c]]] [1 [2 [3]]]
1028   (list a b c))
1029 @result{} (1 2 3)
1030 @end group
1031 @group
1032 (seq-let [a b &rest others] [1 2 3 4]
1033   others)
1034 @end group
1035 @result{} [3 4]
1036 @end example
1037 @end defmac
1039 @defun seq-random-elt sequence
1040   This function returns an element of @var{sequence} taken at random.
1042 @example
1043 @group
1044 (seq-random-elt [1 2 3 4])
1045 @result{} 3
1046 (seq-random-elt [1 2 3 4])
1047 @result{} 2
1048 (seq-random-elt [1 2 3 4])
1049 @result{} 4
1050 (seq-random-elt [1 2 3 4])
1051 @result{} 2
1052 (seq-random-elt [1 2 3 4])
1053 @result{} 1
1054 @end group
1055 @end example
1057   If @var{sequence} is empty, this function signals an error.
1058 @end defun
1060 @node Arrays
1061 @section Arrays
1062 @cindex array
1064   An @dfn{array} object has slots that hold a number of other Lisp
1065 objects, called the elements of the array.  Any element of an array
1066 may be accessed in constant time.  In contrast, the time to access an
1067 element of a list is proportional to the position of that element in
1068 the list.
1070   Emacs defines four types of array, all one-dimensional:
1071 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
1072 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
1073 @dfn{char-tables} (@pxref{Char-Table Type}).  Vectors and char-tables
1074 can hold elements of any type, but strings can only hold characters,
1075 and bool-vectors can only hold @code{t} and @code{nil}.
1077   All four kinds of array share these characteristics:
1079 @itemize @bullet
1080 @item
1081 The first element of an array has index zero, the second element has
1082 index 1, and so on.  This is called @dfn{zero-origin} indexing.  For
1083 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
1085 @item
1086 The length of the array is fixed once you create it; you cannot
1087 change the length of an existing array.
1089 @item
1090 For purposes of evaluation, the array is a constant---i.e.,
1091 it evaluates to itself.
1093 @item
1094 The elements of an array may be referenced or changed with the functions
1095 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
1096 @end itemize
1098     When you create an array, other than a char-table, you must specify
1099 its length.  You cannot specify the length of a char-table, because that
1100 is determined by the range of character codes.
1102   In principle, if you want an array of text characters, you could use
1103 either a string or a vector.  In practice, we always choose strings for
1104 such applications, for four reasons:
1106 @itemize @bullet
1107 @item
1108 They occupy one-fourth the space of a vector of the same elements.
1110 @item
1111 Strings are printed in a way that shows the contents more clearly
1112 as text.
1114 @item
1115 Strings can hold text properties.  @xref{Text Properties}.
1117 @item
1118 Many of the specialized editing and I/O facilities of Emacs accept only
1119 strings.  For example, you cannot insert a vector of characters into a
1120 buffer the way you can insert a string.  @xref{Strings and Characters}.
1121 @end itemize
1123   By contrast, for an array of keyboard input characters (such as a key
1124 sequence), a vector may be necessary, because many keyboard input
1125 characters are outside the range that will fit in a string.  @xref{Key
1126 Sequence Input}.
1128 @node Array Functions
1129 @section Functions that Operate on Arrays
1131   In this section, we describe the functions that accept all types of
1132 arrays.
1134 @defun arrayp object
1135 This function returns @code{t} if @var{object} is an array (i.e., a
1136 vector, a string, a bool-vector or a char-table).
1138 @example
1139 @group
1140 (arrayp [a])
1141      @result{} t
1142 (arrayp "asdf")
1143      @result{} t
1144 (arrayp (syntax-table))    ;; @r{A char-table.}
1145      @result{} t
1146 @end group
1147 @end example
1148 @end defun
1150 @defun aref arr index
1151 @cindex array elements
1152 This function returns the @var{index}th element of the array or record
1153 @var{arr}.  The first element is at index zero.
1155 @example
1156 @group
1157 (setq primes [2 3 5 7 11 13])
1158      @result{} [2 3 5 7 11 13]
1159 (aref primes 4)
1160      @result{} 11
1161 @end group
1162 @group
1163 (aref "abcdefg" 1)
1164      @result{} 98           ; @r{@samp{b} is @acronym{ASCII} code 98.}
1165 @end group
1166 @end example
1168 See also the function @code{elt}, in @ref{Sequence Functions}.
1169 @end defun
1171 @defun aset array index object
1172 This function sets the @var{index}th element of @var{array} to be
1173 @var{object}.  It returns @var{object}.
1175 @example
1176 @group
1177 (setq w [foo bar baz])
1178      @result{} [foo bar baz]
1179 (aset w 0 'fu)
1180      @result{} fu
1182      @result{} [fu bar baz]
1183 @end group
1185 @group
1186 (setq x "asdfasfd")
1187      @result{} "asdfasfd"
1188 (aset x 3 ?Z)
1189      @result{} 90
1191      @result{} "asdZasfd"
1192 @end group
1193 @end example
1195 If @var{array} is a string and @var{object} is not a character, a
1196 @code{wrong-type-argument} error results.  The function converts a
1197 unibyte string to multibyte if necessary to insert a character.
1198 @end defun
1200 @defun fillarray array object
1201 This function fills the array @var{array} with @var{object}, so that
1202 each element of @var{array} is @var{object}.  It returns @var{array}.
1204 @example
1205 @group
1206 (setq a [a b c d e f g])
1207      @result{} [a b c d e f g]
1208 (fillarray a 0)
1209      @result{} [0 0 0 0 0 0 0]
1211      @result{} [0 0 0 0 0 0 0]
1212 @end group
1213 @group
1214 (setq s "When in the course")
1215      @result{} "When in the course"
1216 (fillarray s ?-)
1217      @result{} "------------------"
1218 @end group
1219 @end example
1221 If @var{array} is a string and @var{object} is not a character, a
1222 @code{wrong-type-argument} error results.
1223 @end defun
1225 The general sequence functions @code{copy-sequence} and @code{length}
1226 are often useful for objects known to be arrays.  @xref{Sequence Functions}.
1228 @node Vectors
1229 @section Vectors
1230 @cindex vector (type)
1232   A @dfn{vector} is a general-purpose array whose elements can be any
1233 Lisp objects.  (By contrast, the elements of a string can only be
1234 characters.  @xref{Strings and Characters}.)  Vectors are used in
1235 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
1236 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
1237 representation of a byte-compiled function (@pxref{Byte Compilation}),
1238 and more.
1240   Like other arrays, vectors use zero-origin indexing: the first
1241 element has index 0.
1243   Vectors are printed with square brackets surrounding the elements.
1244 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
1245 @code{a} is printed as @code{[a b a]}.  You can write vectors in the
1246 same way in Lisp input.
1248   A vector, like a string or a number, is considered a constant for
1249 evaluation: the result of evaluating it is the same vector.  This does
1250 not evaluate or even examine the elements of the vector.
1251 @xref{Self-Evaluating Forms}.
1253   Here are examples illustrating these principles:
1255 @example
1256 @group
1257 (setq avector [1 two '(three) "four" [five]])
1258      @result{} [1 two (quote (three)) "four" [five]]
1259 (eval avector)
1260      @result{} [1 two (quote (three)) "four" [five]]
1261 (eq avector (eval avector))
1262      @result{} t
1263 @end group
1264 @end example
1266 @node Vector Functions
1267 @section Functions for Vectors
1269   Here are some functions that relate to vectors:
1271 @defun vectorp object
1272 This function returns @code{t} if @var{object} is a vector.
1274 @example
1275 @group
1276 (vectorp [a])
1277      @result{} t
1278 (vectorp "asdf")
1279      @result{} nil
1280 @end group
1281 @end example
1282 @end defun
1284 @defun vector &rest objects
1285 This function creates and returns a vector whose elements are the
1286 arguments, @var{objects}.
1288 @example
1289 @group
1290 (vector 'foo 23 [bar baz] "rats")
1291      @result{} [foo 23 [bar baz] "rats"]
1292 (vector)
1293      @result{} []
1294 @end group
1295 @end example
1296 @end defun
1298 @defun make-vector length object
1299 This function returns a new vector consisting of @var{length} elements,
1300 each initialized to @var{object}.
1302 @example
1303 @group
1304 (setq sleepy (make-vector 9 'Z))
1305      @result{} [Z Z Z Z Z Z Z Z Z]
1306 @end group
1307 @end example
1308 @end defun
1310 @defun vconcat &rest sequences
1311 @cindex copying vectors
1312 This function returns a new vector containing all the elements of
1313 @var{sequences}.  The arguments @var{sequences} may be true lists,
1314 vectors, strings or bool-vectors.  If no @var{sequences} are given,
1315 the empty vector is returned.
1317 The value is either the empty vector, or is a newly constructed
1318 nonempty vector that is not @code{eq} to any existing vector.
1320 @example
1321 @group
1322 (setq a (vconcat '(A B C) '(D E F)))
1323      @result{} [A B C D E F]
1324 (eq a (vconcat a))
1325      @result{} nil
1326 @end group
1327 @group
1328 (vconcat)
1329      @result{} []
1330 (vconcat [A B C] "aa" '(foo (6 7)))
1331      @result{} [A B C 97 97 foo (6 7)]
1332 @end group
1333 @end example
1335 The @code{vconcat} function also allows byte-code function objects as
1336 arguments.  This is a special feature to make it easy to access the entire
1337 contents of a byte-code function object.  @xref{Byte-Code Objects}.
1339 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
1340 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
1341 in @ref{Building Lists}.
1342 @end defun
1344   The @code{append} function also provides a way to convert a vector into a
1345 list with the same elements:
1347 @example
1348 @group
1349 (setq avector [1 two (quote (three)) "four" [five]])
1350      @result{} [1 two (quote (three)) "four" [five]]
1351 (append avector nil)
1352      @result{} (1 two (quote (three)) "four" [five])
1353 @end group
1354 @end example
1356 @node Char-Tables
1357 @section Char-Tables
1358 @cindex char-tables
1359 @cindex extra slots of char-table
1361   A char-table is much like a vector, except that it is indexed by
1362 character codes.  Any valid character code, without modifiers, can be
1363 used as an index in a char-table.  You can access a char-table's
1364 elements with @code{aref} and @code{aset}, as with any array.  In
1365 addition, a char-table can have @dfn{extra slots} to hold additional
1366 data not associated with particular character codes.  Like vectors,
1367 char-tables are constants when evaluated, and can hold elements of any
1368 type.
1370 @cindex subtype of char-table
1371   Each char-table has a @dfn{subtype}, a symbol, which serves two
1372 purposes:
1374 @itemize @bullet
1375 @item
1376 The subtype provides an easy way to tell what the char-table is for.
1377 For instance, display tables are char-tables with @code{display-table}
1378 as the subtype, and syntax tables are char-tables with
1379 @code{syntax-table} as the subtype.  The subtype can be queried using
1380 the function @code{char-table-subtype}, described below.
1382 @item
1383 The subtype controls the number of @dfn{extra slots} in the
1384 char-table.  This number is specified by the subtype's
1385 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1386 Properties}), whose value should be an integer between 0 and 10.  If
1387 the subtype has no such symbol property, the char-table has no extra
1388 slots.
1389 @end itemize
1391 @cindex parent of char-table
1392   A char-table can have a @dfn{parent}, which is another char-table.  If
1393 it does, then whenever the char-table specifies @code{nil} for a
1394 particular character @var{c}, it inherits the value specified in the
1395 parent.  In other words, @code{(aref @var{char-table} @var{c})} returns
1396 the value from the parent of @var{char-table} if @var{char-table} itself
1397 specifies @code{nil}.
1399 @cindex default value of char-table
1400   A char-table can also have a @dfn{default value}.  If so, then
1401 @code{(aref @var{char-table} @var{c})} returns the default value
1402 whenever the char-table does not specify any other non-@code{nil} value.
1404 @defun make-char-table subtype &optional init
1405 Return a newly-created char-table, with subtype @var{subtype} (a
1406 symbol).  Each element is initialized to @var{init}, which defaults to
1407 @code{nil}.  You cannot alter the subtype of a char-table after the
1408 char-table is created.
1410 There is no argument to specify the length of the char-table, because
1411 all char-tables have room for any valid character code as an index.
1413 If @var{subtype} has the @code{char-table-extra-slots} symbol
1414 property, that specifies the number of extra slots in the char-table.
1415 This should be an integer between 0 and 10; otherwise,
1416 @code{make-char-table} raises an error.  If @var{subtype} has no
1417 @code{char-table-extra-slots} symbol property (@pxref{Property
1418 Lists}), the char-table has no extra slots.
1419 @end defun
1421 @defun char-table-p object
1422 This function returns @code{t} if @var{object} is a char-table, and
1423 @code{nil} otherwise.
1424 @end defun
1426 @defun char-table-subtype char-table
1427 This function returns the subtype symbol of @var{char-table}.
1428 @end defun
1430 There is no special function to access default values in a char-table.
1431 To do that, use @code{char-table-range} (see below).
1433 @defun char-table-parent char-table
1434 This function returns the parent of @var{char-table}.  The parent is
1435 always either @code{nil} or another char-table.
1436 @end defun
1438 @defun set-char-table-parent char-table new-parent
1439 This function sets the parent of @var{char-table} to @var{new-parent}.
1440 @end defun
1442 @defun char-table-extra-slot char-table n
1443 This function returns the contents of extra slot @var{n} (zero based)
1444 of @var{char-table}.  The number of extra slots in a char-table is
1445 determined by its subtype.
1446 @end defun
1448 @defun set-char-table-extra-slot char-table n value
1449 This function stores @var{value} in extra slot @var{n} (zero based) of
1450 @var{char-table}.
1451 @end defun
1453   A char-table can specify an element value for a single character code;
1454 it can also specify a value for an entire character set.
1456 @defun char-table-range char-table range
1457 This returns the value specified in @var{char-table} for a range of
1458 characters @var{range}.  Here are the possibilities for @var{range}:
1460 @table @asis
1461 @item @code{nil}
1462 Refers to the default value.
1464 @item @var{char}
1465 Refers to the element for character @var{char}
1466 (supposing @var{char} is a valid character code).
1468 @item @code{(@var{from} . @var{to})}
1469 A cons cell refers to all the characters in the inclusive range
1470 @samp{[@var{from}..@var{to}]}.
1471 @end table
1472 @end defun
1474 @defun set-char-table-range char-table range value
1475 This function sets the value in @var{char-table} for a range of
1476 characters @var{range}.  Here are the possibilities for @var{range}:
1478 @table @asis
1479 @item @code{nil}
1480 Refers to the default value.
1482 @item @code{t}
1483 Refers to the whole range of character codes.
1485 @item @var{char}
1486 Refers to the element for character @var{char}
1487 (supposing @var{char} is a valid character code).
1489 @item @code{(@var{from} . @var{to})}
1490 A cons cell refers to all the characters in the inclusive range
1491 @samp{[@var{from}..@var{to}]}.
1492 @end table
1493 @end defun
1495 @defun map-char-table function char-table
1496 This function calls its argument @var{function} for each element of
1497 @var{char-table} that has a non-@code{nil} value.  The call to
1498 @var{function} is with two arguments, a key and a value.  The key
1499 is a possible @var{range} argument for @code{char-table-range}---either
1500 a valid character or a cons cell @code{(@var{from} . @var{to})},
1501 specifying a range of characters that share the same value.  The value is
1502 what @code{(char-table-range @var{char-table} @var{key})} returns.
1504 Overall, the key-value pairs passed to @var{function} describe all the
1505 values stored in @var{char-table}.
1507 The return value is always @code{nil}; to make calls to
1508 @code{map-char-table} useful, @var{function} should have side effects.
1509 For example, here is how to examine the elements of the syntax table:
1511 @example
1512 (let (accumulator)
1513    (map-char-table
1514     #'(lambda (key value)
1515         (setq accumulator
1516               (cons (list
1517                      (if (consp key)
1518                          (list (car key) (cdr key))
1519                        key)
1520                      value)
1521                     accumulator)))
1522     (syntax-table))
1523    accumulator)
1524 @result{}
1525 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1526  ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1527  ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1528 @end example
1529 @end defun
1531 @node Bool-Vectors
1532 @section Bool-vectors
1533 @cindex Bool-vectors
1535   A bool-vector is much like a vector, except that it stores only the
1536 values @code{t} and @code{nil}.  If you try to store any non-@code{nil}
1537 value into an element of the bool-vector, the effect is to store
1538 @code{t} there.  As with all arrays, bool-vector indices start from 0,
1539 and the length cannot be changed once the bool-vector is created.
1540 Bool-vectors are constants when evaluated.
1542   Several functions work specifically with bool-vectors; aside
1543 from that, you manipulate them with same functions used for other kinds
1544 of arrays.
1546 @defun make-bool-vector length initial
1547 Return a new bool-vector of @var{length} elements,
1548 each one initialized to @var{initial}.
1549 @end defun
1551 @defun bool-vector &rest objects
1552 This function creates and returns a bool-vector whose elements are the
1553 arguments, @var{objects}.
1554 @end defun
1556 @defun bool-vector-p object
1557 This returns @code{t} if @var{object} is a bool-vector,
1558 and @code{nil} otherwise.
1559 @end defun
1561 There are also some bool-vector set operation functions, described below:
1563 @defun bool-vector-exclusive-or a b &optional c
1564 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1565 If optional argument @var{c} is given, the result of this operation is
1566 stored into @var{c}.  All arguments should be bool vectors of the same length.
1567 @end defun
1569 @defun bool-vector-union a b &optional c
1570 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}.  If
1571 optional argument @var{c} is given, the result of this operation is
1572 stored into @var{c}.  All arguments should be bool vectors of the same length.
1573 @end defun
1575 @defun bool-vector-intersection a b &optional c
1576 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}.  If
1577 optional argument @var{c} is given, the result of this operation is
1578 stored into @var{c}.  All arguments should be bool vectors of the same length.
1579 @end defun
1581 @defun bool-vector-set-difference a b &optional c
1582 Return @dfn{set difference} of bool vectors @var{a} and @var{b}.  If
1583 optional argument @var{c} is given, the result of this operation is
1584 stored into @var{c}.  All arguments should be bool vectors of the same length.
1585 @end defun
1587 @defun bool-vector-not a &optional b
1588 Return @dfn{set complement} of bool vector @var{a}.  If optional
1589 argument @var{b} is given, the result of this operation is stored into
1590 @var{b}.  All arguments should be bool vectors of the same length.
1591 @end defun
1593 @defun bool-vector-subsetp a b
1594 Return @code{t} if every @code{t} value in @var{a} is also t in
1595 @var{b}, @code{nil} otherwise.  All arguments should be bool vectors of the
1596 same length.
1597 @end defun
1599 @defun bool-vector-count-consecutive a b i
1600 Return the number of consecutive elements in @var{a} equal @var{b}
1601 starting at @var{i}.  @code{a} is a bool vector, @var{b} is @code{t}
1602 or @code{nil}, and @var{i} is an index into @code{a}.
1603 @end defun
1605 @defun bool-vector-count-population a
1606 Return the number of elements that are @code{t} in bool vector @var{a}.
1607 @end defun
1609   The printed form represents up to 8 boolean values as a single
1610 character:
1612 @example
1613 @group
1614 (bool-vector t nil t nil)
1615      @result{} #&4"^E"
1616 (bool-vector)
1617      @result{} #&0""
1618 @end group
1619 @end example
1621 You can use @code{vconcat} to print a bool-vector like other vectors:
1623 @example
1624 @group
1625 (vconcat (bool-vector nil t nil t))
1626      @result{} [nil t nil t]
1627 @end group
1628 @end example
1630   Here is another example of creating, examining, and updating a
1631 bool-vector:
1633 @example
1634 (setq bv (make-bool-vector 5 t))
1635      @result{} #&5"^_"
1636 (aref bv 1)
1637      @result{} t
1638 (aset bv 3 nil)
1639      @result{} nil
1641      @result{} #&5"^W"
1642 @end example
1644 @noindent
1645 These results make sense because the binary codes for control-_ and
1646 control-W are 11111 and 10111, respectively.
1648 @node Rings
1649 @section Managing a Fixed-Size Ring of Objects
1651 @cindex ring data structure
1652   A @dfn{ring} is a fixed-size data structure that supports insertion,
1653 deletion, rotation, and modulo-indexed reference and traversal.  An
1654 efficient ring data structure is implemented by the @code{ring}
1655 package.  It provides the functions listed in this section.
1657   Note that several rings in Emacs, like the kill ring and the
1658 mark ring, are actually implemented as simple lists, @emph{not} using
1659 the @code{ring} package; thus the following functions won't work on
1660 them.
1662 @defun make-ring size
1663 This returns a new ring capable of holding @var{size} objects.
1664 @var{size} should be an integer.
1665 @end defun
1667 @defun ring-p object
1668 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1669 @end defun
1671 @defun ring-size ring
1672 This returns the maximum capacity of the @var{ring}.
1673 @end defun
1675 @defun ring-length ring
1676 This returns the number of objects that @var{ring} currently contains.
1677 The value will never exceed that returned by @code{ring-size}.
1678 @end defun
1680 @defun ring-elements ring
1681 This returns a list of the objects in @var{ring}, in order, newest first.
1682 @end defun
1684 @defun ring-copy ring
1685 This returns a new ring which is a copy of @var{ring}.
1686 The new ring contains the same (@code{eq}) objects as @var{ring}.
1687 @end defun
1689 @defun ring-empty-p ring
1690 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1691 @end defun
1693   The newest element in the ring always has index 0.  Higher indices
1694 correspond to older elements.  Indices are computed modulo the ring
1695 length.  Index @minus{}1 corresponds to the oldest element, @minus{}2
1696 to the next-oldest, and so forth.
1698 @defun ring-ref ring index
1699 This returns the object in @var{ring} found at index @var{index}.
1700 @var{index} may be negative or greater than the ring length.  If
1701 @var{ring} is empty, @code{ring-ref} signals an error.
1702 @end defun
1704 @defun ring-insert ring object
1705 This inserts @var{object} into @var{ring}, making it the newest
1706 element, and returns @var{object}.
1708 If the ring is full, insertion removes the oldest element to
1709 make room for the new element.
1710 @end defun
1712 @defun ring-remove ring &optional index
1713 Remove an object from @var{ring}, and return that object.  The
1714 argument @var{index} specifies which item to remove; if it is
1715 @code{nil}, that means to remove the oldest item.  If @var{ring} is
1716 empty, @code{ring-remove} signals an error.
1717 @end defun
1719 @defun ring-insert-at-beginning ring object
1720 This inserts @var{object} into @var{ring}, treating it as the oldest
1721 element.  The return value is not significant.
1723 If the ring is full, this function removes the newest element to make
1724 room for the inserted element.
1725 @end defun
1727 @cindex fifo data structure
1728   If you are careful not to exceed the ring size, you can
1729 use the ring as a first-in-first-out queue.  For example:
1731 @lisp
1732 (let ((fifo (make-ring 5)))
1733   (mapc (lambda (obj) (ring-insert fifo obj))
1734         '(0 one "two"))
1735   (list (ring-remove fifo) t
1736         (ring-remove fifo) t
1737         (ring-remove fifo)))
1738      @result{} (0 t one t "two")
1739 @end lisp