]> code.delx.au - gnu-emacs/blob - doc/lispref/sequences.texi
Merge from emacs-24; up to 2014-06-02T11:35:40Z!michael.albinus@gmx.de
[gnu-emacs] / doc / lispref / sequences.texi
1 @c -*-texinfo-*-
2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990-1995, 1998-1999, 2001-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
9
10 The @dfn{sequence} type is the union of two other Lisp types: lists
11 and arrays. In other words, any list is a sequence, and any array is
12 a sequence. The common property that all sequences have is that each
13 is an ordered collection of elements.
14
15 An @dfn{array} is a fixed-length object with a slot for each of its
16 elements. All the elements are accessible in constant time. The four
17 types of arrays are strings, vectors, char-tables and bool-vectors.
18
19 A list is a sequence of elements, but it is not a single primitive
20 object; it is made of cons cells, one cell per element. Finding the
21 @var{n}th element requires looking through @var{n} cons cells, so
22 elements farther from the beginning of the list take longer to access.
23 But it is possible to add elements to the list, or remove elements.
24
25 The following diagram shows the relationship between these types:
26
27 @example
28 @group
29 _____________________________________________
30 | |
31 | Sequence |
32 | ______ ________________________________ |
33 | | | | | |
34 | | List | | Array | |
35 | | | | ________ ________ | |
36 | |______| | | | | | | |
37 | | | Vector | | String | | |
38 | | |________| |________| | |
39 | | ____________ _____________ | |
40 | | | | | | | |
41 | | | Char-table | | Bool-vector | | |
42 | | |____________| |_____________| | |
43 | |________________________________| |
44 |_____________________________________________|
45 @end group
46 @end example
47
48 @menu
49 * Sequence Functions:: Functions that accept any kind of sequence.
50 * Arrays:: Characteristics of arrays in Emacs Lisp.
51 * Array Functions:: Functions specifically for arrays.
52 * Vectors:: Special characteristics of Emacs Lisp vectors.
53 * Vector Functions:: Functions specifically for vectors.
54 * Char-Tables:: How to work with char-tables.
55 * Bool-Vectors:: How to work with bool-vectors.
56 * Rings:: Managing a fixed-size ring of objects.
57 @end menu
58
59 @node Sequence Functions
60 @section Sequences
61
62 This section describes functions that accept any kind of sequence.
63
64 @defun sequencep object
65 This function returns @code{t} if @var{object} is a list, vector,
66 string, bool-vector, or char-table, @code{nil} otherwise.
67 @end defun
68
69 @defun length sequence
70 @cindex string length
71 @cindex list length
72 @cindex vector length
73 @cindex sequence length
74 @cindex char-table length
75 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.
80
81 @xref{Definition of safe-length}, for the related function @code{safe-length}.
82
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
106
107 @noindent
108 See also @code{string-bytes}, in @ref{Text Representations}.
109
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.
114
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.
123
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
147
148 This function generalizes @code{aref} (@pxref{Array Functions}) and
149 @code{nth} (@pxref{Definition of nth}).
150 @end defun
151
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.
157
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.
164
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}.
169
170 This function does not work for dotted lists. Trying to copy a
171 circular list may cause an infinite loop.
172
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.
176
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
190
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
203
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
210
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
219
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.
228
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)
237 x
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]
247 x
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"
257 x
258 @result{} "xyzzy"
259 @end group
260 @end example
261 @end defun
262
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.
269
270 For example:
271
272 @example
273 @group
274 (setq x '(a b c))
275 @result{} (a b c)
276 @end group
277 @group
278 x
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.}
285 x
286 @result{} (a)
287 @end group
288 @end example
289
290 To avoid confusion, we usually store the result of @code{nreverse}
291 back in the same variable which held the original list:
292
293 @example
294 (setq x (nreverse x))
295 @end example
296
297 Here is the @code{nreverse} of our favorite example, @code{(a b c)},
298 presented graphically:
299
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
312
313 For the vector, it is even simpler because you don't need setq:
314
315 @example
316 (setq x [1 2 3 4])
317 @result{} [1 2 3 4]
318 (nreverse x)
319 @result{} [4 3 2 1]
320 x
321 @result{} [4 3 2 1]
322 @end example
323
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.
327
328 @end defun
329
330 @node Arrays
331 @section Arrays
332 @cindex array
333
334 An @dfn{array} object has slots that hold a number of other Lisp
335 objects, called the elements of the array. Any element of an array
336 may be accessed in constant time. In contrast, the time to access an
337 element of a list is proportional to the position of that element in
338 the list.
339
340 Emacs defines four types of array, all one-dimensional:
341 @dfn{strings} (@pxref{String Type}), @dfn{vectors} (@pxref{Vector
342 Type}), @dfn{bool-vectors} (@pxref{Bool-Vector Type}), and
343 @dfn{char-tables} (@pxref{Char-Table Type}). Vectors and char-tables
344 can hold elements of any type, but strings can only hold characters,
345 and bool-vectors can only hold @code{t} and @code{nil}.
346
347 All four kinds of array share these characteristics:
348
349 @itemize @bullet
350 @item
351 The first element of an array has index zero, the second element has
352 index 1, and so on. This is called @dfn{zero-origin} indexing. For
353 example, an array of four elements has indices 0, 1, 2, @w{and 3}.
354
355 @item
356 The length of the array is fixed once you create it; you cannot
357 change the length of an existing array.
358
359 @item
360 For purposes of evaluation, the array is a constant---i.e.,
361 it evaluates to itself.
362
363 @item
364 The elements of an array may be referenced or changed with the functions
365 @code{aref} and @code{aset}, respectively (@pxref{Array Functions}).
366 @end itemize
367
368 When you create an array, other than a char-table, you must specify
369 its length. You cannot specify the length of a char-table, because that
370 is determined by the range of character codes.
371
372 In principle, if you want an array of text characters, you could use
373 either a string or a vector. In practice, we always choose strings for
374 such applications, for four reasons:
375
376 @itemize @bullet
377 @item
378 They occupy one-fourth the space of a vector of the same elements.
379
380 @item
381 Strings are printed in a way that shows the contents more clearly
382 as text.
383
384 @item
385 Strings can hold text properties. @xref{Text Properties}.
386
387 @item
388 Many of the specialized editing and I/O facilities of Emacs accept only
389 strings. For example, you cannot insert a vector of characters into a
390 buffer the way you can insert a string. @xref{Strings and Characters}.
391 @end itemize
392
393 By contrast, for an array of keyboard input characters (such as a key
394 sequence), a vector may be necessary, because many keyboard input
395 characters are outside the range that will fit in a string. @xref{Key
396 Sequence Input}.
397
398 @node Array Functions
399 @section Functions that Operate on Arrays
400
401 In this section, we describe the functions that accept all types of
402 arrays.
403
404 @defun arrayp object
405 This function returns @code{t} if @var{object} is an array (i.e., a
406 vector, a string, a bool-vector or a char-table).
407
408 @example
409 @group
410 (arrayp [a])
411 @result{} t
412 (arrayp "asdf")
413 @result{} t
414 (arrayp (syntax-table)) ;; @r{A char-table.}
415 @result{} t
416 @end group
417 @end example
418 @end defun
419
420 @defun aref array index
421 @cindex array elements
422 This function returns the @var{index}th element of @var{array}. The
423 first element is at index zero.
424
425 @example
426 @group
427 (setq primes [2 3 5 7 11 13])
428 @result{} [2 3 5 7 11 13]
429 (aref primes 4)
430 @result{} 11
431 @end group
432 @group
433 (aref "abcdefg" 1)
434 @result{} 98 ; @r{@samp{b} is @acronym{ASCII} code 98.}
435 @end group
436 @end example
437
438 See also the function @code{elt}, in @ref{Sequence Functions}.
439 @end defun
440
441 @defun aset array index object
442 This function sets the @var{index}th element of @var{array} to be
443 @var{object}. It returns @var{object}.
444
445 @example
446 @group
447 (setq w [foo bar baz])
448 @result{} [foo bar baz]
449 (aset w 0 'fu)
450 @result{} fu
451 w
452 @result{} [fu bar baz]
453 @end group
454
455 @group
456 (setq x "asdfasfd")
457 @result{} "asdfasfd"
458 (aset x 3 ?Z)
459 @result{} 90
460 x
461 @result{} "asdZasfd"
462 @end group
463 @end example
464
465 If @var{array} is a string and @var{object} is not a character, a
466 @code{wrong-type-argument} error results. The function converts a
467 unibyte string to multibyte if necessary to insert a character.
468 @end defun
469
470 @defun fillarray array object
471 This function fills the array @var{array} with @var{object}, so that
472 each element of @var{array} is @var{object}. It returns @var{array}.
473
474 @example
475 @group
476 (setq a [a b c d e f g])
477 @result{} [a b c d e f g]
478 (fillarray a 0)
479 @result{} [0 0 0 0 0 0 0]
480 a
481 @result{} [0 0 0 0 0 0 0]
482 @end group
483 @group
484 (setq s "When in the course")
485 @result{} "When in the course"
486 (fillarray s ?-)
487 @result{} "------------------"
488 @end group
489 @end example
490
491 If @var{array} is a string and @var{object} is not a character, a
492 @code{wrong-type-argument} error results.
493 @end defun
494
495 The general sequence functions @code{copy-sequence} and @code{length}
496 are often useful for objects known to be arrays. @xref{Sequence Functions}.
497
498 @node Vectors
499 @section Vectors
500 @cindex vector (type)
501
502 A @dfn{vector} is a general-purpose array whose elements can be any
503 Lisp objects. (By contrast, the elements of a string can only be
504 characters. @xref{Strings and Characters}.) Vectors are used in
505 Emacs for many purposes: as key sequences (@pxref{Key Sequences}), as
506 symbol-lookup tables (@pxref{Creating Symbols}), as part of the
507 representation of a byte-compiled function (@pxref{Byte Compilation}),
508 and more.
509
510 Like other arrays, vectors use zero-origin indexing: the first
511 element has index 0.
512
513 Vectors are printed with square brackets surrounding the elements.
514 Thus, a vector whose elements are the symbols @code{a}, @code{b} and
515 @code{a} is printed as @code{[a b a]}. You can write vectors in the
516 same way in Lisp input.
517
518 A vector, like a string or a number, is considered a constant for
519 evaluation: the result of evaluating it is the same vector. This does
520 not evaluate or even examine the elements of the vector.
521 @xref{Self-Evaluating Forms}.
522
523 Here are examples illustrating these principles:
524
525 @example
526 @group
527 (setq avector [1 two '(three) "four" [five]])
528 @result{} [1 two (quote (three)) "four" [five]]
529 (eval avector)
530 @result{} [1 two (quote (three)) "four" [five]]
531 (eq avector (eval avector))
532 @result{} t
533 @end group
534 @end example
535
536 @node Vector Functions
537 @section Functions for Vectors
538
539 Here are some functions that relate to vectors:
540
541 @defun vectorp object
542 This function returns @code{t} if @var{object} is a vector.
543
544 @example
545 @group
546 (vectorp [a])
547 @result{} t
548 (vectorp "asdf")
549 @result{} nil
550 @end group
551 @end example
552 @end defun
553
554 @defun vector &rest objects
555 This function creates and returns a vector whose elements are the
556 arguments, @var{objects}.
557
558 @example
559 @group
560 (vector 'foo 23 [bar baz] "rats")
561 @result{} [foo 23 [bar baz] "rats"]
562 (vector)
563 @result{} []
564 @end group
565 @end example
566 @end defun
567
568 @defun make-vector length object
569 This function returns a new vector consisting of @var{length} elements,
570 each initialized to @var{object}.
571
572 @example
573 @group
574 (setq sleepy (make-vector 9 'Z))
575 @result{} [Z Z Z Z Z Z Z Z Z]
576 @end group
577 @end example
578 @end defun
579
580 @defun vconcat &rest sequences
581 @cindex copying vectors
582 This function returns a new vector containing all the elements of
583 @var{sequences}. The arguments @var{sequences} may be true lists,
584 vectors, strings or bool-vectors. If no @var{sequences} are given,
585 the empty vector is returned.
586
587 The value is either the empty vector, or is a newly constructed
588 nonempty vector that is not @code{eq} to any existing vector.
589
590 @example
591 @group
592 (setq a (vconcat '(A B C) '(D E F)))
593 @result{} [A B C D E F]
594 (eq a (vconcat a))
595 @result{} nil
596 @end group
597 @group
598 (vconcat)
599 @result{} []
600 (vconcat [A B C] "aa" '(foo (6 7)))
601 @result{} [A B C 97 97 foo (6 7)]
602 @end group
603 @end example
604
605 The @code{vconcat} function also allows byte-code function objects as
606 arguments. This is a special feature to make it easy to access the entire
607 contents of a byte-code function object. @xref{Byte-Code Objects}.
608
609 For other concatenation functions, see @code{mapconcat} in @ref{Mapping
610 Functions}, @code{concat} in @ref{Creating Strings}, and @code{append}
611 in @ref{Building Lists}.
612 @end defun
613
614 The @code{append} function also provides a way to convert a vector into a
615 list with the same elements:
616
617 @example
618 @group
619 (setq avector [1 two (quote (three)) "four" [five]])
620 @result{} [1 two (quote (three)) "four" [five]]
621 (append avector nil)
622 @result{} (1 two (quote (three)) "four" [five])
623 @end group
624 @end example
625
626 @node Char-Tables
627 @section Char-Tables
628 @cindex char-tables
629 @cindex extra slots of char-table
630
631 A char-table is much like a vector, except that it is indexed by
632 character codes. Any valid character code, without modifiers, can be
633 used as an index in a char-table. You can access a char-table's
634 elements with @code{aref} and @code{aset}, as with any array. In
635 addition, a char-table can have @dfn{extra slots} to hold additional
636 data not associated with particular character codes. Like vectors,
637 char-tables are constants when evaluated, and can hold elements of any
638 type.
639
640 @cindex subtype of char-table
641 Each char-table has a @dfn{subtype}, a symbol, which serves two
642 purposes:
643
644 @itemize @bullet
645 @item
646 The subtype provides an easy way to tell what the char-table is for.
647 For instance, display tables are char-tables with @code{display-table}
648 as the subtype, and syntax tables are char-tables with
649 @code{syntax-table} as the subtype. The subtype can be queried using
650 the function @code{char-table-subtype}, described below.
651
652 @item
653 The subtype controls the number of @dfn{extra slots} in the
654 char-table. This number is specified by the subtype's
655 @code{char-table-extra-slots} symbol property (@pxref{Symbol
656 Properties}), whose value should be an integer between 0 and 10. If
657 the subtype has no such symbol property, the char-table has no extra
658 slots.
659 @end itemize
660
661 @cindex parent of char-table
662 A char-table can have a @dfn{parent}, which is another char-table. If
663 it does, then whenever the char-table specifies @code{nil} for a
664 particular character @var{c}, it inherits the value specified in the
665 parent. In other words, @code{(aref @var{char-table} @var{c})} returns
666 the value from the parent of @var{char-table} if @var{char-table} itself
667 specifies @code{nil}.
668
669 @cindex default value of char-table
670 A char-table can also have a @dfn{default value}. If so, then
671 @code{(aref @var{char-table} @var{c})} returns the default value
672 whenever the char-table does not specify any other non-@code{nil} value.
673
674 @defun make-char-table subtype &optional init
675 Return a newly-created char-table, with subtype @var{subtype} (a
676 symbol). Each element is initialized to @var{init}, which defaults to
677 @code{nil}. You cannot alter the subtype of a char-table after the
678 char-table is created.
679
680 There is no argument to specify the length of the char-table, because
681 all char-tables have room for any valid character code as an index.
682
683 If @var{subtype} has the @code{char-table-extra-slots} symbol
684 property, that specifies the number of extra slots in the char-table.
685 This should be an integer between 0 and 10; otherwise,
686 @code{make-char-table} raises an error. If @var{subtype} has no
687 @code{char-table-extra-slots} symbol property (@pxref{Property
688 Lists}), the char-table has no extra slots.
689 @end defun
690
691 @defun char-table-p object
692 This function returns @code{t} if @var{object} is a char-table, and
693 @code{nil} otherwise.
694 @end defun
695
696 @defun char-table-subtype char-table
697 This function returns the subtype symbol of @var{char-table}.
698 @end defun
699
700 There is no special function to access default values in a char-table.
701 To do that, use @code{char-table-range} (see below).
702
703 @defun char-table-parent char-table
704 This function returns the parent of @var{char-table}. The parent is
705 always either @code{nil} or another char-table.
706 @end defun
707
708 @defun set-char-table-parent char-table new-parent
709 This function sets the parent of @var{char-table} to @var{new-parent}.
710 @end defun
711
712 @defun char-table-extra-slot char-table n
713 This function returns the contents of extra slot @var{n} of
714 @var{char-table}. The number of extra slots in a char-table is
715 determined by its subtype.
716 @end defun
717
718 @defun set-char-table-extra-slot char-table n value
719 This function stores @var{value} in extra slot @var{n} of
720 @var{char-table}.
721 @end defun
722
723 A char-table can specify an element value for a single character code;
724 it can also specify a value for an entire character set.
725
726 @defun char-table-range char-table range
727 This returns the value specified in @var{char-table} for a range of
728 characters @var{range}. Here are the possibilities for @var{range}:
729
730 @table @asis
731 @item @code{nil}
732 Refers to the default value.
733
734 @item @var{char}
735 Refers to the element for character @var{char}
736 (supposing @var{char} is a valid character code).
737
738 @item @code{(@var{from} . @var{to})}
739 A cons cell refers to all the characters in the inclusive range
740 @samp{[@var{from}..@var{to}]}.
741 @end table
742 @end defun
743
744 @defun set-char-table-range char-table range value
745 This function sets the value in @var{char-table} for a range of
746 characters @var{range}. Here are the possibilities for @var{range}:
747
748 @table @asis
749 @item @code{nil}
750 Refers to the default value.
751
752 @item @code{t}
753 Refers to the whole range of character codes.
754
755 @item @var{char}
756 Refers to the element for character @var{char}
757 (supposing @var{char} is a valid character code).
758
759 @item @code{(@var{from} . @var{to})}
760 A cons cell refers to all the characters in the inclusive range
761 @samp{[@var{from}..@var{to}]}.
762 @end table
763 @end defun
764
765 @defun map-char-table function char-table
766 This function calls its argument @var{function} for each element of
767 @var{char-table} that has a non-@code{nil} value. The call to
768 @var{function} is with two arguments, a key and a value. The key
769 is a possible @var{range} argument for @code{char-table-range}---either
770 a valid character or a cons cell @code{(@var{from} . @var{to})},
771 specifying a range of characters that share the same value. The value is
772 what @code{(char-table-range @var{char-table} @var{key})} returns.
773
774 Overall, the key-value pairs passed to @var{function} describe all the
775 values stored in @var{char-table}.
776
777 The return value is always @code{nil}; to make calls to
778 @code{map-char-table} useful, @var{function} should have side effects.
779 For example, here is how to examine the elements of the syntax table:
780
781 @example
782 (let (accumulator)
783 (map-char-table
784 #'(lambda (key value)
785 (setq accumulator
786 (cons (list
787 (if (consp key)
788 (list (car key) (cdr key))
789 key)
790 value)
791 accumulator)))
792 (syntax-table))
793 accumulator)
794 @result{}
795 (((2597602 4194303) (2)) ((2597523 2597601) (3))
796 ... (65379 (5 . 65378)) (65378 (4 . 65379)) (65377 (1))
797 ... (12 (0)) (11 (3)) (10 (12)) (9 (0)) ((0 8) (3)))
798 @end example
799 @end defun
800
801 @node Bool-Vectors
802 @section Bool-vectors
803 @cindex Bool-vectors
804
805 A bool-vector is much like a vector, except that it stores only the
806 values @code{t} and @code{nil}. If you try to store any non-@code{nil}
807 value into an element of the bool-vector, the effect is to store
808 @code{t} there. As with all arrays, bool-vector indices start from 0,
809 and the length cannot be changed once the bool-vector is created.
810 Bool-vectors are constants when evaluated.
811
812 Several functions work specifically with bool-vectors; aside
813 from that, you manipulate them with same functions used for other kinds
814 of arrays.
815
816 @defun make-bool-vector length initial
817 Return a new bool-vector of @var{length} elements,
818 each one initialized to @var{initial}.
819 @end defun
820
821 @defun bool-vector &rest objects
822 This function creates and returns a bool-vector whose elements are the
823 arguments, @var{objects}.
824 @end defun
825
826 @defun bool-vector-p object
827 This returns @code{t} if @var{object} is a bool-vector,
828 and @code{nil} otherwise.
829 @end defun
830
831 There are also some bool-vector set operation functions, described below:
832
833 @defun bool-vector-exclusive-or a b &optional c
834 Return @dfn{bitwise exclusive or} of bool vectors @var{a} and @var{b}.
835 If optional argument @var{c} is given, the result of this operation is
836 stored into @var{c}. All arguments should be bool vectors of the same length.
837 @end defun
838
839 @defun bool-vector-union a b &optional c
840 Return @dfn{bitwise or} of bool vectors @var{a} and @var{b}. If
841 optional argument @var{c} is given, the result of this operation is
842 stored into @var{c}. All arguments should be bool vectors of the same length.
843 @end defun
844
845 @defun bool-vector-intersection a b &optional c
846 Return @dfn{bitwise and} of bool vectors @var{a} and @var{b}. If
847 optional argument @var{c} is given, the result of this operation is
848 stored into @var{c}. All arguments should be bool vectors of the same length.
849 @end defun
850
851 @defun bool-vector-set-difference a b &optional c
852 Return @dfn{set difference} of bool vectors @var{a} and @var{b}. If
853 optional argument @var{c} is given, the result of this operation is
854 stored into @var{c}. All arguments should be bool vectors of the same length.
855 @end defun
856
857 @defun bool-vector-not a &optional b
858 Return @dfn{set complement} of bool vector @var{a}. If optional
859 argument @var{b} is given, the result of this operation is stored into
860 @var{b}. All arguments should be bool vectors of the same length.
861 @end defun
862
863 @defun bool-vector-subsetp a b
864 Return @code{t} if every @code{t} value in @var{a} is also t in
865 @var{b}, @code{nil} otherwise. All arguments should be bool vectors of the
866 same length.
867 @end defun
868
869 @defun bool-vector-count-consecutive a b i
870 Return the number of consecutive elements in @var{a} equal @var{b}
871 starting at @var{i}. @code{a} is a bool vector, @var{b} is @code{t}
872 or @code{nil}, and @var{i} is an index into @code{a}.
873 @end defun
874
875 @defun bool-vector-count-population a
876 Return the number of elements that are @code{t} in bool vector @var{a}.
877 @end defun
878
879 The printed form represents up to 8 boolean values as a single
880 character:
881
882 @example
883 @group
884 (bool-vector t nil t nil)
885 @result{} #&4"^E"
886 (bool-vector)
887 @result{} #&0""
888 @end group
889 @end example
890
891 You can use @code{vconcat} to print a bool-vector like other vectors:
892
893 @example
894 @group
895 (vconcat (bool-vector nil t nil t))
896 @result{} [nil t nil t]
897 @end group
898 @end example
899
900 Here is another example of creating, examining, and updating a
901 bool-vector:
902
903 @example
904 (setq bv (make-bool-vector 5 t))
905 @result{} #&5"^_"
906 (aref bv 1)
907 @result{} t
908 (aset bv 3 nil)
909 @result{} nil
910 bv
911 @result{} #&5"^W"
912 @end example
913
914 @noindent
915 These results make sense because the binary codes for control-_ and
916 control-W are 11111 and 10111, respectively.
917
918 @node Rings
919 @section Managing a Fixed-Size Ring of Objects
920
921 @cindex ring data structure
922 A @dfn{ring} is a fixed-size data structure that supports insertion,
923 deletion, rotation, and modulo-indexed reference and traversal. An
924 efficient ring data structure is implemented by the @code{ring}
925 package. It provides the functions listed in this section.
926
927 Note that several ``rings'' in Emacs, like the kill ring and the
928 mark ring, are actually implemented as simple lists, @emph{not} using
929 the @code{ring} package; thus the following functions won't work on
930 them.
931
932 @defun make-ring size
933 This returns a new ring capable of holding @var{size} objects.
934 @var{size} should be an integer.
935 @end defun
936
937 @defun ring-p object
938 This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
939 @end defun
940
941 @defun ring-size ring
942 This returns the maximum capacity of the @var{ring}.
943 @end defun
944
945 @defun ring-length ring
946 This returns the number of objects that @var{ring} currently contains.
947 The value will never exceed that returned by @code{ring-size}.
948 @end defun
949
950 @defun ring-elements ring
951 This returns a list of the objects in @var{ring}, in order, newest first.
952 @end defun
953
954 @defun ring-copy ring
955 This returns a new ring which is a copy of @var{ring}.
956 The new ring contains the same (@code{eq}) objects as @var{ring}.
957 @end defun
958
959 @defun ring-empty-p ring
960 This returns @code{t} if @var{ring} is empty, @code{nil} otherwise.
961 @end defun
962
963 The newest element in the ring always has index 0. Higher indices
964 correspond to older elements. Indices are computed modulo the ring
965 length. Index @minus{}1 corresponds to the oldest element, @minus{}2
966 to the next-oldest, and so forth.
967
968 @defun ring-ref ring index
969 This returns the object in @var{ring} found at index @var{index}.
970 @var{index} may be negative or greater than the ring length. If
971 @var{ring} is empty, @code{ring-ref} signals an error.
972 @end defun
973
974 @defun ring-insert ring object
975 This inserts @var{object} into @var{ring}, making it the newest
976 element, and returns @var{object}.
977
978 If the ring is full, insertion removes the oldest element to
979 make room for the new element.
980 @end defun
981
982 @defun ring-remove ring &optional index
983 Remove an object from @var{ring}, and return that object. The
984 argument @var{index} specifies which item to remove; if it is
985 @code{nil}, that means to remove the oldest item. If @var{ring} is
986 empty, @code{ring-remove} signals an error.
987 @end defun
988
989 @defun ring-insert-at-beginning ring object
990 This inserts @var{object} into @var{ring}, treating it as the oldest
991 element. The return value is not significant.
992
993 If the ring is full, this function removes the newest element to make
994 room for the inserted element.
995 @end defun
996
997 @cindex fifo data structure
998 If you are careful not to exceed the ring size, you can
999 use the ring as a first-in-first-out queue. For example:
1000
1001 @lisp
1002 (let ((fifo (make-ring 5)))
1003 (mapc (lambda (obj) (ring-insert fifo obj))
1004 '(0 one "two"))
1005 (list (ring-remove fifo) t
1006 (ring-remove fifo) t
1007 (ring-remove fifo)))
1008 @result{} (0 t one t "two")
1009 @end lisp