Skip to content

Commit

Permalink
collect returns Array
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed Jul 11, 2020
1 parent feba24a commit d420919
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
7 changes: 4 additions & 3 deletions src/HalfIntegerArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,13 @@ _all_match_first_shadow(f::F, inds) where F<:Function = true
end

function Base.collect(h::AbstractHalfIntegerArrayWrapper)
b = HalfIntArray{eltype(h)}(undef,axes(h))
@inbounds for i in eachindex(h, b)
b[i] = h[i]
b = Array{eltype(h)}(undef,size(h))
@inbounds for (bi,hi) in zip(eachindex(b), eachindex(h))
b[bi] = h[hi]
end
b
end
Base.Array{T,N}(h::AbstractHalfIntegerArray{T,N}) where {T,N} = collect(h)

# Similar and reshape

Expand Down
3 changes: 1 addition & 2 deletions src/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ function Base.show(io::IO, X::AdjOrTransAbsHalfIntVecOrMat)
end

function Base.show(io::IO, X::Union{LinearIndicesHalfInt,CartesianIndicesHalfInt})
Y = unwraphalfint(collect(X))
show(io, Y)
show(io, collect(X))
end

Base.show(io::IO, i::CartesianIndexHalfInt) = (print(io, "CartesianIndexHalfInt"); show(io, Tuple(i)))
Expand Down
4 changes: 2 additions & 2 deletions src/halfintarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ const HIAorSM = Union{HalfIntArray,SpinMatrix}
# unwrap

unwraphalfint(a::AbstractArray) = unwraphalfint(a, parent(a))
unwraphalfint(a::A, b::B) where {A, B} = unwraphalfint(b, parent(b))
unwraphalfint(a::AbstractHalfIntegerArray, b::B) where {A, B} = b
unwraphalfint(a, b) = unwraphalfint(b, parent(b))
unwraphalfint(a::AbstractHalfIntegerArray, b) = b

offset(axparent::AbstractUnitRange, ax::AbstractUnitRange) = HalfInt(first(ax) - first(axparent))
offset(axparent::AbstractUnitRange, ax::Integer) = HalfInt(one(first(axparent)) - first(axparent))
Expand Down
30 changes: 22 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import HalfIntegerArrays: IdOffsetRange, OneTo, offset, parentindex
@test eltype(h) == T
@test HalfIntegerArrays.unwraphalfint(h) === arr
@test HalfIntegerArrays.unwraphalfint(h,parent(h)) === arr
@test collect(h) == collect(parent(h))
@test Array(h) == collect(h)

@test HalfIntArray(h,Tuple(0 for i=1:N)) == h
@test HalfIntArray(h,Tuple(half(0) for i=1:N)) == h
Expand Down Expand Up @@ -155,6 +157,8 @@ import HalfIntegerArrays: IdOffsetRange, OneTo, offset, parentindex
@test HalfIntegerArrays.unwraphalfint(h,parent(h)) === arr
@test SpinMatrix(h) === h
@test SpinMatrix(parent(h), h.j) == h
@test collect(h) == collect(parent(h))
@test Array(h) == collect(h)
end
function testSMfromArray(j)
for T in [Float64,ComplexF64]
Expand Down Expand Up @@ -368,7 +372,7 @@ end
lininds = LinearIndicesHalfInt(l)
@test lininds.lininds == l
@test lininds.offsets == (HalfInt(0),HalfInt(0))
@test axes(lininds) == axes(l)
@test axes(lininds) == axes(l)
end
h = HalfIntArray(rand(2,2), -half(1):half(1), -half(1):half(1))
lininds = LinearIndicesHalfInt(h)
Expand All @@ -381,6 +385,8 @@ end
end
@test first(lininds) == 1
@test last(lininds) == length(lininds) == length(h)
@test collect(lininds) == parent(lininds)
@test Array(lininds) == parent(lininds)

@test lininds[lininds] == lininds
@test lininds[cartinds] == lininds
Expand Down Expand Up @@ -417,6 +423,8 @@ end
for (i,LI) in enumerate(lininds)
@test linindsHI[LI] == lininds[LI] == linindsHIP[i] == LI
end
@test collect(linindsHI) == collect(parent(linindsHI)) == collect(lininds)
@test Array(linindsHI) == collect(parent(linindsHI)) == collect(lininds)

r = axes(linindsHI,1)
@test linindsHI[r] == r
Expand All @@ -441,6 +449,9 @@ end
@test linindsHI[LI] == lininds[LI] == linindsHIP[i] == LI
end

@test collect(linindsHI) == collect(parent(linindsHI)) == collect(lininds)
@test Array(linindsHI) == collect(parent(linindsHI)) == collect(lininds)

@test linindsHI[1:length(linindsHI)] == 1:length(linindsHI)

h = HalfIntArray(rand(2,2), -half(1):half(1), -half(1):half(1))
Expand Down Expand Up @@ -609,10 +620,16 @@ end

h = HalfIntArray(rand(2,2),-half(1):half(1),-half(1):half(1))
c = CartesianIndicesHalfInt(axes(h))
lininds = LinearIndicesHalfInt(h)
@test c isa CartesianIndicesHalfInt{2,Tuple{Base.OneTo{Int},Base.OneTo{Int}}}
@test size(c) == (2,2)
@test length(c) == 4
@test axes(c) === axes(h)
@test collect(c) == parent(c)
@test Array(c) == parent(c)
@test c[lininds] == c
@test h[c] == h
@test_throws Exception CartesianIndices(h)

r = (-half(1):half(1),-half(1):half(1))
c = CartesianIndicesHalfInt(r)
Expand All @@ -621,13 +638,6 @@ end
@test length(c) == 4
@test axes(c) === (IdOffsetRange(Base.OneTo(2),-half(3)),IdOffsetRange(Base.OneTo(2),-half(3)))

h = HalfIntArray(rand(2,2),-half(1):half(1),-half(1):half(1))
lininds = LinearIndicesHalfInt(h)
cartinds = CartesianIndicesHalfInt(h)
@test cartinds[lininds] == cartinds
@test h[cartinds] == h
@test_throws Exception CartesianIndices(h)

h = HalfIntArray(rand(2,2),1:2,1:2)
cindsHI = CartesianIndicesHalfInt(h)
cinds = CartesianIndices(h)
Expand All @@ -646,11 +656,15 @@ end
@test h[CartesianIndicesHalfInt(()),1,1][] == h[1,1]
@test Base.checkbounds_indices(Bool, axes(h), (CartesianIndicesHalfInt(()),1,1))

@test collect(cindsHI) == parent(cindsHI)
@test Array(cindsHI) == collect(parent(cindsHI))

h = HalfIntArray(rand(3), -1:1)
c = CartesianIndicesHalfInt(h)
@test h[c] == h
@test h[CartesianIndicesHalfInt(()),-1][] == h[-1]
@test Base.checkbounds_indices(Bool, axes(h), (CartesianIndicesHalfInt(()),-1))
@test collect(c) == parent(c)

h = HalfIntArray(zeros())
c = CartesianIndicesHalfInt(h)
Expand Down

0 comments on commit d420919

Please sign in to comment.