-
Notifications
You must be signed in to change notification settings - Fork 145
/
indexing.jl
822 lines (689 loc) · 24.7 KB
/
indexing.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
######################
## Linear Indexing ##
######################
# What to do about @boundscheck and @inbounds? It's worse sometimes than @inline, for tuples...
@generated function getindex{SA<:StaticArray, S}(a::SA, inds::NTuple{S,Integer})
newtype = similar_type(SA, (S,))
exprs = [:(a[inds[$i]]) for i = 1:S]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
# Static Vector indexing into AbstractArrays
@generated function getindex{T, I <: Integer}(
a::AbstractArray{T}, inds::StaticVector{I}
)
S = length(inds)
newtype = similar_type(inds, T, (S,))
exprs = [:(a[inds[$i]]) for i = 1:S]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
# Convert to StaticArrays using tuples
# TODO think about bounds checks here.
@generated function getindex{T, I <: Integer}(
m::AbstractArray{T}, inds1::StaticVector{I}, i2::Integer
)
S = length(inds1)
newtype = similar_type(inds1, T, (S,)) # drop singular dimension like in base
exprs = [:(m[inds1[$j], i2]) for j = 1:S]
return Expr(:call, newtype, Expr(:tuple, exprs...))
end
@generated function getindex{T, I <: Integer}(
m::AbstractArray{T}, i1::Integer, inds2::StaticVector{I}
)
S = length(inds2)
newtype = similar_type(inds2, T, (S,))
exprs = [:(m[i1, inds2[$j]]) for j = 1:S]
return Expr(:call, newtype, Expr(:tuple, exprs...))
end
@generated function getindex{I1 <: Integer,I2 <: Integer, T}(
m::AbstractArray{T}, inds1::StaticVector{I1}, inds2::StaticVector{I2}
)
S1, S2 = length(inds1), lengths(inds2)
exprs = [:(m[inds1[$j1], inds2[$j2]]) for j1 = 1:S1, j2 = 1:S2]
return Expr(:call, SMatrix{S1, S2, T}, Expr(:tuple, exprs...)) # I guess SMatrix should be fine?
end
@generated function getindex{SA<:StaticArray}(a::SA, ::Colon)
if SA <: StaticVector && a == similar_type(SA)
return quote
$(Expr(:meta, :inline))
a
end
else
l = length(SA)
inds = 1:l
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
$(Expr(:call, :getindex, :a, Expr(:tuple, inds...)))
end
end
end
# MAYBE: fixed-size indexing with bools? They would have to be Val's...
# Size-indeterminate linear indexing seems to be provided by AbstractArray,
# returning a `Vector`.
# We seem to get an error from Base's implementation with UnitRange
#=
function Base.getindex(v::StaticVector, r::UnitRange)
l = length(r)
out = similar(v, (l,))
for i in r
out[i] = v[i]
end
return out
end
=#
# Same for setindex!
@generated function setindex!{SA <: StaticArray, I <: Integer}(
a::SA, vals, inds::Union{Tuple{Vararg{I}}, StaticVector{I}}
)
S = inds <: Tuple ? length(inds.parameters) : length(inds)
exprs = [:(a[inds[$i]] = vals[$i]) for i = 1:S]
if vals <: StaticArray
if length(vals) != S # We should be able to annotate that the RHS of the above exprs is @inbounds, but not the LHS...
error("Dimension mismatch")
end
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
$(Expr(:block, exprs...))
return :vals
end
else
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
@boundscheck if length(vals) != $S
error("Dimension mismatch")
end
$(Expr(:block, exprs...))
return :vals
end
end
end
# this one for consistency
@generated function setindex!{I <: Integer}(
a::Array, vals::AbstractArray, inds::StaticVector{I}
)
S = inds <: Tuple ? length(inds.parameters) : length(inds)
exprs = [:(a[inds[$i]] = vals[$i]) for i = 1:S]
if vals <: StaticArray
if length(vals) != S
error("Dimension mismatch")
end
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
$(Expr(:block, exprs...))
return :vals
end
else
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
@boundscheck if length(vals) != $S
error("Dimension mismatch")
end
$(Expr(:block, exprs...))
return :vals
end
end
end
@generated function setindex!{SA<:StaticArray}(a::SA, vals, ::Colon)
exprs = [:(a[$i] = vals[$i]) for i = 1:length(SA)]
if vals <: StaticArray
if length(vals) != length(SA)
error("Dimension mismatch")
end
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
$(Expr(:block, exprs...))
return :vals
end
else
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
@boundscheck if length(vals) != $(length(SA))
error("Dimension mismatch")
end
$(Expr(:block, exprs...))
return :vals
end
end
end
###############################
## Two-dimensional Indexing ##
###############################
# Special 2D case to begin with (and possibly good for avoiding splatting penalties?)
# Furthermore, avoids stupidity regarding two-dimensional indexing on 3+ dimensional arrays!
@generated function getindex{SM<:StaticMatrix}(m::SM, i1::Integer, i2::Integer)
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
@boundscheck if (i1 < 1 || i1 > $(size(SM,1)) || i2 < 1 || i2 > $(size(SM,2)))
throw(BoundsError(m, (i1,i2)))
end
@inbounds return m[i1 + $(size(SM,1))*(i2-1)]
end
end
# TODO put bounds checks here, as they should have less overhead here
@generated function getindex{SM<:StaticMatrix, S1, S2}(m::SM, inds1::NTuple{S1,Integer}, inds2::NTuple{S2,Integer})
newtype = similar_type(SM, (S1, S2))
exprs = [:(m[inds1[$i1], inds2[$i2]]) for i1 = 1:S1, i2 = 1:S2]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function getindex{SM<:StaticMatrix, S2}(m::SM, i1::Integer, inds2::NTuple{S2,Integer})
newtype = similar_type(SM, (S2,))
exprs = [:(m[i1, inds2[$i2]]) for i2 = 1:S2]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
@generated function getindex{SM<:StaticMatrix, S1}(m::SM, inds1::NTuple{S1,Integer}, i2::Integer)
newtype = similar_type(SM, (S1,))
exprs = [:(m[inds1[$i1], i2]) for i1 = 1:S1]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
return $(Expr(:call, newtype, Expr(:tuple, exprs...)))
end
end
# TODO: bounds checking?
@generated function setindex!{SM<:StaticMatrix, S1, S2}(m::SM, val, inds1::NTuple{S1,Integer}, inds2::NTuple{S2,Integer})
exprs = [:(m[inds1[$i1], inds2[$i2]] = val[$i1,$i2]) for i1 = 1:S1, i2 = 1:S2]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
$(Expr(:block, exprs...))
return val
end
end
# TODO put bounds checks here, as they should have less overhead here
@generated function getindex{SM<:StaticMatrix}(m::SM, ::Colon, inds2::Union{Integer, Tuple{Vararg{Integer}}})
inds1 = ntuple(identity, size(SM,1))
quote
$(Expr(:meta, :inline, :propagate_inbounds))
m[$inds1, inds2]
end
end
# TODO put bounds checks here, as they should have less overhead here
@generated function getindex{SM<:StaticMatrix}(m::SM, inds1::Union{Integer, Tuple{Vararg{Integer}}}, ::Colon)
inds2 = ntuple(identity, size(SM,2))
quote
$(Expr(:meta, :inline, :propagate_inbounds))
m[inds1, $inds2]
end
end
@generated function getindex{SM<:StaticMatrix}(m::SM, ::Colon, ::Colon)
inds1 = ntuple(identity, size(SM,1))
inds2 = ntuple(identity, size(SM,2))
quote
$(Expr(:meta, :inline))
@inbounds return m[$inds1, $inds2]
end
end
@generated function setindex!{SM<:StaticMatrix, S2}(m::SM, val, i1::Integer, inds2::NTuple{S2,Integer})
newtype = similar_type(SM, (S2,))
exprs = [:(m[i1, inds2[$i2]] = val[$i2]) for i2 = 1:S2]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
return $(Expr(:block, exprs...))
end
end
# TODO expand out tuples to vectors in size-indeterminate cases
# 2D setindex!
@generated function setindex!{SM<:StaticMatrix}(m::SM, val, i1::Integer, i2::Integer)
return quote
$(Expr(:meta, :inline))
@boundscheck if (i1 < 1 || i1 > $(size(SM,1)) || i2 < 1 || i2 > $(size(SM,2)))
throw(BoundsError(m, (i1,i2)))
end
@inbounds return m[i1 + $(size(SM,1))*(i2-1)] = val
end
end
@generated function setindex!{SM<:StaticMatrix, S1}(m::SM, val, inds1::NTuple{S1,Integer}, i2::Integer)
newtype = similar_type(SM, (S1,))
exprs = [:(m[inds1[$i1], i2] = val[$i1]) for i1 = 1:S1]
return quote
$(Expr(:meta, :inline, :propagate_inbounds))
return $(Expr(:block, exprs...))
end
end
# TODO put bounds checks here, as they should have less overhead here
@generated function setindex!{SM<:StaticMatrix}(m::SM, val, ::Colon, inds2::Union{Integer, Tuple{Vararg{Integer}}})
inds1 = ntuple(identity, size(SM,1))
quote
$(Expr(:meta, :inline, :propagate_inbounds))
m[$inds1, inds2] = val
end
end
# TODO put bounds checks here, as they should have less overhead here
@generated function setindex!{SM<:StaticMatrix}(m::SM, val, inds1::Union{Integer, Tuple{Vararg{Integer}}}, ::Colon)
inds2 = ntuple(identity, size(SM,2))
quote
$(Expr(:meta, :inline, :propagate_inbounds))
m[inds1, $inds2] = val
end
end
# Check bounds for val here?
@generated function setindex!{SM<:StaticMatrix}(m::SM, val, ::Colon, ::Colon)
inds1 = ntuple(identity, size(SM,1))
inds2 = ntuple(identity, size(SM,2))
quote
$(Expr(:meta, :inline))
return m[$inds1, $inds2] = val
end
end
#################################
## Multi-dimensional Indexing ##
#################################
# Scalar indexing:
@generated function getindex(a::StaticArray, i1::Integer, i2::Integer, i3::Integer)
@assert ndims(a) <= 3
strides = [1, size(a,1), size(a,1)*size(a,2)]
N = 3
ind_exprs = [:i1, :i2, :i3]
exprs = [j == 1 ? ind_exprs[1] : :( $(strides[j]) * ($(ind_exprs[j])-1) ) for j = 1:N]
ind_expr = Expr(:call, :+, exprs...)
bounds_expr = :(i1 < 1 || i1 > $(size(a,1)) || i2 < 1 || i2 > $(size(a,2)) || i3 < 1 || i3 > $(size(a,3)))
return quote
$(Expr(:meta, :inline))
@boundscheck if $bounds_expr
throw(BoundsError(a, (i1,i2,i3)))
end
@inbounds return a[$ind_expr]
end
end
# TODO check speed (splatting penalty)
@generated function getindex(a::StaticArray, i1::Integer, i2::Integer, i_n::Integer...)
@assert ndims(a) <= 2 + length(i_n)
N = 2 + length(i_n)
strides = ones(Int, N)
for j = 2:N
for k = 1:j - 1
strides[j] *= size(a, k)
end
end
ind_exprs = [:i1, :i2, [:(i_n[$j]) for j = 1:length(i_n)]...]
exprs = [j == 1 ? ind_exprs[1] : :( $(strides[j]) * ($(ind_exprs[j])-1) ) for j = 1:N]
ind_expr = Expr(:call, :+, exprs...)
bounds_expr = :(i1 < 1 || i1 > $(size(a,1)) || i2 < 1 || i2 > $(size(a,2)))
for j = 1:N-2
bound_expr = :($bounds_expr || i_n[$j] < 1 || i_n[$j] > $(size(a,2+j)))
end
return quote
$(Expr(:meta, :inline))
@boundscheck if $bounds_expr
throw(BoundsError(a, (i1,i2,i_n...)))
end
@inbounds return a[$ind_expr]
end
end
@generated function setindex!(a::StaticArray, val, i1::Integer, i2::Integer, i3::Integer)
@assert ndims(a) <= 3
strides = [1, size(a,1), size(a,1)*size(a,2)]
N = 3
ind_exprs = [:i1, :i2, :i3]
exprs = [j == 1 ? ind_exprs[1] : :($(strides[j]) * ($(ind_exprs[j])-1) ) for j = 1:N]
ind_expr = Expr(:call, :+, exprs...)
bounds_expr = :(i1 < 1 || i1 > $(size(a,1)) || i2 < 1 || i2 > $(size(a,2)) || i3 < 1 || i3 > $(size(a,3)))
return quote
$(Expr(:meta, :inline))
@boundscheck if $bounds_expr
throw(BoundsError(a, (i1,i2,3)))
end
@inbounds return a[$ind_expr] = val
end
end
# TODO check speed (splatting penalty)
@generated function setindex!(a::StaticArray, val, i1::Integer, i2::Integer, i_n::Integer...)
@assert ndims(a) <= 2 + length(i_n)
N = 2 + length(i_n)
strides = ones(Int, N)
for j = 2:N
for k = 1:j - 1
strides[j] *= size(a, k)
end
end
ind_exprs = [:i1, :i2, [:(i_n[$j]) for j = 1:length(i_n)]...]
exprs = [j == 1 ? ind_exprs[1] : :( $(strides[j]) * ($(ind_exprs[j])-1) ) for j = 1:N]
ind_expr = Expr(:call, :+, exprs...)
bounds_expr = :(i1 < 1 || i1 > $(size(a,1)) || i2 < 1 || i2 > $(size(a,2)))
for j = 1:N-2
bound_expr = :($bounds_expr || i_n[$j] < 1 || i_n[$j] > $(size(a,2+j)))
end
return quote
$(Expr(:meta, :inline))
@boundscheck if $bounds_expr
throw(BoundsError(a, (i1,i2,i_n...)))
end
@inbounds return a[$ind_expr] = val
end
end
# TODO higher dimensional versions with tuples and :
#=
##############
## Indexing ##
##############
# Indexing with no components
Base.getindex(a::StaticArray) = a.data[1]
# Can index linearly with a scalar, a tuple, or colon (overspecified to ovoid ambiguity problems in julia 0.5)
Base.getindex{Sizes,T,N,D}(a::SArray{Sizes,T,N,D}, i::Int) = a.data[i]
Base.getindex{Sizes,T,N,D}(a::MArray{Sizes,T,N,D}, i::Int) = a.data[i]
@generated function Base.getindex{N}(a::SArray, i::NTuple{N,Int})
newtype = similar_type(a, Val{(N,)})
exprs = ntuple(n -> :(a[i[$n]]), N)
return :($newtype($(Expr(:tuple, exprs...))))
end
@generated function Base.getindex{N}(a::MArray, i::NTuple{N,Int})
newtype = similar_type(a, Val{(N,)})
exprs = ntuple(n -> :(a[i[$n]]), N)
return :($newtype($(Expr(:tuple, exprs...))))
end
Base.getindex(a::SArray, ::Colon) = SVector(a.data)
Base.getindex(a::MArray, ::Colon) = MVector(a.data) # Makes a copy?
# Multidimensional index generalizes the above
@generated function Base.getindex{Sizes,T,N}(a::SArray{Sizes,T,N}, i...)
# Striding lengths
strides = [1, cumprod(collect(Sizes)[1:end-1])...]
# Get the parameters of the new matrix
apl_slicing = VERSION >= v"0.5-"
NewN = 0
NewSizes = Vector{Int}()
OldSizes = Vector{Int}() # Same as NewSizes but includes singleton 1's
at_end = true
is_singleton = trues(N)
for j = length(i):-1:1
if i[j] == Int
unshift!(OldSizes,1)
if !apl_slicing
if !at_end
NewN += 1
unshift!(NewSizes,1)
is_singleton[j] = false
end
end
elseif i[j] <: TupleN{Int}
NewN += 1
unshift!(NewSizes,length(i[j].parameters))
unshift!(OldSizes,length(i[j].parameters))
is_singleton[j] = false
at_end = false
elseif i[j] == Colon
NewN += 1
unshift!(NewSizes,Sizes[j])
unshift!(OldSizes,Sizes[j])
is_singleton[j] = false
at_end = false
else
str = "Cannot index dimension $j of $a with a $(i[j]). Use an Int, NTuple{N,Int} or Colon."
return :(error($str))
end
end
NewSizes = (NewSizes...)
NewM = prod(NewSizes)
# Bail early if possible
if NewN == 0
return :(a.data[sub2ind(Sizes,i...)])
end
NewType = MArray{NewSizes,T,NewN,NTuple{NewM,T}}
# Now we build an expression for each new element
exprs = Vector{Expr}()
inds_old = ones(Int,N)
for j = 1:NewM
sum_exprs = ntuple(p -> :($(strides[p]) * (i[$p][$(inds_old[p])] - 1)), N)
push!(exprs, :(a.data[$(Expr(:call, :+, 1, sum_exprs...))]))
if j < NewM
inds_old[1] += 1
end
for k = 1:N
if inds_old[k] > OldSizes[k]
inds_old[k] = 1
inds_old[k+1] += 1
else
break
end
end
end
return :(SArray{$NewSizes,$T,$NewN,NTuple{$NewM,$T}}($(Expr(:tuple, exprs...))))
end
@generated function Base.getindex{Sizes,T,N}(a::MArray{Sizes,T,N}, i...)
# Striding lengths
strides = [1, cumprod(collect(Sizes)[1:end-1])...]
# Get the parameters of the new matrix
apl_slicing = VERSION >= v"0.5-"
NewN = 0
NewSizes = Vector{Int}()
OldSizes = Vector{Int}() # Same as NewSizes but includes singleton 1's
at_end = true
is_singleton = trues(N)
for j = length(i):-1:1
if i[j] == Int
unshift!(OldSizes,1)
if !apl_slicing
if !at_end
NewN += 1
unshift!(NewSizes,1)
is_singleton[j] = false
end
end
elseif i[j] <: TupleN{Int}
NewN += 1
unshift!(NewSizes,length(i[j].parameters))
unshift!(OldSizes,length(i[j].parameters))
is_singleton[j] = false
at_end = false
elseif i[j] == Colon
NewN += 1
unshift!(NewSizes,Sizes[j])
unshift!(OldSizes,Sizes[j])
is_singleton[j] = false
at_end = false
else
str = "Cannot index dimension $j of $a with a $(i[j]). Use an Int, NTuple{N,Int} or Colon."
return :(error($str))
end
end
NewSizes = (NewSizes...)
NewM = prod(NewSizes)
# Bail early if possible
if NewN == 0
return :(a.data[sub2ind(Sizes,i...)])
end
NewType = MArray{NewSizes,T,NewN,NTuple{NewM,T}}
# Now we build an expression for each new element
exprs = Vector{Expr}()
inds_old = ones(Int,N)
for j = 1:NewM
sum_exprs = ntuple(p -> :($(strides[p]) * (i[$p][$(inds_old[p])] - 1)), N)
push!(exprs, :(a.data[$(Expr(:call, :+, 1, sum_exprs...))]))
if j < NewM
inds_old[1] += 1
end
for k = 1:N
if inds_old[k] > OldSizes[k]
inds_old[k] = 1
inds_old[k+1] += 1
else
break
end
end
end
return :(MArray{$NewSizes,$T,$NewN,NTuple{$NewM,$T}}($(Expr(:tuple, exprs...))))
end
# setindex! (no index)
Base.setindex!(a::MArray, v) = setindex!(a, v, 1)
Base.unsafe_setindex!{Sizes,T}(a::MArray{Sizes,T}, v) = Base.unsafe_setindex!(a, v, 1)
# setindex! (linear scalar)
function Base.setindex!{Sizes,T}(a::MArray{Sizes,T}, v, i::Int)
if i < 0 || i > prod(Sizes)
throw(BoundsError(a,i))
end
Base.unsafe_setindex!(a, v, i)
end
function Base.unsafe_setindex!{Sizes,T}(a::MArray{Sizes,T}, v, i::Int)
# Get a pointer to the object
p = Base.data_pointer_from_objref(a)
p = Base.unsafe_convert(Ptr{T}, p)
# Store the value
Base.unsafe_store!(p,convert(T,v),i)
end
# setindex! arbitray set of indices
function Base.setindex!{Sizes,T}(a::MArray{Sizes,T}, v, i)
M = prod(Sizes)
for ind in i
if ind < 0 || ind > M
throw(BoundsError(a,i))
end
end
Base.unsafe_setindex!(a, v, i)
end
function Base.unsafe_setindex!{Sizes,T}(a::MArray{Sizes,T}, v, i)
N = length(i)
# Check if v is OK in size
if length(v) != N
throw(DimensionMismatch("tried to assign $(prod(size(v))) elements to $N destinations"))
end
# Get a pointer to the object
p = Base.data_pointer_from_objref(a)
p = Base.unsafe_convert(Ptr{T}, p)
# Store the value
for j in i
Base.unsafe_store!(p,convert(T,v[j]),i[j])
end
end
# setindex! (linear tuple optimization)
@generated function Base.unsafe_setindex!{Sizes,T,N}(a::MArray{Sizes,T}, v::StaticArray{Sizes}, i::NTuple{N,Int})
# Compile-time check if v is OK in size
if length(v) != N
str = "tried to assign $(length(v)) elements to $N destinations"
return :(throw(DimensionMismatch($str)))
end
quote
# Get a pointer to the object
p = Base.data_pointer_from_objref(a)
p = Base.unsafe_convert(Ptr{T}, p)
# Store the value
for j in i
Base.unsafe_store!(p,convert(T,v[j]),i[j])
end
end
end
# setindex! linear with : (Colon)
function Base.setindex!{Sizes,T}(a::MArray{Sizes,T}, v, i::Colon)
Base.unsafe_setindex!(a, v, i)
end
function Base.unsafe_setindex!{Sizes,T}(a::MArray{Sizes,T}, v, ::Colon)
# Check if v is OK in size
if length(v) != length(a)
throw(DimensionMismatch("tried to assign $(prod(size(v))) elements to $(length(a)) destinations"))
end
# Get a pointer to the object
p = Base.data_pointer_from_objref(a)
p = Base.unsafe_convert(Ptr{T}, p)
# Store the value
for j = 1:length(a)
Base.unsafe_store!(p,convert(T,v[j]),j)
end
end
# setindex(m_array, static_array, :) - optimization for know sizes
@generated function Base.unsafe_setindex!{Sizes1,Sizes2,T}(a::MArray{Sizes1,T}, v::StaticArray{Sizes2}, i::Colon)
# Compile-time check if v is OK in size
if length(v) != length(a)
str = "tried to assign $(length(v)) elements to $(length(a)) destinations"
return :(throw(DimensionMismatch($str)))
end
quote
# Get a pointer to the object
p = Base.data_pointer_from_objref(a)
p = Base.unsafe_convert(Ptr{T}, p)
# Store the value
for j = 1:$(length(a))
Base.unsafe_store!(p,convert(T,v[j]),j)
end
end
end
# setindex! multi-dimensional general case
function Base.setindex!{Sizes,T}(a::MArray{Sizes,T}, v, i...)
# check that all i > 0
for ii in 1:length(i)
if isa(i[ii], Colon)
continue
elseif any(j -> j<1 || j>Sizes[ii], i[ii])
error("Indices must be positive integers and in-range: got $i for $Sizes-dimensional array")
end
end
Base.unsafe_setindex!(a, v, i...)
end
function Base.unsafe_setindex!{Sizes,T}(a::MArray{Sizes,T}, v, i...)
# First check v and i have correct sizes (ignoring singleton dimensions, mimicking Base.Array)
i_sizes = ntuple(j -> i[j] == Colon() ? Sizes[j] : length(i[j]), length(i) )
# "Flatten" out any singleton dimensions on input v and indices i
Sizes2 = size(v)
Sizes2_flattened = Vector{Int}()
for j = 1:length(Sizes2)
if Sizes2[j] > 1
push!(Sizes2_flattened, Sizes2[j])
end
end
i_sizes_flattened = Vector{Int}()
for j = 1:length(i_sizes)
if i_sizes[j] > 1
push!(i_sizes_flattened, i_sizes[j])
end
end
# If they aren't consistent, then we can't find a sensible way to assign the data
if Sizes2_flattened != i_sizes_flattened
println(Sizes2_flattened)
println(i_sizes_flattened)
throw(DimensionMismatch("tried to assign $(Sizes2)-dimensional data to $(i_sizes)-dimensional destination"))
end
M = prod(Sizes2)
# Get a pointer to the object
p = Base.data_pointer_from_objref(a)
p = Base.unsafe_convert(Ptr{T}, p)
# Store the value
for ind_v = 1:M
sub_i = ind2sub(i_sizes, ind_v)
sub_a = ntuple(k -> i[k][sub_i[k]], length(i_sizes))
ind_a = sub2ind(Sizes, sub_a...)
Base.unsafe_store!(p, convert(T, v[ind_v]), ind_a)
end
end
# setindex! multi-dimensional run-time-optimized case
@generated function Base.unsafe_setindex!{Sizes1,Sizes2,T}(a::MArray{Sizes1,T}, v::StaticArray{Sizes2}, i::Union{Int,TupleN{Int},Colon}...)
# First check v and i have correct sizes (ignoring singleton dimensions, mimicking Base.Array)
i_sizes = ntuple(j -> i[j] == Colon ? Sizes1[j] : length(i[j].parameters), length(i) )
# "Flatten" out any singleton dimensions on input v and indices i
Sizes2_flattened = Vector{Int}()
for j = 1:length(Sizes2)
if Sizes2[j] > 1
push!(Sizes2_flattened, Sizes2[j])
end
end
i_sizes_flattened = Vector{Int}()
for j = 1:length(i_sizes)
if i_sizes[j] > 1
push!(i_sizes_flattened, i_sizes[j])
end
end
# If they aren't consistent, then we can't find a sensible way to assign the data
if Sizes2_flattened != i_sizes_flattened
str = "tried to assign $(Sizes2)-dimensional data to $(i_sizes)-dimensional destination"
return :(throw(DimensionMismatch($str)))
end
M = prod(Sizes2)
return quote
# Get a pointer to the object
p = Base.data_pointer_from_objref(a)
p = Base.unsafe_convert(Ptr{T}, p)
# Store the value
for ind_v = 1:$M
sub_i = ind2sub($(i_sizes), ind_v)
sub_a = ntuple(k -> i[k][sub_i[k]], $(length(i_sizes)))
ind_a = sub2ind($(Sizes1), sub_a...)
Base.unsafe_store!(p, convert(T, v[ind_v]), ind_a)
end
end
end
=#