Sequences, Arrays, and Vectors
******************************

Recall that the sequence type is the union of two other Lisp types: lists and arrays.  In other words, any list is a sequence, and any array is a sequence.  The common property that all sequences have is that each is an ordered collection of elements.

An array is a single primitive object that has a slot for each of its elements.  All the elements are accessible in constant time, but the length of an existing array cannot be changed.  Strings, vectors, char-tables and bool-vectors are the four types of arrays.

A list is a sequence of elements, but it is not a single primitive object; it is made of cons cells, one cell per element.  Finding the Nth element requires looking through N cons cells, so elements farther from the beginning of the list take longer to access. But it is possible to add elements to the list, or remove elements.

The following diagram shows the relationship between these types:

               _____________________________________________
              |                                             |
              |          Sequence                           |
              |  ______   ________________________________  |
              | |      | |                                | |
              | | List | |             Array              | |
              | |      | |    ________       ________     | |
              | |______| |   |        |     |        |    | |
              |          |   | Vector |     | String |    | |
              |          |   |________|     |________|    | |
              |          |  ____________   _____________  | |
              |          | |            | |             | | |
              |          | | Char-table | | Bool-vector | | |
              |          | |____________| |_____________| | |
              |          |________________________________| |
              |_____________________________________________|

The elements of vectors and lists may be any Lisp objects.  The elements of strings are all characters.

Sequences
=========

In Emacs Lisp, a sequence is either a list or an array.  The common property of all sequences is that they are ordered collections of elements.  This section describes functions that accept any kind of sequence.

 -- Function: sequencep object
     Returns t if OBJECT is a list, vector, string, or char-table, nil otherwise.

 -- Function: length sequence
     This function returns the number of elements in SEQUENCE.  If SEQUENCE is a cons cell that is not a list (because the final CDR is not nil), a wrong-type-argument error is signaled.  For a char-table, the value returned is always one more than the maximum Emacs character code.

     See List Elements, for the related function safe-length.

          (length '(1 2 3))
              => 3

          (length ())
              => 0

          (length "foobar")
              => 6

          (length [1 2 3])
              => 3

          (length (make-bool-vector 5 nil))
              => 5

 -- Function: string-bytes string
     This function returns the number of bytes in STRING. If STRING is a multibyte string, this is greater than (length STRING).

 -- Function: elt sequence index
     This function returns the element of SEQUENCE indexed by INDEX.  Legitimate values of INDEX are integers ranging from 0 up to one less than the length of SEQUENCE.  If SEQUENCE is a list, then out-of-range values of INDEX return nil; otherwise, they trigger an args-out-of-range error.

          (elt [1 2 3 4] 2)
               => 3

          (elt '(1 2 3 4) 2)
               => 3

          ;; We use string to show clearly which character elt returns.
          (string (elt "1234" 2))
               => "3"

          (elt [1 2 3 4] 4)
               error--> Args out of range: [1 2 3 4], 4

          (elt [1 2 3 4] -1)
               error--> Args out of range: [1 2 3 4], -1

     This function generalizes aref (see Array Functions) and nth (see List Elements).

 -- Function: copy-sequence sequence
     Returns a copy of SEQUENCE.  The copy is the same type of object as the original sequence, and it has the same elements in the same order.

     Storing a new element into the copy does not affect the original SEQUENCE, and vice versa.  However, the elements of the new sequence are not copies; they are identical (eq) to the elements of the original.  Therefore, changes made within these elements, as found via the copied sequence, are also visible in the original sequence.

     If the sequence is a string with text properties, the property list in the copy is itself a copy, not shared with the original's property list.  However, the actual values of the properties are shared. See Text Properties.

     See also append in Building Lists, concat in Creating Strings, and vconcat in Vectors, for other ways to copy sequences.

          (setq bar '(1 2))
               => (1 2)

          (setq x (vector 'foo bar))
               => [foo (1 2)]

          (setq y (copy-sequence x))
               => [foo (1 2)]

          (eq x y)
               => nil

          (equal x y)
               => t

          (eq (elt x 1) (elt y 1))
               => t

          ;; Replacing an element of one sequence.
          (aset x 0 'quux)
          x => [quux (1 2)]
          y => [foo (1 2)]

          ;; Modifying the inside of a shared element.
          (setcar (aref x 1) 69)
          x => [quux (69 2)]
          y => [foo (69 2)]

Arrays
======

An array object has slots that hold a number of other Lisp objects, called the elements of the array.  Any element of an array may be accessed in constant time.  In contrast, an element of a list requires access time that is proportional to the position of the element in the list.

Emacs defines four types of array, all one-dimensional: strings, vectors, bool-vectors and char-tables.  A vector is a general array; its elements can be any Lisp objects.  A string is a specialized array; its elements must be characters.  Each type of array has its own read syntax. See String Type, and Vector Type.

All four kinds of array share these characteristics:

  * The first element of an array has index zero, the second element has index 1, and so on.  This is called zero-origin indexing.  For example, an array of four elements has indices 0, 1, 2, and 3.

  * The length of the array is fixed once you create it; you cannot change the length of an existing array.

  * The array is a constant, for evaluation---in other words, it evaluates to itself.

  * The elements of an array may be referenced or changed with the functions aref and aset, respectively (see Array Functions).

When you create an array, other than a char-table, you must specify its length.  You cannot specify the length of a char-table, because that is determined by the range of character codes.

In principle, if you want an array of text characters, you could use either a string or a vector.  In practice, we always choose strings for such applications, for four reasons:

  * They occupy one-fourth the space of a vector of the same elements.

  * Strings are printed in a way that shows the contents more clearly as text.

  * Strings can hold text properties.  See Text Properties.

  * Many of the specialized editing and I/O facilities of Emacs accept only strings.  For example, you cannot insert a vector of characters into a buffer the way you can insert a string.  See Strings and Characters.

By contrast, for an array of keyboard input characters (such as a key sequence), a vector may be necessary, because many keyboard input characters are outside the range that will fit in a string.  See Key Sequence Input.

Functions that Operate on Arrays
================================

In this section, we describe the functions that accept all types of arrays.

 -- Function: arrayp object
     This function returns t if OBJECT is an array (i.e., a vector, a string, a bool-vector or a char-table).

          (arrayp [a])
               => t
          (arrayp "asdf")
               => t
          (arrayp (syntax-table))    ;; A char-table.
               => t

 -- Function: aref array index
     This function returns the INDEXth element of ARRAY.  The first element is at index zero.

          (setq primes [2 3 5 7 11 13])
               => [2 3 5 7 11 13]
          (aref primes 4)
               => 11

          (aref "abcdefg" 1)
               => 98           ; b is ASCII code 98.

     See also the function elt, in Sequence Functions.

 -- Function: aset array index object
     This function sets the INDEXth element of ARRAY to be OBJECT.  It returns OBJECT.

          (setq w [foo bar baz])
               => [foo bar baz]
          (aset w 0 'fu)
               => fu
          w
               => [fu bar baz]

          (setq x "asdfasfd")
               => "asdfasfd"
          (aset x 3 ?Z)
               => 90
          x
               => "asdZasfd"

     If ARRAY is a string and OBJECT is not a character, a wrong-type-argument error results.  The function converts a unibyte string to multibyte if necessary to insert a character.

 -- Function: fillarray array object
     This function fills the array ARRAY with OBJECT, so that each element of ARRAY is OBJECT.  It returns ARRAY.

          (setq a [a b c d e f g])
               => [a b c d e f g]
          (fillarray a 0)
               => [0 0 0 0 0 0 0]
          a
               => [0 0 0 0 0 0 0]

          (setq s "When in the course")
               => "When in the course"
          (fillarray s ?-)
               => "------------------"

     If ARRAY is a string and OBJECT is not a character, a wrong-type-argument error results.

The general sequence functions copy-sequence and length are often useful for objects known to be arrays.  See Sequence Functions.

Vectors
=======

Arrays in Lisp, like arrays in most languages, are blocks of memory whose elements can be accessed in constant time.  A vector is a general-purpose array of specified length; its elements can be any Lisp objects.  (By contrast, a string can hold only characters as elements.) Vectors in Emacs are used for obarrays (vectors of symbols), and as part of keymaps (vectors of commands).  They are also used internally as part of the representation of a byte-compiled function; if you print such a function, you will see a vector in it.

In Emacs Lisp, the indices of the elements of a vector start from zero and count up from there.

Vectors are printed with square brackets surrounding the elements. Thus, a vector whose elements are the symbols a, b and a is printed as [a b a].  You can write vectors in the same way in Lisp input.

A vector, like a string or a number, is considered a constant for evaluation: the result of evaluating it is the same vector.  This does not evaluate or even examine the elements of the vector. See Self-Evaluating Forms.

Here are examples illustrating these principles:

     (setq avector [1 two '(three) "four" [five]])
          => [1 two (quote (three)) "four" [five]]
     (eval avector)
          => [1 two (quote (three)) "four" [five]]
     (eq avector (eval avector))
          => t

Functions for Vectors
=====================

Here are some functions that relate to vectors:

 -- Function: vectorp object
     This function returns t if OBJECT is a vector.

          (vectorp [a])
               => t
          (vectorp "asdf")
               => nil

 -- Function: vector &rest objects
     This function creates and returns a vector whose elements are the arguments, OBJECTS.

          (vector 'foo 23 [bar baz] "rats")
               => [foo 23 [bar baz] "rats"]
          (vector)
               => []

 -- Function: make-vector length object
     This function returns a new vector consisting of LENGTH elements, each initialized to OBJECT.

          (setq sleepy (make-vector 9 'Z))
               => [Z Z Z Z Z Z Z Z Z]

 -- Function: vconcat &rest sequences
     This function returns a new vector containing all the elements of the SEQUENCES.  The arguments SEQUENCES may be any kind of arrays, including lists, vectors, or strings.  If no SEQUENCES are given, an empty vector is returned.

     The value is a newly constructed vector that is not eq to any existing vector.

          (setq a (vconcat '(A B C) '(D E F)))
               => [A B C D E F]
          (eq a (vconcat a))
               => nil

          (vconcat)
               => []
          (vconcat [A B C] "aa" '(foo (6 7)))
               => [A B C 97 97 foo (6 7)]

     The vconcat function also allows byte-code function objects as arguments.  This is a special feature to make it easy to access the entire contents of a byte-code function object.  See Byte-Code Objects.

     In Emacs versions before 21, the vconcat function allowed integers as arguments, converting them to strings of digits, but that feature has been eliminated.  The proper way to convert an integer to a decimal number in this way is with format (see Formatting Strings) or number-to-string (see String Conversion).

     For other concatenation functions, see mapconcat in Mapping Functions, concat in Creating Strings, and append in Building Lists.

The append function provides a way to convert a vector into a list with the same elements (see Building Lists):

     (setq avector [1 two (quote (three)) "four" [five]])
          => [1 two (quote (three)) "four" [five]]
     (append avector nil)
          => (1 two (quote (three)) "four" [five])

Char-Tables
===========

A char-table is much like a vector, except that it is indexed by character codes.  Any valid character code, without modifiers, can be used as an index in a char-table.  You can access a char-table's elements with aref and aset, as with any array.  In addition, a char-table can have extra slots to hold additional data not associated with particular character codes.  Char-tables are constants when evaluated.

Each char-table has a subtype which is a symbol.  The subtype has two purposes: to distinguish char-tables meant for different uses, and to control the number of extra slots.  For example, display tables are char-tables with display-table as the subtype, and syntax tables are char-tables with syntax-table as the subtype.  A valid subtype must have a char-table-extra-slots property which is an integer between 0 and 10.  This integer specifies the number of extra slots in the char-table.

A char-table can have a parent, which is another char-table.  If it does, then whenever the char-table specifies nil for a particular character C, it inherits the value specified in the parent.  In other words, (aref CHAR-TABLE C) returns the value from the parent of CHAR-TABLE if CHAR-TABLE itself specifies nil.

A char-table can also have a default value.  If so, then (aref CHAR-TABLE C) returns the default value whenever the char-table does not specify any other non-nil value.

 -- Function: make-char-table subtype &optional init
     Return a newly created char-table, with subtype SUBTYPE.  Each element is initialized to INIT, which defaults to nil.  You cannot alter the subtype of a char-table after the char-table is created.

     There is no argument to specify the length of the char-table, because all char-tables have room for any valid character code as an index.

 -- Function: char-table-p object
     This function returns t if OBJECT is a char-table, otherwise nil.

 -- Function: char-table-subtype char-table
     This function returns the subtype symbol of CHAR-TABLE.

 -- Function: set-char-table-default char-table new-default
     This function sets the default value of CHAR-TABLE to NEW-DEFAULT.

     There is no special function to access the default value of a char-table. To do that, use (char-table-range CHAR-TABLE nil).

 -- Function: char-table-parent char-table
     This function returns the parent of CHAR-TABLE.  The parent is always either nil or another char-table.

 -- Function: set-char-table-parent char-table new-parent
     This function sets the parent of CHAR-TABLE to NEW-PARENT.

 -- Function: char-table-extra-slot char-table n
     This function returns the contents of extra slot N of CHAR-TABLE.  The number of extra slots in a char-table is determined by its subtype.

 -- Function: set-char-table-extra-slot char-table n value
     This function stores VALUE in extra slot N of CHAR-TABLE.

A char-table can specify an element value for a single character code; it can also specify a value for an entire character set.

 -- Function: char-table-range char-table range
     This returns the value specified in CHAR-TABLE for a range of characters RANGE.  Here are the possibilities for RANGE:

     nil
          Refers to the default value.

     CHAR
          Refers to the element for character CHAR (supposing CHAR is a valid character code).

     CHARSET
          Refers to the value specified for the whole character set CHARSET (see Character Sets).

     GENERIC-CHAR
          A generic character stands for a character set; specifying the generic character as argument is equivalent to specifying the character set name.  See Splitting Characters, for a description of generic characters.

 -- Function: set-char-table-range char-table range value
     This function sets the value in CHAR-TABLE for a range of characters RANGE.  Here are the possibilities for RANGE:

     nil
          Refers to the default value.

     t
          Refers to the whole range of character codes.

     CHAR
          Refers to the element for character CHAR (supposing CHAR is a valid character code).

     CHARSET
          Refers to the value specified for the whole character set CHARSET (see Character Sets).

     GENERIC-CHAR
          A generic character stands for a character set; specifying the generic character as argument is equivalent to specifying the character set name.  See Splitting Characters, for a description of generic characters.

 -- Function: map-char-table function char-table
     This function calls FUNCTION for each element of CHAR-TABLE. FUNCTION is called with two arguments, a key and a value.  The key is a possible RANGE argument for char-table-range---either a valid character or a generic character---and the value is (char-table-range CHAR-TABLE KEY).

     Overall, the key-value pairs passed to FUNCTION describe all the values stored in CHAR-TABLE.

     The return value is always nil; to make this function useful, FUNCTION should have side effects.  For example, here is how to examine each element of the syntax table:

          (let (accumulator)
            (map-char-table
             #'(lambda (key value)
                 (setq accumulator
                       (cons (list key value) accumulator)))
             (syntax-table))
            accumulator)
          =>
          ((475008 nil) (474880 nil) (474752 nil) (474624 nil)
           ... (5 (3)) (4 (3)) (3 (3)) (2 (3)) (1 (3)) (0 (3)))

Bool-vectors
============

A bool-vector is much like a vector, except that it stores only the values t and nil.  If you try to store any non-nil value into an element of the bool-vector, the effect is to store t there.  As with all arrays, bool-vector indices start from 0, and the length cannot be changed once the bool-vector is created. Bool-vectors are constants when evaluated.

There are two special functions for working with bool-vectors; aside from that, you manipulate them with same functions used for other kinds of arrays.

 -- Function: make-bool-vector length initial
     Return a new bool-vector of LENGTH elements, each one initialized to INITIAL.

 -- Function: bool-vector-p object
     This returns t if OBJECT is a bool-vector, and nil otherwise.

Here is an example of creating, examining, and updating a bool-vector.  Note that the printed form represents up to 8 boolean values as a single character.

     (setq bv (make-bool-vector 5 t))
          => #&5"^_"
     (aref bv 1)
          => t
     (aset bv 3 nil)
          => nil
     bv
          => #&5"^W"

These results make sense because the binary codes for control-_ and control-W are 11111 and 10111, respectively.