2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-2015 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)]
220 @defun reverse sequence
221 @cindex string reverse
223 @cindex vector reverse
224 @cindex sequence reverse
225 This function creates a new sequence whose elements are the elements
226 of @var{sequence}, but in reverse order. The original argument @var{sequence}
227 is @emph{not} altered. Note that char-tables cannot be reversed.
263 @defun nreverse sequence
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{sequence}.
268 Unlike @code{reverse} the original @var{sequence} may be modified.
284 ;; @r{The cons cell that was first is now last.}
290 To avoid confusion, we usually store the result of @code{nreverse}
291 back in the same variable which held the original list:
294 (setq x (nreverse x))
297 Here is the @code{nreverse} of our favorite example, @code{(a b c)},
298 presented graphically:
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 ------------- | --------- | - | -------- | -
309 ------------- ------------
313 For the vector, it is even simpler because you don't need setq:
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.
330 @defun sort sequence predicate
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:
370 (setq nums '(1 3 2 6 5 4 0))
371 @result{} (1 3 2 6 5 4 0)
375 @result{} (0 1 2 3 4 5 6)
379 @result{} (1 2 3 4 5 6)
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:
392 (setq nums (sort nums '<))
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:
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")]
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")]
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}.
422 @cindex sequence functions in seq
424 The @file{seq.el} library provides the following additional sequence
425 manipulation macros and functions, prefixed with @code{seq-}. To use
426 them, you must first load the @file{seq} library.
428 All functions defined in this library are free of side-effects;
429 i.e., they do not modify any sequence (list, vector, or string) that
430 you pass as an argument. Unless otherwise stated, the result is a
431 sequence of the same type as the input. For those functions that take
432 a predicate, this should be a function of one argument.
434 @defun seq-drop sequence n
435 This function returns all but the first @var{n} (an integer)
436 elements of @var{sequence}. If @var{n} is negative or zero,
437 the result is @var{sequence}.
441 (seq-drop [1 2 3 4 5 6] 3)
445 (seq-drop "hello world" -4)
446 @result{} "hello world"
451 @defun seq-take sequence n
452 This function returns the first @var{n} (an integer) elements of
453 @var{sequence}. If @var{n} is negative or zero, the result
458 (seq-take '(1 2 3 4) 3)
462 (seq-take [1 2 3 4] 0)
468 @defun seq-take-while predicate sequence
469 This function returns the members of @var{sequence} in order,
470 stopping before the first one for which @var{predicate} returns @code{nil}.
474 (seq-take-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
478 (seq-take-while (lambda (elt) (> elt 0)) [-1 4 6])
484 @defun seq-drop-while predicate sequence
485 This function returns the members of @var{sequence} in order,
486 starting from the first one for which @var{predicate} returns @code{nil}.
490 (seq-drop-while (lambda (elt) (> elt 0)) '(1 2 3 -1 -2))
494 (seq-drop-while (lambda (elt) (< elt 0)) [1 4 6])
500 @defun seq-filter predicate sequence
501 @cindex filtering sequences
502 This function returns a list of all the elements in @var{sequence}
503 for which @var{predicate} returns non-@code{nil}.
507 (seq-filter (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
511 (seq-filter (lambda (elt) (> elt 0)) '(-1 -3 -5))
517 @defun seq-remove predicate sequence
518 @cindex removing from sequences
519 This function returns a list of all the elements in @var{sequence}
520 for which @var{predicate} returns @code{nil}.
524 (seq-remove (lambda (elt) (> elt 0)) [1 -1 3 -3 5])
528 (seq-remove (lambda (elt) (< elt 0)) '(-1 -3 -5))
534 @defun seq-reduce function sequence initial-value
535 @cindex reducing sequences
536 This function returns the result of calling @var{function} with
537 @var{initial-value} and the first element of @var{sequence}, then calling
538 @var{function} with that result and the second element of @var{sequence},
539 then with that result and the third element of @var{sequence}, etc.
540 @var{function} should be a function of two arguments. If
541 @var{sequence} is empty, this returns @var{initial-value} without
542 calling @var{function}.
546 (seq-reduce #'+ [1 2 3 4] 0)
550 (seq-reduce #'+ '(1 2 3 4) 5)
554 (seq-reduce #'+ '() 3)
560 @defun seq-some-p predicate sequence
561 This function returns the first member of sequence for which @var{predicate}
562 returns non-@code{nil}.
566 (seq-some-p #'numberp ["abc" 1 nil])
570 (seq-some-p #'numberp ["abc" "def"])
576 @defun seq-every-p predicate sequence
577 This function returns non-@code{nil} if applying @var{predicate}
578 to every element of @var{sequence} returns non-@code{nil}.
582 (seq-every-p #'numberp [2 4 6])
586 (seq-some-p #'numberp [2 4 "6"])
592 @defun seq-empty-p sequence
593 This function returns non-@code{nil} if @var{sequence} is empty.
597 (seq-empty-p "not empty")
607 @defun seq-count predicate sequence
608 This function returns the number of elements in @var{sequence} for which
609 @var{predicate} returns non-@code{nil}.
612 (seq-count (lambda (elt) (> elt 0)) [-1 2 0 3 -2])
617 @cindex sorting sequences
618 @defun seq-sort function sequence
619 This function returns a copy of @var{sequence} that is sorted
620 according to @var{function}, a function of two arguments that returns
621 non-@code{nil} if the first argument should sort before the second.
624 @defun seq-contains-p sequence elt &optional function
625 This function returns the first element in @var{sequence} that is equal to
626 @var{elt}. If the optional argument @var{function} is non-@code{nil},
627 it is a function of two arguments to use instead of the default @code{equal}.
631 (seq-contains-p '(symbol1 symbol2) 'symbol1)
635 (seq-contains-p '(symbol1 symbol2) 'symbol3)
642 @defun seq-uniq sequence &optional function
643 This function returns a list of the elements of @var{sequence} with
644 duplicates removed. If the optional argument @var{function} is non-@code{nil},
645 it is a function of two arguments to use instead of the default @code{equal}.
649 (seq-uniq '(1 2 2 1 3))
653 (seq-uniq '(1 2 2.0 1.0) #'=)
659 @defun seq-subseq sequence start &optional end
660 This function returns a subset of @var{sequence} from @var{start}
661 to @var{end}, both integers (@var{end} defaults to the last element).
662 If @var{start} or @var{end} is negative, it counts from the end of
667 (seq-subseq '(1 2 3 4 5) 1)
671 (seq-subseq '[1 2 3 4 5] 1 3)
675 (seq-subseq '[1 2 3 4 5] -3 -1)
681 @defun seq-concatenate type &rest sequences
682 This function returns a sequence of type @var{type} made of the
683 concatenation of @var{sequences}. @var{type} may be: @code{vector},
684 @code{list} or @code{string}.
688 (seq-concatenate 'list '(1 2) '(3 4) [5 6])
689 @result{} (1 2 3 5 6)
692 (seq-concatenate 'string "Hello " "world")
693 @result{} "Hello world"
698 @defmac seq-doseq (var sequence [result]) body@dots{}
699 @cindex sequence iteration
700 This macro is like @code{dolist}, except that @var{sequence} can be a list,
701 vector or string (@pxref{Iteration} for more information about the
702 @code{dolist} macro). This is primarily useful for side-effects.
709 An @dfn{array} object has slots that hold a number of other Lisp
710 objects, called the elements of the array. Any element of an array
711 may be accessed in constant time. In contrast, the time to access an
712 element of a list is proportional to the position of that element in
715 Emacs defines four types of array, all one-dimensional:
716 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
717 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
718 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
719 can hold elements of any type, but strings can only hold characters,
720 and bool-vectors can only hold @code{t} and @code{nil}.
722 All four kinds of array share these characteristics:
726 The first element of an array has index zero, the second element has
727 index 1, and so on. This is called @dfn{zero-origin} indexing. For
728 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
731 The length of the array is fixed once you create it; you cannot
732 change the length of an existing array.
735 For purposes of evaluation, the array is a constant---i.e.,
736 it evaluates to itself.
739 The elements of an array may be referenced or changed with the functions
740 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
743 When you create an array, other than a char-table, you must specify
744 its length. You cannot specify the length of a char-table, because that
745 is determined by the range of character codes.
747 In principle, if you want an array of text characters, you could use
748 either a string or a vector. In practice, we always choose strings for
749 such applications, for four reasons:
753 They occupy one-fourth the space of a vector of the same elements.
756 Strings are printed in a way that shows the contents more clearly
760 Strings can hold text properties. @xref{Text Properties}.
763 Many of the specialized editing and I/O facilities of Emacs accept only
764 strings. For example, you cannot insert a vector of characters into a
765 buffer the way you can insert a string. @xref{Strings and Characters}.
768 By contrast, for an array of keyboard input characters (such as a key
769 sequence), a vector may be necessary, because many keyboard input
770 characters are outside the range that will fit in a string. @xref{Key
773 @node Array Functions
774 @section Functions that Operate on Arrays
776 In this section, we describe the functions that accept all types of
780 This function returns @code{t} if @var{object} is an array (i.e., a
781 vector, a string, a bool-vector or a char-table).
789 (arrayp (syntax-table)) ;; @r{A char-table.}
795 @defun aref array index
796 @cindex array elements
797 This function returns the @var{index}th element of @var{array}. The
798 first element is at index zero.
802 (setq primes [2 3 5 7 11 13])
803 @result{} [2 3 5 7 11 13]
809 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
813 See also the function @code{elt}, in @ref{Sequence Functions}.
816 @defun aset array index object
817 This function sets the @var{index}th element of @var{array} to be
818 @var{object}. It returns @var{object}.
822 (setq w [foo bar baz])
823 @result{} [foo bar baz]
827 @result{} [fu bar baz]
840 If @var{array} is a string and @var{object} is not a character, a
841 @code{wrong-type-argument} error results. The function converts a
842 unibyte string to multibyte if necessary to insert a character.
845 @defun fillarray array object
846 This function fills the array @var{array} with @var{object}, so that
847 each element of @var{array} is @var{object}. It returns @var{array}.
851 (setq a [a b c d e f g])
852 @result{} [a b c d e f g]
854 @result{} [0 0 0 0 0 0 0]
856 @result{} [0 0 0 0 0 0 0]
859 (setq s "When in the course")
860 @result{} "When in the course"
862 @result{} "------------------"
866 If @var{array} is a string and @var{object} is not a character, a
867 @code{wrong-type-argument} error results.
870 The general sequence functions @code{copy-sequence} and @code{length}
871 are often useful for objects known to be arrays. @xref{Sequence Functions}.
875 @cindex vector (type)
877 A @dfn{vector} is a general-purpose array whose elements can be any
878 Lisp objects. (By contrast, the elements of a string can only be
879 characters. @xref{Strings and Characters}.) Vectors are used in
880 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
881 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
882 representation of a byte-compiled function (@pxref{Byte Compilation}),
885 Like other arrays, vectors use zero-origin indexing: the first
888 Vectors are printed with square brackets surrounding the elements.
889 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
890 @code{a} is printed as @code{[a b a]}. You can write vectors in the
891 same way in Lisp input.
893 A vector, like a string or a number, is considered a constant for
894 evaluation: the result of evaluating it is the same vector. This does
895 not evaluate or even examine the elements of the vector.
896 @xref{Self-Evaluating Forms}.
898 Here are examples illustrating these principles:
902 (setq avector [1 two '(three) "four" [five]])
903 @result{} [1 two (quote (three)) "four" [five]]
905 @result{} [1 two (quote (three)) "four" [five]]
906 (eq avector (eval avector))
911 @node Vector Functions
912 @section Functions for Vectors
914 Here are some functions that relate to vectors:
916 @defun vectorp object
917 This function returns @code{t} if @var{object} is a vector.
929 @defun vector &rest objects
930 This function creates and returns a vector whose elements are the
931 arguments, @var{objects}.
935 (vector 'foo 23 [bar baz] "rats")
936 @result{} [foo 23 [bar baz] "rats"]
943 @defun make-vector length object
944 This function returns a new vector consisting of @var{length} elements,
945 each initialized to @var{object}.
949 (setq sleepy (make-vector 9 'Z))
950 @result{} [Z Z Z Z Z Z Z Z Z]
955 @defun vconcat &rest sequences
956 @cindex copying vectors
957 This function returns a new vector containing all the elements of
958 @var{sequences}. The arguments @var{sequences} may be true lists,
959 vectors, strings or bool-vectors. If no @var{sequences} are given,
960 the empty vector is returned.
962 The value is either the empty vector, or is a newly constructed
963 nonempty vector that is not @code{eq} to any existing vector.
967 (setq a (vconcat '(A B C) '(D E F)))
968 @result{} [A B C D E F]
975 (vconcat [A B C] "aa" '(foo (6 7)))
976 @result{} [A B C 97 97 foo (6 7)]
980 The @code{vconcat} function also allows byte-code function objects as
981 arguments. This is a special feature to make it easy to access the entire
982 contents of a byte-code function object. @xref{Byte-Code Objects}.
984 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
985 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
986 in @ref{Building Lists}.
989 The @code{append} function also provides a way to convert a vector into a
990 list with the same elements:
994 (setq avector [1 two (quote (three)) "four" [five]])
995 @result{} [1 two (quote (three)) "four" [five]]
997 @result{} (1 two (quote (three)) "four" [five])
1002 @section Char-Tables
1004 @cindex extra slots of char-table
1006 A char-table is much like a vector, except that it is indexed by
1007 character codes. Any valid character code, without modifiers, can be
1008 used as an index in a char-table. You can access a char-table's
1009 elements with @code{aref} and @code{aset}, as with any array. In
1010 addition, a char-table can have @dfn{extra slots} to hold additional
1011 data not associated with particular character codes. Like vectors,
1012 char-tables are constants when evaluated, and can hold elements of any
1015 @cindex subtype of char-table
1016 Each char-table has a @dfn{subtype}, a symbol, which serves two
1021 The subtype provides an easy way to tell what the char-table is for.
1022 For instance, display tables are char-tables with @code{display-table}
1023 as the subtype, and syntax tables are char-tables with
1024 @code{syntax-table} as the subtype. The subtype can be queried using
1025 the function @code{char-table-subtype}, described below.
1028 The subtype controls the number of @dfn{extra slots} in the
1029 char-table. This number is specified by the subtype's
1030 @code{char-table-extra-slots} symbol property (@pxref{Symbol
1031 Properties}), whose value should be an integer between 0 and 10. If
1032 the subtype has no such symbol property, the char-table has no extra
1036 @cindex parent of char-table
1037 A char-table can have a @dfn{parent}, which is another char-table. If
1038 it does, then whenever the char-table specifies @code{nil} for a
1039 particular character @var{c}, it inherits the value specified in the
1040 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
1041 the value from the parent of @var{char-table} if @var{char-table} itself
1042 specifies @code{nil}.
1044 @cindex default value of char-table
1045 A char-table can also have a @dfn{default value}. If so, then
1046 @code{(aref @var{char-table} @var{c})} returns the default value
1047 whenever the char-table does not specify any other non-@code{nil} value.
1049 @defun make-char-table subtype &optional init
1050 Return a newly-created char-table, with subtype @var{subtype} (a
1051 symbol). Each element is initialized to @var{init}, which defaults to
1052 @code{nil}. You cannot alter the subtype of a char-table after the
1053 char-table is created.
1055 There is no argument to specify the length of the char-table, because
1056 all char-tables have room for any valid character code as an index.
1058 If @var{subtype} has the @code{char-table-extra-slots} symbol
1059 property, that specifies the number of extra slots in the char-table.
1060 This should be an integer between 0 and 10; otherwise,
1061 @code{make-char-table} raises an error. If @var{subtype} has no
1062 @code{char-table-extra-slots} symbol property (@pxref{Property
1063 Lists}), the char-table has no extra slots.
1066 @defun char-table-p object
1067 This function returns @code{t} if @var{object} is a char-table, and
1068 @code{nil} otherwise.
1071 @defun char-table-subtype char-table
1072 This function returns the subtype symbol of @var{char-table}.
1075 There is no special function to access default values in a char-table.
1076 To do that, use @code{char-table-range} (see below).
1078 @defun char-table-parent char-table
1079 This function returns the parent of @var{char-table}. The parent is
1080 always either @code{nil} or another char-table.
1083 @defun set-char-table-parent char-table new-parent
1084 This function sets the parent of @var{char-table} to @var{new-parent}.
1087 @defun char-table-extra-slot char-table n
1088 This function returns the contents of extra slot @var{n} of
1089 @var{char-table}. The number of extra slots in a char-table is
1090 determined by its subtype.
1093 @defun set-char-table-extra-slot char-table n value
1094 This function stores @var{value} in extra slot @var{n} of
1098 A char-table can specify an element value for a single character code;
1099 it can also specify a value for an entire character set.
1101 @defun char-table-range char-table range
1102 This returns the value specified in @var{char-table} for a range of
1103 characters @var{range}. Here are the possibilities for @var{range}:
1107 Refers to the default value.
1110 Refers to the element for character @var{char}
1111 (supposing @var{char} is a valid character code).
1113 @item @code{(@var{from} . @var{to})}
1114 A cons cell refers to all the characters in the inclusive range
1115 @samp{[@var{from}..@var{to}]}.
1119 @defun set-char-table-range char-table range value
1120 This function sets the value in @var{char-table} for a range of
1121 characters @var{range}. Here are the possibilities for @var{range}:
1125 Refers to the default value.
1128 Refers to the whole range of character codes.
1131 Refers to the element for character @var{char}
1132 (supposing @var{char} is a valid character code).
1134 @item @code{(@var{from} . @var{to})}
1135 A cons cell refers to all the characters in the inclusive range
1136 @samp{[@var{from}..@var{to}]}.
1140 @defun map-char-table function char-table
1141 This function calls its argument @var{function} for each element of
1142 @var{char-table} that has a non-@code{nil} value. The call to
1143 @var{function} is with two arguments, a key and a value. The key
1144 is a possible @var{range} argument for @code{char-table-range}---either
1145 a valid character or a cons cell @code{(@var{from} . @var{to})},
1146 specifying a range of characters that share the same value. The value is
1147 what @code{(char-table-range @var{char-table} @var{key})} returns.
1149 Overall, the key-value pairs passed to @var{function} describe all the
1150 values stored in @var{char-table}.
1152 The return value is always @code{nil}; to make calls to
1153 @code{map-char-table} useful, @var{function} should have side effects.
1154 For example, here is how to examine the elements of the syntax table:
1159 #'(lambda (key value)
1163 (list (car key) (cdr key))
1170 (((2597602 4194303) (2)) ((2597523 2597601) (3))
1171 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
1172 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
1177 @section Bool-vectors
1178 @cindex Bool-vectors
1180 A bool-vector is much like a vector, except that it stores only the
1181 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
1182 value into an element of the bool-vector, the effect is to store
1183 @code{t} there. As with all arrays, bool-vector indices start from 0,
1184 and the length cannot be changed once the bool-vector is created.
1185 Bool-vectors are constants when evaluated.
1187 Several functions work specifically with bool-vectors; aside
1188 from that, you manipulate them with same functions used for other kinds
1191 @defun make-bool-vector length initial
1192 Return a new bool-vector of @var{length} elements,
1193 each one initialized to @var{initial}.
1196 @defun bool-vector &rest objects
1197 This function creates and returns a bool-vector whose elements are the
1198 arguments, @var{objects}.
1201 @defun bool-vector-p object
1202 This returns @code{t} if @var{object} is a bool-vector,
1203 and @code{nil} otherwise.
1206 There are also some bool-vector set operation functions, described below:
1208 @defun bool-vector-exclusive-or a b &optional c
1209 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
1210 If optional argument @var{c} is given, the result of this operation is
1211 stored into @var{c}. All arguments should be bool vectors of the same length.
1214 @defun bool-vector-union a b &optional c
1215 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}. If
1216 optional argument @var{c} is given, the result of this operation is
1217 stored into @var{c}. All arguments should be bool vectors of the same length.
1220 @defun bool-vector-intersection a b &optional c
1221 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}. If
1222 optional argument @var{c} is given, the result of this operation is
1223 stored into @var{c}. All arguments should be bool vectors of the same length.
1226 @defun bool-vector-set-difference a b &optional c
1227 Return @dfn{set difference} of bool vectors @var{a} and @var{b}. If
1228 optional argument @var{c} is given, the result of this operation is
1229 stored into @var{c}. All arguments should be bool vectors of the same length.
1232 @defun bool-vector-not a &optional b
1233 Return @dfn{set complement} of bool vector @var{a}. If optional
1234 argument @var{b} is given, the result of this operation is stored into
1235 @var{b}. All arguments should be bool vectors of the same length.
1238 @defun bool-vector-subsetp a b
1239 Return @code{t} if every @code{t} value in @var{a} is also t in
1240 @var{b}, @code{nil} otherwise. All arguments should be bool vectors of the
1244 @defun bool-vector-count-consecutive a b i
1245 Return the number of consecutive elements in @var{a} equal @var{b}
1246 starting at @var{i}. @code{a} is a bool vector, @var{b} is @code{t}
1247 or @code{nil}, and @var{i} is an index into @code{a}.
1250 @defun bool-vector-count-population a
1251 Return the number of elements that are @code{t} in bool vector @var{a}.
1254 The printed form represents up to 8 boolean values as a single
1259 (bool-vector t nil t nil)
1266 You can use @code{vconcat} to print a bool-vector like other vectors:
1270 (vconcat (bool-vector nil t nil t))
1271 @result{} [nil t nil t]
1275 Here is another example of creating, examining, and updating a
1279 (setq bv (make-bool-vector 5 t))
1290 These results make sense because the binary codes for control-_ and
1291 control-W are 11111 and 10111, respectively.
1294 @section Managing a Fixed-Size Ring of Objects
1296 @cindex ring data structure
1297 A @dfn{ring} is a fixed-size data structure that supports insertion,
1298 deletion, rotation, and modulo-indexed reference and traversal. An
1299 efficient ring data structure is implemented by the @code{ring}
1300 package. It provides the functions listed in this section.
1302 Note that several ``rings'' in Emacs, like the kill ring and the
1303 mark ring, are actually implemented as simple lists, @emph{not} using
1304 the @code{ring} package; thus the following functions won't work on
1307 @defun make-ring size
1308 This returns a new ring capable of holding @var{size} objects.
1309 @var{size} should be an integer.
1312 @defun ring-p object
1313 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
1316 @defun ring-size ring
1317 This returns the maximum capacity of the @var{ring}.
1320 @defun ring-length ring
1321 This returns the number of objects that @var{ring} currently contains.
1322 The value will never exceed that returned by @code{ring-size}.
1325 @defun ring-elements ring
1326 This returns a list of the objects in @var{ring}, in order, newest first.
1329 @defun ring-copy ring
1330 This returns a new ring which is a copy of @var{ring}.
1331 The new ring contains the same (@code{eq}) objects as @var{ring}.
1334 @defun ring-empty-p ring
1335 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
1338 The newest element in the ring always has index 0. Higher indices
1339 correspond to older elements. Indices are computed modulo the ring
1340 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
1341 to the next-oldest, and so forth.
1343 @defun ring-ref ring index
1344 This returns the object in @var{ring} found at index @var{index}.
1345 @var{index} may be negative or greater than the ring length. If
1346 @var{ring} is empty, @code{ring-ref} signals an error.
1349 @defun ring-insert ring object
1350 This inserts @var{object} into @var{ring}, making it the newest
1351 element, and returns @var{object}.
1353 If the ring is full, insertion removes the oldest element to
1354 make room for the new element.
1357 @defun ring-remove ring &optional index
1358 Remove an object from @var{ring}, and return that object. The
1359 argument @var{index} specifies which item to remove; if it is
1360 @code{nil}, that means to remove the oldest item. If @var{ring} is
1361 empty, @code{ring-remove} signals an error.
1364 @defun ring-insert-at-beginning ring object
1365 This inserts @var{object} into @var{ring}, treating it as the oldest
1366 element. The return value is not significant.
1368 If the ring is full, this function removes the newest element to make
1369 room for the inserted element.
1372 @cindex fifo data structure
1373 If you are careful not to exceed the ring size, you can
1374 use the ring as a first-in-first-out queue. For example:
1377 (let ((fifo (make-ring 5)))
1378 (mapc (lambda (obj) (ring-insert fifo obj))
1380 (list (ring-remove fifo) t
1381 (ring-remove fifo) t
1382 (ring-remove fifo)))
1383 @result{} (0 t one t "two")