ea7137cd177c40a59d2088b348f3312ddf534a0e
[bpt/guile.git] / srfi / srfi-4.scm
1 ;;;; srfi-4.scm --- Homogeneous numeric vector datatypes.
2 ;;;;
3 ;;;; Copyright (C) 2001 Free Software Foundation, Inc.
4 ;;;;
5 ;;;; This program is free software; you can redistribute it and/or
6 ;;;; modify it under the terms of the GNU General Public License as
7 ;;;; published by the Free Software Foundation; either version 2, or
8 ;;;; (at your option) any later version.
9 ;;;;
10 ;;;; This program is distributed in the hope that it will be useful,
11 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;;; General Public License for more details.
14 ;;;;
15 ;;;; You should have received a copy of the GNU General Public License
16 ;;;; along with this software; see the file COPYING. If not, write to
17 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 ;;;; Boston, MA 02111-1307 USA
19 ;;;;
20 ;;;; As a special exception, the Free Software Foundation gives permission
21 ;;;; for additional uses of the text contained in its release of GUILE.
22 ;;;;
23 ;;;; The exception is that, if you link the GUILE library with other files
24 ;;;; to produce an executable, this does not by itself cause the
25 ;;;; resulting executable to be covered by the GNU General Public License.
26 ;;;; Your use of that executable is in no way restricted on account of
27 ;;;; linking the GUILE library code into it.
28 ;;;;
29 ;;;; This exception does not however invalidate any other reasons why
30 ;;;; the executable file might be covered by the GNU General Public License.
31 ;;;;
32 ;;;; This exception applies only to the code released by the
33 ;;;; Free Software Foundation under the name GUILE. If you copy
34 ;;;; code from other Free Software Foundation releases into a copy of
35 ;;;; GUILE, as the General Public License permits, the exception does
36 ;;;; not apply to the code that you add in this way. To avoid misleading
37 ;;;; anyone as to the status of such modified files, you must delete
38 ;;;; this exception notice from them.
39 ;;;;
40 ;;;; If you write modifications of your own for GUILE, it is your choice
41 ;;;; whether to permit this exception to apply to your modifications.
42 ;;;; If you do not wish that, delete this exception notice.
43
44 ;;; Commentary:
45
46 ;;; This module implements homogeneous numeric vectors as defined in SRFI-4.
47
48 ;;; Code:
49
50 ;;; Author: Martin Grabmueller <mgrabmue@cs.tu-berlin.de>
51
52 (define-module (srfi srfi-4))
53
54 (export
55 ;;; Unsigned 8-bit vectors.
56 u8vector? make-u8vector u8vector u8vector-length u8vector-ref
57 u8vector-set! u8vector->list list->u8vector
58
59 ;;; Signed 8-bit vectors.
60 s8vector? make-s8vector s8vector s8vector-length s8vector-ref
61 s8vector-set! s8vector->list list->s8vector
62
63 ;;; Unsigned 16-bit vectors.
64 u16vector? make-u16vector u16vector u16vector-length u16vector-ref
65 u16vector-set! u16vector->list list->u16vector
66
67 ;;; Signed 16-bit vectors.
68 s16vector? make-s16vector s16vector s16vector-length s16vector-ref
69 s16vector-set! s16vector->list list->s16vector
70
71 ;;; Unsigned 32-bit vectors.
72 u32vector? make-u32vector u32vector u32vector-length u32vector-ref
73 u32vector-set! u32vector->list list->u32vector
74
75 ;;; Signed 32-bit vectors.
76 s32vector? make-s32vector s32vector s32vector-length s32vector-ref
77 s32vector-set! s32vector->list list->s32vector
78
79 ;;; Unsigned 64-bit vectors.
80 u64vector? make-u64vector u64vector u64vector-length u64vector-ref
81 u64vector-set! u64vector->list list->u64vector
82
83 ;;; Signed 64-bit vectors.
84 s64vector? make-s64vector s64vector s64vector-length s64vector-ref
85 s64vector-set! s64vector->list list->s64vector
86
87 ;;; 32-bit floating point vectors.
88 f32vector? make-f32vector f32vector f32vector-length f32vector-ref
89 f32vector-set! f32vector->list list->f32vector
90
91 ;;; 64-bit floating point vectors.
92 f64vector? make-f64vector f64vector f64vector-length f64vector-ref
93 f64vector-set! f64vector->list list->f64vector
94 )
95
96
97 ;; Make 'srfi-4 available as a feature identifiere to `cond-expand'.
98 ;;
99 (cond-expand-provide (current-module) '(srfi-4))
100
101
102 ;; Load the compiled primitives from the shared library.
103 ;;
104 (load-extension "libguile-srfi-srfi-4" "scm_init_srfi_4")
105
106
107 ;; Reader extension for #f32() and #f64() vectors.
108 ;;
109 (define (hash-f char port)
110 (if (or (char=? (peek-char port) #\3)
111 (char=? (peek-char port) #\6))
112 (let* ((obj (read port)))
113 (if (number? obj)
114 (cond ((= obj 32)
115 (let ((l (read port)))
116 (if (list? l)
117 (list->f32vector l)
118 (error "syntax error in #f32() vector literal"))))
119 ((= obj 64)
120 (let ((l (read port)))
121 (if (list? l)
122 (list->f64vector l)
123 (error "syntax error in #f64() vector literal"))))
124 (else
125 (error "syntax error in #f...() vector literal")))
126 (error "syntax error in #f...() vector literal")))
127 #f))
128
129
130 ;; Reader extension for #u8(), #u16(), #u32() and #u64() vectors.
131 ;;
132 (define (hash-u char port)
133 (if (or (char=? (peek-char port) #\8)
134 (char=? (peek-char port) #\1)
135 (char=? (peek-char port) #\3)
136 (char=? (peek-char port) #\6))
137 (let ((obj (read port)))
138 (cond ((= obj 8)
139 (let ((l (read port)))
140 (if (list? l)
141 (list->u8vector l)
142 (error "syntax error in #u8() vector literal"))))
143 ((= obj 16)
144 (let ((l (read port)))
145 (if (list? l)
146 (list->u16vector l)
147 (error "syntax error in #u16() vector literal"))))
148 ((= obj 32)
149 (let ((l (read port)))
150 (if (list? l)
151 (list->u32vector l)
152 (error "syntax error in #u32() vector literal"))))
153 ((= obj 64)
154 (let ((l (read port)))
155 (if (list? l)
156 (list->u64vector l)
157 (error "syntax error in #u64() vector literal"))))
158 (else
159 (error "syntax error in #u...() vector literal"))))
160 (error "syntax error in #u...() vector literal")))
161
162
163 ;; Reader extension for #s8(), #s16(), #s32() and #s64() vectors.
164 ;;
165 (define (hash-s char port)
166 (if (or (char=? (peek-char port) #\8)
167 (char=? (peek-char port) #\1)
168 (char=? (peek-char port) #\3)
169 (char=? (peek-char port) #\6))
170 (let ((obj (read port)))
171 (cond ((= obj 8)
172 (let ((l (read port)))
173 (if (list? l)
174 (list->s8vector l)
175 (error "syntax error in #s8() vector literal"))))
176 ((= obj 16)
177 (let ((l (read port)))
178 (if (list? l)
179 (list->s16vector l)
180 (error "syntax error in #s16() vector literal"))))
181 ((= obj 32)
182 (let ((l (read port)))
183 (if (list? l)
184 (list->s32vector l)
185 (error "syntax error in #s32() vector literal"))))
186 ((= obj 64)
187 (let ((l (read port)))
188 (if (list? l)
189 (list->s64vector l)
190 (error "syntax error in #s64() vector literal"))))
191 (else
192 (error "syntax error in #s...() vector literal"))))
193 (error "syntax error in #s...() vector literal")))
194
195
196 ;; Install the hash extensions.
197 ;;
198 (read-hash-extend #\f hash-f)
199 (read-hash-extend #\u hash-u)
200 (read-hash-extend #\s hash-s)