Skip to content

Commit

Permalink
Allow mixing substrings and strings, fixes #140
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Aug 4, 2024
1 parent 574603d commit 9ad1132
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
14 changes: 8 additions & 6 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
## This code is licensed under the MIT license
## See the file LICENSE.md in this distribution

using DataStructures: OrderedDict

letter(i) = string(Char((64+i) % 256))

## helpers for constructing names dictionaries
Expand Down Expand Up @@ -46,10 +48,10 @@ end
NamedArray(a::AbstractArray{T,N}, names::Tuple{N,AbstractVector}, dimnames::NTuple{N,Any}))
NamedArray(a::AbstractArray{T,N}; names::Tuple{N,AbstractVector}, dimnames::NTuple{N,Any}))
Construct a NamedArray from array `a`, with names for the indices in each dimesion `names`,
and names of the dimensions `dimnames`.
Construct a NamedArray from array `a`, with names for the indices in each dimesion `names`,
and names of the dimensions `dimnames`.
If `dimnames` is unspecified, dimensions are named `:A, :B, ...`,
If `dimnames` is unspecified, dimensions are named `:A, :B, ...`,
if `names` are unspecified, names are `"1", "2", "3", ...`.
# Examples
Expand Down Expand Up @@ -118,9 +120,9 @@ end
## @Base.deprecate NamedArray(array::AbstractArray{T,N}, names::AbstractVector{<:AbstractVector}, dimnames::AbstractVector) where {T,N} NamedArray(array::AbstractArray{T,N}, names::NTuple{N}, dimnames::NTuple{N}) where {T,N}

## special case for 1-dim array to circumvent Julia tuple-comma-oddity, #86
NamedArray(array::AbstractVector{T},
names::AbstractVector{VT}=defaultnames(length(array)),
dimname=defaultdimname(1)) where {T,VT} =
NamedArray(array::AbstractVector{T},
names::AbstractVector{VT}=defaultnames(length(array)),
dimname=defaultdimname(1)) where {T,VT} =
NamedArray(array, (names,), (dimname,))

function NamedArray(array::AbstractArray{T,N}, names::NamedTuple) where {T, N}
Expand Down
4 changes: 4 additions & 0 deletions src/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ indices(dict::AbstractDict{K,V}, ::Colon) where {K,V<:Integer} = collect(1:lengt
indices(dict::AbstractDict{K,V}, i::K) where {K<:Not,V<:Integer} = dict[i]
indices(dict::AbstractDict, i::Not) = setdiff(1:length(dict), indices(dict, i.skip))

## AbstractString difficulties, see bug 140
indices(dict::AbstractDict{S, Int64}, i::SubString{S}) where {S <: AbstractString} = indices(dict, S(i))
indices(dict::AbstractDict{SubString{S}, Int64}, i::S) where {S <: AbstractString} = indices(dict, SubString(i))

## namedgetindex collects the elements from the array, and takes care of the index names
## `index` is an integer now, or an array of integers, or a cartesianindex
## and has been computed by `indices()`
Expand Down
13 changes: 12 additions & 1 deletion test/bugs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,15 @@ end
@test fun(n, corrected=corrected) == fun(n.array, corrected=corrected)
end
end
end
end

@testset "issue 140" begin
substrings = split("a b c", " ")
letters = ["a", "b", "c"]
n1 = NamedArray(randn(2, 3), (letters[1:2], letters))
n2 = NamedArray(randn(2, 3), (substrings[1:2], substrings))
for i in 1:2, j in 1:3
@test n1[substrings[i], substrings[j]] == n1[i, j]
@test n2[letters[i], letters[j]] == n2[i, j]
end
end

0 comments on commit 9ad1132

Please sign in to comment.