From fea6f5d467affbd699ef70aeb3cf69af1629bebe Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 29 Sep 2020 12:31:14 -0400 Subject: array-list: Fix the bug fix in array-list-delete! procedure. Ugh!!!! --- chickadee/array-list.scm | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/chickadee/array-list.scm b/chickadee/array-list.scm index 2208471..da450c8 100644 --- a/chickadee/array-list.scm +++ b/chickadee/array-list.scm @@ -106,23 +106,24 @@ (let loop ((i 0)) (when (< i n) (if (equal? item (vector-ref v i)) - (if fast? - ;; Fast: Swap the last element with the element to be - ;; deleted. Constant time but does not preserve - ;; order. - (let ((last (- n 1))) - (vector-set! v i (vector-ref v last)) - (vector-set! v last #f)) - ;; Slow: Shift all elements to the left. Linear time - ;; but preserves order. - (let shift ((j (+ i 1))) - (if (= j n) - (vector-set! v j #f) - (begin - (vector-set! v (- j 1) (vector-ref v j)) - (shift (+ j 1)))))) - (loop (+ i 1))))) - (set-array-list-size! array-list (- n 1)))) + (begin + (if fast? + ;; Fast: Swap the last element with the element to be + ;; deleted. Constant time but does not preserve + ;; order. + (let ((last (- n 1))) + (vector-set! v i (vector-ref v last)) + (vector-set! v last #f)) + ;; Slow: Shift all elements to the left. Linear time + ;; but preserves order. + (let shift ((j (+ i 1))) + (if (= j n) + (vector-set! v j #f) + (begin + (vector-set! v (- j 1) (vector-ref v j)) + (shift (+ j 1)))))) + (set-array-list-size! array-list (- n 1))) + (loop (+ i 1))))))) (define (array-list-clear! array-list) (let ((size (array-list-size array-list)) -- cgit v1.2.3