2 ms·
With adjustable arrays it's easy: (defun vector-delete! (vector index) "Delete the element at INDEX from the given VECTOR. Destructively modifies the
by tmtvl 3y ago
With adjustable arrays it's easy:
(defun vector-delete! (vector index)
"Delete the element at INDEX from the given VECTOR.
Destructively modifies the vector.
The vector must have a fill-pointer.
This is not thread-safe, use at your own risk.
Examples:
(let* ((x (make-array 4 :fill-pointer 4 :initial-contents '(1 2 3 4)))
(y x))
(vector-delete! x 0)
y)
;; => #(2 3 4)"
(declare (type (Integer 0 *) index)
(type Vector vector))
(assert (array-has-fill-pointer-p vector))
(let ((l (length vector)))
(assert (< index l))
(let ((removed-element (aref vector index)))
(loop :for i :from (1+ index) :below l
:do
(setf (aref vector (1- i))
(aref vector i)))
(decf (fill-pointer vector))
removed-element)))
(Granted, it could be done in 6 lines of code, but I'm already cringing at not providing a continuation when calling it with an out-of-bounds index)