@@ -6,7 +6,7 @@ Base.@deprecate_binding (..) Colon()
6
6
7
7
using Base: Indices, LinearSlow, LinearFast, tail
8
8
9
- export OffsetArray
9
+ export OffsetArray, @unsafe
10
10
11
11
immutable OffsetArray{T,N,AA<: AbstractArray } <: AbstractArray{T,N}
12
12
parent:: AA
@@ -128,4 +128,37 @@ _offset(out, ::Tuple{}, ::Tuple{}) = out
128
128
indexoffset (r:: Range ) = first (r) - 1
129
129
indexoffset (i:: Integer ) = 0
130
130
131
+ macro unsafe (ex)
132
+ esc (unsafe (ex))
133
+ end
134
+ unsafe (ex) = ex
135
+ function unsafe (ex:: Expr )
136
+ if ex. head ∈ (:+= , :-= , :*= , :/= )
137
+ ex = Expr (:(= ), ex. args[1 ], Expr (:call , Symbol (string (ex. head)[1 ]), ex. args... ))
138
+ end
139
+ if ex. head == :(= )
140
+ a = ex. args[1 ]
141
+ if isa (a, Expr) && (a:: Expr ). head == :ref
142
+ # setindex!
143
+ newargs = map (unsafe, ex. args[2 : end ])
144
+ @assert length (newargs) == 1
145
+ return Expr (:call , :(OffsetArrays. unsafe_setindex!), (a:: Expr ). args[1 ], newargs[1 ], (a:: Expr ). args[2 : end ]. .. )
146
+ end
147
+ end
148
+ newargs = map (unsafe, ex. args)
149
+ if ex. head == :ref
150
+ # getindex
151
+ return Expr (:call , :(OffsetArrays. unsafe_getindex), newargs... )
152
+ end
153
+ Expr (ex. head, newargs... )
154
+ end
155
+
156
+ @inline unsafe_getindex (a:: AbstractArray , I... ) = (@inbounds ret = a[I... ]; ret)
157
+ @inline unsafe_setindex! (a:: AbstractArray , val, I... ) = (@inbounds a[I... ] = val; val)
158
+
159
+ @inline unsafe_getindex (a:: OffsetArray , I:: Int... ) = (@inbounds ret = parent (a)[offset (a. offsets, I)... ]; ret)
160
+ @inline unsafe_setindex! (a:: OffsetArray , val, I:: Int... ) = (@inbounds parent (a)[offset (a. offsets, I)... ] = val; val)
161
+ @inline unsafe_getindex (a:: OffsetArray , I... ) = unsafe_getindex (a, Base. IteratorsMD. flatten (I)... )
162
+ @inline unsafe_setindex! (a:: OffsetArray , val, I... ) = unsafe_setindex! (a, val, Base. IteratorsMD. flatten (I)... )
163
+
131
164
end # module
0 commit comments