aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lispref/lists.texi88
1 files changed, 88 insertions, 0 deletions
diff --git a/lispref/lists.texi b/lispref/lists.texi
index ab7d496e461..02a900e3e00 100644
--- a/lispref/lists.texi
+++ b/lispref/lists.texi
@@ -24,6 +24,7 @@ the whole list.
24* Modifying Lists:: Storing new pieces into an existing list. 24* Modifying Lists:: Storing new pieces into an existing list.
25* Sets And Lists:: A list can represent a finite mathematical set. 25* Sets And Lists:: A list can represent a finite mathematical set.
26* Association Lists:: A list can represent a finite relation or mapping. 26* Association Lists:: A list can represent a finite relation or mapping.
27* Rings:: Managing a fixed-size ring of objects.
27@end menu 28@end menu
28 29
29@node Cons Cells 30@node Cons Cells
@@ -1676,6 +1677,93 @@ compares the @sc{cdr} of each @var{alist} association instead of the
1676@sc{car}. 1677@sc{car}.
1677@end defun 1678@end defun
1678 1679
1680@node Rings
1681@section Managing a Fixed-Size Ring of Objects
1682
1683@cindex ring data structure
1684 This section describes functions for operating on rings. A
1685@dfn{ring} is a fixed-size data structure that supports insertion,
1686deletion, rotation, and modulo-indexed reference and traversal.
1687
1688@defun make-ring size
1689This returns a new ring capable of holding @var{size} objects.
1690@var{size} should be an integer.
1691@end defun
1692
1693@defun ring-p object
1694This returns @code{t} if @var{object} is a ring.
1695@end defun
1696
1697@defun ring-size ring
1698This returns the maximum capacity of the @var{ring}.
1699@end defun
1700
1701@defun ring-length ring
1702This returns the number of objects that @var{ring} currently contains.
1703The value will never exceed that returned by @code{ring-size}.
1704@end defun
1705
1706@defun ring-elements ring
1707This returns a list of the objects in @var{ring}, in no particular
1708order.
1709@end defun
1710
1711@defun ring-copy ring
1712This returns a new ring which is a copy of @var{ring}.
1713The new ring contains the same objects as @var{ring}.
1714@end defun
1715
1716@defun ring-empty-p ring
1717This returns @code{t} if @var{ring} is empty.
1718@end defun
1719
1720 The newest element in the ring always has index 0. Higher indexes
1721correspond to older elements. Index @minus{}1 corresponds to the
1722oldest element, @minus{}2 to the next-oldest, and so forth.
1723
1724@defun ring-ref ring index
1725This returns the object in @var{ring} found at index @var{index}.
1726@var{index} may be negative or greater than the ring length. If
1727@var{ring} is empty, @code{ring-ref} signals an error.
1728@end defun
1729
1730@defun ring-insert ring object
1731This inserts @var{object} into @var{ring}, making it the newest
1732element, and returns @var{object}.
1733
1734If the ring is full, insertion removes the oldest element to
1735make room for the new element.
1736@end defun
1737
1738@defun ring-remove ring &optional index
1739Remove an object from @var{ring}, and return that object. The
1740argument @var{index} specifies which item to remove; if it is
1741@code{nil}, that means to remove the oldest item. If @var{ring} is
1742empty, @code{ring-remove} signals an error.
1743@end defun
1744
1745@defun ring-insert-at-beginning ring object
1746This inserts @var{object} into @var{ring}, treating it as the oldest
1747element, and returns @var{object}.
1748
1749If the ring is full, this function removes the newest element to make
1750room for the inserted element.
1751@end defun
1752
1753@cindex fifo data structure
1754 If you are careful not to exceed the ring size, you can
1755use the ring as a first-in-first-out queue. For example:
1756
1757@lisp
1758(let ((fifo (make-ring 5)))
1759 (mapc (lambda (obj) (ring-insert fifo obj))
1760 '(0 one "two"))
1761 (list (ring-remove fifo) t
1762 (ring-remove fifo) t
1763 (ring-remove fifo)))
1764 @result{} (0 t one t "two")
1765@end lisp
1766
1679@ignore 1767@ignore
1680 arch-tag: 31fb8a4e-4aa8-4a74-a206-aa00451394d4 1768 arch-tag: 31fb8a4e-4aa8-4a74-a206-aa00451394d4
1681@end ignore 1769@end ignore