Skip to content

Commit 0f1a7ec

Browse files
ptiedecscherrer
andauthored
Added a similar method and updated getindex to preserve tuplevector state (#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]>
1 parent d10e232 commit 0f1a7ec

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TupleVectors"
22
uuid = "615932cf-77b6-4358-adcd-5b7eba981d7e"
33
authors = ["Chad Scherrer <[email protected]> and contributors"]
4-
version = "0.1.4"
4+
version = "0.1.5"
55

66
[deps]
77
ArraysOfArrays = "65a8f2f4-9b39-5baf-92e2-a9cc46fdf018"

src/optional/Sobol.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Sobol: SobolSeq
1+
using .Sobol: SobolSeq
22
import .Sobol
33
using ..TupleVectors
44

src/tuplevector.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ function TupleVector(a::AbstractVector{T}) where {T}
2323
return x
2424
end
2525

26+
function Base.similar(a::TupleVector, ::Type{T}, dims::Dims) where {T}
27+
data = unwrap(a)
28+
sim = rmap(x->similar(x, dims), data)
29+
return TupleVector(sim)
30+
end
31+
2632
function TupleVector(::UndefInitializer, x::T, n::Int) where {T<:NamedTuple}
2733

2834
function initialize(n::Int)
2935
f(x::T) where {T} = ElasticVector{T}(undef, n)
3036
f(x::DenseArray{T,N}) where {T,N} = nestedview(ElasticArray{T,N+1}(undef, size(x)..., n), N)
31-
return f
37+
return f
3238
end
3339

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

3945
function TupleVector(; kwargs...)
40-
return TupleVector(NamedTuple(kwargs))
46+
return TupleVector(NamedTuple(kwargs))
4147
end
4248

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

82-
function Base.getindex(x::TupleVector, j)
83-
88+
function Base.getindex(x::TupleVector, j::Integer)
89+
8490
# TODO: Bounds checking doesn't affect performance, am I doing it right?
8591
function f(arr)
8692
# @boundscheck all(j .∈ axes(arr))
@@ -90,7 +96,7 @@ function Base.getindex(x::TupleVector, j)
9096
modify(f, unwrap(x), Leaves())
9197
end
9298

93-
function Base.setindex!(a::TupleVector, x, j::Int)
99+
function Base.setindex!(a::TupleVector, x, j::Integer)
94100
a1 = flatten(unwrap(a))
95101
x1 = flatten(x)
96102

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,11 @@ using LinearAlgebra
1212
nt = (w=w, y=y)
1313
A = TupleVector(nt)
1414
B = TupleVector(; w=w, y=y)
15+
C = similar(B)
16+
@test typeof(C) <: TupleVector
17+
@test typeof(C[1:3]) <: TupleVector
1518
@test A.w == B.w
16-
19+
1720
@test chainvec(3,5)[1] == 3
1821

1922

0 commit comments

Comments
 (0)