Skip to content

Commit

Permalink
Added a similar method and updated getindex to preserve tuplevector s…
Browse files Browse the repository at this point in the history
…tate (#26)

* slices and similar now construct tuplevectors

* removed getindex since similar fixes it

* Update src/tuplevector.jl

Co-authored-by: Chad Scherrer <[email protected]>

* Update src/tuplevector.jl

Co-authored-by: Chad Scherrer <[email protected]>

* Update src/tuplevector.jl

Co-authored-by: Chad Scherrer <[email protected]>

* Update src/tuplevector.jl

Co-authored-by: Chad Scherrer <[email protected]>

* Update src/tuplevector.jl

Co-authored-by: Chad Scherrer <[email protected]>

* Update src/tuplevector.jl

Co-authored-by: Chad Scherrer <[email protected]>

* bump version

Co-authored-by: Chad Scherrer <[email protected]>
  • Loading branch information
ptiede and cscherrer authored Feb 18, 2022
1 parent d10e232 commit 0f1a7ec
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TupleVectors"
uuid = "615932cf-77b6-4358-adcd-5b7eba981d7e"
authors = ["Chad Scherrer <[email protected]> and contributors"]
version = "0.1.4"
version = "0.1.5"

[deps]
ArraysOfArrays = "65a8f2f4-9b39-5baf-92e2-a9cc46fdf018"
Expand Down
2 changes: 1 addition & 1 deletion src/optional/Sobol.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Sobol: SobolSeq
using .Sobol: SobolSeq
import .Sobol
using ..TupleVectors

Expand Down
16 changes: 11 additions & 5 deletions src/tuplevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ function TupleVector(a::AbstractVector{T}) where {T}
return x
end

function Base.similar(a::TupleVector, ::Type{T}, dims::Dims) where {T}
data = unwrap(a)
sim = rmap(x->similar(x, dims), data)
return TupleVector(sim)
end

function TupleVector(::UndefInitializer, x::T, n::Int) where {T<:NamedTuple}

function initialize(n::Int)
f(x::T) where {T} = ElasticVector{T}(undef, n)
f(x::DenseArray{T,N}) where {T,N} = nestedview(ElasticArray{T,N+1}(undef, size(x)..., n), N)
return f
return f
end

data = rmap(initialize(n), x)
Expand All @@ -37,7 +43,7 @@ function TupleVector(::UndefInitializer, x::T, n::Int) where {T<:NamedTuple}
end

function TupleVector(; kwargs...)
return TupleVector(NamedTuple(kwargs))
return TupleVector(NamedTuple(kwargs))
end

# function TupleVector(x::Union{Tuple, NamedTuple})
Expand Down Expand Up @@ -79,8 +85,8 @@ function Base.show(io::IO, ::MIME"text/plain", v::Vector{TV}) where {TV <: Tuple
foreach(v) do tv println(io, summarize(tv)) end
end

function Base.getindex(x::TupleVector, j)
function Base.getindex(x::TupleVector, j::Integer)

# TODO: Bounds checking doesn't affect performance, am I doing it right?
function f(arr)
# @boundscheck all(j .∈ axes(arr))
Expand All @@ -90,7 +96,7 @@ function Base.getindex(x::TupleVector, j)
modify(f, unwrap(x), Leaves())
end

function Base.setindex!(a::TupleVector, x, j::Int)
function Base.setindex!(a::TupleVector, x, j::Integer)
a1 = flatten(unwrap(a))
x1 = flatten(x)

Expand Down
5 changes: 4 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ using LinearAlgebra
nt = (w=w, y=y)
A = TupleVector(nt)
B = TupleVector(; w=w, y=y)
C = similar(B)
@test typeof(C) <: TupleVector
@test typeof(C[1:3]) <: TupleVector
@test A.w == B.w

@test chainvec(3,5)[1] == 3


Expand Down

2 comments on commit 0f1a7ec

@cscherrer
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/54936

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.5 -m "<description of version>" 0f1a7ec61480c41324f881bd01467a7a6559b3e2
git push origin v0.1.5

Please sign in to comment.