|
| 1 | +# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors |
| 2 | +# This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | +# License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | +# file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | + |
| 6 | +""" |
| 7 | + default_container(indices) |
| 8 | +
|
| 9 | +If `indices` is a [`NestedIterator`](@ref), return a |
| 10 | +[`SparseAxisArray`](@ref). Otherwise, `indices` should be |
| 11 | +a `VectorizedProductIterator` and the function returns |
| 12 | +`Array` if all iterators of the product are `Base.OneTo` and retunrs |
| 13 | +[`DenseAxisArray`](@ref) otherwise. |
| 14 | +""" |
| 15 | +function default_container end |
| 16 | + |
| 17 | +""" |
| 18 | + container(f::Function, indices, ::Type{C}) |
| 19 | +
|
| 20 | +Create a container of type `C` with indices `indices` and values at given |
| 21 | +indices given by `f`. |
| 22 | +
|
| 23 | + container(f::Function, indices) |
| 24 | +
|
| 25 | +Create a container with indices `indices` and values at given indices given by |
| 26 | +`f`. The type of container used is determined by [`default_container`](@ref). |
| 27 | +
|
| 28 | +## Examples |
| 29 | +
|
| 30 | +```@jldoctest |
| 31 | +julia> Containers.container((i, j) -> i + j, Containers.vectorized_product(Base.OneTo(3), Base.OneTo(3))) |
| 32 | +3×3 Array{Int64,2}: |
| 33 | + 2 3 4 |
| 34 | + 3 4 5 |
| 35 | + 4 5 6 |
| 36 | +
|
| 37 | +julia> Containers.container((i, j) -> i + j, Containers.vectorized_product(1:3, 1:3)) |
| 38 | +2-dimensional DenseAxisArray{Int64,2,...} with index sets: |
| 39 | + Dimension 1, 1:3 |
| 40 | + Dimension 2, 1:3 |
| 41 | +And data, a 3×3 Array{Int64,2}: |
| 42 | + 2 3 4 |
| 43 | + 3 4 5 |
| 44 | + 4 5 6 |
| 45 | +
|
| 46 | +julia> Containers.container((i, j) -> i + j, Containers.vectorized_product(2:3, Base.OneTo(3))) |
| 47 | +2-dimensional DenseAxisArray{Int64,2,...} with index sets: |
| 48 | + Dimension 1, 2:3 |
| 49 | + Dimension 2, Base.OneTo(3) |
| 50 | +And data, a 2×3 Array{Int64,2}: |
| 51 | + 3 4 5 |
| 52 | + 4 5 6 |
| 53 | +
|
| 54 | +julia> Containers.container((i, j) -> i + j, Containers.nested(() -> 1:3, i -> i:3, condition = (i, j) -> isodd(i) || isodd(j))) |
| 55 | +SparseAxisArray{Int64,2,Tuple{Int64,Int64}} with 5 entries: |
| 56 | + [1, 2] = 3 |
| 57 | + [2, 3] = 5 |
| 58 | + [3, 3] = 6 |
| 59 | + [1, 1] = 2 |
| 60 | + [1, 3] = 4 |
| 61 | +``` |
| 62 | +""" |
| 63 | +function container end |
| 64 | + |
| 65 | +container(f::Function, indices) = container(f, indices, default_container(indices)) |
| 66 | + |
| 67 | +const ArrayIndices{N} = VectorizedProductIterator{NTuple{N, Base.OneTo{Int}}} |
| 68 | +default_container(::ArrayIndices) = Array |
| 69 | +function container(f::Function, indices::ArrayIndices, ::Type{Array}) |
| 70 | + return map(I -> f(I...), indices) |
| 71 | +end |
| 72 | +function _oneto(indices) |
| 73 | + if indices isa UnitRange{Int} && indices == 1:length(indices) |
| 74 | + return Base.OneTo(length(indices)) |
| 75 | + end |
| 76 | + error("Index set for array is not one-based interval.") |
| 77 | +end |
| 78 | +function container(f::Function, indices::VectorizedProductIterator, |
| 79 | + ::Type{Array}) |
| 80 | + container(f, vectorized_product(_oneto.(indices.prod.iterators)...), Array) |
| 81 | +end |
| 82 | +default_container(::VectorizedProductIterator) = DenseAxisArray |
| 83 | +function container(f::Function, indices::VectorizedProductIterator, |
| 84 | + ::Type{DenseAxisArray}) |
| 85 | + return DenseAxisArray(map(I -> f(I...), indices), indices.prod.iterators...) |
| 86 | +end |
| 87 | +default_container(::NestedIterator) = SparseAxisArray |
| 88 | +function container(f::Function, indices, |
| 89 | + ::Type{SparseAxisArray}) |
| 90 | + # Same as `map` but does not allocate the resulting vector. |
| 91 | + mappings = Base.Generator(I -> I => f(I...), indices) |
| 92 | + # Same as `Dict(mapping)` but it will error if two indices are the same. |
| 93 | + data = NoDuplicateDict(mappings) |
| 94 | + return SparseAxisArray(data.dict) |
| 95 | +end |
0 commit comments