-
-
Notifications
You must be signed in to change notification settings - Fork 399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create containers with map instead of for loops #2070
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8ce7535
Move Containers tests to Containers folder
blegat 05f4242
Create containers with map instead of for loops
blegat c771e5d
Update docstrings
blegat 63ff9cc
Fix for iterators with shape
blegat 974dfee
Address comments
blegat 61d5e1a
Add a longer comment
blegat bc881f7
Add axis_constraints.jl benchmark
blegat 37f9a0c
Faster SparseAxisArrays construction in macros
blegat 1192fd8
Type stability fixes in NestedIterator
blegat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# Copyright 2017, Iain Dunning, Joey Huchette, Miles Lubin, and contributors | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this | ||
# file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
""" | ||
default_container(indices) | ||
|
||
If `indices` is a [`NestedIterator`](@ref), return a | ||
[`SparseAxisArray`](@ref). Otherwise, `indices` should be | ||
a `VectorizedProductIterator` and the function returns | ||
`Array` if all iterators of the product are `Base.OneTo` and retunrs | ||
[`DenseAxisArray`](@ref) otherwise. | ||
""" | ||
function default_container end | ||
|
||
""" | ||
container(f::Function, indices, ::Type{C}) | ||
|
||
Create a container of type `C` with indices `indices` and values at given | ||
indices given by `f`. | ||
|
||
container(f::Function, indices) | ||
|
||
Create a container with indices `indices` and values at given indices given by | ||
`f`. The type of container used is determined by [`default_container`](@ref). | ||
|
||
## Examples | ||
|
||
```@jldoctest | ||
julia> Containers.container((i, j) -> i + j, Containers.vectorized_product(Base.OneTo(3), Base.OneTo(3))) | ||
3×3 Array{Int64,2}: | ||
2 3 4 | ||
3 4 5 | ||
4 5 6 | ||
|
||
julia> Containers.container((i, j) -> i + j, Containers.vectorized_product(1:3, 1:3)) | ||
2-dimensional DenseAxisArray{Int64,2,...} with index sets: | ||
Dimension 1, 1:3 | ||
Dimension 2, 1:3 | ||
And data, a 3×3 Array{Int64,2}: | ||
2 3 4 | ||
3 4 5 | ||
4 5 6 | ||
|
||
julia> Containers.container((i, j) -> i + j, Containers.vectorized_product(2:3, Base.OneTo(3))) | ||
2-dimensional DenseAxisArray{Int64,2,...} with index sets: | ||
Dimension 1, 2:3 | ||
Dimension 2, Base.OneTo(3) | ||
And data, a 2×3 Array{Int64,2}: | ||
3 4 5 | ||
4 5 6 | ||
|
||
julia> Containers.container((i, j) -> i + j, Containers.nested(() -> 1:3, i -> i:3, condition = (i, j) -> isodd(i) || isodd(j))) | ||
SparseAxisArray{Int64,2,Tuple{Int64,Int64}} with 5 entries: | ||
[1, 2] = 3 | ||
[2, 3] = 5 | ||
[3, 3] = 6 | ||
[1, 1] = 2 | ||
[1, 3] = 4 | ||
``` | ||
""" | ||
function container end | ||
|
||
container(f::Function, indices) = container(f, indices, default_container(indices)) | ||
|
||
const ArrayIndices{N} = VectorizedProductIterator{NTuple{N, Base.OneTo{Int}}} | ||
default_container(::ArrayIndices) = Array | ||
function container(f::Function, indices::ArrayIndices, ::Type{Array}) | ||
return map(I -> f(I...), indices) | ||
end | ||
function _oneto(indices) | ||
if indices isa UnitRange{Int} && indices == 1:length(indices) | ||
return Base.OneTo(length(indices)) | ||
end | ||
error("Index set for array is not one-based interval.") | ||
end | ||
function container(f::Function, indices::VectorizedProductIterator, | ||
blegat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
::Type{Array}) | ||
container(f, vectorized_product(_oneto.(indices.prod.iterators)...), Array) | ||
end | ||
default_container(::VectorizedProductIterator) = DenseAxisArray | ||
function container(f::Function, indices::VectorizedProductIterator, | ||
::Type{DenseAxisArray}) | ||
return DenseAxisArray(map(I -> f(I...), indices), indices.prod.iterators...) | ||
end | ||
default_container(::NestedIterator) = SparseAxisArray | ||
function container(f::Function, indices, | ||
::Type{SparseAxisArray}) | ||
# Same as `map` but does not allocate the resulting vector. | ||
mappings = Base.Generator(I -> I => f(I...), indices) | ||
# Same as `Dict(mapping)` but it will error if two indices are the same. | ||
data = NoDuplicateDict(mappings) | ||
return SparseAxisArray(data.dict) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty bad printing experience.