]> code.delx.au - gnu-emacs/blob - doc/lispref/sequences.texi
Merge from origin/emacs-25
[gnu-emacs] / doc / lispref / sequences.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-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
9
10 The @dfn{sequence} type is the union of two other Lisp types: lists
11 and arrays. In other words, any list is a sequence, and any array is
12 a sequence. The common property that all sequences have is that each
13 is an ordered collection of elements.
14
15 An @dfn{array} is a fixed-length object with a slot for each of its
16 elements. All the elements are accessible in constant time. The four
17 types of arrays are strings, vectors, char-tables and bool-vectors.
18
19 A list is a sequence of elements, but it is not a single primitive
20 object; it is made of cons cells, one cell per element. Finding the
21 @var{n}th element requires looking through @var{n} cons cells, so
22 elements farther from the beginning of the list take longer to access.
23 But it is possible to add elements to the list, or remove elements.
24
25 The following diagram shows the relationship between these types:
26
27 @example
28 @group
29 _____________________________________________
30 | |
31 | Sequence |
32 | ______ ________________________________ |
33 | | | | | |
34 | | List | | Array | |
35 | | | | ________ ________ | |
36 | |______| | | | | | | |
37 | | | Vector | | String | | |
38 | | |________| |________| | |
39 | | ____________ _____________ | |
40 | | | | | | | |
41 | | | Char-table | | Bool-vector | | |
42 | | |____________| |_____________| | |
43 | |________________________________| |
44 |_____________________________________________|
45 @end group
46 @end example
47
48 @menu
49 * Sequence Functions:: Functions that accept any kind of sequence.
50 * Arrays:: Characteristics of arrays in Emacs Lisp.
51 * Array Functions:: Functions specifically for arrays.
52 * Vectors:: Special characteristics of Emacs Lisp vectors.
53 * Vector Functions:: Functions specifically for vectors.
54 * Char-Tables:: How to work with char-tables.
55 * Bool-Vectors:: How to work with bool-vectors.
56 * Rings:: Managing a fixed-size ring of objects.
57 @end menu
58
59 @node Sequence Functions
60 @section Sequences
61
62 This section describes functions that accept any kind of sequence.
63
64 @defun sequencep object
65 This function returns @code{t} if @var{object} is a list, vector,
66 string, bool-vector, or char-table, @code{nil} otherwise.
67 @end defun
68
69 @defun length sequence
70 @cindex string length
71 @cindex list length
72 @cindex vector length
73 @cindex sequence length
74 @cindex char-table length
75 @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.
81
82 @xref{Definition of safe-length}, for the related function @code{safe-length}.
83
84 @example
85 @group
86 (length '(1 2 3))
87 @result{} 3
88 @end group
89 @group
90 (length ())
91 @result{} 0
92 @end group
93 @group
94 (length "foobar")
95 @result{} 6
96 @end group
97 @group
98 (length [1 2 3])
99 @result{} 3
100 @end group
101 @group
102 (length (make-bool-vector 5 nil))
103 @result{} 5
104 @end group
105 @end example
106 @end defun
107
108 @noindent
109 See also @code{string-bytes}, in @ref{Text Representations}.
110
111 If you need to compute the width of a string on display, you should 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.
115
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.
125
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
149
150 This function generalizes @code{aref} (@pxref{Array Functions}) and
151 @code{nth} (@pxref{Definition of nth}).
152 @end defun
153
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.
159
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.
166
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}.
171
172 This function does not work for dotted lists. Trying to copy a
173 circular list may cause an infinite loop.
174
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.
178
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
192
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
205
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
212
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
221
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.
230
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)
239 x
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]
249 x
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"
259 x
260 @result{} "xyzzy"
261 @end group
262 @end example
263 @end defun
264
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.
271
272 For example:
273
274 @example
275 @group
276 (setq x '(a b c))
277 @result{} (a b c)
278 @end group
279 @group
280 x
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.}
287 x
288 @result{} (a)
289 @end group
290 @end example
291
292 To avoid confusion, we usually store the result of @code{nreverse}
293 back in the same variable which held the original list:
294
295 @example
296 (setq x (nreverse x))
297 @end example
298
299 Here is the @code{nreverse} of our favorite example, @code{(a b c)},
300 presented graphically:
301
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
314
315 For the vector, it is even simpler because you don't need setq:
316
317 @example
318 (setq x [1 2 3 4])
319 @result{} [1 2 3 4]
320 (nreverse x)
321 @result{} [4 3 2 1]
322 x
323 @result{} [4 3 2 1]
324 @end example
325
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.
329
330 @end defun
331
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.
343
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.
348
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.
357
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.
363
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:
369
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
384
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:
392
393 @example
394 (setq nums (sort nums '<))
395 @end example
396
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:
402
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
418
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
423
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.
429
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.
435
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}.
439
440 @defun seq-elt sequence index
441 This function the element at the index @var{index} in
442 @var{sequence}. @var{index} can be an integer from zero up to the
443 length of @var{sequence} minus one. For out-of-range values on
444 built-in sequence types, @code{seq-elt} behaves like @code{elt}.
445 @xref{Definition of elt}.
446
447 @example
448 @group
449 (seq-elt [1 2 3 4] 2)
450 @result{} 3
451 @end group
452
453 @code{seq-elt} returns settable places using @code{setf}.
454
455 @group
456 (setq vec [1 2 3 4])
457 (setf (seq-elt vec 2) 5)
458 vec
459 @result{} [1 2 5 4]
460 @end group
461 @end example
462 @end defun
463
464 @defun seq-length sequence
465 This function returns the number of elements in @var{sequence}. For
466 built-in sequence types, @code{seq-length} behaves like @code{length}.
467 @xref{Definition of length}.
468 @end defun
469
470 @defun seqp sequence
471 This function returns non-@code{nil} if @var{sequence} is a sequence
472 (a list or array), or any additional type of sequence defined via
473 @file{seq.el} generic functions.
474
475 @example
476 @group
477 (seqp [1 2])
478 @result{} t
479 @end group
480 @group
481 (seqp 2)
482 @result{} nil
483 @end group
484 @end example
485 @end defun
486
487 @defun seq-drop sequence n
488 This function returns all but the first @var{n} (an integer)
489 elements of @var{sequence}. If @var{n} is negative or zero,
490 the result is @var{sequence}.
491
492 @example
493 @group
494 (seq-drop [1 2 3 4 5 6] 3)
495 @result{} [4 5 6]
496 @end group
497 @group
498 (seq-drop "hello world" -4)
499 @result{} "hello world"
500 @end group
501 @end example
502 @end defun
503
504 @defun seq-take sequence n
505 This function returns the first @var{n} (an integer) elements of
506 @var{sequence}. If @var{n} is negative or zero, the result
507 is @code{nil}.
508
509 @example
510 @group
511 (seq-take '(1 2 3 4) 3)
512 @result{} (1 2 3)
513 @end group
514 @group
515 (seq-take [1 2 3 4] 0)
516 @result{} []
517 @end group
518 @end example
519 @end defun
520
521 @defun seq-take-while predicate sequence
522 This function returns the members of @var{sequence} in order,
523 stopping before the first one for which @var{predicate} returns @code{nil}.
524
525 @example
526 @group
527 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
528 @result{} (1 2 3)
529 @end group
530 @group
531 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
532 @result{} []
533 @end group
534 @end example
535 @end defun
536
537 @defun seq-drop-while predicate sequence
538 This function returns the members of @var{sequence} in order,
539 starting from the first one for which @var{predicate} returns @code{nil}.
540
541 @example
542 @group
543 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
544 @result{} (-1 -2)
545 @end group
546 @group
547 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
548 @result{} [1 4 6]
549 @end group
550 @end example
551 @end defun
552
553 @defun seq-do function sequence
554 This function applies @var{function} to each element of
555 @var{sequence} in turn (presumably for side effects) and returns
556 @var{sequence}.
557 @end defun
558
559 @defun seq-map function sequence
560 This function returns the result of applying @var{function} to each
561 element of @var{sequence}. The returned value is a list.
562
563 @example
564 @group
565 (seq-map #'1+ '(2 4 6))
566 @result{} (3 5 7)
567 @end group
568 @group
569 (seq-map #'symbol-name [foo bar])
570 @result{} ("foo" "bar")
571 @end group
572 @end example
573 @end defun
574
575 @defun seq-map-indexed function sequence
576 This function returns the result of applying @var{function} to each
577 element of @var{sequence} and its index within @var{seq}. The
578 returned value is a list.
579
580 @example
581 @group
582 (seq-map-indexed (lambda (elt idx)
583 (list idx elt))
584 '(a b c))
585 @result{} ((0 a) (b 1) (c 2))
586 @end group
587 @end example
588 @end defun
589
590 @defun seq-mapn function &rest sequences
591 This function returns the result of applying @var{function} to each
592 element of @var{sequences}. The arity of @var{function} must match
593 the number of sequences. Mapping stops at the shortest sequence, and
594 the returned value is a list.
595
596 @example
597 @group
598 (seq-mapn #'+ '(2 4 6) '(20 40 60))
599 @result{} (22 44 66)
600 @end group
601 @group
602 (seq-mapn #'concat '("moskito" "bite") ["bee" "sting"])
603 @result{} ("moskitobee" "bitesting")
604 @end group
605 @end example
606 @end defun
607
608 @defun seq-filter predicate sequence
609 @cindex filtering sequences
610 This function returns a list of all the elements in @var{sequence}
611 for which @var{predicate} returns non-@code{nil}.
612
613 @example
614 @group
615 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
616 @result{} (1 3 5)
617 @end group
618 @group
619 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
620 @result{} nil
621 @end group
622 @end example
623 @end defun
624
625 @defun seq-remove predicate sequence
626 @cindex removing from sequences
627 This function returns a list of all the elements in @var{sequence}
628 for which @var{predicate} returns @code{nil}.
629
630 @example
631 @group
632 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
633 @result{} (-1 -3)
634 @end group
635 @group
636 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
637 @result{} nil
638 @end group
639 @end example
640 @end defun
641
642 @defun seq-reduce function sequence initial-value
643 @cindex reducing sequences
644 This function returns the result of calling @var{function} with
645 @var{initial-value} and the first element of @var{sequence}, then calling
646 @var{function} with that result and the second element of @var{sequence},
647 then with that result and the third element of @var{sequence}, etc.
648 @var{function} should be a function of two arguments. If
649 @var{sequence} is empty, this returns @var{initial-value} without
650 calling @var{function}.
651
652 @example
653 @group
654 (seq-reduce #'+ [1 2 3 4] 0)
655 @result{} 10
656 @end group
657 @group
658 (seq-reduce #'+ '(1 2 3 4) 5)
659 @result{} 15
660 @end group
661 @group
662 (seq-reduce #'+ '() 3)
663 @result{} 3
664 @end group
665 @end example
666 @end defun
667
668 @defun seq-some predicate sequence
669 This function returns the first non-@code{nil} value returned by
670 applying @var{predicate} to each element of @var{sequence} in turn.
671
672 @example
673 @group
674 (seq-some #'numberp ["abc" 1 nil])
675 @result{} t
676 @end group
677 @group
678 (seq-some #'numberp ["abc" "def"])
679 @result{} nil
680 @end group
681 @group
682 (seq-some #'null ["abc" 1 nil])
683 @result{} t
684 @end group
685 @group
686 (seq-some #'1+ [2 4 6])
687 @result{} 3
688 @end group
689 @end example
690 @end defun
691
692 @defun seq-find predicate sequence &optional default
693 This function returns the first element for which @var{predicate}
694 returns non-@code{nil} in @var{sequence}. If no element matches
695 @var{predicate}, @var{default} is returned.
696
697 Note that this function has an ambiguity if the found element is
698 identical to @var{default}, as it cannot be known if an element was
699 found or not.
700
701 @example
702 @group
703 (seq-find #'numberp ["abc" 1 nil])
704 @result{} 1
705 @end group
706 @group
707 (seq-find #'numberp ["abc" "def"])
708 @result{} nil
709 @end group
710 @end example
711 @end defun
712
713 @defun seq-every-p predicate sequence
714 This function returns non-@code{nil} if applying @var{predicate}
715 to every element of @var{sequence} returns non-@code{nil}.
716
717 @example
718 @group
719 (seq-every-p #'numberp [2 4 6])
720 @result{} t
721 @end group
722 @group
723 (seq-some #'numberp [2 4 "6"])
724 @result{} nil
725 @end group
726 @end example
727 @end defun
728
729 @defun seq-empty-p sequence
730 This function returns non-@code{nil} if @var{sequence} is empty.
731
732 @example
733 @group
734 (seq-empty-p "not empty")
735 @result{} nil
736 @end group
737 @group
738 (seq-empty-p "")
739 @result{} t
740 @end group
741 @end example
742 @end defun
743
744 @defun seq-count predicate sequence
745 This function returns the number of elements in @var{sequence} for which
746 @var{predicate} returns non-@code{nil}.
747
748 @example
749 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
750 @result{} 2
751 @end example
752 @end defun
753
754 @cindex sorting sequences
755 @defun seq-sort function sequence
756 This function returns a copy of @var{sequence} that is sorted
757 according to @var{function}, a function of two arguments that returns
758 non-@code{nil} if the first argument should sort before the second.
759 @end defun
760
761 @defun seq-contains sequence elt &optional function
762 This function returns the first element in @var{sequence} that is equal to
763 @var{elt}. If the optional argument @var{function} is non-@code{nil},
764 it is a function of two arguments to use instead of the default @code{equal}.
765
766 @example
767 @group
768 (seq-contains '(symbol1 symbol2) 'symbol1)
769 @result{} symbol1
770 @end group
771 @group
772 (seq-contains '(symbol1 symbol2) 'symbol3)
773 @result{} nil
774 @end group
775 @end example
776
777 @end defun
778
779 @defun seq-position sequence elt &optional function
780 This function returns the index of the first element in
781 @var{sequence} that is equal to @var{elt}. If the optional argument
782 @var{function} is non-@code{nil}, it is a function of two arguments to
783 use instead of the default @code{equal}.
784
785 @example
786 @group
787 (seq-position '(a b c) 'b)
788 @result{} 1
789 @end group
790 @group
791 (seq-position '(a b c) 'd)
792 @result{} nil
793 @end group
794 @end example
795 @end defun
796
797
798 @defun seq-uniq sequence &optional function
799 This function returns a list of the elements of @var{sequence} with
800 duplicates removed. If the optional argument @var{function} is non-@code{nil},
801 it is a function of two arguments to use instead of the default @code{equal}.
802
803 @example
804 @group
805 (seq-uniq '(1 2 2 1 3))
806 @result{} (1 2 3)
807 @end group
808 @group
809 (seq-uniq '(1 2 2.0 1.0) #'=)
810 @result{} [3 4]
811 @end group
812 @end example
813 @end defun
814
815 @defun seq-subseq sequence start &optional end
816 This function returns a subset of @var{sequence} from @var{start}
817 to @var{end}, both integers (@var{end} defaults to the last element).
818 If @var{start} or @var{end} is negative, it counts from the end of
819 @var{sequence}.
820
821 @example
822 @group
823 (seq-subseq '(1 2 3 4 5) 1)
824 @result{} (2 3 4 5)
825 @end group
826 @group
827 (seq-subseq '[1 2 3 4 5] 1 3)
828 @result{} [2 3]
829 @end group
830 @group
831 (seq-subseq '[1 2 3 4 5] -3 -1)
832 @result{} [3 4]
833 @end group
834 @end example
835 @end defun
836
837 @defun seq-concatenate type &rest sequences
838 This function returns a sequence of type @var{type} made of the
839 concatenation of @var{sequences}. @var{type} may be: @code{vector},
840 @code{list} or @code{string}.
841
842 @example
843 @group
844 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
845 @result{} (1 2 3 5 6)
846 @end group
847 @group
848 (seq-concatenate 'string "Hello " "world")
849 @result{} "Hello world"
850 @end group
851 @end example
852 @end defun
853
854 @defun seq-mapcat function sequence &optional type
855 This function returns the result of applying @code{seq-concatenate}
856 to the result of applying @var{function} to each element of
857 @var{sequence}. The result is a sequence of type @var{type}, or a
858 list if @var{type} is @code{nil}.
859
860 @example
861 @group
862 (seq-mapcat #'seq-reverse '((3 2 1) (6 5 4)))
863 @result{} (1 2 3 4 5 6)
864 @end group
865 @end example
866 @end defun
867
868 @defun seq-partition sequence n
869 This function returns a list of the elements of @var{sequence}
870 grouped into sub-sequences of length @var{n}. The last sequence may
871 contain less elements than @var{n}. @var{n} must be an integer. If
872 @var{n} is a negative integer or 0, nil is returned.
873
874 @example
875 @group
876 (seq-partition '(0 1 2 3 4 5 6 7) 3)
877 @result{} ((0 1 2) (3 4 5) (6 7))
878 @end group
879 @end example
880 @end defun
881
882 @defun seq-intersection sequence1 sequence2 &optional function
883 This function returns a list of the elements that appear both in
884 @var{sequence1} and @var{sequence2}. If the optional argument
885 @var{function} is non-@code{nil}, it is a function of two arguments to
886 use to compare elements instead of the default @code{equal}.
887
888 @example
889 @group
890 (seq-intersection [2 3 4 5] [1 3 5 6 7])
891 @result{} (3 5)
892 @end group
893 @end example
894 @end defun
895
896
897 @defun seq-difference sequence1 sequence2 &optional function
898 This function returns a list of the elements that appear in
899 @var{sequence1} but not in @var{sequence2}. If the optional argument
900 @var{function} is non-@code{nil}, it is a function of two arguments to
901 use to compare elements instead of the default @code{equal}.
902
903 @example
904 @group
905 (seq-difference '(2 3 4 5) [1 3 5 6 7])
906 @result{} (2 4)
907 @end group
908 @end example
909 @end defun
910
911 @defun seq-group-by function sequence
912 This function separates the elements of @var{sequence} into an alist
913 whose keys are the result of applying @var{function} to each element
914 of @var{sequence}. Keys are compared using @code{equal}.
915
916 @example
917 @group
918 (seq-group-by #'integerp '(1 2.1 3 2 3.2))
919 @result{} ((t 1 3 2) (nil 2.1 3.2))
920 @end group
921 @group
922 (seq-group-by #'car '((a 1) (b 2) (a 3) (c 4)))
923 @result{} ((b (b 2)) (a (a 1) (a 3)) (c (c 4)))
924 @end group
925 @end example
926 @end defun
927
928 @defun seq-into sequence type
929 This function converts the sequence @var{sequence} into a sequence
930 of type @var{type}. @var{type} can be one of the following symbols:
931 @code{vector}, @code{string} or @code{list}.
932
933 @example
934 @group
935 (seq-into [1 2 3] 'list)
936 @result{} (1 2 3)
937 @end group
938 @group
939 (seq-into nil 'vector)
940 @result{} []
941 @end group
942 @group
943 (seq-into "hello" 'vector)
944 @result{} [104 101 108 108 111]
945 @end group
946 @end example
947 @end defun
948
949 @defun seq-min sequence
950 This function returns the smallest element of
951 @var{sequence}. @var{sequence} must be a sequence of numbers or
952 markers.
953
954 @example
955 @group
956 (seq-min [3 1 2])
957 @result{} 1
958 @end group
959 @group
960 (seq-min "Hello")
961 @result{} 72
962 @end group
963 @end example
964 @end defun
965
966 @defun seq-max sequence
967 This function returns the largest element of
968 @var{sequence}. @var{sequence} must be a sequence of numbers or
969 markers.
970
971 @example
972 @group
973 (seq-max [1 3 2])
974 @result{} 3
975 @end group
976 @group
977 (seq-max "Hello")
978 @result{} 111
979 @end group
980 @end example
981 @end defun
982
983 @defmac seq-doseq (var sequence) body@dots{}
984 @cindex sequence iteration
985 This macro is like @code{dolist}, except that @var{sequence} can be a list,
986 vector or string (@pxref{Iteration} for more information about the
987 @code{dolist} macro). This is primarily useful for side-effects.
988 @end defmac
989
990 @defmac seq-let arguments sequence body@dots{}
991 @cindex sequence destructuring
992 This macro binds the variables defined in @var{arguments} to the
993 elements of the sequence @var{sequence}. @var{arguments} can itself
994 include sequences allowing for nested destructuring.
995
996 The @var{arguments} sequence can also include the @code{&rest} marker
997 followed by a variable name to be bound to the rest of
998 @code{sequence}.
999
1000 @example
1001 @group
1002 (seq-let [first second] [1 2 3 4]
1003 (list first second))
1004 @result{} (1 2)
1005 @end group
1006 @group
1007 (seq-let (_ a _ b) '(1 2 3 4)
1008 (list a b))
1009 @result{} (2 4)
1010 @end group
1011 @group
1012 (seq-let [a [b [c]]] [1 [2 [3]]]
1013 (list a b c))
1014 @result{} (1 2 3)
1015 @end group
1016 @group
1017 (seq-let [a b &rest others] [1 2 3 4]
1018 others)
1019 @end group
1020 @result{} [3 4]
1021 @end example
1022 @end defmac
1023
1024
1025 @node Arrays
1026 @section Arrays
1027 @cindex array
1028
1029 An @dfn{array} object has slots that hold a number of other Lisp
1030 objects, called the elements of the array. Any element of an array
1031 may be accessed in constant time. In contrast, the time to access an
1032 element of a list is proportional to the position of that element in
1033 the list.
1034
1035 Emacs defines four types of array, all one-dimensional:
1036 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
1037 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
1038 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
1039 can hold elements of any type, but strings can only hold characters,
1040 and bool-vectors can only hold @code{t} and @code{nil}.
1041
1042 All four kinds of array share these characteristics:
1043
1044 @itemize @bullet
1045 @item
1046 The first element of an array has index zero, the second element has
1047 index 1, and so on. This is called @dfn{zero-origin} indexing. For
1048 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
1049
1050 @item
1051 The length of the array is fixed once you create it; you cannot
1052 change the length of an existing array.
1053
1054 @item
1055 For purposes of evaluation, the array is a constant---i.e.,
1056 it evaluates to itself.
1057
1058 @item
1059 The elements of an array may be referenced or changed with the functions
1060 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
1061 @end itemize
1062
1063 When you create an array, other than a char-table, you must specify
1064 its length. You cannot specify the length of a char-table, because that
1065 is determined by the range of character codes.
1066
1067 In principle, if you want an array of text characters, you could use
1068 either a string or a vector. In practice, we always choose strings for
1069 such applications, for four reasons:
1070
1071 @itemize @bullet
1072 @item
1073 They occupy one-fourth the space of a vector of the same elements.
1074
1075 @item
1076 Strings are printed in a way that shows the contents more clearly
1077 as text.
1078
1079 @item
1080 Strings can hold text properties. @xref{Text Properties}.
1081
1082 @item
1083 Many of the specialized editing and I/O facilities of Emacs accept only
1084 strings. For example, you cannot insert a vector of characters into a
1085 buffer the way you can insert a string. @xref{Strings and Characters}.
1086 @end itemize
1087
1088 By contrast, for an array of keyboard input characters (such as a key
1089 sequence), a vector may be necessary, because many keyboard input
1090 characters are outside the range that will fit in a string. @xref{Key
1091 Sequence Input}.
1092
1093 @node Array Functions
1094 @section Functions that Operate on Arrays
1095
1096 In this section, we describe the functions that accept all types of
1097 arrays.
1098
1099 @defun arrayp object
1100 This function returns @code{t} if @var{object} is an array (i.e., a
1101 vector, a string, a bool-vector or a char-table).
1102
1103 @example
1104 @group
1105 (arrayp [a])
1106 @result{} t
1107 (arrayp "asdf")
1108 @result{} t
1109 (arrayp (syntax-table)) ;; @r{A char-table.}
1110 @result{} t
1111 @end group
1112 @end example
1113 @end defun
1114
1115 @defun aref array index
1116 @cindex array elements
1117 This function returns the @var{index}th element of @var{array}. The
1118 first element is at index zero.
1119
1120 @example
1121 @group
1122 (setq primes [2 3 5 7 11 13])
1123 @result{} [2 3 5 7 11 13]
1124 (aref primes 4)
1125 @result{} 11
1126 @end group
1127 @group
1128 (aref "abcdefg" 1)
1129 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
1130 @end group
1131 @end example
1132
1133 See also the function @code{elt}, in @ref{Sequence Functions}.
1134 @end defun
1135
1136 @defun aset array index object
1137 This function sets the @var{index}th element of @var{array} to be
1138 @var{object}. It returns @var{object}.
1139
1140 @example
1141 @group
1142 (setq w [foo bar baz])
1143 @result{} [foo bar baz]
1144 (aset w 0 'fu)
1145 @result{} fu
1146 w
1147 @result{} [fu bar baz]
1148 @end group
1149
1150 @group
1151 (setq x "asdfasfd")
1152 @result{} "asdfasfd"
1153 (aset x 3 ?Z)
1154 @result{} 90
1155 x
1156 @result{} "asdZasfd"
1157 @end group
1158 @end example
1159
1160 If @var{array} is a string and @var{object} is not a character, a
1161 @code{wrong-type-argument} error results. The function converts a
1162 unibyte string to multibyte if necessary to insert a character.
1163 @end defun
1164
1165 @defun fillarray array object
1166 This function fills the array @var{array} with @var{object}, so that
1167 each element of @var{array} is @var{object}. It returns @var{array}.
1168
1169 @example
1170 @group
1171 (setq a [a b c d e f g])
1172 @result{} [a b c d e f g]
1173 (fillarray a 0)
1174 @result{} [0 0 0 0 0 0 0]
1175 a
1176 @result{} [0 0 0 0 0 0 0]
1177 @end group
1178 @group
1179 (setq s "When in the course")
1180 @result{} "When in the course"
1181 (fillarray s ?-)
1182 @result{} "------------------"
1183 @end group
1184 @end example
1185
1186 If @var{array} is a string and @var{object} is not a character, a
1187 @code{wrong-type-argument} error results.
1188 @end defun
1189
1190 The general sequence functions @code{copy-sequence} and @code{length}
1191 are often useful for objects known to be arrays. @xref{Sequence Functions}.
1192
1193 @node Vectors
1194 @section Vectors
1195 @cindex vector (type)
1196
1197 A @dfn{vector} is a general-purpose array whose elements can be any
1198 Lisp objects. (By contrast, the elements of a string can only be
1199 characters. @xref{Strings and Characters}.) Vectors are used in
1200 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
1201 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
1202 representation of a byte-compiled function (@pxref{Byte Compilation}),
1203 and more.
1204
1205 Like other arrays, vectors use zero-origin indexing: the first
1206 element has index 0.
1207
1208 Vectors are printed with square brackets surrounding the elements.
1209 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
1210 @code{a} is printed as @code{[a b a]}. You can write vectors in the
1211 same way in Lisp input.
1212
1213 A vector, like a string or a number, is considered a constant for
1214 evaluation: the result of evaluating it is the same vector. This does
1215 not evaluate or even examine the elements of the vector.
1216 @xref{Self-Evaluating Forms}.
1217
1218 Here are examples illustrating these principles:
1219
1220 @example
1221 @group
1222 (setq avector [1 two '(three) "four" [five]])
1223 @result{} [1 two (quote (three)) "four" [five]]
1224 (eval avector)
1225 @result{} [1 two (quote (three)) "four" [five]]
1226 (eq avector (eval avector))
1227 @result{} t
1228 @end group
1229 @end example
1230
1231 @node Vector Functions
1232 @section Functions for Vectors
1233
1234 Here are some functions that relate to vectors:
1235
1236 @defun vectorp object
1237 This function returns @code{t} if @var{object} is a vector.
1238
1239 @example
1240 @group
1241 (vectorp [a])
1242 @result{} t
1243 (vectorp "asdf")
1244 @result{} nil
1245 @end group
1246 @end example
1247 @end defun
1248
1249 @defun vector &rest objects
1250 This function creates and returns a vector whose elements are the
1251 arguments, @var{objects}.
1252
1253 @example
1254 @group
1255 (vector 'foo 23 [bar baz] "rats")
1256 @result{} [foo 23 [bar baz] "rats"]
1257 (vector)
1258 @result{} []
1259 @end group
1260 @end example
1261 @end defun
1262
1263 @defun make-vector length object
1264 This function returns a new vector consisting of @var{length} elements,
1265 each initialized to @var{object}.
1266
1267 @example
1268 @group
1269 (setq sleepy (make-vector 9 'Z))
1270 @result{} [Z Z Z Z Z Z Z Z Z]
1271 @end group
1272 @end example
1273 @end defun
1274
1275 @defun vconcat &rest sequences
1276 @cindex copying vectors
1277 This function returns a new vector containing all the elements of
1278 @var{sequences}. The arguments @var{sequences} may be true lists,
1279 vectors, strings or bool-vectors. If no @var{sequences} are given,
1280 the empty vector is returned.
1281
1282 The value is either the empty vector, or is a newly constructed
1283 nonempty vector that is not @code{eq} to any existing vector.
1284
1285 @example
1286 @group
1287 (setq a (vconcat '(A B C) '(D E F)))
1288 @result{} [A B C D E F]
1289 (eq a (vconcat a))
1290 @result{} nil
1291 @end group
1292 @group
1293 (vconcat)
1294 @result{} []
1295 (vconcat [A B C] "aa" '(foo (6 7)))
1296 @result{} [A B C 97 97 foo (6 7)]
1297 @end group
1298 @end example
1299
1300 The @code{vconcat} function also allows byte-code function objects as
1301 arguments. This is a special feature to make it easy to access the entire
1302 contents of a byte-code function object. @xref{Byte-Code Objects}.
1303
1304 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
1305 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
1306 in @ref{Building Lists}.
1307 @end defun
1308
1309 The @code{append} function also provides a way to convert a vector into a
1310 list with the same elements:
1311
1312 @example
1313 @group
1314 (setq avector [1 two (quote (three)) "four" [five]])
1315 @result{} [1 two (quote (three)) "four" [five]]
1316 (append avector nil)
1317 @result{} (1 two (quote (three)) "four" [five])
1318 @end group
1319 @end example
1320
1321 @node Char-Tables
1322 @section Char-Tables
1323 @cindex char-tables
1324 @cindex extra slots of char-table
1325
1326 A char-table is much like a vector, except that it is indexed by
1327 character codes. Any valid character code, without modifiers, can be
1328 used as an index in a char-table. You can access a char-table's
1329 elements with @code{aref} and @code{aset}, as with any array. In
1330 addition, a char-table can have @dfn{extra slots} to hold additional
1331 data not associated with particular character codes. Like vectors,
1332 char-tables are constants when evaluated, and can hold elements of any
1333 type.
1334
1335 @cindex subtype of char-table
1336 Each char-table has a @dfn{subtype}, a symbol, which serves two
1337 purposes:
1338
1339 @itemize @bullet
1340 @item
1341 The subtype provides an easy way to tell what the char-table is for.
1342 For instance, display tables are char-tables with @code{display-table}
1343 as the subtype, and syntax tables are char-tables with
1344 @code{syntax-table} as the subtype. The subtype can be queried using
1345 the function @code{char-table-subtype}, described below.
1346
1347 @item
1348 The subtype controls the number of @dfn{extra slots} in the
1349 char-table. This number is specified by the subtype's
1350 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1351 Properties}), whose value should be an integer between 0 and 10. If
1352 the subtype has no such symbol property, the char-table has no extra
1353 slots.
1354 @end itemize
1355
1356 @cindex parent of char-table
1357 A char-table can have a @dfn{parent}, which is another char-table. If
1358 it does, then whenever the char-table specifies @code{nil} for a
1359 particular character @var{c}, it inherits the value specified in the
1360 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
1361 the value from the parent of @var{char-table} if @var{char-table} itself
1362 specifies @code{nil}.
1363
1364 @cindex default value of char-table
1365 A char-table can also have a @dfn{default value}. If so, then
1366 @code{(aref @var{char-table} @var{c})} returns the default value
1367 whenever the char-table does not specify any other non-@code{nil} value.
1368
1369 @defun make-char-table subtype &optional init
1370 Return a newly-created char-table, with subtype @var{subtype} (a
1371 symbol). Each element is initialized to @var{init}, which defaults to
1372 @code{nil}. You cannot alter the subtype of a char-table after the
1373 char-table is created.
1374
1375 There is no argument to specify the length of the char-table, because
1376 all char-tables have room for any valid character code as an index.
1377
1378 If @var{subtype} has the @code{char-table-extra-slots} symbol
1379 property, that specifies the number of extra slots in the char-table.
1380 This should be an integer between 0 and 10; otherwise,
1381 @code{make-char-table} raises an error. If @var{subtype} has no
1382 @code{char-table-extra-slots} symbol property (@pxref{Property
1383 Lists}), the char-table has no extra slots.
1384 @end defun
1385
1386 @defun char-table-p object
1387 This function returns @code{t} if @var{object} is a char-table, and
1388 @code{nil} otherwise.
1389 @end defun
1390
1391 @defun char-table-subtype char-table
1392 This function returns the subtype symbol of @var{char-table}.
1393 @end defun
1394
1395 There is no special function to access default values in a char-table.
1396 To do that, use @code{char-table-range} (see below).
1397
1398 @defun char-table-parent char-table
1399 This function returns the parent of @var{char-table}. The parent is
1400 always either @code{nil} or another char-table.
1401 @end defun
1402
1403 @defun set-char-table-parent char-table new-parent
1404 This function sets the parent of @var{char-table} to @var{new-parent}.
1405 @end defun
1406
1407 @defun char-table-extra-slot char-table n
1408 This function returns the contents of extra slot @var{n} (zero based)
1409 of @var{char-table}. The number of extra slots in a char-table is
1410 determined by its subtype.
1411 @end defun
1412
1413 @defun set-char-table-extra-slot char-table n value
1414 This function stores @var{value} in extra slot @var{n} (zero based) of
1415 @var{char-table}.
1416 @end defun
1417
1418 A char-table can specify an element value for a single character code;
1419 it can also specify a value for an entire character set.
1420
1421 @defun char-table-range char-table range
1422 This returns the value specified in @var{char-table} for a range of
1423 characters @var{range}. Here are the possibilities for @var{range}:
1424
1425 @table @asis
1426 @item @code{nil}
1427 Refers to the default value.
1428
1429 @item @var{char}
1430 Refers to the element for character @var{char}
1431 (supposing @var{char} is a valid character code).
1432
1433 @item @code{(@var{from} . @var{to})}
1434 A cons cell refers to all the characters in the inclusive range
1435 @samp{[@var{from}..@var{to}]}.
1436 @end table
1437 @end defun
1438
1439 @defun set-char-table-range char-table range value
1440 This function sets the value in @var{char-table} for a range of
1441 characters @var{range}. Here are the possibilities for @var{range}:
1442
1443 @table @asis
1444 @item @code{nil}
1445 Refers to the default value.
1446
1447 @item @code{t}
1448 Refers to the whole range of character codes.
1449
1450 @item @var{char}
1451 Refers to the element for character @var{char}
1452 (supposing @var{char} is a valid character code).
1453
1454 @item @code{(@var{from} . @var{to})}
1455 A cons cell refers to all the characters in the inclusive range
1456 @samp{[@var{from}..@var{to}]}.
1457 @end table
1458 @end defun
1459
1460 @defun map-char-table function char-table
1461 This function calls its argument @var{function} for each element of
1462 @var{char-table} that has a non-@code{nil} value. The call to
1463 @var{function} is with two arguments, a key and a value. The key
1464 is a possible @var{range} argument for @code{char-table-range}---either
1465 a valid character or a cons cell @code{(@var{from} . @var{to})},
1466 specifying a range of characters that share the same value. The value is
1467 what @code{(char-table-range @var{char-table} @var{key})} returns.
1468
1469 Overall, the key-value pairs passed to @var{function} describe all the
1470 values stored in @var{char-table}.
1471
1472 The return value is always @code{nil}; to make calls to
1473 @code{map-char-table} useful, @var{function} should have side effects.
1474 For example, here is how to examine the elements of the syntax table:
1475
1476 @example
1477 (let (accumulator)
1478 (map-char-table
1479 #'(lambda (key value)
1480 (setq accumulator
1481 (cons (list
1482 (if (consp key)
1483 (list (car key) (cdr key))
1484 key)
1485 value)
1486 accumulator)))
1487 (syntax-table))
1488 accumulator)
1489 @result{}
1490 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1491 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1492 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1493 @end example
1494 @end defun
1495
1496 @node Bool-Vectors
1497 @section Bool-vectors
1498 @cindex Bool-vectors
1499
1500 A bool-vector is much like a vector, except that it stores only the
1501 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
1502 value into an element of the bool-vector, the effect is to store
1503 @code{t} there. As with all arrays, bool-vector indices start from 0,
1504 and the length cannot be changed once the bool-vector is created.
1505 Bool-vectors are constants when evaluated.
1506
1507 Several functions work specifically with bool-vectors; aside
1508 from that, you manipulate them with same functions used for other kinds
1509 of arrays.
1510
1511 @defun make-bool-vector length initial
1512 Return a new bool-vector of @var{length} elements,
1513 each one initialized to @var{initial}.
1514 @end defun
1515
1516 @defun bool-vector &rest objects
1517 This function creates and returns a bool-vector whose elements are the
1518 arguments, @var{objects}.
1519 @end defun
1520
1521 @defun bool-vector-p object
1522 This returns @code{t} if @var{object} is a bool-vector,
1523 and @code{nil} otherwise.
1524 @end defun
1525
1526 There are also some bool-vector set operation functions, described below:
1527
1528 @defun bool-vector-exclusive-or a b &optional c
1529 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1530 If optional argument @var{c} is given, the result of this operation is
1531 stored into @var{c}. All arguments should be bool vectors of the same length.
1532 @end defun
1533
1534 @defun bool-vector-union a b &optional c
1535 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}. If
1536 optional argument @var{c} is given, the result of this operation is
1537 stored into @var{c}. All arguments should be bool vectors of the same length.
1538 @end defun
1539
1540 @defun bool-vector-intersection a b &optional c
1541 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}. If
1542 optional argument @var{c} is given, the result of this operation is
1543 stored into @var{c}. All arguments should be bool vectors of the same length.
1544 @end defun
1545
1546 @defun bool-vector-set-difference a b &optional c
1547 Return @dfn{set difference} of bool vectors @var{a} and @var{b}. If
1548 optional argument @var{c} is given, the result of this operation is
1549 stored into @var{c}. All arguments should be bool vectors of the same length.
1550 @end defun
1551
1552 @defun bool-vector-not a &optional b
1553 Return @dfn{set complement} of bool vector @var{a}. If optional
1554 argument @var{b} is given, the result of this operation is stored into
1555 @var{b}. All arguments should be bool vectors of the same length.
1556 @end defun
1557
1558 @defun bool-vector-subsetp a b
1559 Return @code{t} if every @code{t} value in @var{a} is also t in
1560 @var{b}, @code{nil} otherwise. All arguments should be bool vectors of the
1561 same length.
1562 @end defun
1563
1564 @defun bool-vector-count-consecutive a b i
1565 Return the number of consecutive elements in @var{a} equal @var{b}
1566 starting at @var{i}. @code{a} is a bool vector, @var{b} is @code{t}
1567 or @code{nil}, and @var{i} is an index into @code{a}.
1568 @end defun
1569
1570 @defun bool-vector-count-population a
1571 Return the number of elements that are @code{t} in bool vector @var{a}.
1572 @end defun
1573
1574 The printed form represents up to 8 boolean values as a single
1575 character:
1576
1577 @example
1578 @group
1579 (bool-vector t nil t nil)
1580 @result{} #&4"^E"
1581 (bool-vector)
1582 @result{} #&0""
1583 @end group
1584 @end example
1585
1586 You can use @code{vconcat} to print a bool-vector like other vectors:
1587
1588 @example
1589 @group
1590 (vconcat (bool-vector nil t nil t))
1591 @result{} [nil t nil t]
1592 @end group
1593 @end example
1594
1595 Here is another example of creating, examining, and updating a
1596 bool-vector:
1597
1598 @example
1599 (setq bv (make-bool-vector 5 t))
1600 @result{} #&5"^_"
1601 (aref bv 1)
1602 @result{} t
1603 (aset bv 3 nil)
1604 @result{} nil
1605 bv
1606 @result{} #&5"^W"
1607 @end example
1608
1609 @noindent
1610 These results make sense because the binary codes for control-_ and
1611 control-W are 11111 and 10111, respectively.
1612
1613 @node Rings
1614 @section Managing a Fixed-Size Ring of Objects
1615
1616 @cindex ring data structure
1617 A @dfn{ring} is a fixed-size data structure that supports insertion,
1618 deletion, rotation, and modulo-indexed reference and traversal. An
1619 efficient ring data structure is implemented by the @code{ring}
1620 package. It provides the functions listed in this section.
1621
1622 Note that several rings in Emacs, like the kill ring and the
1623 mark ring, are actually implemented as simple lists, @emph{not} using
1624 the @code{ring} package; thus the following functions won't work on
1625 them.
1626
1627 @defun make-ring size
1628 This returns a new ring capable of holding @var{size} objects.
1629 @var{size} should be an integer.
1630 @end defun
1631
1632 @defun ring-p object
1633 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1634 @end defun
1635
1636 @defun ring-size ring
1637 This returns the maximum capacity of the @var{ring}.
1638 @end defun
1639
1640 @defun ring-length ring
1641 This returns the number of objects that @var{ring} currently contains.
1642 The value will never exceed that returned by @code{ring-size}.
1643 @end defun
1644
1645 @defun ring-elements ring
1646 This returns a list of the objects in @var{ring}, in order, newest first.
1647 @end defun
1648
1649 @defun ring-copy ring
1650 This returns a new ring which is a copy of @var{ring}.
1651 The new ring contains the same (@code{eq}) objects as @var{ring}.
1652 @end defun
1653
1654 @defun ring-empty-p ring
1655 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1656 @end defun
1657
1658 The newest element in the ring always has index 0. Higher indices
1659 correspond to older elements. Indices are computed modulo the ring
1660 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
1661 to the next-oldest, and so forth.
1662
1663 @defun ring-ref ring index
1664 This returns the object in @var{ring} found at index @var{index}.
1665 @var{index} may be negative or greater than the ring length. If
1666 @var{ring} is empty, @code{ring-ref} signals an error.
1667 @end defun
1668
1669 @defun ring-insert ring object
1670 This inserts @var{object} into @var{ring}, making it the newest
1671 element, and returns @var{object}.
1672
1673 If the ring is full, insertion removes the oldest element to
1674 make room for the new element.
1675 @end defun
1676
1677 @defun ring-remove ring &optional index
1678 Remove an object from @var{ring}, and return that object. The
1679 argument @var{index} specifies which item to remove; if it is
1680 @code{nil}, that means to remove the oldest item. If @var{ring} is
1681 empty, @code{ring-remove} signals an error.
1682 @end defun
1683
1684 @defun ring-insert-at-beginning ring object
1685 This inserts @var{object} into @var{ring}, treating it as the oldest
1686 element. The return value is not significant.
1687
1688 If the ring is full, this function removes the newest element to make
1689 room for the inserted element.
1690 @end defun
1691
1692 @cindex fifo data structure
1693 If you are careful not to exceed the ring size, you can
1694 use the ring as a first-in-first-out queue. For example:
1695
1696 @lisp
1697 (let ((fifo (make-ring 5)))
1698 (mapc (lambda (obj) (ring-insert fifo obj))
1699 '(0 one "two"))
1700 (list (ring-remove fifo) t
1701 (ring-remove fifo) t
1702 (ring-remove fifo)))
1703 @result{} (0 t one t "two")
1704 @end lisp