-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray.scm
188 lines (147 loc) · 5.66 KB
/
array.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
;; arrays
;; guile version
;; This file is part of Scheme+
;; Copyright 2021-2023 Damien MATTEI
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;; TODO : make a version vector with resizable arrays using classes
;; cf: https://www.gnu.org/software/guile/manual/html_node/GOOPS.html
(define-module (array)
#:use-module (for_next_step)
#:export (make-array-2d
array-2d-ref
array-2d-set!
create-vector-2d
negative-vector-index
function-array-n-dim-ref
function-array-n-dim-set!
display-array-2d
dv-2d
funct-array-2d-set!
funct-array-2d-ref
array-ref-set!
srfi25-array-set!))
;; the value v should be put before in a let to avoid multiple evaluation after macro expand
(define-syntax make-array-2d
(syntax-rules ()
((_ lin col) (let* ((array (make-vector lin)))
(for-basic (i 0 (- lin 1))
(vector-set! array i (make-vector col)))
array))
((_ lin col v) (let* ((array (make-vector lin)))
(for-basic (i 0 (- lin 1))
(vector-set! array i (make-vector col v)))
array))
;; error :same form as previous?
;; ((_ array sx sy) (begin
;; (set! (quote array) (make-vector sy))
;; (for-basic (i 0 (- sy 1))
;; (vector-set! (quote array) i (make-vector sx)))))
((_ array lin col v) (begin
(set! (quote array) (make-vector lin))
(for-basic (i 0 (- lin 1))
(vector-set! (quote array) i (make-vector col v)))))))
;; order of indexes match Matrix and arrays conventions
(define-syntax array-2d-ref
(syntax-rules ()
((_ array lin col) (vector-ref (vector-ref array lin) col))))
(define-syntax array-2d-set!
(syntax-rules ()
((_ array lin col val) (vector-set! (vector-ref array lin) col val))))
;; create a vector of line and column with a function
;; (define (create-vector-2d fct lin col)
;; {v <+ (make-vector lin)}
;; ;;(display "ok") (newline)
;; (for ({l <+ 0} {l < lin} {l <- l + 1})
;; {v[l] <- (make-vector col)}
;; (for ({c <+ 0} {c < col} {c <- c + 1})
;; {v[l][c] <- (fct l c)}))
;; v)
;; create a vector (or array) of line and column with a function
(define (create-vector-2d fct lin col)
(define v (make-vector lin))
(for ((define l 0) (< l lin) (set! l (+ l 1)))
(vector-set! v l (make-vector col))
(for ((define c 0) (< c col) (set! c (+ c 1)))
(array-2d-set! v l c (fct l c))))
v)
;; scheme@(guile-user)> (define arr (make-array-2d 10 7 0))
;; scheme@(guile-user)> (array-n-dim-ref arr 4 3)
;; 0
;; scheme@(guile-user)> (array-n-dim-set! arr 7 4 3)
;; scheme@(guile-user)> (array-n-dim-ref arr 4 3)
;; 7
;; (define-syntax array-n-dim-ref
;; (syntax-rules ()
;; ((_ array x) (vector-ref array x))
;; ((_ array x y ...) (vector-ref (array-n-dim-ref array y ...) x))))
(define (negative-vector-index index v)
(if (< index 0)
(+ (vector-length v) index)
index))
;; this one is used by array.scm
(define (function-array-n-dim-ref array L-reversed-indexes)
;;(display L-reversed-indexes) (newline)
(if (= 1 (length L-reversed-indexes))
(vector-ref array (negative-vector-index (car L-reversed-indexes) ;; compatible with negative indexes
array))
(vector-ref (function-array-n-dim-ref array (cdr L-reversed-indexes))
(negative-vector-index (car L-reversed-indexes)
array))))
;; (define-syntax array-n-dim-set!
;; (syntax-rules ()
;; ((_ array val x) (vector-set! array x val))
;; ((_ array val x y ...) (vector-set! (array-n-dim-ref array y ...) x val))))
(define (function-array-n-dim-set! array val L-reversed-indexes)
(if (= 1 (length L-reversed-indexes))
(vector-set! array
(negative-vector-index (car L-reversed-indexes)
array)
val)
(vector-set! (function-array-n-dim-ref array (cdr L-reversed-indexes))
(negative-vector-index (car L-reversed-indexes)
array)
val)))
(define-syntax display-array-2d
(syntax-rules ()
((_ array)
(for-basic (y 0 (- (vector-length array) 1))
(display (vector-ref array y))
(newline)))))
;; > (define _quai 34)
;; > (dv _quai)
;; _quai = 34
(define-syntax dv-2d
(syntax-rules ()
((_ var) (begin
;;(display (symbol->string (quote var)))
(display (quote var))
(display " = ")(newline)
(display-array-2d var)
(newline)))))
;; function ? to be used with map
;; TODO: make n dimension versions recursive with variable number of parameters
(define (funct-array-2d-set! array x y val) (vector-set! (vector-ref array y) x val))
(define (funct-array-2d-ref array x y) (vector-ref (vector-ref array y) x))
;; scheme@(guile-user)> (array-ref-set! dyna 7 3 4)
;; $4 = 7
;; scheme@(guile-user)> (array-ref dyna 3 4)
;; $5 = 7
(define-syntax array-ref-set!
(syntax-rules ()
((_ array expr x y) (let ((v expr))
(array-set! array v x y)
v))))
;; this use srfi 25 syntax to be compatible with the non srfi 25 syntax of guile's arrays
;; obj argument not at the same place in srfi 25 and guile's arrays
(define-syntax srfi25-array-set!
(syntax-rules ()
((_ array index ... obj) (array-set! array obj index ...))))