This repository was archived by the owner on Feb 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcore.jl
297 lines (291 loc) · 8.12 KB
/
core.jl
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
using Base.Test
using ArrayViewsAPL
A = reshape(float64(1:300), 5, 5, 3, 4)
S = subview(A, 1:2, 1:2, 1:2, 1:2)
@test isa(S, View{Float64, 4})
@test size(S) == (2,2,2,2)
@test strides(S) == (1,5,25,75)
S = sliceview(A, 1:2, 1:2, 1:2, 1:2)
@test isa(S, View{Float64, 4})
@test size(S) == (2,2,2,2)
@test strides(S) == (1,5,25,75)
S = subview(A, 1:2, 1:2, 1:2, 2)
@test isa(S, View{Float64, 3, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},UnitRange{Int},Int)})
@test size(S) == (2,2,2)
@test strides(S) == (1,5,25)
S = sliceview(A, 1:2, 1:2, 1:2, 2)
@test isa(S, View{Float64, 3, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},UnitRange{Int},Int)})
@test size(S) == (2,2,2)
@test strides(S) == (1,5,25)
S = subview(A, 1:2, 1:2, 2, 1:2)
@test isa(S, View{Float64, 4, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},UnitRange{Int},UnitRange{Int})})
@test size(S) == (2,2,1,2)
@test strides(S) == (1,5,25,75)
S = sliceview(A, 1:2, 1:2, 2, 1:2)
@test isa(S, View{Float64, 3, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},Int,UnitRange{Int})})
@test size(S) == (2,2,2)
@test strides(S) == (1,5,75)
P = S
S = subview(P, 1:2, 1:2, 1:2)
@test isa(S, View{Float64, 3, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},Int,UnitRange{Int})})
@test size(S) == (2,2,2)
@test strides(S) == (1,5,75)
S = sliceview(P, 1:2, 1:2, 1:2)
@test isa(S, View{Float64, 3, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},Int,UnitRange{Int})})
@test size(S) == (2,2,2)
@test strides(S) == (1,5,75)
S = subview(P, 1:2, 1:2, 2)
@test isa(S, View{Float64, 2, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},Int,Int)})
@test size(S) == (2,2)
@test strides(S) == (1,5)
S = sliceview(P, 1:2, 1:2, 2)
@test isa(S, View{Float64, 2, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},Int,Int)})
@test size(S) == (2,2)
@test strides(S) == (1,5)
S = subview(P, 1:2, 2, 1:2)
@test isa(S, View{Float64, 3, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},Int,UnitRange{Int})})
@test size(S) == (2,1,2)
@test strides(S) == (1,5,75)
S = sliceview(P, 1:2, 2, 1:2)
@test isa(S, View{Float64, 2, Array{Float64,4}, (UnitRange{Int},Int,Int,UnitRange{Int})})
@test size(S) == (2,2)
@test strides(S) == (1,75)
S = subview(P, 2, 1:2, 1:2)
@test isa(S, View{Float64, 3, Array{Float64,4}, (UnitRange{Int},UnitRange{Int},Int,UnitRange{Int})})
@test size(S) == (1,2,2)
@test strides(S) == (1,5,75)
S = sliceview(P, 2, 1:2, 1:2)
@test isa(S, View{Float64, 2, Array{Float64,4}, (Int,UnitRange{Int},Int,UnitRange{Int})})
@test size(S) == (2,2)
@test strides(S) == (5,75)
## 2d
# Whole-array views
A = reshape(1:24, 6, 4)
@test typeof(A[1:2,1:2]) == Array{Int, 2} # so we won't be fooled when this changes
B = subview(A, 1:6, 1:4)
@test B[2] == A[2]
@test B[3,1] == A[3,1]
@test B[4,2,1] == A[4,2]
@test B == A
@test strides(B) == (1,6)
@test pointer(B) == pointer(A)
B = sliceview(A, 1:6, 1:4)
@test B == A
@test B[2] == A[2]
@test B[3,1] == A[3,1]
@test B[4,2,1] == A[4,2]
@test pointer(B) == pointer(A)
B = subview(A, :, :)
@test B == A
B = sliceview(A, :, :)
@test B == A
# Views with extra dimensions
B = subview(A, 1:6, 1:4, 1)
@test ndims(B) == 2
@test size(B) == (6,4)
@test strides(B) == (1,6)
@test B[7] == A[7]
@test B[6,4] == A[6,4]
@test B[3,3,1] == A[3,3]
@test B[4,2,1,1] == A[4,2]
@test B == A
B = sliceview(A, 1:6, 1:4, 1)
@test ndims(B) == 2
@test size(B) == (6,4)
@test strides(B) == (1,6)
@test B[7] == A[7]
@test B[6,4] == A[6,4]
@test B[3,3,1] == A[3,3]
@test B[4,2,1,1] == A[4,2]
@test B == A
B = subview(A, 1:6, 1:4, 2)
@test_throws BoundsError B[1,1]
@test_throws BoundsError B[1]
B = sliceview(A, 1:6, 1:4, 2)
@test_throws BoundsError B[1,1]
@test_throws BoundsError B[1]
# Partial views
B = subview(A, 2:2:6, 2)
@test ndims(B) == 1
@test strides(B) == (2,)
@test pointer(B) == pointer(A)+7*sizeof(Int)
@test B == [8:2:12]
@test B[1] == 8
@test B[1,1] == 8
@test B[3] == 12
@test B[3,1] == 12
B = sliceview(A, 2:2:6, 2)
@test ndims(B) == 1
@test pointer(B) == pointer(A)+7*sizeof(Int)
@test B == [8:2:12]
@test B[1] == 8
@test B[1,1] == 8
@test B[3] == 12
@test B[3,1] == 12
B = subview(A, 2, 2:4)
@test ndims(B) == 2
@test strides(B) == (1,6)
@test pointer(B) == pointer(A)+7*sizeof(Int)
@test B == A[2,2:4]
@test B[1] == 8
@test B[1,1] == 8
@test B[1,1,1] == 8
@test B[3] == 20
@test B[1,3] == 20
@test B[1,3,1] == 20
B = sliceview(A, 2, 2:4)
@test ndims(B) == 1
@test strides(B) == (6,)
@test pointer(B) == pointer(A)+7*sizeof(Int)
@test B == squeeze(A[2,2:4], 1)
@test B[1] == 8
@test B[1,1] == 8
@test B[1,1,1] == 8
@test B[3] == 20
@test B[3,1] == 20
# Views created with fewer dimensions (linear indexing)
B = subview(A, 5:13)
@test ndims(B) == 1
@test strides(B) == (1,)
@test pointer(B) == pointer(A) + 4*sizeof(Int)
@test B == [5:13]
@test B[1] == 5
@test B[9] == 13
@test B[2,1] == 6
@test B[8,1] == 12
B = sliceview(A, 5:13)
@test ndims(B) == 1
@test pointer(B) == pointer(A) + 4*sizeof(Int)
@test B == [5:13]
@test B[1] == 5
@test B[9] == 13
@test B[2,1] == 6
@test B[8,1] == 12
B = subview(A, 1:3:6, 2:2:4)
@test pointer(B) == pointer(A) + 6*sizeof(Int)
@test size(B) == (2,2)
@test strides(B) == (3,12)
@test B[1] == 7
@test B[3] == 19
@test B[2,1] == 10
@test B[2,2] == 22
@test B[1,2,1] == 19
# Flipped axes
B = subview(A, 5:-1:3, 1:4)
@test size(B) == (3,4)
@test strides(B) == (-1,6)
@test pointer(B) == pointer(A) + 4*sizeof(Int)
@test B[1,1] == A[5,1]
@test B[2] == A[4,1]
@test B[2,3,1] == A[4,3]
B = subview(A, 5:-1:3, 4:-2:1)
@test size(B) == (3,2)
@test strides(B) == (-1,-12)
@test pointer(B) == pointer(A) + 22*sizeof(Int)
@test B[1] == 23
@test B[5] == 10
@test B[3,1] == 21
@test B[2,2] == 10
@test B[3,2,1] == 9
# Views with Vector{Int}
B = subview(A, [1,3,4], 1:2:4)
@test B[2,2] == A[3,3]
@test_throws ErrorException strides(B)
@test_throws MethodError pointer(B)
I1 = 2:2:8
I2 = 5:3:12
dims = (13,) # could supply a 2nd size, but it's not used so why bother
I12 = [54,56,58,60,93,95,97,99,132,134,136,138]
L = length(I12)
ind = ArrayViewsAPL.merge_indexes((I1, I2), dims, 1:L)
@test ind == I12
ind = ArrayViewsAPL.merge_indexes((I1, I2), dims, 3:7)
@test ind == I12[3:7]
ind = ArrayViewsAPL.merge_indexes_div((I1, I2), dims, 1:L)
@test ind == I12
ind = ArrayViewsAPL.merge_indexes_div((I1, I2), dims, 3:7)
@test ind == I12[3:7]
## 2d, views-of-views
# Whole-array views
B = subview(A, :, :)
C = subview(B, :, :)
@test C == A
C = sliceview(B, :, :)
@test C == A
# with extra indexes
C = subview(B, 1:6, 1:4, 1)
@test ndims(C) == 2
@test size(C) == (6,4)
@test strides(C) == (1,6)
@test pointer(C) == pointer(A)
@test C[2] == A[2]
@test C[3,2] == A[3,2]
@test C[4,3,1] == A[4,3]
C = sliceview(B, 1:6, 1:4, 1)
@test ndims(C) == 2
@test size(C) == (6,4)
@test strides(C) == (1,6)
@test pointer(C) == pointer(A)
@test C[2] == A[2]
@test C[3,2] == A[3,2]
@test C[4,3,1] == A[4,3]
C = subview(B, 1:6, 1:4, 2)
@test_throws BoundsError C[1]
C = subview(B, 1:6, 1:4, 1:1)
@test ndims(C) == 3
@test size(C) == (6,4,1)
@test strides(C) == (1,6,24)
@test pointer(C) == pointer(A)
@test C[2] == A[2]
@test C[3,2] == A[3,2]
@test C[4,3,1] == A[4,3]
C = sliceview(B, 1:6, 1:4, 1:1)
@test ndims(C) == 3
@test size(C) == (6,4,1)
@test strides(C) == (1,6,24)
@test pointer(C) == pointer(A)
@test C[2] == A[2]
@test C[3,2] == A[3,2]
@test C[4,3,1] == A[4,3]
# Views of partial views
# For this, we need a bigger A
A = reshape(1:13*14, 13, 14)
I1, I2 = 3:2:13, 4:3:11
B = subview(A, I1, I2)
As = A[I1, I2]
@test B == As
C = subview(B, 1:6, 1:3)
@test C == As
@test strides(C) == (2,13*3)
C = sliceview(B, 1:6, 1:3)
@test C == As
C = subview(B, 2:2:5, 2:3)
@test C == As[2:2:5, 2:3]
@test strides(C) == (4,13*3)
C = sliceview(B, 2:2:5, 2:3)
@test C == As[2:2:5, 2:3]
C = subview(B, 3:5, 1:2:3)
@test C == As[3:5, 1:2:3]
@test strides(C) == (2,13*6)
# With fewer indexes (linear indexing)
C = subview(B, 2:2:8)
@test ndims(C) == 1
@test size(C) == (4,)
@test_throws ErrorException strides(C)
@test C[1] == As[2]
@test C[2,1] == As[4]
C = sliceview(B, 2:2:8)
@test ndims(C) == 1
@test size(C) == (4,)
@test_throws ErrorException strides(C)
@test C[1] == As[2]
@test C[2,1] == As[4]
# with extra indexes
C = sliceview(B, 1:6, 3, 1:1)
@test ndims(C) == 2
@test size(C) == (6,1)
@test strides(C) == (2,13*14)
@test C[2] == As[2,3]
@test C[4,1] == As[4,3]
@test C[3,1,1] == As[3,3]
@test unsafe_load(pointer(C)) == As[1,3]