Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

refactor bulk stream operations; also fix float function docstring ge…

…neration
  • Loading branch information...
commit 535f975025b139930d16580f728c4efc50a4fd47 1 parent fc26ced
Nathan Froyd authored

Showing 1 changed file with 43 additions and 56 deletions. Show diff stats Hide diff stats

  1. +43 56 streams.lisp
99 streams.lisp
@@ -45,6 +45,38 @@
45 45 (read-n-bytes-into stream n-bytes v)
46 46 (setf (first rem) (funcall reffer v 0))))
47 47
  48 +(declaim (inline read-fresh-sequence))
  49 +(defun read-fresh-sequence (result-type stream count
  50 + element-type n-bytes reffer)
  51 + (ecase result-type
  52 + (list
  53 + (let ((list (make-list count)))
  54 + (read-into-list* stream list 0 count n-bytes reffer)))
  55 + (vector
  56 + (let ((vector (make-array count :element-type element-type)))
  57 + (read-into-vector* stream vector 0 count n-bytes reffer)))))
  58 +
  59 +(defun write-sequence-with-writer (seq stream start end writer)
  60 + (declare (type function writer))
  61 + (etypecase seq
  62 + (list
  63 + (mapc (lambda (e) (funcall writer e stream))
  64 + (subseq seq start end))
  65 + seq)
  66 + (vector
  67 + (loop with end = (or end (length seq))
  68 + for i from start below end
  69 + do (funcall writer (aref seq i) stream)
  70 + finally (return seq)))))
  71 +
  72 +(defun read-into-sequence (seq stream start end n-bytes reffer)
  73 + (etypecase seq
  74 + (list
  75 + (read-into-list* stream seq start end n-bytes reffer))
  76 + (vector
  77 + (let ((end (or end (length seq))))
  78 + (read-into-vector* stream seq start end n-bytes reffer)))))
  79 +
48 80 #.(loop for i from 0 upto #b10111
49 81 for bitsize = (ecase (ldb (byte 2 3) i)
50 82 (0 16)
@@ -68,44 +100,21 @@
68 100 (result-type stream count)
69 101 ,(format-docstring "Return a sequence of type RESULT-TYPE, containing COUNT elements read from STREAM. Each element is a ~D-bit ~:[un~;~]signed integer read in ~:[little~;big~]-endian order. RESULT-TYPE must be either CL:VECTOR or CL:LIST. STREAM must have an element type of (UNSIGNED-BYTE 8)."
70 102 bitsize signedp big-endian-p)
71   - (ecase result-type
72   - (list
73   - (let ((list (make-list count)))
74   - (read-into-list* stream list 0 count
75   - ,n-bytes #',byte-fun)))
76   - (vector
77   - (let ((vector (make-array count
78   - :element-type ',element-type)))
79   - (read-into-vector* stream vector 0 count
80   - ,n-bytes #',byte-fun))))) into forms
  103 + (read-fresh-sequence result-type stream count
  104 + ',element-type ,n-bytes #',byte-fun)) into forms
81 105 else
82 106 collect `(defun ,(stream-seq-fun-name bitsize nil signedp big-endian-p)
83 107 (seq stream &key (start 0) end)
84 108 ,(format-docstring "Write elements from SEQ between START and END as ~D-bit ~:[un~;~]signed integers in ~:[little~;big~]-endian order to STREAM. SEQ may be either a vector or a list. STREAM must have an element type of (UNSIGNED-BYTE 8)."
85 109 bitsize signedp big-endian-p)
86   - (etypecase seq
87   - (list
88   - (mapc (lambda (e) (,name e stream))
89   - (subseq seq start end))
90   - seq)
91   - (vector
92   - (loop with end = (or end (length seq))
93   - for i from start below end
94   - do (,name (aref seq i) stream)
95   - finally (return seq))))) into forms
  110 + (write-sequence-with-writer seq stream start end #',name)) into forms
96 111 if readp
97 112 collect `(defun ,(intern (format nil "READ-~:[U~;S~]B~D/~:[LE~;BE~]-INTO-SEQUENCE"
98 113 signedp bitsize big-endian-p))
99 114 (seq stream &key (start 0) end)
100 115 ,(format-docstring "Destructively modify SEQ by replacing the elements of SEQ between START and END with elements read from STREAM. Each element is a ~D-bit ~:[un~;~]signed integer read in ~:[little~;big~]-endian order. SEQ may be either a vector or a list. STREAM must have an element type of (UNSIGNED-BYTE 8)."
101 116 bitsize signedp big-endian-p)
102   - (etypecase seq
103   - (list (read-into-list* stream seq start end
104   - ,n-bytes #',byte-fun))
105   - (vector
106   - (let ((end (or end (length seq))))
107   - (read-into-vector* stream seq start end
108   - ,n-bytes #',byte-fun))))) into forms
  117 + (read-into-sequence seq stream start end ,n-bytes #',byte-fun)) into forms
109 118 finally (return `(progn ,@forms)))
110 119
111 120 #.(loop for i from 0 upto #b111
@@ -126,42 +135,20 @@
126 135 collect `(defun ,(stream-float-seq-fun-name float-type t big-endian-p)
127 136 (result-type stream count)
128 137 ,(format-docstring "Return a sequence of type RESULT-TYPE, containing COUNT elements read from STREAM. Each element is a ~A read in ~:[little~;big~]-endian byte order. RESULT-TYPE must be either CL:VECTOR or CL:LIST. STREAM must have an element type of (UNSIGNED-BYTE 8)."
129   - element-type)
130   - (ecase result-type
131   - (list
132   - (let ((list (make-list count)))
133   - (read-into-list* stream list 0 count
134   - ,n-bytes #',single-fun)))
135   - (vector
136   - (let ((vector (make-array count
137   - :element-type ',element-type)))
138   - (read-into-vector* stream vector 0 count
139   - ,n-bytes #',single-fun))))) into forms
  138 + element-type big-endian-p)
  139 + (read-fresh-sequence result-type stream count
  140 + ',element-type ,n-bytes #',single-fun)) into forms
140 141 else
141 142 collect `(defun ,(stream-float-seq-fun-name float-type nil big-endian-p)
142 143 (seq stream &key (start 0) end)
143 144 ,(format-docstring "Write elements from SEQ between START and END as ~As in ~:[little~;big~]-endian byte order to STREAM. SEQ may be either a vector or a list. STREAM must have an element type of (UNSIGNED-BYTE 8)."
144   - element-type)
145   - (etypecase seq
146   - (list
147   - (mapc (lambda (e) (,name e stream))
148   - (subseq seq start end)))
149   - (vector
150   - (loop with end = (or end (length seq))
151   - for i from start below end
152   - do (,name (aref seq i) stream)
153   - finally (return seq))))) into forms
  145 + element-type big-endian-p)
  146 + (write-sequence-with-writer seq stream start end #',name)) into forms
154 147 if readp
155 148 collect `(defun ,(intern (format nil "READ-IEEE-~A/~:[LE~;BE~]-INTO-SEQUENCE"
156 149 float-type big-endian-p))
157 150 (seq stream &key (start 0) end)
158 151 ,(format-docstring "Destructively modify SEQ by replacing the elements of SEQ between START and END with elements read from STREAM. Each element is a ~A read in ~:[little~;big~]-endian byte order. SEQ may be either a vector or a list. STREAM must have na element type of (UNSIGNED-BYTE 8)."
159   - element-type)
160   - (etypecase seq
161   - (list (read-into-list* stream seq start end
162   - ,n-bytes #',single-fun))
163   - (vector
164   - (let ((end (or end (length seq))))
165   - (read-into-vector* stream seq start end
166   - ,n-bytes #',single-fun))))) into forms
  152 + element-type big-endian-p)
  153 + (read-into-sequence seq stream start end ,n-bytes #',single-fun)) into forms
167 154 finally (return `(progn ,@forms)))

0 comments on commit 535f975

Please sign in to comment.
Something went wrong with that request. Please try again.