# HG changeset patch # User Steve Losh # Date 1471534079 0 # Node ID d5a7efa01e937ab5f73cdae4da58ae02e805a42b # Parent 2c1b475352811a3b39350a7c1581d696132bbc5d Add `fill-multidimensional-array` diff -r 2c1b47535281 -r d5a7efa01e93 DOCUMENTATION.markdown --- a/DOCUMENTATION.markdown Wed Aug 17 02:09:50 2016 +0000 +++ b/DOCUMENTATION.markdown Thu Aug 18 15:27:59 2016 +0000 @@ -46,6 +46,17 @@ +### `FILL-MULTIDIMENSIONAL-ARRAY` (function) + + (FILL-MULTIDIMENSIONAL-ARRAY ARRAY ITEM) + +Fill `array` with `item`. + + Unlike `fill`, this works on multidimensional arrays. It won't cons on SBCL, + but it may in other implementations. + + + ## Package `LOSH.CONTROL-FLOW` Utilities for managing control flow. diff -r 2c1b47535281 -r d5a7efa01e93 losh.lisp --- a/losh.lisp Wed Aug 17 02:09:50 2016 +0000 +++ b/losh.lisp Thu Aug 18 15:27:59 2016 +0000 @@ -340,6 +340,9 @@ ;;;; Arrays +(declaim (ftype (function ((array t *) t)) + fill-multidimensional-array)) + (defmacro do-array ((value array) &body body) "Perform `body` once for each element in `array` using `value` for the place. @@ -366,6 +369,39 @@ (finally (return ,array)))))) +(defun fill-multidimensional-array (array item) + "Fill `array` with `item`. + + Unlike `fill`, this works on multidimensional arrays. It won't cons on SBCL, + but it may in other implementations. + + " + ;; from #lisp: + ;; + ;; sjl: the problem with the displaced array version is that it + ;; accumulates weak pointers to displaced arrays when the arrays are created + ;; and only removes them when the arrays are gced. that list is traversed each + ;; time a displaced array is created. so it can get much worse with more + ;; repetitions and depends on gc behavior + ;; + ;; scymtym: ugh, that's an sbcl-specific thing then? + ;; + ;; sjl: probably. i don't know how other implementations handle the + ;; problem. the reason for this weak pointer mechanism is that resizing the + ;; displaced-to array can propagate to the displaced array which has to be + ;; a pretty rare case + #+sbcl + (fill (sb-ext:array-storage-vector array) item) + #-(or sbcl) + (fill (make-array (array-total-size array) + :adjustable nil + :fill-pointer nil + :displaced-to array + :element-type (array-element-type array)) + item) + array) + + ;;;; Hash Tables (defmacro gethash-or-init (key hash-table default-form) "Get `key`'s value in `hash-table`, initializing if necessary. diff -r 2c1b47535281 -r d5a7efa01e93 package.lisp --- a/package.lisp Wed Aug 17 02:09:50 2016 +0000 +++ b/package.lisp Thu Aug 18 15:27:59 2016 +0000 @@ -90,7 +90,8 @@ (defsubpackage #:losh.arrays (:documentation "Utilities related to arrays.") (:export - #:do-array)) + #:do-array + #:fill-multidimensional-array)) (defsubpackage #:losh.hash-tables (:documentation "Utilities related to hash tables.")