2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2014 Free Software
5 @c See the file elisp.texi for copying conditions.
6 @node Sequences Arrays Vectors
7 @chapter Sequences, Arrays, and Vectors
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:
29 _____________________________________________
32 | ______ ________________________________ |
34 | | List | | Array | |
35 | | | | ________ ________ | |
36 | |______| | | | | | | |
37 | | | Vector | | String | | |
38 | | |________| |________| | |
39 | | ____________ _____________ | |
41 | | | Char-table | | Bool-vector | | |
42 | | |____________| |_____________| | |
43 | |________________________________| |
44 |_____________________________________________|
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.
59 @node Sequence Functions
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.
69 @defun length sequence
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
81 @xref{Definition of safe-length}, for the related function @code{safe-length}.
101 (length (make-bool-vector 5 nil))
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.
134 ;; @r{We use @code{string} to show clearly which character @code{elt} returns.}
135 (string (elt "1234" 2))
140 @error{} Args out of range: [1 2 3 4], 4
144 @error{} Args out of range: [1 2 3 4], -1
148 This function generalizes @code{aref} (@pxref{Array Functions}) and
149 @code{nth} (@pxref{Definition of nth}).
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
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
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.
183 (setq x (vector 'foo bar))
184 @result{} [foo (1 2)]
187 (setq y (copy-sequence x))
188 @result{} [foo (1 2)]
200 (eq (elt x 1) (elt y 1))
205 ;; @r{Replacing an element of one sequence.}
207 x @result{} [quux (1 2)]
208 y @result{} [foo (1 2)]
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)]
224 An @dfn{array} object has slots that hold a number of other Lisp
225 objects, called the elements of the array. Any element of an array
226 may be accessed in constant time. In contrast, the time to access an
227 element of a list is proportional to the position of that element in
230 Emacs defines four types of array, all one-dimensional:
231 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
232 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
233 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
234 can hold elements of any type, but strings can only hold characters,
235 and bool-vectors can only hold @code{t} and @code{nil}.
237 All four kinds of array share these characteristics:
241 The first element of an array has index zero, the second element has
242 index 1, and so on. This is called @dfn{zero-origin} indexing. For
243 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
246 The length of the array is fixed once you create it; you cannot
247 change the length of an existing array.
250 For purposes of evaluation, the array is a constant---i.e.,
251 it evaluates to itself.
254 The elements of an array may be referenced or changed with the functions
255 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
258 When you create an array, other than a char-table, you must specify
259 its length. You cannot specify the length of a char-table, because that
260 is determined by the range of character codes.
262 In principle, if you want an array of text characters, you could use
263 either a string or a vector. In practice, we always choose strings for
264 such applications, for four reasons:
268 They occupy one-fourth the space of a vector of the same elements.
271 Strings are printed in a way that shows the contents more clearly
275 Strings can hold text properties. @xref{Text Properties}.
278 Many of the specialized editing and I/O facilities of Emacs accept only
279 strings. For example, you cannot insert a vector of characters into a
280 buffer the way you can insert a string. @xref{Strings and Characters}.
283 By contrast, for an array of keyboard input characters (such as a key
284 sequence), a vector may be necessary, because many keyboard input
285 characters are outside the range that will fit in a string. @xref{Key
288 @node Array Functions
289 @section Functions that Operate on Arrays
291 In this section, we describe the functions that accept all types of
295 This function returns @code{t} if @var{object} is an array (i.e., a
296 vector, a string, a bool-vector or a char-table).
304 (arrayp (syntax-table)) ;; @r{A char-table.}
310 @defun aref array index
311 @cindex array elements
312 This function returns the @var{index}th element of @var{array}. The
313 first element is at index zero.
317 (setq primes [2 3 5 7 11 13])
318 @result{} [2 3 5 7 11 13]
324 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
328 See also the function @code{elt}, in @ref{Sequence Functions}.
331 @defun aset array index object
332 This function sets the @var{index}th element of @var{array} to be
333 @var{object}. It returns @var{object}.
337 (setq w [foo bar baz])
338 @result{} [foo bar baz]
342 @result{} [fu bar baz]
355 If @var{array} is a string and @var{object} is not a character, a
356 @code{wrong-type-argument} error results. The function converts a
357 unibyte string to multibyte if necessary to insert a character.
360 @defun fillarray array object
361 This function fills the array @var{array} with @var{object}, so that
362 each element of @var{array} is @var{object}. It returns @var{array}.
366 (setq a [a b c d e f g])
367 @result{} [a b c d e f g]
369 @result{} [0 0 0 0 0 0 0]
371 @result{} [0 0 0 0 0 0 0]
374 (setq s "When in the course")
375 @result{} "When in the course"
377 @result{} "------------------"
381 If @var{array} is a string and @var{object} is not a character, a
382 @code{wrong-type-argument} error results.
385 The general sequence functions @code{copy-sequence} and @code{length}
386 are often useful for objects known to be arrays. @xref{Sequence Functions}.
390 @cindex vector (type)
392 A @dfn{vector} is a general-purpose array whose elements can be any
393 Lisp objects. (By contrast, the elements of a string can only be
394 characters. @xref{Strings and Characters}.) Vectors are used in
395 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
396 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
397 representation of a byte-compiled function (@pxref{Byte Compilation}),
400 Like other arrays, vectors use zero-origin indexing: the first
403 Vectors are printed with square brackets surrounding the elements.
404 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
405 @code{a} is printed as @code{[a b a]}. You can write vectors in the
406 same way in Lisp input.
408 A vector, like a string or a number, is considered a constant for
409 evaluation: the result of evaluating it is the same vector. This does
410 not evaluate or even examine the elements of the vector.
411 @xref{Self-Evaluating Forms}.
413 Here are examples illustrating these principles:
417 (setq avector [1 two '(three) "four" [five]])
418 @result{} [1 two (quote (three)) "four" [five]]
420 @result{} [1 two (quote (three)) "four" [five]]
421 (eq avector (eval avector))
426 @node Vector Functions
427 @section Functions for Vectors
429 Here are some functions that relate to vectors:
431 @defun vectorp object
432 This function returns @code{t} if @var{object} is a vector.
444 @defun vector &rest objects
445 This function creates and returns a vector whose elements are the
446 arguments, @var{objects}.
450 (vector 'foo 23 [bar baz] "rats")
451 @result{} [foo 23 [bar baz] "rats"]
458 @defun make-vector length object
459 This function returns a new vector consisting of @var{length} elements,
460 each initialized to @var{object}.
464 (setq sleepy (make-vector 9 'Z))
465 @result{} [Z Z Z Z Z Z Z Z Z]
470 @defun vconcat &rest sequences
471 @cindex copying vectors
472 This function returns a new vector containing all the elements of
473 @var{sequences}. The arguments @var{sequences} may be true lists,
474 vectors, strings or bool-vectors. If no @var{sequences} are given,
475 the empty vector is returned.
477 The value is either the empty vector, or is a newly constructed
478 nonempty vector that is not @code{eq} to any existing vector.
482 (setq a (vconcat '(A B C) '(D E F)))
483 @result{} [A B C D E F]
490 (vconcat [A B C] "aa" '(foo (6 7)))
491 @result{} [A B C 97 97 foo (6 7)]
495 The @code{vconcat} function also allows byte-code function objects as
496 arguments. This is a special feature to make it easy to access the entire
497 contents of a byte-code function object. @xref{Byte-Code Objects}.
499 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
500 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
501 in @ref{Building Lists}.
504 The @code{append} function also provides a way to convert a vector into a
505 list with the same elements:
509 (setq avector [1 two (quote (three)) "four" [five]])
510 @result{} [1 two (quote (three)) "four" [five]]
512 @result{} (1 two (quote (three)) "four" [five])
519 @cindex extra slots of char-table
521 A char-table is much like a vector, except that it is indexed by
522 character codes. Any valid character code, without modifiers, can be
523 used as an index in a char-table. You can access a char-table's
524 elements with @code{aref} and @code{aset}, as with any array. In
525 addition, a char-table can have @dfn{extra slots} to hold additional
526 data not associated with particular character codes. Like vectors,
527 char-tables are constants when evaluated, and can hold elements of any
530 @cindex subtype of char-table
531 Each char-table has a @dfn{subtype}, a symbol, which serves two
536 The subtype provides an easy way to tell what the char-table is for.
537 For instance, display tables are char-tables with @code{display-table}
538 as the subtype, and syntax tables are char-tables with
539 @code{syntax-table} as the subtype. The subtype can be queried using
540 the function @code{char-table-subtype}, described below.
543 The subtype controls the number of @dfn{extra slots} in the
544 char-table. This number is specified by the subtype's
545 @code{char-table-extra-slots} symbol property (@pxref{Symbol
546 Properties}), whose value should be an integer between 0 and 10. If
547 the subtype has no such symbol property, the char-table has no extra
551 @cindex parent of char-table
552 A char-table can have a @dfn{parent}, which is another char-table. If
553 it does, then whenever the char-table specifies @code{nil} for a
554 particular character @var{c}, it inherits the value specified in the
555 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
556 the value from the parent of @var{char-table} if @var{char-table} itself
557 specifies @code{nil}.
559 @cindex default value of char-table
560 A char-table can also have a @dfn{default value}. If so, then
561 @code{(aref @var{char-table} @var{c})} returns the default value
562 whenever the char-table does not specify any other non-@code{nil} value.
564 @defun make-char-table subtype &optional init
565 Return a newly-created char-table, with subtype @var{subtype} (a
566 symbol). Each element is initialized to @var{init}, which defaults to
567 @code{nil}. You cannot alter the subtype of a char-table after the
568 char-table is created.
570 There is no argument to specify the length of the char-table, because
571 all char-tables have room for any valid character code as an index.
573 If @var{subtype} has the @code{char-table-extra-slots} symbol
574 property, that specifies the number of extra slots in the char-table.
575 This should be an integer between 0 and 10; otherwise,
576 @code{make-char-table} raises an error. If @var{subtype} has no
577 @code{char-table-extra-slots} symbol property (@pxref{Property
578 Lists}), the char-table has no extra slots.
581 @defun char-table-p object
582 This function returns @code{t} if @var{object} is a char-table, and
583 @code{nil} otherwise.
586 @defun char-table-subtype char-table
587 This function returns the subtype symbol of @var{char-table}.
590 There is no special function to access default values in a char-table.
591 To do that, use @code{char-table-range} (see below).
593 @defun char-table-parent char-table
594 This function returns the parent of @var{char-table}. The parent is
595 always either @code{nil} or another char-table.
598 @defun set-char-table-parent char-table new-parent
599 This function sets the parent of @var{char-table} to @var{new-parent}.
602 @defun char-table-extra-slot char-table n
603 This function returns the contents of extra slot @var{n} of
604 @var{char-table}. The number of extra slots in a char-table is
605 determined by its subtype.
608 @defun set-char-table-extra-slot char-table n value
609 This function stores @var{value} in extra slot @var{n} of
613 A char-table can specify an element value for a single character code;
614 it can also specify a value for an entire character set.
616 @defun char-table-range char-table range
617 This returns the value specified in @var{char-table} for a range of
618 characters @var{range}. Here are the possibilities for @var{range}:
622 Refers to the default value.
625 Refers to the element for character @var{char}
626 (supposing @var{char} is a valid character code).
628 @item @code{(@var{from} . @var{to})}
629 A cons cell refers to all the characters in the inclusive range
630 @samp{[@var{from}..@var{to}]}.
634 @defun set-char-table-range char-table range value
635 This function sets the value in @var{char-table} for a range of
636 characters @var{range}. Here are the possibilities for @var{range}:
640 Refers to the default value.
643 Refers to the whole range of character codes.
646 Refers to the element for character @var{char}
647 (supposing @var{char} is a valid character code).
649 @item @code{(@var{from} . @var{to})}
650 A cons cell refers to all the characters in the inclusive range
651 @samp{[@var{from}..@var{to}]}.
655 @defun map-char-table function char-table
656 This function calls its argument @var{function} for each element of
657 @var{char-table} that has a non-@code{nil} value. The call to
658 @var{function} is with two arguments, a key and a value. The key
659 is a possible @var{range} argument for @code{char-table-range}---either
660 a valid character or a cons cell @code{(@var{from} . @var{to})},
661 specifying a range of characters that share the same value. The value is
662 what @code{(char-table-range @var{char-table} @var{key})} returns.
664 Overall, the key-value pairs passed to @var{function} describe all the
665 values stored in @var{char-table}.
667 The return value is always @code{nil}; to make calls to
668 @code{map-char-table} useful, @var{function} should have side effects.
669 For example, here is how to examine the elements of the syntax table:
674 #'(lambda (key value)
678 (list (car key) (cdr key))
685 (((2597602 4194303) (2)) ((2597523 2597601) (3))
686 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
687 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
692 @section Bool-vectors
695 A bool-vector is much like a vector, except that it stores only the
696 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
697 value into an element of the bool-vector, the effect is to store
698 @code{t} there. As with all arrays, bool-vector indices start from 0,
699 and the length cannot be changed once the bool-vector is created.
700 Bool-vectors are constants when evaluated.
702 There are two special functions for working with bool-vectors; aside
703 from that, you manipulate them with same functions used for other kinds
706 @defun make-bool-vector length initial
707 Return a new bool-vector of @var{length} elements,
708 each one initialized to @var{initial}.
711 @defun bool-vector-p object
712 This returns @code{t} if @var{object} is a bool-vector,
713 and @code{nil} otherwise.
716 There are also some bool-vector set operation functions, described below:
718 @defun bool-vector-exclusive-or a b &optional c
719 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
720 If optional argument @var{c} is given, the result of this operation is
721 stored into @var{c}. All arguments should be bool vectors of the same length.
724 @defun bool-vector-union a b &optional c
725 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}. If
726 optional argument @var{c} is given, the result of this operation is
727 stored into @var{c}. All arguments should be bool vectors of the same length.
730 @defun bool-vector-intersection a b &optional c
731 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}. If
732 optional argument @var{c} is given, the result of this operation is
733 stored into @var{c}. All arguments should be bool vectors of the same length.
736 @defun bool-vector-set-difference a b &optional c
737 Return @dfn{set difference} of bool vectors @var{a} and @var{b}. If
738 optional argument @var{c} is given, the result of this operation is
739 stored into @var{c}. All arguments should be bool vectors of the same length.
742 @defun bool-vector-not a &optional b
743 Return @dfn{set complement} of bool vector @var{a}. If optional
744 argument @var{b} is given, the result of this operation is stored into
745 @var{b}. All arguments should be bool vectors of the same length.
748 @defun bool-vector-subsetp a b
749 Return @code{t} if every @code{t} value in @var{a} is also t in
750 @var{b}, nil otherwise. All arguments should be bool vectors of the
754 @defun bool-vector-count-consecutive a b i
755 Return the number of consecutive elements in @var{a} equal @var{b}
756 starting at @var{i}. @code{a} is a bool vector, @var{b} is @code{t}
757 or @code{nil}, and @var{i} is an index into @code{a}.
760 @defun bool-vector-count-population a
761 Return the number of elements that are @code{t} in bool vector @var{a}.
764 Here is an example of creating, examining, and updating a
765 bool-vector. Note that the printed form represents up to 8 boolean
766 values as a single character.
769 (setq bv (make-bool-vector 5 t))
780 These results make sense because the binary codes for control-_ and
781 control-W are 11111 and 10111, respectively.
784 @section Managing a Fixed-Size Ring of Objects
786 @cindex ring data structure
787 A @dfn{ring} is a fixed-size data structure that supports insertion,
788 deletion, rotation, and modulo-indexed reference and traversal. An
789 efficient ring data structure is implemented by the @code{ring}
790 package. It provides the functions listed in this section.
792 Note that several ``rings'' in Emacs, like the kill ring and the
793 mark ring, are actually implemented as simple lists, @emph{not} using
794 the @code{ring} package; thus the following functions won't work on
797 @defun make-ring size
798 This returns a new ring capable of holding @var{size} objects.
799 @var{size} should be an integer.
803 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
806 @defun ring-size ring
807 This returns the maximum capacity of the @var{ring}.
810 @defun ring-length ring
811 This returns the number of objects that @var{ring} currently contains.
812 The value will never exceed that returned by @code{ring-size}.
815 @defun ring-elements ring
816 This returns a list of the objects in @var{ring}, in order, newest first.
819 @defun ring-copy ring
820 This returns a new ring which is a copy of @var{ring}.
821 The new ring contains the same (@code{eq}) objects as @var{ring}.
824 @defun ring-empty-p ring
825 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
828 The newest element in the ring always has index 0. Higher indices
829 correspond to older elements. Indices are computed modulo the ring
830 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
831 to the next-oldest, and so forth.
833 @defun ring-ref ring index
834 This returns the object in @var{ring} found at index @var{index}.
835 @var{index} may be negative or greater than the ring length. If
836 @var{ring} is empty, @code{ring-ref} signals an error.
839 @defun ring-insert ring object
840 This inserts @var{object} into @var{ring}, making it the newest
841 element, and returns @var{object}.
843 If the ring is full, insertion removes the oldest element to
844 make room for the new element.
847 @defun ring-remove ring &optional index
848 Remove an object from @var{ring}, and return that object. The
849 argument @var{index} specifies which item to remove; if it is
850 @code{nil}, that means to remove the oldest item. If @var{ring} is
851 empty, @code{ring-remove} signals an error.
854 @defun ring-insert-at-beginning ring object
855 This inserts @var{object} into @var{ring}, treating it as the oldest
856 element. The return value is not significant.
858 If the ring is full, this function removes the newest element to make
859 room for the inserted element.
862 @cindex fifo data structure
863 If you are careful not to exceed the ring size, you can
864 use the ring as a first-in-first-out queue. For example:
867 (let ((fifo (make-ring 5)))
868 (mapc (lambda (obj) (ring-insert fifo obj))
870 (list (ring-remove fifo) t
873 @result{} (0 t one t "two")