Skip to content

Commit

Permalink
Fix unenqueue
Browse files Browse the repository at this point in the history
Don't lose the penultimate element when dequeuing one by one.
  • Loading branch information
glv2 committed May 24, 2024
1 parent 748f0a1 commit d82347e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
14 changes: 7 additions & 7 deletions collectors.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,18 @@ FUNCTION and INITIAL-VALUE are passed directly to MAKE-REDUCER."
(len (length head))
(div (- len (+ 1 n)))
(rtn (cond
((plusp div)
((minusp div)
(setf (value o) nil)
(when (typep o 'collector)
(setf (tail o) nil))
head)
(t
(let* ((c (nthcdr div head))
(rtn (cdr c)))
(setf (cdr c) nil)
(when (typep o 'collector)
(setf (tail o) c))
rtn))
(t
(setf (value o) nil)
(when (typep o 'collector)
(setf (tail o) nil))
head))))
rtn)))))
(if (= 1 n)
(car rtn)
rtn)))
Expand Down
8 changes: 6 additions & 2 deletions tests/collectors.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,9 @@
(it 1 2 3 4 5 6)
(assert-equal 6 (unenqueue-it))
(assert-equal '(4 5) (unenqueue-it 2))
(assert-equal '(1 2 3) (it))
))
(assert-equal '(1 2 3) (it)))
(with-collector (it)
(it 0 1 2)
(assert-equal 2 (unenqueue-it))
(assert-equal 1 (unenqueue-it))
(assert-equal 0 (unenqueue-it))))

0 comments on commit d82347e

Please sign in to comment.