aboutsummaryrefslogtreecommitdiffstats
path: root/doc/lispref/sequences.texi
diff options
context:
space:
mode:
authorJoakim Verona2012-01-23 15:10:06 +0100
committerJoakim Verona2012-01-23 15:10:06 +0100
commit0322b140eead7c94de7f0f6d19a90bd15690b4eb (patch)
tree950c011783cc896d0450084cb5155e54548bfe5b /doc/lispref/sequences.texi
parentd5114bfea3ea4c37c57e2af0f3b095be9fcd8bac (diff)
parentcb5850f27c1b4d26957d58e2da2314dd12498671 (diff)
downloademacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.tar.gz
emacs-0322b140eead7c94de7f0f6d19a90bd15690b4eb.zip
upstream
Diffstat (limited to 'doc/lispref/sequences.texi')
-rw-r--r--doc/lispref/sequences.texi120
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
12types: lists and arrays. In other words, any list is a sequence, and 12and arrays. In other words, any list is a sequence, and any array is
13any array is a sequence. The common property that all sequences have is 13a sequence. The common property that all sequences have is that each
14that each is an ordered collection of elements. 14is 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
17elements. All the elements are accessible in constant time. The four 17elements. 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.
63common property of all sequences is that they are ordered collections of
64elements. This section describes functions that accept any kind of
65sequence.
66 64
67@defun sequencep object 65@defun sequencep object
68Returns @code{t} if @var{object} is a list, vector, string, 66This function returns @code{t} if @var{object} is a list, vector,
69bool-vector, or char-table, @code{nil} otherwise. 67string, 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
152Returns a copy of @var{sequence}. The copy is the same type of object 150This function returns a copy of @var{sequence}. The copy is the same
153as the original sequence, and it has the same elements in the same order. 151type of object as the original sequence, and it has the same elements
152in the same order.
154 153
155Storing a new element into the copy does not affect the original 154Storing 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
394representation of a byte-compiled function (@pxref{Byte Compilation}), 393representation of a byte-compiled function (@pxref{Byte Compilation}),
395and more. 394and 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
398and count up from there. 397element 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.
401Thus, a vector whose elements are the symbols @code{a}, @code{b} and 400Thus, a vector whose elements are the symbols @code{a}, @code{b} and
@@ -728,3 +727,96 @@ bv
728@noindent 727@noindent
729These results make sense because the binary codes for control-_ and 728These results make sense because the binary codes for control-_ and
730control-W are 11111 and 10111, respectively. 729control-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,
736deletion, rotation, and modulo-indexed reference and traversal. An
737efficient ring data structure is implemented by the @code{ring}
738package. It provides the functions listed in this section.
739
740 Note that several ``rings'' in Emacs, like the kill ring and the
741mark ring, are actually implemented as simple lists, @emph{not} using
742the @code{ring} package; thus the following functions won't work on
743them.
744
745@defun make-ring size
746This 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
751This returns @code{t} if @var{object} is a ring, @code{nil} otherwise.
752@end defun
753
754@defun ring-size ring
755This returns the maximum capacity of the @var{ring}.
756@end defun
757
758@defun ring-length ring
759This returns the number of objects that @var{ring} currently contains.
760The value will never exceed that returned by @code{ring-size}.
761@end defun
762
763@defun ring-elements ring
764This returns a list of the objects in @var{ring}, in order, newest first.
765@end defun
766
767@defun ring-copy ring
768This returns a new ring which is a copy of @var{ring}.
769The new ring contains the same (@code{eq}) objects as @var{ring}.
770@end defun
771
772@defun ring-empty-p ring
773This 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
777correspond to older elements. Indices are computed modulo the ring
778length. Index @minus{}1 corresponds to the oldest element, @minus{}2
779to the next-oldest, and so forth.
780
781@defun ring-ref ring index
782This 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
788This inserts @var{object} into @var{ring}, making it the newest
789element, and returns @var{object}.
790
791If the ring is full, insertion removes the oldest element to
792make room for the new element.
793@end defun
794
795@defun ring-remove ring &optional index
796Remove an object from @var{ring}, and return that object. The
797argument @var{index} specifies which item to remove; if it is
798@code{nil}, that means to remove the oldest item. If @var{ring} is
799empty, @code{ring-remove} signals an error.
800@end defun
801
802@defun ring-insert-at-beginning ring object
803This inserts @var{object} into @var{ring}, treating it as the oldest
804element. The return value is not significant.
805
806If the ring is full, this function removes the newest element to make
807room 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
812use 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