24677
|
1 |
"{ Encoding: utf8 }"
|
|
2 |
|
25121
|
3 |
"
|
|
4 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
5 |
All Rights Reserved
|
|
6 |
|
|
7 |
This software is furnished under a license and may be used
|
|
8 |
only in accordance with the terms of that license and with the
|
|
9 |
inclusion of the above copyright notice. This software may not
|
|
10 |
be provided or otherwise made available to, or used by, any
|
|
11 |
other person. No title to or ownership of the software is
|
|
12 |
hereby transferred.
|
|
13 |
"
|
24575
|
14 |
"{ Package: 'stx:libbasic' }"
|
|
15 |
|
|
16 |
"{ NameSpace: Smalltalk }"
|
|
17 |
|
|
18 |
AbstractMultidimensionalArray variableSubclass:#MatrixAccessor
|
|
19 |
instanceVariableNames:'array dimensions'
|
|
20 |
classVariableNames:''
|
|
21 |
poolDictionaries:''
|
|
22 |
category:'Collections-MultiDimensional'
|
|
23 |
!
|
|
24 |
|
|
25 |
!MatrixAccessor class methodsFor:'documentation'!
|
|
26 |
|
25121
|
27 |
copyright
|
|
28 |
"
|
|
29 |
COPYRIGHT (c) 2018 by eXept Software AG
|
|
30 |
All Rights Reserved
|
|
31 |
|
|
32 |
This software is furnished under a license and may be used
|
|
33 |
only in accordance with the terms of that license and with the
|
|
34 |
inclusion of the above copyright notice. This software may not
|
|
35 |
be provided or otherwise made available to, or used by, any
|
|
36 |
other person. No title to or ownership of the software is
|
|
37 |
hereby transferred.
|
|
38 |
"
|
|
39 |
!
|
|
40 |
|
24575
|
41 |
documentation
|
|
42 |
"
|
24677
|
43 |
Matrix-Access for arbitrary sequenceable collections.
|
24678
|
44 |
Allows arbitrary collections which offer access by an integer index
|
|
45 |
to function as matrices.
|
24575
|
46 |
|
|
47 |
[author:]
|
|
48 |
Claus Gittinger (cg@sinir)
|
|
49 |
|
|
50 |
[instance variables:]
|
|
51 |
|
|
52 |
[class variables:]
|
|
53 |
|
|
54 |
[see also:]
|
|
55 |
Matrix
|
|
56 |
"
|
|
57 |
!
|
|
58 |
|
|
59 |
examples
|
|
60 |
"
|
|
61 |
You have to enable the Parsers arrayIndexingExtension support
|
|
62 |
in order to be able to execute the examples below.
|
|
63 |
|
|
64 |
Parser allowArrayIndexSyntaxExtension:true
|
|
65 |
[exBegin]
|
|
66 |
|m|
|
|
67 |
|
|
68 |
m := FloatArray[3,3].
|
|
69 |
m [2,1] := 1.
|
|
70 |
m [2,2] := 2.
|
|
71 |
m [2,3] := 3.
|
|
72 |
m
|
|
73 |
[exEnd]
|
|
74 |
"
|
|
75 |
! !
|
|
76 |
|
|
77 |
!MatrixAccessor class methodsFor:'instance creation'!
|
|
78 |
|
|
79 |
collection:coll dimensions:dimensions
|
|
80 |
^ self new collection:coll dimensions:dimensions
|
|
81 |
! !
|
|
82 |
|
|
83 |
!MatrixAccessor methodsFor:'accessing'!
|
|
84 |
|
|
85 |
_at:index
|
|
86 |
"this is a synthetic selector, generated by the compiler,
|
|
87 |
if a construct of the form expr[idx...] is parsed.
|
|
88 |
I.e.
|
|
89 |
foo[n]
|
|
90 |
generates
|
|
91 |
foo _at: n
|
|
92 |
"
|
|
93 |
dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
94 |
^ array at:index.
|
|
95 |
!
|
|
96 |
|
|
97 |
_at:index1 at:index2
|
|
98 |
"this is a synthetic selector, generated by the compiler,
|
|
99 |
if a construct of the form expr[idx...] is parsed.
|
|
100 |
I.e.
|
24587
|
101 |
foo[n][m]
|
24575
|
102 |
generates
|
|
103 |
foo _at:n at:m
|
|
104 |
"
|
|
105 |
|idx|
|
|
106 |
|
|
107 |
dimensions size ~~ 2 ifTrue:[self dimensionError].
|
|
108 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
109 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
110 |
|
|
111 |
idx := ((index1-1) * (dimensions at:2)).
|
|
112 |
idx := idx + index2.
|
|
113 |
^ array at:idx.
|
|
114 |
!
|
|
115 |
|
|
116 |
_at:index1 at:index2 at:index3
|
|
117 |
"this is a synthetic selector, generated by the compiler,
|
|
118 |
if a construct of the form expr[idx...] is parsed.
|
|
119 |
I.e.
|
24587
|
120 |
foo[n][m][o]
|
24575
|
121 |
generates
|
|
122 |
foo _at:n at:m at:o
|
|
123 |
"
|
|
124 |
|idx|
|
|
125 |
|
|
126 |
dimensions size ~~ 3 ifTrue:[self dimensionError].
|
|
127 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
128 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
129 |
(index3 between:1 and:(dimensions at:3)) ifFalse:[self subscriptBoundsError:index3].
|
|
130 |
|
|
131 |
idx := ((((index1-1) * (dimensions at:2)) + (index2-1)) * (dimensions at:3)).
|
|
132 |
idx := idx + index3.
|
|
133 |
^ array at:idx.
|
|
134 |
|
|
135 |
"
|
|
136 |
|m|
|
|
137 |
m := MatrixAccessor new
|
|
138 |
collection:
|
|
139 |
#(
|
|
140 |
111 112 113 114
|
|
141 |
121 122 123 124
|
|
142 |
131 132 133 134
|
|
143 |
|
|
144 |
211 212 213 214
|
|
145 |
221 222 223 224
|
|
146 |
231 232 233 234 )
|
|
147 |
dimensions:
|
|
148 |
#(2 3 4).
|
|
149 |
|
|
150 |
m[2,1,4]
|
|
151 |
"
|
|
152 |
!
|
|
153 |
|
|
154 |
_at:index1 at:index2 at:index3 put:val
|
|
155 |
"this is a synthetic selector, generated by the compiler,
|
|
156 |
if a construct of the form expr[idx...] := val is parsed.
|
|
157 |
I.e.
|
24587
|
158 |
foo[n][m][o] := val
|
24575
|
159 |
generates
|
|
160 |
foo _at:n at:m at:o put:val
|
|
161 |
"
|
|
162 |
|idx|
|
|
163 |
|
|
164 |
dimensions size ~~ 3 ifTrue:[self dimensionError].
|
|
165 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
166 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
167 |
(index3 between:1 and:(dimensions at:3)) ifFalse:[self subscriptBoundsError:index3].
|
|
168 |
|
|
169 |
idx := ((((index1-1) * (dimensions at:2)) + (index2-1)) * (dimensions at:3)).
|
|
170 |
idx := idx + index3.
|
|
171 |
^ array at:idx put:val.
|
|
172 |
!
|
|
173 |
|
|
174 |
_at:index1 at:index2 put:val
|
|
175 |
"this is a synthetic selector, generated by the compiler,
|
|
176 |
if a construct of the form expr[idx...] := val is parsed.
|
|
177 |
I.e.
|
24587
|
178 |
foo[n][m] := val
|
24575
|
179 |
generates
|
|
180 |
foo _at:n at:m put:val
|
|
181 |
"
|
|
182 |
|idx|
|
|
183 |
|
|
184 |
dimensions size ~~ 2 ifTrue:[self dimensionError].
|
|
185 |
(index1 between:1 and:(dimensions at:1)) ifFalse:[self subscriptBoundsError:index1].
|
|
186 |
(index2 between:1 and:(dimensions at:2)) ifFalse:[self subscriptBoundsError:index2].
|
|
187 |
|
|
188 |
idx := ((index1-1) * (dimensions at:2)).
|
|
189 |
idx := idx + index2.
|
|
190 |
^ array at:idx put:val.
|
|
191 |
!
|
|
192 |
|
|
193 |
_at:index put:val
|
|
194 |
"this is a synthetic selector, generated by the compiler,
|
|
195 |
if a construct of the form expr[idx...] := val is parsed.
|
|
196 |
I.e.
|
|
197 |
foo[n] := val
|
|
198 |
generates
|
|
199 |
foo _at:n put:val
|
|
200 |
"
|
|
201 |
dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
202 |
|
|
203 |
^ array at:index put:val.
|
|
204 |
!
|
|
205 |
|
|
206 |
at:index
|
|
207 |
"/ dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
208 |
^ array at:index.
|
|
209 |
!
|
|
210 |
|
|
211 |
at:index put:val
|
|
212 |
"/ dimensions size ~~ 1 ifTrue:[self dimensionError].
|
|
213 |
^ array at:index put:val.
|
|
214 |
!
|
|
215 |
|
24589
|
216 |
dimensions
|
|
217 |
^ dimensions
|
|
218 |
|
|
219 |
!
|
|
220 |
|
24575
|
221 |
size
|
|
222 |
^ dimensions inject:1 into:[:szSoFar :thisDim | szSoFar * thisDim].
|
|
223 |
! !
|
|
224 |
|
|
225 |
!MatrixAccessor methodsFor:'private-initialization'!
|
|
226 |
|
|
227 |
collection:arrayArg dimensions:dimensionsArg
|
|
228 |
array := arrayArg.
|
|
229 |
dimensions := dimensionsArg.
|
|
230 |
! !
|
|
231 |
|
24590
|
232 |
!MatrixAccessor methodsFor:'queries'!
|
|
233 |
|
|
234 |
isSquare
|
|
235 |
^ dimensions size == 2
|
|
236 |
and:[(dimensions at:1) == (dimensions at:2)]
|
|
237 |
! !
|
|
238 |
|
24575
|
239 |
!MatrixAccessor class methodsFor:'documentation'!
|
|
240 |
|
|
241 |
version_CVS
|
|
242 |
^ '$Header$'
|
|
243 |
! !
|
|
244 |
|