+;;;; implementing the concept of "vector" in (almost) portable
+;;;; Common Lisp
+;;;;
+;;;; "If you only need to do such simple things, it doesn't really
+;;;; matter which language you use." -- _ANSI Common Lisp_, p. 1, Paul
+;;;; Graham (evidently not considering the abstraction "vector" to be
+;;;; such a simple thing:-)
+
+(eval-when (:compile-toplevel :load-toplevel :execute)
+ (defconstant +smallvec-length+
+ (expt 2 16)))
+
+;;; an element of a BIGVEC -- a vector small enough that we have
+;;; a good chance of it being portable to other Common Lisps
+(deftype smallvec ()
+ `(simple-array (unsigned-byte 8) (,+smallvec-length+)))
+
+(defun make-smallvec ()
+ (make-array +smallvec-length+ :element-type '(unsigned-byte 8)))
+
+;;; a big vector, implemented as a vector of SMALLVECs
+;;;
+;;; KLUDGE: This implementation seems portable enough for our
+;;; purposes, since realistically every modern implementation is
+;;; likely to support vectors of at least 2^16 elements. But if you're
+;;; masochistic enough to read this far into the contortions imposed
+;;; on us by ANSI and the Lisp community, for daring to use the
+;;; abstraction of a large linearly addressable memory space, which is
+;;; after all only directly supported by the underlying hardware of at
+;;; least 99% of the general-purpose computers in use today, then you
+;;; may be titillated to hear that in fact this code isn't really
+;;; portable, because as of sbcl-0.7.4 we need somewhat more than
+;;; 16Mbytes to represent a core, and ANSI only guarantees that
+;;; ARRAY-DIMENSION-LIMIT is not less than 1024. -- WHN 2002-06-13
+(defstruct bigvec
+ (outer-vector (vector (make-smallvec)) :type (vector smallvec)))
+
+;;; analogous to SVREF, but into a BIGVEC
+(defun bvref (bigvec index)
+ (multiple-value-bind (outer-index inner-index)
+ (floor index +smallvec-length+)
+ (aref (the smallvec
+ (svref (bigvec-outer-vector bigvec) outer-index))
+ inner-index)))
+(defun (setf bvref) (new-value bigvec index)
+ (multiple-value-bind (outer-index inner-index)
+ (floor index +smallvec-length+)
+ (setf (aref (the smallvec
+ (svref (bigvec-outer-vector bigvec) outer-index))
+ inner-index)
+ new-value)))
+
+;;; analogous to LENGTH, but for a BIGVEC
+;;;
+;;; the length of BIGVEC, measured in the number of BVREFable bytes it
+;;; can hold
+(defun bvlength (bigvec)
+ (* (length (bigvec-outer-vector bigvec))
+ +smallvec-length+))
+
+;;; analogous to WRITE-SEQUENCE, but for a BIGVEC
+(defun write-bigvec-as-sequence (bigvec stream &key (start 0) end)
+ (loop for i of-type index from start below (or end (bvlength bigvec)) do
+ (write-byte (bvref bigvec i)
+ stream)))
+
+;;; analogous to READ-SEQUENCE-OR-DIE, but for a BIGVEC
+(defun read-bigvec-as-sequence-or-die (bigvec stream &key (start 0) end)
+ (loop for i of-type index from start below (or end (bvlength bigvec)) do
+ (setf (bvref bigvec i)
+ (read-byte stream))))
+
+;;; Grow BIGVEC (exponentially, so that large increases in size have
+;;; asymptotic logarithmic cost per byte).
+(defun expand-bigvec (bigvec)
+ (let* ((old-outer-vector (bigvec-outer-vector bigvec))
+ (length-old-outer-vector (length old-outer-vector))
+ (new-outer-vector (make-array (* 2 length-old-outer-vector))))
+ (dotimes (i length-old-outer-vector)
+ (setf (svref new-outer-vector i)
+ (svref old-outer-vector i)))
+ (loop for i from length-old-outer-vector below (length new-outer-vector) do
+ (setf (svref new-outer-vector i)
+ (make-smallvec)))
+ (setf (bigvec-outer-vector bigvec)
+ new-outer-vector))
+ bigvec)
+\f
+;;;; looking up bytes and multi-byte values in a BIGVEC (considering
+;;;; it as an image of machine memory)
+
+;;; BVREF-32 and friends. These are like SAP-REF-n, except that
+;;; instead of a SAP we use a BIGVEC.
+(macrolet ((make-bvref-n
+ (n)
+ (let* ((name (intern (format nil "BVREF-~A" n)))
+ (number-octets (/ n 8))
+ (ash-list-le
+ (loop for i from 0 to (1- number-octets)
+ collect `(ash (bvref bigvec (+ byte-index ,i))
+ ,(* i 8))))
+ (ash-list-be
+ (loop for i from 0 to (1- number-octets)
+ collect `(ash (bvref bigvec
+ (+ byte-index
+ ,(- number-octets 1 i)))
+ ,(* i 8))))
+ (setf-list-le
+ (loop for i from 0 to (1- number-octets)
+ append
+ `((bvref bigvec (+ byte-index ,i))
+ (ldb (byte 8 ,(* i 8)) new-value))))
+ (setf-list-be
+ (loop for i from 0 to (1- number-octets)
+ append
+ `((bvref bigvec (+ byte-index ,i))
+ (ldb (byte 8 ,(- n 8 (* i 8))) new-value)))))
+ `(progn
+ (defun ,name (bigvec byte-index)
+ (aver (= sb!vm:n-word-bits 32))
+ (aver (= sb!vm:n-byte-bits 8))
+ (logior ,@(ecase sb!c:*backend-byte-order*
+ (:little-endian ash-list-le)
+ (:big-endian ash-list-be))))
+ (defun (setf ,name) (new-value bigvec byte-index)
+ (aver (= sb!vm:n-word-bits 32))
+ (aver (= sb!vm:n-byte-bits 8))
+ (setf ,@(ecase sb!c:*backend-byte-order*
+ (:little-endian setf-list-le)
+ (:big-endian setf-list-be))))))))
+ (make-bvref-n 8)
+ (make-bvref-n 16)
+ (make-bvref-n 32))
+\f