-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changing get_index and creating PriorityValue structure
- Loading branch information
1 parent
677b095
commit 76b5e2c
Showing
4 changed files
with
53 additions
and
46 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,14 @@ | ||
module WildcardArrays | ||
|
||
using OrderedCollections | ||
import Base: show, size, ndims, eltype | ||
|
||
struct WildcardArray{T,N} <: AbstractArray{T,N} | ||
data::OrderedDict{NTuple{N,Int},T} # this would be the ordered map | ||
dims::NTuple{N,Int} | ||
default::T | ||
end | ||
|
||
WildcardArray(s::String, values::Vector{T}; default=0.0, startindex=0) where T = parse(s, values, default=default, startindex=startindex) | ||
|
||
Base.size(wa::WildcardArray) = wa.dims | ||
Base.ndims(wa::WildcardArray) = length(wa.dims) | ||
function Base.show(io::IO, m::MIME"text/plain", wa::WildcardArray{T,N}) where {T,N} | ||
println("WildcardArray{$T,$N}") | ||
[println("$(kk) => $(vv)") for (kk,vv) in zip(keys(wa.data), values(wa.data))] | ||
end | ||
Base.show(wa::WildcardArray) = Base.show(stdout, MIME("text/plain"), wa) | ||
Base.eltype(::WildcardArray{T,N}) where {T,N} = T | ||
|
||
dict(wa::WildcardArray) = wa.data | ||
|
||
include("./getindex.jl") | ||
include("./types.jl") # WildcardArray and PriorityValue | ||
include("./getindex.jl") # implementing get_index for WildcardArray | ||
|
||
export | ||
WildcardArray, | ||
dict | ||
include("./parse.jl") | ||
WildcardArray | ||
|
||
include("./parse.jl") | ||
|
||
end | ||
|
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 |
---|---|---|
@@ -1,27 +1,29 @@ | ||
function Base.getindex(wa::WildcardArray{T,N}, i::Vararg{Int,N})::T where {T,N} | ||
keys_obj = dict(wa) |> keys |> collect | ||
# println(keys_obj) | ||
# println(eltype(wa)) | ||
dict_wildcard = dict(wa) | ||
|
||
if any(x -> x <= 0, i) || any(i[index] > size(wa)[index] for index in 1:N) | ||
error("Indices must be integers larger than zero and less than or equal to the entries of the vector $(size(wa))") | ||
end | ||
|
||
index = 0 | ||
priority = 0 | ||
index = Vector{Int}(undef, N) | ||
|
||
for kk in Iterators.product([[0, index] for index in i]...) | ||
temp_index = findfirst(x -> isequal(x, kk), keys_obj) | ||
if haskey(dict_wildcard, kk) | ||
tmp_priority = dict_wildcard[kk].priority | ||
else | ||
tmp_priority = -Inf | ||
end | ||
|
||
if !isnothing(temp_index) | ||
if temp_index > index | ||
index = temp_index | ||
end | ||
if tmp_priority > priority | ||
index = kk | ||
priority = tmp_priority | ||
end | ||
end | ||
|
||
if index == 0 | ||
if priority == 0 | ||
return wa.default | ||
end | ||
|
||
return dict(wa)[keys_obj[index]] # put implementation here | ||
return dict_wildcard[index].value # put implementation here | ||
end |
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,23 @@ | ||
struct PriorityValue{T} | ||
priority::Int | ||
value::T | ||
end | ||
|
||
struct WildcardArray{T,N} <: AbstractArray{T,N} | ||
data::Dict{NTuple{N,Int},PriorityValue{T}} | ||
dims::NTuple{N,Int} | ||
default::T | ||
end | ||
|
||
WildcardArray(s::String, values::Vector{T}; default=0.0, startindex=0) where T = parse(s, values, default=default, startindex=startindex) | ||
|
||
Base.size(wa::WildcardArray) = wa.dims | ||
Base.ndims(wa::WildcardArray) = length(wa.dims) | ||
Base.eltype(::WildcardArray{T,N}) where {T,N} = T | ||
|
||
function Base.show(io::IO, m::MIME"text/plain", wa::WildcardArray{T,N}) where {T,N} | ||
println("WildcardArray{$T,$N}") | ||
[println("$(kk) => $(vv)") for (kk,vv) in zip(keys(wa.data), values(wa.data))] | ||
end | ||
|
||
dict(wa::WildcardArray) = wa.data |