diff options
| author | Joakim Verona | 2012-01-23 15:10:06 +0100 |
|---|---|---|
| committer | Joakim Verona | 2012-01-23 15:10:06 +0100 |
| commit | 0322b140eead7c94de7f0f6d19a90bd15690b4eb (patch) | |
| tree | 950c011783cc896d0450084cb5155e54548bfe5b /doc/lispref/sequences.texi | |
| parent | d5114bfea3ea4c37c57e2af0f3b095be9fcd8bac (diff) | |
| parent | cb5850f27c1b4d26957d58e2da2314dd12498671 (diff) | |
| download | emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.tar.gz emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.zip | |
upstream
Diffstat (limited to 'doc/lispref/sequences.texi')
| -rw-r--r-- | doc/lispref/sequences.texi | 120 |
1 files changed, 106 insertions, 14 deletions
diff --git a/doc/lispref/sequences.texi b/doc/lispref/sequences.texi index 0ea32f99e12..94f1bf666d2 100644 --- a/doc/lispref/sequences.texi +++ b/doc/lispref/sequences.texi | |||
| @@ -8,10 +8,10 @@ | |||
| 8 | @chapter Sequences, Arrays, and Vectors | 8 | @chapter Sequences, Arrays, and Vectors |
| 9 | @cindex sequence | 9 | @cindex sequence |
| 10 | 10 | ||
| 11 | Recall that the @dfn{sequence} type is the union of two other Lisp | 11 | The @dfn{sequence} type is the union of two other Lisp types: lists |
| 12 | types: lists and arrays. In other words, any list is a sequence, and | 12 | and arrays. In other words, any list is a sequence, and any array is |
| 13 | any array is a sequence. The common property that all sequences have is | 13 | a sequence. The common property that all sequences have is that each |
| 14 | that each is an ordered collection of elements. | 14 | is an ordered collection of elements. |
| 15 | 15 | ||
| 16 | An @dfn{array} is a fixed-length object with a slot for each of its | 16 | An @dfn{array} is a fixed-length object with a slot for each of its |
| 17 | elements. All the elements are accessible in constant time. The four | 17 | elements. All the elements are accessible in constant time. The four |
| @@ -54,19 +54,17 @@ But it is possible to add elements to the list, or remove elements. | |||
| 54 | * Vector Functions:: Functions specifically for vectors. | 54 | * Vector Functions:: Functions specifically for vectors. |
| 55 | * Char-Tables:: How to work with char-tables. | 55 | * Char-Tables:: How to work with char-tables. |
| 56 | * Bool-Vectors:: How to work with bool-vectors. | 56 | * Bool-Vectors:: How to work with bool-vectors. |
| 57 | * Rings:: Managing a fixed-size ring of objects. | ||
| 57 | @end menu | 58 | @end menu |
| 58 | 59 | ||
| 59 | @node Sequence Functions | 60 | @node Sequence Functions |
| 60 | @section Sequences | 61 | @section Sequences |
| 61 | 62 | ||
| 62 | In Emacs Lisp, a @dfn{sequence} is either a list or an array. The | 63 | This section describes functions that accept any kind of sequence. |
| 63 | common property of all sequences is that they are ordered collections of | ||
| 64 | elements. This section describes functions that accept any kind of | ||
| 65 | sequence. | ||
| 66 | 64 | ||
| 67 | @defun sequencep object | 65 | @defun sequencep object |
| 68 | Returns @code{t} if @var{object} is a list, vector, string, | 66 | This function returns @code{t} if @var{object} is a list, vector, |
| 69 | bool-vector, or char-table, @code{nil} otherwise. | 67 | string, bool-vector, or char-table, @code{nil} otherwise. |
| 70 | @end defun | 68 | @end defun |
| 71 | 69 | ||
| 72 | @defun length sequence | 70 | @defun length sequence |
| @@ -149,8 +147,9 @@ This function generalizes @code{aref} (@pxref{Array Functions}) and | |||
| 149 | 147 | ||
| 150 | @defun copy-sequence sequence | 148 | @defun copy-sequence sequence |
| 151 | @cindex copying sequences | 149 | @cindex copying sequences |
| 152 | Returns a copy of @var{sequence}. The copy is the same type of object | 150 | This function returns a copy of @var{sequence}. The copy is the same |
| 153 | as the original sequence, and it has the same elements in the same order. | 151 | type of object as the original sequence, and it has the same elements |
| 152 | in the same order. | ||
| 154 | 153 | ||
| 155 | Storing a new element into the copy does not affect the original | 154 | Storing a new element into the copy does not affect the original |
| 156 | @var{sequence}, and vice versa. However, the elements of the new | 155 | @var{sequence}, and vice versa. However, the elements of the new |
| @@ -394,8 +393,8 @@ symbol-lookup tables (@pxref{Creating Symbols}), as part of the | |||
| 394 | representation of a byte-compiled function (@pxref{Byte Compilation}), | 393 | representation of a byte-compiled function (@pxref{Byte Compilation}), |
| 395 | and more. | 394 | and more. |
| 396 | 395 | ||
| 397 | In Emacs Lisp, the indices of the elements of a vector start from zero | 396 | Like other arrays, vectors use zero-origin indexing: the first |
| 398 | and count up from there. | 397 | element has index 0. |
| 399 | 398 | ||
| 400 | Vectors are printed with square brackets surrounding the elements. | 399 | Vectors are printed with square brackets surrounding the elements. |
| 401 | Thus, a vector whose elements are the symbols @code{a}, @code{b} and | 400 | Thus, a vector whose elements are the symbols @code{a}, @code{b} and |
| @@ -728,3 +727,96 @@ bv | |||
| 728 | @noindent | 727 | @noindent |
| 729 | These results make sense because the binary codes for control-_ and | 728 | These results make sense because the binary codes for control-_ and |
| 730 | control-W are 11111 and 10111, respectively. | 729 | control-W are 11111 and 10111, respectively. |
| 730 | |||
| 731 | @node Rings | ||
| 732 | @section Managing a Fixed-Size Ring of Objects | ||
| 733 | |||
| 734 | @cindex ring data structure | ||
| 735 | A @dfn{ring} is a fixed-size data structure that supports insertion, | ||
| 736 | deletion, rotation, and modulo-indexed reference and traversal. An | ||
| 737 | efficient ring data structure is implemented by the @code{ring} | ||
| 738 | package. It provides the functions listed in this section. | ||
| 739 | |||
| 740 | Note that several ``rings'' in Emacs, like the kill ring and the | ||
| 741 | mark ring, are actually implemented as simple lists, @emph{not} using | ||
| 742 | the @code{ring} package; thus the following functions won't work on | ||
| 743 | them. | ||
| 744 | |||
| 745 | @defun make-ring size | ||
| 746 | This returns a new ring capable of holding @var{size} objects. | ||
| 747 | @var{size} should be an integer. | ||
| 748 | @end defun | ||
| 749 | |||
| 750 | @defun ring-p object | ||
| 751 | This returns @code{t} if @var{object} is a ring, @code{nil} otherwise. | ||
| 752 | @end defun | ||
| 753 | |||
| 754 | @defun ring-size ring | ||
| 755 | This returns the maximum capacity of the @var{ring}. | ||
| 756 | @end defun | ||
| 757 | |||
| 758 | @defun ring-length ring | ||
| 759 | This returns the number of objects that @var{ring} currently contains. | ||
| 760 | The value will never exceed that returned by @code{ring-size}. | ||
| 761 | @end defun | ||
| 762 | |||
| 763 | @defun ring-elements ring | ||
| 764 | This returns a list of the objects in @var{ring}, in order, newest first. | ||
| 765 | @end defun | ||
| 766 | |||
| 767 | @defun ring-copy ring | ||
| 768 | This returns a new ring which is a copy of @var{ring}. | ||
| 769 | The new ring contains the same (@code{eq}) objects as @var{ring}. | ||
| 770 | @end defun | ||
| 771 | |||
| 772 | @defun ring-empty-p ring | ||
| 773 | This returns @code{t} if @var{ring} is empty, @code{nil} otherwise. | ||
| 774 | @end defun | ||
| 775 | |||
| 776 | The newest element in the ring always has index 0. Higher indices | ||
| 777 | correspond to older elements. Indices are computed modulo the ring | ||
| 778 | length. Index @minus{}1 corresponds to the oldest element, @minus{}2 | ||
| 779 | to the next-oldest, and so forth. | ||
| 780 | |||
| 781 | @defun ring-ref ring index | ||
| 782 | This returns the object in @var{ring} found at index @var{index}. | ||
| 783 | @var{index} may be negative or greater than the ring length. If | ||
| 784 | @var{ring} is empty, @code{ring-ref} signals an error. | ||
| 785 | @end defun | ||
| 786 | |||
| 787 | @defun ring-insert ring object | ||
| 788 | This inserts @var{object} into @var{ring}, making it the newest | ||
| 789 | element, and returns @var{object}. | ||
| 790 | |||
| 791 | If the ring is full, insertion removes the oldest element to | ||
| 792 | make room for the new element. | ||
| 793 | @end defun | ||
| 794 | |||
| 795 | @defun ring-remove ring &optional index | ||
| 796 | Remove an object from @var{ring}, and return that object. The | ||
| 797 | argument @var{index} specifies which item to remove; if it is | ||
| 798 | @code{nil}, that means to remove the oldest item. If @var{ring} is | ||
| 799 | empty, @code{ring-remove} signals an error. | ||
| 800 | @end defun | ||
| 801 | |||
| 802 | @defun ring-insert-at-beginning ring object | ||
| 803 | This inserts @var{object} into @var{ring}, treating it as the oldest | ||
| 804 | element. The return value is not significant. | ||
| 805 | |||
| 806 | If the ring is full, this function removes the newest element to make | ||
| 807 | room for the inserted element. | ||
| 808 | @end defun | ||
| 809 | |||
| 810 | @cindex fifo data structure | ||
| 811 | If you are careful not to exceed the ring size, you can | ||
| 812 | use the ring as a first-in-first-out queue. For example: | ||
| 813 | |||
| 814 | @lisp | ||
| 815 | (let ((fifo (make-ring 5))) | ||
| 816 | (mapc (lambda (obj) (ring-insert fifo obj)) | ||
| 817 | '(0 one "two")) | ||
| 818 | (list (ring-remove fifo) t | ||
| 819 | (ring-remove fifo) t | ||
| 820 | (ring-remove fifo))) | ||
| 821 | @result{} (0 t one t "two") | ||
| 822 | @end lisp | ||