f556a4cf23b0

Add different type versions of `fill-multidimensional-array` for speed
[view raw] [browse files]
author Steve Losh <steve@stevelosh.com>
date Thu, 18 Aug 2016 16:16:00 +0000
parents d5a7efa01e93
children 25f352f92356
branches/tags (none)
files DOCUMENTATION.markdown losh.lisp package.lisp

Changes

--- a/DOCUMENTATION.markdown	Thu Aug 18 15:27:59 2016 +0000
+++ b/DOCUMENTATION.markdown	Thu Aug 18 16:16:00 2016 +0000
@@ -57,6 +57,39 @@
 
   
 
+### `FILL-MULTIDIMENSIONAL-ARRAY-FIXNUM` (function)
+
+    (FILL-MULTIDIMENSIONAL-ARRAY-FIXNUM ARRAY ITEM)
+
+Fill `array` (which must be of type `(array FIXNUM *)`) with `item`.
+
+  Unlike `fill`, this works on multidimensional arrays.  It won't cons on SBCL,
+  but it may in other implementations.
+
+  
+
+### `FILL-MULTIDIMENSIONAL-ARRAY-SINGLE-FLOAT` (function)
+
+    (FILL-MULTIDIMENSIONAL-ARRAY-SINGLE-FLOAT ARRAY ITEM)
+
+Fill `array` (which must be of type `(array SINGLE-FLOAT *)`) with `item`.
+
+  Unlike `fill`, this works on multidimensional arrays.  It won't cons on SBCL,
+  but it may in other implementations.
+
+  
+
+### `FILL-MULTIDIMENSIONAL-ARRAY-T` (function)
+
+    (FILL-MULTIDIMENSIONAL-ARRAY-T ARRAY ITEM)
+
+Fill `array` (which must be of type `(array T *)`) 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	Thu Aug 18 15:27:59 2016 +0000
+++ b/losh.lisp	Thu Aug 18 16:16:00 2016 +0000
@@ -8,10 +8,17 @@
      (declaim (notinline ,name))
      ',name))
 
+(defmacro defun-inline (name &body body)
+  `(progn
+     (declaim (inline ,name))
+     (defun ,name ,@body)
+     ',name))
+
 
 ;;;; Symbols
-(defun symbolize (&rest args)
-  "Slap `args` together stringishly into a symbol and intern it.
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defun symbolize (&rest args)
+    "Slap `args` together stringishly into a symbol and intern it.
 
   Example:
 
@@ -19,7 +26,7 @@
     => 'foobarbaz
 
   "
-  (intern (format nil "~{~A~}" args)))
+    (intern (format nil "~{~A~}" args))))
 
 
 ;;;; Math
@@ -340,8 +347,12 @@
 
 
 ;;;; Arrays
-(declaim (ftype (function ((array t *) t))
-                fill-multidimensional-array))
+(declaim
+  (ftype (function ((array * *) t)) fill-multidimensional-array)
+  (ftype (function ((array t *) t)) fill-multidimensional-array-t)
+  (ftype (function ((array fixnum *) fixnum)) fill-multidimensional-array-fixnum)
+  (ftype (function ((array single-float *) single-float)) fill-multidimensional-array-single-float))
+
 
 (defmacro do-array ((value array) &body body)
   "Perform `body` once for each element in `array` using `value` for the place.
@@ -369,13 +380,7 @@
         (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.
-
-  "
+(defun-inline fill-mda (array item)
   ;; from #lisp:
   ;;
   ;; <scymtym> sjl: the problem with the displaced array version is that it
@@ -402,6 +407,35 @@
   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.
+
+  "
+  (fill-mda array item))
+
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+  (defparameter *fmda-docstring*
+    "Fill `array` (which must be of type `(array ~A *)`) with `item`.
+
+  Unlike `fill`, this works on multidimensional arrays.  It won't cons on SBCL,
+  but it may in other implementations.
+
+  "))
+
+(defmacro defun-fmda (type)
+  `(defun ,(symbolize 'fill-multidimensional-array- type) (array item)
+    ,(format nil *fmda-docstring* type)
+    (fill-mda array item)))
+
+(defun-fmda t)
+(defun-fmda fixnum)
+(defun-fmda single-float)
+
+
 ;;;; Hash Tables
 (defmacro gethash-or-init (key hash-table default-form)
   "Get `key`'s value in `hash-table`, initializing if necessary.
--- a/package.lisp	Thu Aug 18 15:27:59 2016 +0000
+++ b/package.lisp	Thu Aug 18 16:16:00 2016 +0000
@@ -91,7 +91,10 @@
   (:documentation "Utilities related to arrays.")
   (:export
     #:do-array
-    #:fill-multidimensional-array))
+    #:fill-multidimensional-array
+    #:fill-multidimensional-array-t
+    #:fill-multidimensional-array-fixnum
+    #:fill-multidimensional-array-single-float))
 
 (defsubpackage #:losh.hash-tables
   (:documentation "Utilities related to hash tables.")