|
| 1 | +using BlockArrays: |
| 2 | + BlockArrays, |
| 3 | + Block, |
| 4 | + BlockedUnitRange, |
| 5 | + BlockRange, |
| 6 | + BlockVector, |
| 7 | + blockedrange, |
| 8 | + BlockIndexRange, |
| 9 | + blockfirsts, |
| 10 | + blocklasts, |
| 11 | + blocklengths, |
| 12 | + findblock, |
| 13 | + findblockindex, |
| 14 | + mortar |
| 15 | +using ..LabelledNumbers: LabelledNumbers, LabelledInteger, label, labelled, unlabel |
| 16 | + |
| 17 | +# Custom `BlockedUnitRange` constructor that takes a unit range |
| 18 | +# and a set of block lengths, similar to `BlockArray(::AbstractArray, blocklengths...)`. |
| 19 | +function blockedunitrange(a::AbstractUnitRange, blocklengths) |
| 20 | + blocklengths_shifted = copy(blocklengths) |
| 21 | + blocklengths_shifted[1] += (first(a) - 1) |
| 22 | + blocklasts = cumsum(blocklengths_shifted) |
| 23 | + return BlockArrays._BlockedUnitRange(first(a), blocklasts) |
| 24 | +end |
| 25 | + |
| 26 | +# Circumvents issue in `findblock` that assumes the `BlockedUnitRange` |
| 27 | +# starts at 1. |
| 28 | +# TODO: Raise an issue with `BlockArrays`. |
| 29 | +function blockedunitrange_findblock(a::BlockedUnitRange, index::Integer) |
| 30 | + @boundscheck index in 1:length(a) || throw(BoundsError(a, index)) |
| 31 | + return @inbounds findblock(a, index + first(a) - 1) |
| 32 | +end |
| 33 | + |
| 34 | +# Circumvents issue in `findblockindex` that assumes the `BlockedUnitRange` |
| 35 | +# starts at 1. |
| 36 | +# TODO: Raise an issue with `BlockArrays`. |
| 37 | +function blockedunitrange_findblockindex(a::BlockedUnitRange, index::Integer) |
| 38 | + @boundscheck index in 1:length(a) || throw(BoundsError()) |
| 39 | + return @inbounds findblockindex(a, index + first(a) - 1) |
| 40 | +end |
| 41 | + |
| 42 | +const GradedUnitRange{BlockLasts<:Vector{<:LabelledInteger}} = BlockedUnitRange{BlockLasts} |
| 43 | + |
| 44 | +function gradedrange(lblocklengths::AbstractVector{<:LabelledInteger}) |
| 45 | + brange = blockedrange(unlabel.(lblocklengths)) |
| 46 | + lblocklasts = labelled.(blocklasts(brange), label.(lblocklengths)) |
| 47 | + # TODO: `first` is forced to be `Int` in `BlockArrays.BlockedUnitRange`, |
| 48 | + # so this doesn't do anything right now. Make a PR to generalize it. |
| 49 | + firstlength = first(lblocklengths) |
| 50 | + lfirst = oneunit(firstlength) |
| 51 | + return BlockArrays._BlockedUnitRange(lfirst, lblocklasts) |
| 52 | +end |
| 53 | + |
| 54 | +Base.last(a::GradedUnitRange) = isempty(a.lasts) ? first(a) - 1 : last(a.lasts) |
| 55 | + |
| 56 | +function gradedrange(lblocklengths::AbstractVector{<:Pair{<:Any,<:Integer}}) |
| 57 | + return gradedrange(labelled.(last.(lblocklengths), first.(lblocklengths))) |
| 58 | +end |
| 59 | + |
| 60 | +function labelled_blocks(a::BlockedUnitRange, labels) |
| 61 | + return BlockArrays._BlockedUnitRange(a.first, labelled.(a.lasts, labels)) |
| 62 | +end |
| 63 | + |
| 64 | +function BlockArrays.findblock(a::GradedUnitRange, index::Integer) |
| 65 | + return blockedunitrange_findblock(unlabel_blocks(a), index) |
| 66 | +end |
| 67 | + |
| 68 | +function blockedunitrange_findblock(a::GradedUnitRange, index::Integer) |
| 69 | + return blockedunitrange_findblock(unlabel_blocks(a), index) |
| 70 | +end |
| 71 | + |
| 72 | +function blockedunitrange_findblockindex(a::GradedUnitRange, index::Integer) |
| 73 | + return blockedunitrange_findblockindex(unlabel_blocks(a), index) |
| 74 | +end |
| 75 | + |
| 76 | +function BlockArrays.findblockindex(a::GradedUnitRange, index::Integer) |
| 77 | + return blockedunitrange_findblockindex(unlabel_blocks(a), index) |
| 78 | +end |
| 79 | + |
| 80 | +## Block label interface |
| 81 | + |
| 82 | +# Internal function |
| 83 | +function get_label(a::BlockedUnitRange, index::Block{1}) |
| 84 | + return label(blocklasts(a)[Int(index)]) |
| 85 | +end |
| 86 | + |
| 87 | +# Internal function |
| 88 | +function get_label(a::BlockedUnitRange, index::Integer) |
| 89 | + return get_label(a, blockedunitrange_findblock(a, index)) |
| 90 | +end |
| 91 | + |
| 92 | +function blocklabels(a::BlockVector) |
| 93 | + return map(BlockRange(a)) do block |
| 94 | + return label(@view(a[block])) |
| 95 | + end |
| 96 | +end |
| 97 | + |
| 98 | +function blocklabels(a::BlockedUnitRange) |
| 99 | + # Using `a.lasts` here since that is what is stored |
| 100 | + # inside of `BlockedUnitRange`, maybe change that. |
| 101 | + # For example, it could be something like: |
| 102 | + # |
| 103 | + # map(BlockRange(a)) do block |
| 104 | + # return label(@view(a[block])) |
| 105 | + # end |
| 106 | + # |
| 107 | + return label.(a.lasts) |
| 108 | +end |
| 109 | + |
| 110 | +# TODO: This relies on internals of `BlockArrays`, maybe redesign |
| 111 | +# to try to avoid that. |
| 112 | +# TODO: Define `set_grades`, `set_sector_labels`, `set_labels`. |
| 113 | +function unlabel_blocks(a::BlockedUnitRange) |
| 114 | + return BlockArrays._BlockedUnitRange(a.first, unlabel.(a.lasts)) |
| 115 | +end |
| 116 | + |
| 117 | +## BlockedUnitRage interface |
| 118 | + |
| 119 | +function Base.axes(ga::GradedUnitRange) |
| 120 | + return map(axes(unlabel_blocks(ga))) do a |
| 121 | + return labelled_blocks(a, blocklabels(ga)) |
| 122 | + end |
| 123 | +end |
| 124 | + |
| 125 | +function BlockArrays.blockfirsts(a::GradedUnitRange) |
| 126 | + return labelled.(blockfirsts(unlabel_blocks(a)), blocklabels(a)) |
| 127 | +end |
| 128 | + |
| 129 | +function BlockArrays.blocklasts(a::GradedUnitRange) |
| 130 | + return labelled.(blocklasts(unlabel_blocks(a)), blocklabels(a)) |
| 131 | +end |
| 132 | + |
| 133 | +function BlockArrays.blocklengths(a::GradedUnitRange) |
| 134 | + return labelled.(blocklengths(unlabel_blocks(a)), blocklabels(a)) |
| 135 | +end |
| 136 | + |
| 137 | +function Base.first(a::GradedUnitRange) |
| 138 | + return labelled(first(unlabel_blocks(a)), label(a[Block(1)])) |
| 139 | +end |
| 140 | + |
| 141 | +function firstblockindices(a::GradedUnitRange) |
| 142 | + return labelled.(firstblockindices(unlabel_blocks(a)), blocklabels(a)) |
| 143 | +end |
| 144 | + |
| 145 | +function blockedunitrange_getindex(a::GradedUnitRange, index) |
| 146 | + # This uses `blocklasts` since that is what is stored |
| 147 | + # in `BlockedUnitRange`, maybe abstract that away. |
| 148 | + return labelled(unlabel_blocks(a)[index], get_label(a, index)) |
| 149 | +end |
| 150 | + |
| 151 | +# Like `a[indices]` but preserves block structure. |
| 152 | +using BlockArrays: block, blockindex |
| 153 | +function blockedunitrange_getindices( |
| 154 | + a::BlockedUnitRange, indices::AbstractUnitRange{<:Integer} |
| 155 | +) |
| 156 | + first_blockindex = blockedunitrange_findblockindex(a, first(indices)) |
| 157 | + last_blockindex = blockedunitrange_findblockindex(a, last(indices)) |
| 158 | + first_block = block(first_blockindex) |
| 159 | + last_block = block(last_blockindex) |
| 160 | + blocklengths = if first_block == last_block |
| 161 | + [length(indices)] |
| 162 | + else |
| 163 | + map(first_block:last_block) do block |
| 164 | + if block == first_block |
| 165 | + return length(a[first_block]) - blockindex(first_blockindex) + 1 |
| 166 | + end |
| 167 | + if block == last_block |
| 168 | + return blockindex(last_blockindex) |
| 169 | + end |
| 170 | + return length(a[block]) |
| 171 | + end |
| 172 | + end |
| 173 | + return blockedunitrange(indices .+ (first(a) - 1), blocklengths) |
| 174 | +end |
| 175 | + |
| 176 | +function blockedunitrange_getindices(a::BlockedUnitRange, indices::BlockIndexRange) |
| 177 | + return a[block(indices)][only(indices.indices)] |
| 178 | +end |
| 179 | + |
| 180 | +function blockedunitrange_getindices(a::BlockedUnitRange, indices::Vector{<:Integer}) |
| 181 | + return map(index -> a[index], indices) |
| 182 | +end |
| 183 | + |
| 184 | +function blockedunitrange_getindices( |
| 185 | + a::BlockedUnitRange, indices::Vector{<:Union{Block{1},BlockIndexRange{1}}} |
| 186 | +) |
| 187 | + return mortar(map(index -> a[index], indices)) |
| 188 | +end |
| 189 | + |
| 190 | +function blockedunitrange_getindices(a::BlockedUnitRange, indices) |
| 191 | + return error("Not implemented.") |
| 192 | +end |
| 193 | + |
| 194 | +# The blocks of the corresponding slice. |
| 195 | +_blocks(a::AbstractUnitRange, indices) = error("Not implemented") |
| 196 | +function _blocks(a::AbstractUnitRange, indices::AbstractUnitRange) |
| 197 | + return findblock(a, first(indices)):findblock(a, last(indices)) |
| 198 | +end |
| 199 | +function _blocks(a::AbstractUnitRange, indices::BlockRange) |
| 200 | + return indices |
| 201 | +end |
| 202 | + |
| 203 | +# The block labels of the corresponding slice. |
| 204 | +function blocklabels(a::AbstractUnitRange, indices) |
| 205 | + return map(_blocks(a, indices)) do block |
| 206 | + return label(a[block]) |
| 207 | + end |
| 208 | +end |
| 209 | + |
| 210 | +function blockedunitrange_getindices( |
| 211 | + ga::GradedUnitRange, indices::AbstractUnitRange{<:Integer} |
| 212 | +) |
| 213 | + a_indices = blockedunitrange_getindices(unlabel_blocks(ga), indices) |
| 214 | + return labelled_blocks(a_indices, blocklabels(ga, indices)) |
| 215 | +end |
| 216 | + |
| 217 | +function blockedunitrange_getindices(ga::GradedUnitRange, indices::BlockRange) |
| 218 | + return labelled_blocks(unlabel_blocks(ga)[indices], blocklabels(ga, indices)) |
| 219 | +end |
| 220 | + |
| 221 | +function Base.getindex(a::GradedUnitRange, index::Integer) |
| 222 | + return blockedunitrange_getindex(a, index) |
| 223 | +end |
| 224 | + |
| 225 | +function Base.getindex(a::GradedUnitRange, index::Block{1}) |
| 226 | + return blockedunitrange_getindex(a, index) |
| 227 | +end |
| 228 | + |
| 229 | +function Base.getindex(a::GradedUnitRange, indices::BlockIndexRange) |
| 230 | + return blockedunitrange_getindices(a, indices) |
| 231 | +end |
| 232 | + |
| 233 | +function Base.getindex( |
| 234 | + a::GradedUnitRange, indices::BlockRange{1,<:Tuple{AbstractUnitRange{Int}}} |
| 235 | +) |
| 236 | + return blockedunitrange_getindices(a, indices) |
| 237 | +end |
| 238 | + |
| 239 | +function Base.getindex(a::GradedUnitRange, indices) |
| 240 | + return blockedunitrange_getindices(a, indices) |
| 241 | +end |
| 242 | + |
| 243 | +function Base.getindex(a::GradedUnitRange, indices::AbstractUnitRange{<:Integer}) |
| 244 | + return blockedunitrange_getindices(a, indices) |
| 245 | +end |
0 commit comments