Speed up package--list-loaded-files a bit
[emacs.git] / doc / lispref / sequences.texi
blob8f8cfe72fa3d3eccb8a94f726ce20dcd116a2aec
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software
4 @c Foundation, Inc.
5 @c See the file elisp.texi for copying conditions.
6 @node Sequences Arrays Vectors
7 @chapter Sequences, Arrays, and Vectors
8 @cindex sequence
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 This function returns the number of elements in @var{sequence}.  If
76 @var{sequence} is a dotted list, a @code{wrong-type-argument} error is
77 signaled.  Circular lists may cause an infinite loop.  For a
78 char-table, the value returned is always one more than the maximum
79 Emacs character code.
81 @xref{Definition of safe-length}, for the related function @code{safe-length}.
83 @example
84 @group
85 (length '(1 2 3))
86     @result{} 3
87 @end group
88 @group
89 (length ())
90     @result{} 0
91 @end group
92 @group
93 (length "foobar")
94     @result{} 6
95 @end group
96 @group
97 (length [1 2 3])
98     @result{} 3
99 @end group
100 @group
101 (length (make-bool-vector 5 nil))
102     @result{} 5
103 @end group
104 @end example
105 @end defun
107 @noindent
108 See also @code{string-bytes}, in @ref{Text Representations}.
110 If you need to compute the width of a string on display, you should use
111 @code{string-width} (@pxref{Size of Displayed Text}), not @code{length},
112 since @code{length} only counts the number of characters, but does not
113 account for the display width of each character.
115 @defun elt sequence index
116 @cindex elements of sequences
117 This function returns the element of @var{sequence} indexed by
118 @var{index}.  Legitimate values of @var{index} are integers ranging
119 from 0 up to one less than the length of @var{sequence}.  If
120 @var{sequence} is a list, out-of-range values behave as for
121 @code{nth}.  @xref{Definition of nth}.  Otherwise, out-of-range values
122 trigger an @code{args-out-of-range} error.
124 @example
125 @group
126 (elt [1 2 3 4] 2)
127      @result{} 3
128 @end group
129 @group
130 (elt '(1 2 3 4) 2)
131      @result{} 3
132 @end group
133 @group
134 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
135 (string (elt "1234" 2))
136      @result{} "3"
137 @end group
138 @group
139 (elt [1 2 3 4] 4)
140      @error{} Args out of range: [1 2 3 4], 4
141 @end group
142 @group
143 (elt [1 2 3 4] -1)
144      @error{} Args out of range: [1 2 3 4], -1
145 @end group
146 @end example
148 This function generalizes @code{aref} (@pxref{Array Functions}) and
149 @code{nth} (@pxref{Definition of nth}).
150 @end defun
152 @defun copy-sequence sequence
153 @cindex copying sequences
154 This function returns a copy of @var{sequence}.  The copy is the same
155 type of object as the original sequence, and it has the same elements
156 in the same order.
158 Storing a new element into the copy does not affect the original
159 @var{sequence}, and vice versa.  However, the elements of the new
160 sequence are not copies; they are identical (@code{eq}) to the elements
161 of the original.  Therefore, changes made within these elements, as
162 found via the copied sequence, are also visible in the original
163 sequence.
165 If the sequence is a string with text properties, the property list in
166 the copy is itself a copy, not shared with the original's property
167 list.  However, the actual values of the properties are shared.
168 @xref{Text Properties}.
170 This function does not work for dotted lists.  Trying to copy a
171 circular list may cause an infinite loop.
173 See also @code{append} in @ref{Building Lists}, @code{concat} in
174 @ref{Creating Strings}, and @code{vconcat} in @ref{Vector Functions},
175 for other ways to copy sequences.
177 @example
178 @group
179 (setq bar '(1 2))
180      @result{} (1 2)
181 @end group
182 @group
183 (setq x (vector 'foo bar))
184      @result{} [foo (1 2)]
185 @end group
186 @group
187 (setq y (copy-sequence x))
188      @result{} [foo (1 2)]
189 @end group
191 @group
192 (eq x y)
193      @result{} nil
194 @end group
195 @group
196 (equal x y)
197      @result{} t
198 @end group
199 @group
200 (eq (elt x 1) (elt y 1))
201      @result{} t
202 @end group
204 @group
205 ;; @r{Replacing an element of one sequence.}
206 (aset x 0 'quux)
207 x @result{} [quux (1 2)]
208 y @result{} [foo (1 2)]
209 @end group
211 @group
212 ;; @r{Modifying the inside of a shared element.}
213 (setcar (aref x 1) 69)
214 x @result{} [quux (69 2)]
215 y @result{} [foo (69 2)]
216 @end group
217 @end example
218 @end defun
220 @defun reverse seq
221 @cindex string reverse
222 @cindex list reverse
223 @cindex vector reverse
224 @cindex sequence reverse
225 This function creates a new sequence whose elements are the elements
226 of @var{seq}, but in reverse order.  The original argument @var{seq}
227 is @emph{not} altered.   Note that char-table cannot be reversed.
229 @example
230 @group
231 (setq x '(1 2 3 4))
232      @result{} (1 2 3 4)
233 @end group
234 @group
235 (reverse x)
236      @result{} (4 3 2 1)
238      @result{} (1 2 3 4)
239 @end group
240 @group
241 (setq x [1 2 3 4])
242      @result{} [1 2 3 4]
243 @end group
244 @group
245 (reverse x)
246      @result{} [4 3 2 1]
248      @result{} [1 2 3 4]
249 @end group
250 @group
251 (setq x "xyzzy")
252      @result{} "xyzzy"
253 @end group
254 @group
255 (reverse x)
256      @result{} "yzzyx"
258      @result{} "xyzzy"
259 @end group
260 @end example
261 @end defun
263 @defun nreverse seq
264 @cindex reversing a string
265 @cindex reversing a list
266 @cindex reversing a vector
267   This function reverses the order of the elements of @var{seq}.
268 Unlike @code{reverse} the original @var{seq} may be modified.
270   For example:
272 @example
273 @group
274 (setq x '(a b c))
275      @result{} (a b c)
276 @end group
277 @group
279      @result{} (a b c)
280 (nreverse x)
281      @result{} (c b a)
282 @end group
283 @group
284 ;; @r{The cons cell that was first is now last.}
286      @result{} (a)
287 @end group
288 @end example
290   To avoid confusion, we usually store the result of @code{nreverse}
291 back in the same variable which held the original list:
293 @example
294 (setq x (nreverse x))
295 @end example
297   Here is the @code{nreverse} of our favorite example, @code{(a b c)},
298 presented graphically:
300 @smallexample
301 @group
302 @r{Original list head:}                       @r{Reversed list:}
303  -------------        -------------        ------------
304 | car  | cdr  |      | car  | cdr  |      | car | cdr  |
305 |   a  |  nil |<--   |   b  |   o  |<--   |   c |   o  |
306 |      |      |   |  |      |   |  |   |  |     |   |  |
307  -------------    |   --------- | -    |   -------- | -
308                   |             |      |            |
309                    -------------        ------------
310 @end group
311 @end smallexample
313   For the vector, it is even simpler because you don't need setq:
315 @example
316 (setq x [1 2 3 4])
317      @result{} [1 2 3 4]
318 (nreverse x)
319      @result{} [4 3 2 1]
321      @result{} [4 3 2 1]
322 @end example
324 Note that unlike @code{reverse}, this function doesn't work with strings.
325 Although you can alter string data by using @code{aset}, it is strongly
326 encouraged to treat strings as immutable.
328 @end defun
330 @defun sort sequence predicate
331 @cindex stable sort
332 @cindex sorting lists
333 @cindex sorting vectors
334 This function sorts @var{sequence} stably.  Note that this function doesn't work
335 for all sequences; it may be used only for lists and vectors.  If @var{sequence}
336 is a list, it is modified destructively.  This functions returns the sorted
337 @var{sequence} and compares elements using @var{predicate}.  A stable sort is
338 one in which elements with equal sort keys maintain their relative order before
339 and after the sort.  Stability is important when successive sorts are used to
340 order elements according to different criteria.
342 The argument @var{predicate} must be a function that accepts two
343 arguments.  It is called with two elements of @var{sequence}.  To get an
344 increasing order sort, the @var{predicate} should return non-@code{nil} if the
345 first element is ``less than'' the second, or @code{nil} if not.
347 The comparison function @var{predicate} must give reliable results for
348 any given pair of arguments, at least within a single call to
349 @code{sort}.  It must be @dfn{antisymmetric}; that is, if @var{a} is
350 less than @var{b}, @var{b} must not be less than @var{a}.  It must be
351 @dfn{transitive}---that is, if @var{a} is less than @var{b}, and @var{b}
352 is less than @var{c}, then @var{a} must be less than @var{c}.  If you
353 use a comparison function which does not meet these requirements, the
354 result of @code{sort} is unpredictable.
356 The destructive aspect of @code{sort} for lists is that it rearranges the
357 cons cells forming @var{sequence} by changing @sc{cdr}s.  A nondestructive
358 sort function would create new cons cells to store the elements in their
359 sorted order.  If you wish to make a sorted copy without destroying the
360 original, copy it first with @code{copy-sequence} and then sort.
362 Sorting does not change the @sc{car}s of the cons cells in @var{sequence};
363 the cons cell that originally contained the element @code{a} in
364 @var{sequence} still has @code{a} in its @sc{car} after sorting, but it now
365 appears in a different position in the list due to the change of
366 @sc{cdr}s.  For example:
368 @example
369 @group
370 (setq nums '(1 3 2 6 5 4 0))
371      @result{} (1 3 2 6 5 4 0)
372 @end group
373 @group
374 (sort nums '<)
375      @result{} (0 1 2 3 4 5 6)
376 @end group
377 @group
378 nums
379      @result{} (1 2 3 4 5 6)
380 @end group
381 @end example
383 @noindent
384 @strong{Warning}: Note that the list in @code{nums} no longer contains
385 0; this is the same cons cell that it was before, but it is no longer
386 the first one in the list.  Don't assume a variable that formerly held
387 the argument now holds the entire sorted list!  Instead, save the result
388 of @code{sort} and use that.  Most often we store the result back into
389 the variable that held the original list:
391 @example
392 (setq nums (sort nums '<))
393 @end example
395 For the better understanding of what stable sort is, consider the following
396 vector example.  After sorting, all items whose @code{car} is 8 are grouped
397 at the beginning of @code{vector}, but their relative order is preserved.
398 All items whose @code{car} is 9 are grouped at the end of @code{vector},
399 but their relative order is also preserved:
401 @example
402 @group
403 (setq
404   vector
405   (vector '(8 . "xxx") '(9 . "aaa") '(8 . "bbb") '(9 . "zzz")
406           '(9 . "ppp") '(8 . "ttt") '(8 . "eee") '(9 . "fff")))
407      @result{} [(8 . "xxx") (9 . "aaa") (8 . "bbb") (9 . "zzz")
408          (9 . "ppp") (8 . "ttt") (8 . "eee") (9 . "fff")]
409 @end group
410 @group
411 (sort vector (lambda (x y) (< (car x) (car y))))
412      @result{} [(8 . "xxx") (8 . "bbb") (8 . "ttt") (8 . "eee")
413          (9 . "aaa") (9 . "zzz") (9 . "ppp") (9 . "fff")]
414 @end group
415 @end example
416                 
417 @xref{Sorting}, for more functions that perform sorting.
418 See @code{documentation} in @ref{Accessing Documentation}, for a
419 useful example of @code{sort}.
420 @end defun
422 @cindex sequence functions in seq
423 @cindex seq library
424   The @file{seq} library provides the following additional sequence
425 manipulation macros and functions, prefixed with @code{seq-}.  To use
426 them, you need to load the @file{seq} library first.
428   All functions defined in the @code{seq} library are free of
429 side-effects, meaning that sequence(s) passed as argument(s) to
430 functions defined in @code{seq} are not modified.
432 @defun seq-drop seq n
433   This function returns a sequence of all but the first @var{n}
434 elements of the sequence @var{seq}.
436 @var{seq} may be a list, vector or string and @var{n} must be an
437 integer.  The result is the same type of sequence as @var{seq}.
439 If @var{n} is a negative integer or zero, @var{seq} is returned.
441 @example
442 @group
443 (seq-drop [1 2 3 4 5 6] 3)
444 @result{} [4 5 6]
445 @end group
446 @group
447 (seq-drop "hello world" -4)
448 @result{} "hello world"
449 @end group
450 @end example
451 @end defun
453 @defun seq-take seq n
454   This function returns a sequence of the first @var{n} elements of
455 @var{seq}.
457 @var{seq} may be a list, vector or string and @var{n} must be an
458 integer.  The result is the same type of sequence as @var{seq}.
460 If @var{n} is a negative integer or zero, an empty sequence is returned.
462 @example
463 @group
464 (seq-take '(1 2 3 4) 3)
465 @result{} (1 2 3)
466 @end group
467 @group
468 (seq-take [1 2 3 4] 0)
469 @result{} []
470 @end group
471 @end example
472 @end defun
474 @defun seq-take-while pred seq
475   This function returns a sub-sequence of the successive elements of
476 @var{seq} for which calling @code{pred} with that element returns
477 non-nil.
479 @var{pred} must be a one-argument function and @var{seq} may be a
480 list, vector or string.  The result is the same type of sequence as
481 @var{seq}.
483 If evaluating @var{pred} with the first element of @var{seq} as argument
484 returns @code{nil}, an empty sequence is returned.
486 @example
487 @group
488 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
489 @result{} (1 2 3)
490 @end group
491 @group
492 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
493 @result{} []
494 @end group
495 @end example
496 @end defun
498 @defun seq-drop-while pred seq
499   This function returns a sub-sequence of @var{seq} from the first
500 element for which calling @var{pred} with that element returns
501 @code{nil}.
503 @var{pred} must be a one-argument function and @var{seq} may be a
504 list, vector or string.  The result is the same type of sequence as
505 @var{seq}.
507 If evaluating @var{pred} with every element of @var{seq} returns
508 @code{nil}, @var{seq} is returned.
510 @example
511 @group
512 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
513 @result{} (-1 -2)
514 @end group
515 @group
516 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
517 @result{} [1 4 6]
518 @end group
519 @end example
520 @end defun
522 @defun seq-filter pred seq
523 @cindex filtering sequences
524   This function returns a list of all the elements in @var{seq} for
525 which calling @var{pred} with that element returns non-nil.
527 @var{pred} must be a one-argument function and @var{seq} may be a
528 list, vector or string.
530 @example
531 @group
532 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
533 @result{} (1 3 5)
534 @end group
535 @group
536 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
537 @result{} nil
538 @end group
539 @end example
540 @end defun
542 @defun seq-remove pred seq
543 @cindex removing from sequences
544   This function returns a list of all the elements in @var{seq} for
545 which calling @var{pred} with that element returns @code{nil}.
547 @var{pred} must be a one-argument function and @var{seq} may be a
548 list, vector or string.
550 @example
551 @group
552 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
553 @result{} (-1 -3)
554 @end group
555 @group
556 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
557 @result{} nil
558 @end group
559 @end example
560 @end defun
562 @defun seq-reduce function seq initial-value
563 @cindex reducing sequences
564   This function returns the result of calling @var{function} with
565 @var{initial-value} and the first element of @var{seq}, then calling
566 @var{function} with that result and the second element of @var{seq},
567 then with that result and the third element of @var{seq}, etc.
569 @var{function} must be a two-arguments function and @var{seq} may be a
570 list, vector or string.
572 If @var{seq} is empty, @var{initial-value} is returned and
573 @var{function} is not called.
575 @example
576 @group
577 (seq-reduce #'+ [1 2 3 4] 0)
578 @result{} 10
579 @end group
580 @group
581 (seq-reduce #'+ '(1 2 3 4) 5)
582 @result{} 15
583 @end group
584 @group
585 (seq-reduce #'+ '() 3)
586 @result{} 3
587 @end group
588 @end example
589 @end defun
591 @defun seq-some-p pred seq
592   This function returns any element in @var{seq} for which calling
593 @var{pred} with that element returns non-nil.  If successively calling
594 @var{pred} with each element of @var{seq} always returns @code{nil},
595 @code{nil} is returned.
597 @var{pred} must be a one-argument function and @var{seq} may be a
598 list, vector or string.
600 @example
601 @group
602 (seq-some-p #'numberp ["abc" 1 nil])
603 @result{} 1
604 @end group
605 @group
606 (seq-some-p #'numberp ["abc" "def"])
607 @result{} nil
608 @end group
609 @end example
610 @end defun
612 @defun seq-every-p pred seq
613   This function returns non-nil if successively calling @var{pred} with
614 each element of @var{seq} always returns non-nil, @code{nil} otherwise.
616 @var{pred} must be a one-argument function and @var{seq} may be a
617 list, vector or string.
619 @example
620 @group
621 (seq-every-p #'numberp [2 4 6])
622 @result{} t
623 @end group
624 @group
625 (seq-some-p #'numberp [2 4 "6"])
626 @result{} nil
627 @end group
628 @end example
629 @end defun
631 @defun seq-empty-p seq
632   This function returns non-nil if the sequence @var{seq} is empty,
633 @code{nil} otherwise.
635 @var{seq} may be a list, vector or string.
637 @example
638 @group
639 (seq-empty-p "not empty")
640 @result{} nil
641 @end group
642 @group
643 (seq-empty-p "")
644 @result{} t
645 @end group
646 @end example
647 @end defun
649 @defun seq-count pred seq
650   This function returns the number of elements in @var{seq} for which
651 calling @var{pred} with that element returns non-nil.
653 @var{pred} must be a one-argument function and @var{seq} may be a
654 list, vector or string.
656 @example
657 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
658 @result{} 2
659 @end example
660 @end defun
662 @defun seq-sort pred seq
663   This function returns a sorted sequence of the elements of
664 @var{seq}, comparing its elements with @var{pred}.  Called with two
665 elements of @var{seq}, @var{pred} should return non-nil if the first
666 element should sort before the second.
668 @var{pred} must be a two-arguments function, @var{seq} may be a list,
669 vector or string.
671 The result is a sequence of the same type as SEQ.
672 @cindex sorting sequences
673 @end defun
675 @defun seq-contains-p seq elt testfn
676   This function returns the first element in @var{seq} that equals to
677 @var{elt}.
679 Equality is defined by @var{testfn} if non-nil or by @code{equal} if
680 @code{nil}.
682 @var{seq} may be a list, vector or string.
684 @example
685 @group
686 (seq-contains-p '(symbol1 symbol2) 'symbol1)
687 @result{} symbol1
688 @end group
689 @group
690 (seq-contains-p '(symbol1 symbol2) 'symbol3)
691 @result{} nil
692 @end group
693 @end example
695 @end defun
697 @defun seq-uniq seq testfn
698   This function returns a list of the elements of @var{seq} with
699 duplicates removed.  @var{testfn} is used to compare elements, or
700 @code{equal} if @var{testfn} is @code{nil}.
702 @var{testfn} must be a two-argument function or @code{nil} and
703 @var{seq} may be a list, vector or string.
705 @example
706 @group
707 (seq-uniq '(1 2 2 1 3))
708 @result{} (1 2 3)
709 @end group
710 @group
711 (seq-uniq '(1 2 2.0 1.0) #'=)
712 @result{} [3 4]
713 @end group
714 @end example
715 @end defun
717 @defun seq-subseq seq start &optional end
718   This function returns a sub-sequence of @var{seq} from @var{start}
719 to @var{end}.  If @var{end} is omitted, it default to the length of
720 @var{seq}. If @var{start} or @var{end} is negative, it counts from
721 the end of @var{seq}.
723 @var{seq} may be a list, vector or string.
724 The result is the same type of sequence as @var{seq}.
726 @example
727 @group
728 (seq-subseq '(1 2 3 4 5) 1)
729 @result{} (2 3 4 5)
730 @end group
731 @group
732 (seq-subseq '[1 2 3 4 5] 1 3)
733 @result{} [2 3]
734 @end group
735 @group
736 (seq-subseq '[1 2 3 4 5] -3 -1)
737 @result{} [3 4]
738 @end group
739 @end example
740 @end defun
742 @defun seq-concatenate type &rest seqs
743   This function returns a sequence made of the concatenation of
744 @var{seqs}.  The result is a sequence of type @var{type}.  @var{type}
745 may be one of the following symbols: @code{vector}, @code{list} or
746 @code{string}.
748 @example
749 @group
750 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
751 @result{} (1 2 3 5 6)
752 @end group
753 @group
754 (seq-concatenate 'string "Hello " "world")
755 @result{} "Hello world"
756 @end group
757 @end example
758 @end defun
760 @defmac seq-doseq (var seq [result]) body@dots{}
761 @cindex sequence iteration
762 This macro is like @code{dolist}, except that @var{seq} can be a list,
763 vector or string (@pxref{Iteration} for more information about the
764 @code{dolist} macro).
766 @var{seq-doseq} is primarily useful for side-effects.
768 @example
769 (seq-doseq (elt [1 2 3])
770   (print (* 2 elt)))
771   @print{}
772   @print{} 2
773   @print{}
774   @print{} 4
775   @print{}
776   @print{} 6
777   @result{} nil
779 @end example
780 @end defmac
782 @node Arrays
783 @section Arrays
784 @cindex array
786   An @dfn{array} object has slots that hold a number of other Lisp
787 objects, called the elements of the array.  Any element of an array
788 may be accessed in constant time.  In contrast, the time to access an
789 element of a list is proportional to the position of that element in
790 the list.
792   Emacs defines four types of array, all one-dimensional:
793 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
794 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
795 @dfn{char-tables} (@pxref{Char-Table Type}).  Vectors and char-tables
796 can hold elements of any type, but strings can only hold characters,
797 and bool-vectors can only hold @code{t} and @code{nil}.
799   All four kinds of array share these characteristics:
801 @itemize @bullet
802 @item
803 The first element of an array has index zero, the second element has
804 index 1, and so on.  This is called @dfn{zero-origin} indexing.  For
805 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
807 @item
808 The length of the array is fixed once you create it; you cannot
809 change the length of an existing array.
811 @item
812 For purposes of evaluation, the array is a constant---i.e.,
813 it evaluates to itself.
815 @item
816 The elements of an array may be referenced or changed with the functions
817 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
818 @end itemize
820     When you create an array, other than a char-table, you must specify
821 its length.  You cannot specify the length of a char-table, because that
822 is determined by the range of character codes.
824   In principle, if you want an array of text characters, you could use
825 either a string or a vector.  In practice, we always choose strings for
826 such applications, for four reasons:
828 @itemize @bullet
829 @item
830 They occupy one-fourth the space of a vector of the same elements.
832 @item
833 Strings are printed in a way that shows the contents more clearly
834 as text.
836 @item
837 Strings can hold text properties.  @xref{Text Properties}.
839 @item
840 Many of the specialized editing and I/O facilities of Emacs accept only
841 strings.  For example, you cannot insert a vector of characters into a
842 buffer the way you can insert a string.  @xref{Strings and Characters}.
843 @end itemize
845   By contrast, for an array of keyboard input characters (such as a key
846 sequence), a vector may be necessary, because many keyboard input
847 characters are outside the range that will fit in a string.  @xref{Key
848 Sequence Input}.
850 @node Array Functions
851 @section Functions that Operate on Arrays
853   In this section, we describe the functions that accept all types of
854 arrays.
856 @defun arrayp object
857 This function returns @code{t} if @var{object} is an array (i.e., a
858 vector, a string, a bool-vector or a char-table).
860 @example
861 @group
862 (arrayp [a])
863      @result{} t
864 (arrayp "asdf")
865      @result{} t
866 (arrayp (syntax-table))    ;; @r{A char-table.}
867      @result{} t
868 @end group
869 @end example
870 @end defun
872 @defun aref array index
873 @cindex array elements
874 This function returns the @var{index}th element of @var{array}.  The
875 first element is at index zero.
877 @example
878 @group
879 (setq primes [2 3 5 7 11 13])
880      @result{} [2 3 5 7 11 13]
881 (aref primes 4)
882      @result{} 11
883 @end group
884 @group
885 (aref "abcdefg" 1)
886      @result{} 98           ; @r{@samp{b} is @acronym{ASCII} code 98.}
887 @end group
888 @end example
890 See also the function @code{elt}, in @ref{Sequence Functions}.
891 @end defun
893 @defun aset array index object
894 This function sets the @var{index}th element of @var{array} to be
895 @var{object}.  It returns @var{object}.
897 @example
898 @group
899 (setq w [foo bar baz])
900      @result{} [foo bar baz]
901 (aset w 0 'fu)
902      @result{} fu
904      @result{} [fu bar baz]
905 @end group
907 @group
908 (setq x "asdfasfd")
909      @result{} "asdfasfd"
910 (aset x 3 ?Z)
911      @result{} 90
913      @result{} "asdZasfd"
914 @end group
915 @end example
917 If @var{array} is a string and @var{object} is not a character, a
918 @code{wrong-type-argument} error results.  The function converts a
919 unibyte string to multibyte if necessary to insert a character.
920 @end defun
922 @defun fillarray array object
923 This function fills the array @var{array} with @var{object}, so that
924 each element of @var{array} is @var{object}.  It returns @var{array}.
926 @example
927 @group
928 (setq a [a b c d e f g])
929      @result{} [a b c d e f g]
930 (fillarray a 0)
931      @result{} [0 0 0 0 0 0 0]
933      @result{} [0 0 0 0 0 0 0]
934 @end group
935 @group
936 (setq s "When in the course")
937      @result{} "When in the course"
938 (fillarray s ?-)
939      @result{} "------------------"
940 @end group
941 @end example
943 If @var{array} is a string and @var{object} is not a character, a
944 @code{wrong-type-argument} error results.
945 @end defun
947 The general sequence functions @code{copy-sequence} and @code{length}
948 are often useful for objects known to be arrays.  @xref{Sequence Functions}.
950 @node Vectors
951 @section Vectors
952 @cindex vector (type)
954   A @dfn{vector} is a general-purpose array whose elements can be any
955 Lisp objects.  (By contrast, the elements of a string can only be
956 characters.  @xref{Strings and Characters}.)  Vectors are used in
957 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
958 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
959 representation of a byte-compiled function (@pxref{Byte Compilation}),
960 and more.
962   Like other arrays, vectors use zero-origin indexing: the first
963 element has index 0.
965   Vectors are printed with square brackets surrounding the elements.
966 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
967 @code{a} is printed as @code{[a b a]}.  You can write vectors in the
968 same way in Lisp input.
970   A vector, like a string or a number, is considered a constant for
971 evaluation: the result of evaluating it is the same vector.  This does
972 not evaluate or even examine the elements of the vector.
973 @xref{Self-Evaluating Forms}.
975   Here are examples illustrating these principles:
977 @example
978 @group
979 (setq avector [1 two '(three) "four" [five]])
980      @result{} [1 two (quote (three)) "four" [five]]
981 (eval avector)
982      @result{} [1 two (quote (three)) "four" [five]]
983 (eq avector (eval avector))
984      @result{} t
985 @end group
986 @end example
988 @node Vector Functions
989 @section Functions for Vectors
991   Here are some functions that relate to vectors:
993 @defun vectorp object
994 This function returns @code{t} if @var{object} is a vector.
996 @example
997 @group
998 (vectorp [a])
999      @result{} t
1000 (vectorp "asdf")
1001      @result{} nil
1002 @end group
1003 @end example
1004 @end defun
1006 @defun vector &rest objects
1007 This function creates and returns a vector whose elements are the
1008 arguments, @var{objects}.
1010 @example
1011 @group
1012 (vector 'foo 23 [bar baz] "rats")
1013      @result{} [foo 23 [bar baz] "rats"]
1014 (vector)
1015      @result{} []
1016 @end group
1017 @end example
1018 @end defun
1020 @defun make-vector length object
1021 This function returns a new vector consisting of @var{length} elements,
1022 each initialized to @var{object}.
1024 @example
1025 @group
1026 (setq sleepy (make-vector 9 'Z))
1027      @result{} [Z Z Z Z Z Z Z Z Z]
1028 @end group
1029 @end example
1030 @end defun
1032 @defun vconcat &rest sequences
1033 @cindex copying vectors
1034 This function returns a new vector containing all the elements of
1035 @var{sequences}.  The arguments @var{sequences} may be true lists,
1036 vectors, strings or bool-vectors.  If no @var{sequences} are given,
1037 the empty vector is returned.
1039 The value is either the empty vector, or is a newly constructed
1040 nonempty vector that is not @code{eq} to any existing vector.
1042 @example
1043 @group
1044 (setq a (vconcat '(A B C) '(D E F)))
1045      @result{} [A B C D E F]
1046 (eq a (vconcat a))
1047      @result{} nil
1048 @end group
1049 @group
1050 (vconcat)
1051      @result{} []
1052 (vconcat [A B C] "aa" '(foo (6 7)))
1053      @result{} [A B C 97 97 foo (6 7)]
1054 @end group
1055 @end example
1057 The @code{vconcat} function also allows byte-code function objects as
1058 arguments.  This is a special feature to make it easy to access the entire
1059 contents of a byte-code function object.  @xref{Byte-Code Objects}.
1061 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
1062 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
1063 in @ref{Building Lists}.
1064 @end defun
1066   The @code{append} function also provides a way to convert a vector into a
1067 list with the same elements:
1069 @example
1070 @group
1071 (setq avector [1 two (quote (three)) "four" [five]])
1072      @result{} [1 two (quote (three)) "four" [five]]
1073 (append avector nil)
1074      @result{} (1 two (quote (three)) "four" [five])
1075 @end group
1076 @end example
1078 @node Char-Tables
1079 @section Char-Tables
1080 @cindex char-tables
1081 @cindex extra slots of char-table
1083   A char-table is much like a vector, except that it is indexed by
1084 character codes.  Any valid character code, without modifiers, can be
1085 used as an index in a char-table.  You can access a char-table's
1086 elements with @code{aref} and @code{aset}, as with any array.  In
1087 addition, a char-table can have @dfn{extra slots} to hold additional
1088 data not associated with particular character codes.  Like vectors,
1089 char-tables are constants when evaluated, and can hold elements of any
1090 type.
1092 @cindex subtype of char-table
1093   Each char-table has a @dfn{subtype}, a symbol, which serves two
1094 purposes:
1096 @itemize @bullet
1097 @item
1098 The subtype provides an easy way to tell what the char-table is for.
1099 For instance, display tables are char-tables with @code{display-table}
1100 as the subtype, and syntax tables are char-tables with
1101 @code{syntax-table} as the subtype.  The subtype can be queried using
1102 the function @code{char-table-subtype}, described below.
1104 @item
1105 The subtype controls the number of @dfn{extra slots} in the
1106 char-table.  This number is specified by the subtype's
1107 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1108 Properties}), whose value should be an integer between 0 and 10.  If
1109 the subtype has no such symbol property, the char-table has no extra
1110 slots.
1111 @end itemize
1113 @cindex parent of char-table
1114   A char-table can have a @dfn{parent}, which is another char-table.  If
1115 it does, then whenever the char-table specifies @code{nil} for a
1116 particular character @var{c}, it inherits the value specified in the
1117 parent.  In other words, @code{(aref @var{char-table} @var{c})} returns
1118 the value from the parent of @var{char-table} if @var{char-table} itself
1119 specifies @code{nil}.
1121 @cindex default value of char-table
1122   A char-table can also have a @dfn{default value}.  If so, then
1123 @code{(aref @var{char-table} @var{c})} returns the default value
1124 whenever the char-table does not specify any other non-@code{nil} value.
1126 @defun make-char-table subtype &optional init
1127 Return a newly-created char-table, with subtype @var{subtype} (a
1128 symbol).  Each element is initialized to @var{init}, which defaults to
1129 @code{nil}.  You cannot alter the subtype of a char-table after the
1130 char-table is created.
1132 There is no argument to specify the length of the char-table, because
1133 all char-tables have room for any valid character code as an index.
1135 If @var{subtype} has the @code{char-table-extra-slots} symbol
1136 property, that specifies the number of extra slots in the char-table.
1137 This should be an integer between 0 and 10; otherwise,
1138 @code{make-char-table} raises an error.  If @var{subtype} has no
1139 @code{char-table-extra-slots} symbol property (@pxref{Property
1140 Lists}), the char-table has no extra slots.
1141 @end defun
1143 @defun char-table-p object
1144 This function returns @code{t} if @var{object} is a char-table, and
1145 @code{nil} otherwise.
1146 @end defun
1148 @defun char-table-subtype char-table
1149 This function returns the subtype symbol of @var{char-table}.
1150 @end defun
1152 There is no special function to access default values in a char-table.
1153 To do that, use @code{char-table-range} (see below).
1155 @defun char-table-parent char-table
1156 This function returns the parent of @var{char-table}.  The parent is
1157 always either @code{nil} or another char-table.
1158 @end defun
1160 @defun set-char-table-parent char-table new-parent
1161 This function sets the parent of @var{char-table} to @var{new-parent}.
1162 @end defun
1164 @defun char-table-extra-slot char-table n
1165 This function returns the contents of extra slot @var{n} of
1166 @var{char-table}.  The number of extra slots in a char-table is
1167 determined by its subtype.
1168 @end defun
1170 @defun set-char-table-extra-slot char-table n value
1171 This function stores @var{value} in extra slot @var{n} of
1172 @var{char-table}.
1173 @end defun
1175   A char-table can specify an element value for a single character code;
1176 it can also specify a value for an entire character set.
1178 @defun char-table-range char-table range
1179 This returns the value specified in @var{char-table} for a range of
1180 characters @var{range}.  Here are the possibilities for @var{range}:
1182 @table @asis
1183 @item @code{nil}
1184 Refers to the default value.
1186 @item @var{char}
1187 Refers to the element for character @var{char}
1188 (supposing @var{char} is a valid character code).
1190 @item @code{(@var{from} . @var{to})}
1191 A cons cell refers to all the characters in the inclusive range
1192 @samp{[@var{from}..@var{to}]}.
1193 @end table
1194 @end defun
1196 @defun set-char-table-range char-table range value
1197 This function sets the value in @var{char-table} for a range of
1198 characters @var{range}.  Here are the possibilities for @var{range}:
1200 @table @asis
1201 @item @code{nil}
1202 Refers to the default value.
1204 @item @code{t}
1205 Refers to the whole range of character codes.
1207 @item @var{char}
1208 Refers to the element for character @var{char}
1209 (supposing @var{char} is a valid character code).
1211 @item @code{(@var{from} . @var{to})}
1212 A cons cell refers to all the characters in the inclusive range
1213 @samp{[@var{from}..@var{to}]}.
1214 @end table
1215 @end defun
1217 @defun map-char-table function char-table
1218 This function calls its argument @var{function} for each element of
1219 @var{char-table} that has a non-@code{nil} value.  The call to
1220 @var{function} is with two arguments, a key and a value.  The key
1221 is a possible @var{range} argument for @code{char-table-range}---either
1222 a valid character or a cons cell @code{(@var{from} . @var{to})},
1223 specifying a range of characters that share the same value.  The value is
1224 what @code{(char-table-range @var{char-table} @var{key})} returns.
1226 Overall, the key-value pairs passed to @var{function} describe all the
1227 values stored in @var{char-table}.
1229 The return value is always @code{nil}; to make calls to
1230 @code{map-char-table} useful, @var{function} should have side effects.
1231 For example, here is how to examine the elements of the syntax table:
1233 @example
1234 (let (accumulator)
1235    (map-char-table
1236     #'(lambda (key value)
1237         (setq accumulator
1238               (cons (list
1239                      (if (consp key)
1240                          (list (car key) (cdr key))
1241                        key)
1242                      value)
1243                     accumulator)))
1244     (syntax-table))
1245    accumulator)
1246 @result{}
1247 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1248  ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1249  ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1250 @end example
1251 @end defun
1253 @node Bool-Vectors
1254 @section Bool-vectors
1255 @cindex Bool-vectors
1257   A bool-vector is much like a vector, except that it stores only the
1258 values @code{t} and @code{nil}.  If you try to store any non-@code{nil}
1259 value into an element of the bool-vector, the effect is to store
1260 @code{t} there.  As with all arrays, bool-vector indices start from 0,
1261 and the length cannot be changed once the bool-vector is created.
1262 Bool-vectors are constants when evaluated.
1264   Several functions work specifically with bool-vectors; aside
1265 from that, you manipulate them with same functions used for other kinds
1266 of arrays.
1268 @defun make-bool-vector length initial
1269 Return a new bool-vector of @var{length} elements,
1270 each one initialized to @var{initial}.
1271 @end defun
1273 @defun bool-vector &rest objects
1274 This function creates and returns a bool-vector whose elements are the
1275 arguments, @var{objects}.
1276 @end defun
1278 @defun bool-vector-p object
1279 This returns @code{t} if @var{object} is a bool-vector,
1280 and @code{nil} otherwise.
1281 @end defun
1283 There are also some bool-vector set operation functions, described below:
1285 @defun bool-vector-exclusive-or a b &optional c
1286 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1287 If optional argument @var{c} is given, the result of this operation is
1288 stored into @var{c}.  All arguments should be bool vectors of the same length.
1289 @end defun
1291 @defun bool-vector-union a b &optional c
1292 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}.  If
1293 optional argument @var{c} is given, the result of this operation is
1294 stored into @var{c}.  All arguments should be bool vectors of the same length.
1295 @end defun
1297 @defun bool-vector-intersection a b &optional c
1298 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}.  If
1299 optional argument @var{c} is given, the result of this operation is
1300 stored into @var{c}.  All arguments should be bool vectors of the same length.
1301 @end defun
1303 @defun bool-vector-set-difference a b &optional c
1304 Return @dfn{set difference} of bool vectors @var{a} and @var{b}.  If
1305 optional argument @var{c} is given, the result of this operation is
1306 stored into @var{c}.  All arguments should be bool vectors of the same length.
1307 @end defun
1309 @defun bool-vector-not a &optional b
1310 Return @dfn{set complement} of bool vector @var{a}.  If optional
1311 argument @var{b} is given, the result of this operation is stored into
1312 @var{b}.  All arguments should be bool vectors of the same length.
1313 @end defun
1315 @defun bool-vector-subsetp a b
1316 Return @code{t} if every @code{t} value in @var{a} is also t in
1317 @var{b}, @code{nil} otherwise.  All arguments should be bool vectors of the
1318 same length.
1319 @end defun
1321 @defun bool-vector-count-consecutive a b i
1322 Return the number of consecutive elements in @var{a} equal @var{b}
1323 starting at @var{i}.  @code{a} is a bool vector, @var{b} is @code{t}
1324 or @code{nil}, and @var{i} is an index into @code{a}.
1325 @end defun
1327 @defun bool-vector-count-population a
1328 Return the number of elements that are @code{t} in bool vector @var{a}.
1329 @end defun
1331   The printed form represents up to 8 boolean values as a single
1332 character:
1334 @example
1335 @group
1336 (bool-vector t nil t nil)
1337      @result{} #&4"^E"
1338 (bool-vector)
1339      @result{} #&0""
1340 @end group
1341 @end example
1343 You can use @code{vconcat} to print a bool-vector like other vectors:
1345 @example
1346 @group
1347 (vconcat (bool-vector nil t nil t))
1348      @result{} [nil t nil t]
1349 @end group
1350 @end example
1352   Here is another example of creating, examining, and updating a
1353 bool-vector:
1355 @example
1356 (setq bv (make-bool-vector 5 t))
1357      @result{} #&5"^_"
1358 (aref bv 1)
1359      @result{} t
1360 (aset bv 3 nil)
1361      @result{} nil
1363      @result{} #&5"^W"
1364 @end example
1366 @noindent
1367 These results make sense because the binary codes for control-_ and
1368 control-W are 11111 and 10111, respectively.
1370 @node Rings
1371 @section Managing a Fixed-Size Ring of Objects
1373 @cindex ring data structure
1374   A @dfn{ring} is a fixed-size data structure that supports insertion,
1375 deletion, rotation, and modulo-indexed reference and traversal.  An
1376 efficient ring data structure is implemented by the @code{ring}
1377 package.  It provides the functions listed in this section.
1379   Note that several ``rings'' in Emacs, like the kill ring and the
1380 mark ring, are actually implemented as simple lists, @emph{not} using
1381 the @code{ring} package; thus the following functions won't work on
1382 them.
1384 @defun make-ring size
1385 This returns a new ring capable of holding @var{size} objects.
1386 @var{size} should be an integer.
1387 @end defun
1389 @defun ring-p object
1390 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1391 @end defun
1393 @defun ring-size ring
1394 This returns the maximum capacity of the @var{ring}.
1395 @end defun
1397 @defun ring-length ring
1398 This returns the number of objects that @var{ring} currently contains.
1399 The value will never exceed that returned by @code{ring-size}.
1400 @end defun
1402 @defun ring-elements ring
1403 This returns a list of the objects in @var{ring}, in order, newest first.
1404 @end defun
1406 @defun ring-copy ring
1407 This returns a new ring which is a copy of @var{ring}.
1408 The new ring contains the same (@code{eq}) objects as @var{ring}.
1409 @end defun
1411 @defun ring-empty-p ring
1412 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1413 @end defun
1415   The newest element in the ring always has index 0.  Higher indices
1416 correspond to older elements.  Indices are computed modulo the ring
1417 length.  Index @minus{}1 corresponds to the oldest element, @minus{}2
1418 to the next-oldest, and so forth.
1420 @defun ring-ref ring index
1421 This returns the object in @var{ring} found at index @var{index}.
1422 @var{index} may be negative or greater than the ring length.  If
1423 @var{ring} is empty, @code{ring-ref} signals an error.
1424 @end defun
1426 @defun ring-insert ring object
1427 This inserts @var{object} into @var{ring}, making it the newest
1428 element, and returns @var{object}.
1430 If the ring is full, insertion removes the oldest element to
1431 make room for the new element.
1432 @end defun
1434 @defun ring-remove ring &optional index
1435 Remove an object from @var{ring}, and return that object.  The
1436 argument @var{index} specifies which item to remove; if it is
1437 @code{nil}, that means to remove the oldest item.  If @var{ring} is
1438 empty, @code{ring-remove} signals an error.
1439 @end defun
1441 @defun ring-insert-at-beginning ring object
1442 This inserts @var{object} into @var{ring}, treating it as the oldest
1443 element.  The return value is not significant.
1445 If the ring is full, this function removes the newest element to make
1446 room for the inserted element.
1447 @end defun
1449 @cindex fifo data structure
1450   If you are careful not to exceed the ring size, you can
1451 use the ring as a first-in-first-out queue.  For example:
1453 @lisp
1454 (let ((fifo (make-ring 5)))
1455   (mapc (lambda (obj) (ring-insert fifo obj))
1456         '(0 one "two"))
1457   (list (ring-remove fifo) t
1458         (ring-remove fifo) t
1459         (ring-remove fifo)))
1460      @result{} (0 t one t "two")
1461 @end lisp