-
Notifications
You must be signed in to change notification settings - Fork 37
/
array_index.jl
290 lines (266 loc) · 9.13 KB
/
array_index.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
"""
ArrayIndex{N}
Subtypes of `ArrayIndex` represent series of transformations for a provided index to some
buffer which is typically accomplished with square brackets (e.g., `buffer[index[inds...]]`).
The only behavior that is required of a subtype of `ArrayIndex` is the ability to transform
individual index elements (i.e. not collections). This does not guarantee bounds checking or
the ability to iterate (although additional functionallity may be provided for specific
types).
"""
abstract type ArrayIndex{N} end
const MatrixIndex = ArrayIndex{2}
const VectorIndex = ArrayIndex{1}
Base.ndims(::Type{<:ArrayIndex{N}}) where {N} = N
struct BidiagonalIndex <: MatrixIndex
count::Int
isup::Bool
end
struct TridiagonalIndex <: MatrixIndex
count::Int# count==nsize+nsize-1+nsize-1
nsize::Int
isrow::Bool
end
struct BandedMatrixIndex <: MatrixIndex
count::Int
rowsize::Int
colsize::Int
bandinds::Array{Int,1}
bandsizes::Array{Int,1}
isrow::Bool
end
function _bandsize(bandind, rowsize, colsize)
-(rowsize - 1) <= bandind <= colsize - 1 || throw(ErrorException("Invalid Bandind"))
if (bandind * (colsize - rowsize) > 0) & (abs(bandind) <= abs(colsize - rowsize))
return min(rowsize, colsize)
elseif bandind * (colsize - rowsize) <= 0
return min(rowsize, colsize) - abs(bandind)
else
return min(rowsize, colsize) - abs(bandind) + abs(colsize - rowsize)
end
end
function BandedMatrixIndex(rowsize, colsize, lowerbandwidth, upperbandwidth, isrow)
upperbandwidth > -lowerbandwidth || throw(ErrorException("Invalid Bandwidths"))
bandinds = upperbandwidth:-1:-lowerbandwidth
bandsizes = [_bandsize(band, rowsize, colsize) for band in bandinds]
BandedMatrixIndex(sum(bandsizes), rowsize, colsize, bandinds, bandsizes, isrow)
end
struct BlockBandedMatrixIndex <: MatrixIndex
count::Int
refinds::Array{Int,1}
refcoords::Array{Int,1}# storing col or row inds at ref points
isrow::Bool
end
function BlockBandedMatrixIndex(nrowblock, ncolblock, rowsizes, colsizes, l, u)
blockrowind = BandedMatrixIndex(nrowblock, ncolblock, l, u, true)
blockcolind = BandedMatrixIndex(nrowblock, ncolblock, l, u, false)
sortedinds = sort(
[(blockrowind[i], blockcolind[i]) for i = 1:length(blockrowind)],
by = x -> x[1],
)
sort!(sortedinds, by = x -> x[2], alg = InsertionSort)# stable sort keeps the second index in order
refinds = Array{Int,1}()
refrowcoords = Array{Int,1}()
refcolcoords = Array{Int,1}()
rowheights = pushfirst!(copy(rowsizes), 1)
cumsum!(rowheights, rowheights)
blockheight = 0
blockrow = 1
blockcol = 1
currenti = 1
lastrowind = sortedinds[1][1] - 1
lastcolind = sortedinds[1][2]
for ind in sortedinds
rowind, colind = ind
if colind == lastcolind
if rowind > lastrowind
blockheight += rowsizes[rowind]
end
else
for j = blockcol:blockcol+colsizes[lastcolind]-1
push!(refinds, currenti)
push!(refrowcoords, blockrow)
push!(refcolcoords, j)
currenti += blockheight
end
blockcol += colsizes[lastcolind]
blockrow = rowheights[rowind]
blockheight = rowsizes[rowind]
end
lastcolind = colind
lastrowind = rowind
end
for j = blockcol:blockcol+colsizes[lastcolind]-1
push!(refinds, currenti)
push!(refrowcoords, blockrow)
push!(refcolcoords, j)
currenti += blockheight
end
push!(refinds, currenti)# guard
push!(refrowcoords, -1)
push!(refcolcoords, -1)
rowindobj = BlockBandedMatrixIndex(currenti - 1, refinds, refrowcoords, true)
colindobj = BlockBandedMatrixIndex(currenti - 1, refinds, refcolcoords, false)
rowindobj, colindobj
end
struct BandedBlockBandedMatrixIndex <: MatrixIndex
count::Int
refinds::Array{Int,1}
refcoords::Array{Int,1}# storing col or row inds at ref points
reflocalinds::Array{BandedMatrixIndex,1}
isrow::Bool
end
function BandedBlockBandedMatrixIndex(
nrowblock,
ncolblock,
rowsizes,
colsizes,
l,
u,
lambda,
mu,
)
blockrowind = BandedMatrixIndex(nrowblock, ncolblock, l, u, true)
blockcolind = BandedMatrixIndex(nrowblock, ncolblock, l, u, false)
sortedinds = sort(
[(blockrowind[i], blockcolind[i]) for i = 1:length(blockrowind)],
by = x -> x[1],
)
sort!(sortedinds, by = x -> x[2], alg = InsertionSort)# stable sort keeps the second index in order
rowheights = pushfirst!(copy(rowsizes), 1)
cumsum!(rowheights, rowheights)
colwidths = pushfirst!(copy(colsizes), 1)
cumsum!(colwidths, colwidths)
currenti = 1
refinds = Array{Int,1}()
refrowcoords = Array{Int,1}()
refcolcoords = Array{Int,1}()
reflocalrowinds = Array{BandedMatrixIndex,1}()
reflocalcolinds = Array{BandedMatrixIndex,1}()
for ind in sortedinds
rowind, colind = ind
localrowind =
BandedMatrixIndex(rowsizes[rowind], colsizes[colind], lambda, mu, true)
localcolind =
BandedMatrixIndex(rowsizes[rowind], colsizes[colind], lambda, mu, false)
push!(refinds, currenti)
push!(refrowcoords, rowheights[rowind])
push!(refcolcoords, colwidths[colind])
push!(reflocalrowinds, localrowind)
push!(reflocalcolinds, localcolind)
currenti += localrowind.count
end
push!(refinds, currenti)
push!(refrowcoords, -1)
push!(refcolcoords, -1)
rowindobj = BandedBlockBandedMatrixIndex(
currenti - 1,
refinds,
refrowcoords,
reflocalrowinds,
true,
)
colindobj = BandedBlockBandedMatrixIndex(
currenti - 1,
refinds,
refcolcoords,
reflocalcolinds,
false,
)
rowindobj, colindobj
end
"""
StrideIndex(x)
Subtype of `ArrayIndex` that transforms and index using stride layout information
derived from `x`.
"""
struct StrideIndex{N,R,C,S,O} <: ArrayIndex{N}
strides::S
offsets::O
function StrideIndex{N,R,C}(s::S, o::O) where {N,R,C,S,O}
return new{N,R::NTuple{N,Int},C,S,O}(s, o)
end
function StrideIndex{N,R,C}(a::A) where {N,R,C,A}
return StrideIndex{N,R,C}(strides(a), offsets(a))
end
function StrideIndex(a::A) where {A}
return StrideIndex{ndims(A),known(stride_rank(A)), known(contiguous_axis(A))}(a)
end
end
Base.firstindex(i::Union{TridiagonalIndex,BandedBlockBandedMatrixIndex,BandedMatrixIndex,BidiagonalIndex,BlockBandedMatrixIndex}) = 1
Base.lastindex(i::Union{TridiagonalIndex,BandedBlockBandedMatrixIndex,BandedMatrixIndex,BidiagonalIndex,BlockBandedMatrixIndex}) = i.count
Base.length(i::Union{TridiagonalIndex,BandedBlockBandedMatrixIndex,BandedMatrixIndex,BidiagonalIndex,BlockBandedMatrixIndex}) = i.count
## getindex
@propagate_inbounds Base.getindex(x::ArrayIndex, i::CanonicalInt, ii::CanonicalInt...) = x[NDIndex(i, ii...)]
@propagate_inbounds function Base.getindex(ind::BidiagonalIndex, i::Int)
@boundscheck 1 <= i <= ind.count || throw(BoundsError(ind, i))
if ind.isup
ii = i + 1
else
ii = i + 1 + 1
end
convert(Int, floor(ii / 2))
end
@propagate_inbounds function Base.getindex(ind::TridiagonalIndex, i::Int)
@boundscheck 1 <= i <= ind.count || throw(BoundsError(ind, i))
offsetu = ind.isrow ? 0 : 1
offsetl = ind.isrow ? 1 : 0
if 1 <= i <= ind.nsize
return i
elseif ind.nsize < i <= ind.nsize + ind.nsize - 1
return i - ind.nsize + offsetu
else
return i - (ind.nsize + ind.nsize - 1) + offsetl
end
end
@propagate_inbounds function Base.getindex(ind::BandedMatrixIndex, i::Int)
@boundscheck 1 <= i <= ind.count || throw(BoundsError(ind, i))
_i = i
p = 1
while _i - ind.bandsizes[p] > 0
_i -= ind.bandsizes[p]
p += 1
end
bandind = ind.bandinds[p]
startfromone = !xor(ind.isrow, (bandind > 0))
if startfromone
return _i
else
return _i + abs(bandind)
end
end
@propagate_inbounds function Base.getindex(ind::BlockBandedMatrixIndex, i::Int)
@boundscheck 1 <= i <= ind.count || throw(BoundsError(ind, i))
p = 1
while i - ind.refinds[p] >= 0
p += 1
end
p -= 1
_i = i - ind.refinds[p]
if ind.isrow
return ind.refcoords[p] + _i
else
return ind.refcoords[p]
end
end
@propagate_inbounds function Base.getindex(ind::BandedBlockBandedMatrixIndex, i::Int)
@boundscheck 1 <= i <= ind.count || throw(BoundsError(ind, i))
p = 1
while i - ind.refinds[p] >= 0
p += 1
end
p -= 1
_i = i - ind.refinds[p] + 1
ind.reflocalinds[p][_i] + ind.refcoords[p] - 1
end
@inline function Base.getindex(x::StrideIndex{N}, i::AbstractCartesianIndex) where {N}
return _strides2int(offsets(x), strides(x), Tuple(i)) + static(1)
end
@generated function _strides2int(o::O, s::S, i::I) where {O,S,I}
N = known_length(S)
out = :()
for i in 1:N
tmp = :(((getfield(i, $i) - getfield(o, $i)) * getfield(s, $i)))
out = ifelse(i === 1, tmp, :($out + $tmp))
end
return Expr(:block, Expr(:meta, :inline), out)
end