24 |
|
|
25 |
;;; Commentary: |
;;; Commentary: |
26 |
|
|
27 |
;; This code defines a ring data structure. A ring is a |
;; This code defines a ring data structure. A ring is a |
28 |
;; (hd-index length . vector) |
;; (hd-index length . vector) |
29 |
;; list. You can insert to, remove from, and rotate a ring. When the ring |
;; list. You can insert to, remove from, and rotate a ring. When the ring |
30 |
;; fills up, insertions cause the oldest elts to be quietly dropped. |
;; fills up, insertions cause the oldest elts to be quietly dropped. |
48 |
|
|
49 |
;;;###autoload |
;;;###autoload |
50 |
(defun ring-p (x) |
(defun ring-p (x) |
51 |
"Returns t if X is a ring; nil otherwise." |
"Return t if X is a ring; nil otherwise." |
52 |
(and (consp x) (integerp (car x)) |
(and (consp x) (integerp (car x)) |
53 |
(consp (cdr x)) (integerp (car (cdr x))) |
(consp (cdr x)) (integerp (car (cdr x))) |
54 |
(vectorp (cdr (cdr x))))) |
(vectorp (cdr (cdr x))))) |
71 |
(setcar (cdr ring) ln))) |
(setcar (cdr ring) ln))) |
72 |
|
|
73 |
(defun ring-plus1 (index veclen) |
(defun ring-plus1 (index veclen) |
74 |
"Returns INDEX+1, with wraparound." |
"Return INDEX+1, with wraparound." |
75 |
(let ((new-index (+ index 1))) |
(let ((new-index (+ index 1))) |
76 |
(if (= new-index veclen) 0 new-index))) |
(if (= new-index veclen) 0 new-index))) |
77 |
|
|
78 |
(defun ring-minus1 (index veclen) |
(defun ring-minus1 (index veclen) |
79 |
"Returns INDEX-1, with wraparound." |
"Return INDEX-1, with wraparound." |
80 |
(- (if (= 0 index) veclen index) 1)) |
(- (if (= 0 index) veclen index) 1)) |
81 |
|
|
82 |
(defun ring-length (ring) |
(defun ring-length (ring) |
83 |
"Returns the number of elements in the RING." |
"Return the number of elements in the RING." |
84 |
(car (cdr ring))) |
(car (cdr ring))) |
85 |
|
|
86 |
(defun ring-index (index head ringlen veclen) |
(defun ring-index (index head ringlen veclen) |
87 |
"Converts nominal ring index INDEX to an internal index. |
"Convert nominal ring index INDEX to an internal index. |
88 |
The internal index refers to the items ordered from newest to oldest. |
The internal index refers to the items ordered from newest to oldest. |
89 |
HEAD is the index of the oldest element in the ring. |
HEAD is the index of the oldest element in the ring. |
90 |
RINGLEN is the number of elements currently in the ring. |
RINGLEN is the number of elements currently in the ring. |
93 |
(mod (1- (+ head (- ringlen index))) veclen)) |
(mod (1- (+ head (- ringlen index))) veclen)) |
94 |
|
|
95 |
(defun ring-empty-p (ring) |
(defun ring-empty-p (ring) |
96 |
"Returns t if RING is empty; nil otherwise." |
"Return t if RING is empty; nil otherwise." |
97 |
(= 0 (car (cdr ring)))) |
(zerop (car (cdr ring)))) |
98 |
|
|
99 |
(defun ring-size (ring) |
(defun ring-size (ring) |
100 |
"Returns the size of RING, the maximum number of elements it can contain." |
"Return the size of RING, the maximum number of elements it can contain." |
101 |
(length (cdr (cdr ring)))) |
(length (cdr (cdr ring)))) |
102 |
|
|
103 |
(defun ring-copy (ring) |
(defun ring-copy (ring) |
104 |
"Returns a copy of RING." |
"Return a copy of RING." |
105 |
(let* ((vec (cdr (cdr ring))) |
(let* ((vec (cdr (cdr ring))) |
106 |
(hd (car ring)) |
(hd (car ring)) |
107 |
(ln (car (cdr ring)))) |
(ln (car (cdr ring)))) |
144 |
oldelt))) |
oldelt))) |
145 |
|
|
146 |
(defun ring-ref (ring index) |
(defun ring-ref (ring index) |
147 |
"Returns RING's INDEX element. |
"Return RING's INDEX element. |
148 |
INDEX = 0 is the most recently inserted; higher indices |
INDEX = 0 is the most recently inserted; higher indices |
149 |
correspond to older elements. |
correspond to older elements. |
150 |
INDEX need not be <= the ring length; the appropriate modulo operation |
INDEX need not be <= the ring length; the appropriate modulo operation |
155 |
(aref vec (ring-index index hd ln (length vec)))))) |
(aref vec (ring-index index hd ln (length vec)))))) |
156 |
|
|
157 |
(defun ring-elements (ring) |
(defun ring-elements (ring) |
158 |
"Return a list of the lements of RING." |
"Return a list of the elements of RING." |
159 |
(mapcar #'identity (cddr ring))) |
(mapcar #'identity (cddr ring))) |
160 |
|
|
161 |
;;; provide ourself: |
;;; provide ourself: |
162 |
|
|
163 |
(provide 'ring) |
(provide 'ring) |
164 |
|
|
165 |
|
;;; arch-tag: e707682b-ed69-47c9-b20f-cf2c68cc92d2 |
166 |
;;; ring.el ends here |
;;; ring.el ends here |