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