Skip to content

Commit

Permalink
Merge #86
Browse files Browse the repository at this point in the history
86: Store alias-to-name map for fast alias getindex r=charleskawczynski a=charleskawczynski

This PR adds a `alias_to_name_map` to `AliasParamDict` so that we can easily support `getindex` so that these dicts behave like ordinary dicts:

```julia
Base.getindex(pd::ParamDict, i) = getindex(pd.data, i)
Base.getindex(pd::AliasParamDict, i) =
    getindex(pd.data, pd.alias_to_name_map[i])
```

Peeled off from #79.

This PR also bumps the version for a new release.

Co-authored-by: Charles Kawczynski <kawczynski.charles@gmail.com>
  • Loading branch information
bors[bot] and charleskawczynski authored May 27, 2022
2 parents e6d501c + 68779c1 commit a60edba
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "CLIMAParameters"
uuid = "6eacf6c3-8458-43b9-ae03-caf5306d3d53"
authors = ["Climate Modeling Alliance"]
version = "0.5.2"
version = "0.6.0"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand Down
15 changes: 15 additions & 0 deletions src/file_parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,20 @@ struct AliasParamDict{FT} <: AbstractTOMLDict{FT}
data::Dict
"either a nothing, or a dictionary representing an override parameter TOML file"
override_dict::Union{Nothing, Dict}
"Alias->name map"
alias_to_name_map::Dict
function AliasParamDict{FT}(
data::Dict,
override_dict::Union{Nothing, Dict},
) where {FT}
alias_to_name_map = Dict(map(collect(keys(data))) do key
Pair(data[key]["alias"], key)
end)
return new{FT}(data, override_dict, alias_to_name_map)
end
end


"""
float_type(::AbstractTOMLDict)
Expand Down Expand Up @@ -90,6 +102,9 @@ end
Base.iterate(pd::ParamDict, state) = Base.iterate(pd.data, state)
Base.iterate(pd::ParamDict) = Base.iterate(pd.data)

Base.getindex(pd::ParamDict, i) = getindex(pd.data, i)
Base.getindex(pd::AliasParamDict, i) =
getindex(pd.data, pd.alias_to_name_map[i])

"""
log_component!(pd::AbstractTOMLDict, names, component)
Expand Down

2 comments on commit a60edba

@charleskawczynski
Copy link
Member

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/61138

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.6.0 -m "<description of version>" a60edbad941c13aacac60ba0b16998802f52421d
git push origin v0.6.0

Please sign in to comment.