d5a7efa01e93

Add `fill-multidimensional-array`
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Thu, 18 Aug 2016 15:27:59 +0000
parents 2c1b47535281
children f556a4cf23b0
branches/tags (none)
files DOCUMENTATION.markdown losh.lisp package.lisp

Changes

--- 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.
--- 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:
+  ;;
+  ;; <scymtym> 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
+  ;;
+  ;; <sjl> scymtym: ugh, that's an sbcl-specific thing then?
+  ;;
+  ;; <scymtym> 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.
--- 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.")