From 1b23776c111a4454be8c13b3160d57736844f134 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Mandl=C3=ADk?= Date: Mon, 3 Jun 2024 17:46:46 +0200 Subject: [PATCH] fixed ordering for Dicts --- src/HierarchicalUtils.jl | 7 ++----- src/iterators.jl | 2 +- src/maps.jl | 2 +- src/printing.jl | 4 ++-- src/utilities.jl | 26 +++++++++----------------- test/runtests.jl | 2 +- test/utilities.jl | 21 +++++++++++++-------- 7 files changed, 29 insertions(+), 35 deletions(-) diff --git a/src/HierarchicalUtils.jl b/src/HierarchicalUtils.jl index 0888fc4..ef8119e 100644 --- a/src/HierarchicalUtils.jl +++ b/src/HierarchicalUtils.jl @@ -10,7 +10,7 @@ struct InnerNode <: NodeType end const PairVec = Vector{<:Pair} NodeType(::Type{T}) where T = error("Define NodeType(::Type{$T}) to be either LeafNode() or InnerNode()") -NodeType(x::T) where T = NodeType(T) +NodeType(::T) where T = NodeType(T) isleaf(n) = isleaf(NodeType(n), n) isleaf(::LeafNode, _) = true @@ -23,11 +23,8 @@ children(_, ::T) where T = printchildren(n) = children(n) -# TODO incorporate setfield -# set_children(n::T, chs::U) where {T, U} = error("Define set_children(n::$T, chs::$U) where chs are new children to use PreOrder maps") - nodeshow(io, x) = Base.show(io, x) -nodecommshow(io, x) = nothing +nodecommshow(_, _) = nothing nchildren(n) = nchildren(NodeType(n), n) nchildren(::LeafNode, n) = 0 diff --git a/src/iterators.jl b/src/iterators.jl index b286843..0221a23 100644 --- a/src/iterators.jl +++ b/src/iterators.jl @@ -126,7 +126,7 @@ end # return r, s # end -# expand(n, s) = append!(s, reverse(collect(_children_sorted(n)))) +# expand(n, s) = append!(s, reverse(collect(_children_ordered(n)))) # function nextstate(it, s) # isempty(s) && return nothing diff --git a/src/maps.jl b/src/maps.jl index 89213fb..b4c1910 100644 --- a/src/maps.jl +++ b/src/maps.jl @@ -22,7 +22,7 @@ function _treemap(f::Function, ts::Tuple, complete::Bool, order::PreOrder) end # function _treemap(f::Function, ts::Tuple, complete::Bool, order::PreOrder) -# n = f(ts, _children_sorted.(ts)) +# n = f(ts, _children_ordered.(ts)) # isleaf(n) && return n # nchs = children(n) # chs = _children_pairs_keys(ts, complete) diff --git a/src/printing.jl b/src/printing.jl index d785b32..561fff6 100644 --- a/src/printing.jl +++ b/src/printing.jl @@ -57,7 +57,7 @@ function paddedprint(io, s; pad=[], color=:default, kwargs...) end _printkeys(ch) = ["" for _ in eachindex(ch)] -_printkeys(ch::Union{NamedTuple, Dict, OrderedDict}) = ["$k: " for k in keys(ch)] +_printkeys(ch::Union{NamedTuple, AbstractDict}) = ["$k: " for k in keys(ch)] _printkeys(ch::PairVec) = ["$k: " for (k, _) in ch] function _print_current(printer, n, c, e, trav, comments) @@ -87,7 +87,7 @@ function _printtree(printer, n, C, d, p, pl, e, trav, htrunc, vtrunc, comments) c = isa(NodeType(n), LeafNode) ? :default : C[1 + d % length(C)] gap = _print_current(printer, n, c, e, trav, comments) - CH = printchildren(n) + CH = _impose_order(printchildren(n)) PK = _printkeys(CH) CH = _iter(CH) nch = length(CH) diff --git a/src/utilities.jl b/src/utilities.jl index db39c4e..d702aa5 100644 --- a/src/utilities.jl +++ b/src/utilities.jl @@ -1,25 +1,17 @@ -_childsort(x::Union{Tuple, Vector, OrderedDict}) = x -_childsort(x::Dict) = sort!(OrderedDict(x)) -_childsort(x::PairVec) = sort(x, by=first) -function _childsort(x::NamedTuple{T}) where T - ks = tuple(sort(collect(T))...) - NamedTuple{ks}(x[k] for k in ks) -end -_children_sorted(n) = _childsort(children(n)) +_impose_order(x::Union{Tuple, NamedTuple, Vector, OrderedDict}) = x +_impose_order(x::AbstractDict) = sort!(OrderedDict(x)) + +_children_ordered(n) = _impose_order(children(n)) _iter(x::PairVec) = last.(x) _iter(x) = values(x) _isnothing_iter(x) = isnothing.(_iter(x)) -function _ith_child(m::Union{Tuple, Vector, NamedTuple}, i::Integer) - return m[i] -end +_ith_child(m::Union{Tuple, Vector, NamedTuple}, i::Integer) = m[i] _ith_child(m::OrderedDict, i::Integer) = _ith_child(collect(m), i) -_ith_child(m::AbstractDict, i::Integer) = _ith_child(_childsort(m), i) -function _ith_child(m::PairVec, i::Integer) - return last(m[i]) -end +_ith_child(m::AbstractDict, i::Integer) = _ith_child(_impose_order(m), i) +_ith_child(m::PairVec, i::Integer) = last(m[i]) _indexed(::Union{Vector, Tuple}) = true _indexed(_) = false @@ -27,8 +19,8 @@ _named(::Union{NamedTuple, Dict, OrderedDict, PairVec}) = true _named(_) = false function _children_pairs(ts, complete::Bool) - chss1 = [_children_sorted(t) for t in ts if !(isnothing(t) || isleaf(t))] - chss2 = [isnothing(t) || isleaf(t) ? nothing : _children_sorted(t) for t in ts] + chss1 = [_children_ordered(t) for t in ts if !(isnothing(t) || isleaf(t))] + chss2 = [isnothing(t) || isleaf(t) ? nothing : _children_ordered(t) for t in ts] isempty(chss1) && return chss1 # type stability here is impossible if all(isa.(chss2, PairVec)) diff --git a/test/runtests.jl b/test/runtests.jl index 2910671..d9681c0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -3,7 +3,7 @@ using HierarchicalUtils using Base.Iterators: product using Combinatorics using HierarchicalUtils: OrderedDict -import HierarchicalUtils: _children_pairs, _childsort, _iter +import HierarchicalUtils: _children_pairs, _impose_order, _iter import HierarchicalUtils: _node_predicate, PairVec using Random; Random.seed!(42) diff --git a/test/utilities.jl b/test/utilities.jl index c36aaab..40edee5 100644 --- a/test/utilities.jl +++ b/test/utilities.jl @@ -44,9 +44,11 @@ n4t = () @test _children_pairs((n, n5nt, nothing), false) == NamedTuple() @test _children_pairs((n, nothing), false) == NamedTuple() @test _children_pairs((n, n5nt), false) == NamedTuple() - res = (; (Symbol(k) => (ch, nothing, nothing) for (k,ch) in _childsort(children(n)) |> pairs)...) + chs = children(n) + ks = sort(collect(keys(chs))) + res = (; (Symbol(k) => (chs[k], nothing, nothing) for k in ks)...) @test _children_pairs((n, n5nt, nothing), true) == res - res = (; (Symbol(k) => (ch, nothing) for (k,ch) in _childsort(children(n)) |> pairs)...) + res = (; (Symbol(k) => (chs[k], nothing) for k in ks)...) @test _children_pairs((n, nothing), true) == res @test _children_pairs((n, n5nt), true) == res end @@ -103,9 +105,12 @@ end @test _children_pairs((n, n5pv, nothing), false) == [] @test _children_pairs((n, nothing), false) == [] @test _children_pairs((n, n5pv), false) == [] - res = [k => (ch, nothing, nothing) for (k,ch) in _childsort(children(n))] + chs = children(n) + ks = sort(first.(chs)) + chs = Dict(chs) + res = [k => (chs[k], nothing, nothing) for k in ks] @test _children_pairs((n, n5pv, nothing), true) == res - res = [k => (ch, nothing) for (k,ch) in _childsort(children(n))] + res = [k => (chs[k], nothing) for k in ks] @test _children_pairs((n, nothing), true) == res @test _children_pairs((n, n5pv), true) == res end @@ -138,9 +143,9 @@ end @test _children_pairs((n, n4v, nothing), false) == [] @test _children_pairs((n, nothing), false) == [] @test _children_pairs((n, n4v), false) == [] - res = [(ch, nothing, nothing) for ch in _childsort(children(n))] + res = [(ch, nothing, nothing) for ch in children(n)] @test _children_pairs((n, n4v, nothing), true) == res - res = [(ch, nothing) for ch in _childsort(children(n))] + res = [(ch, nothing) for ch in children(n)] @test _children_pairs((n, nothing), true) == res @test _children_pairs((n, n4v), true) == res end @@ -160,9 +165,9 @@ end @test _children_pairs((n, n4t, nothing), false) == () @test _children_pairs((n, nothing), false) == () @test _children_pairs((n, n4t), false) == () - res = tuple(((ch, nothing, nothing) for ch in _childsort(children(n)))...) + res = tuple(((ch, nothing, nothing) for ch in children(n))...) @test _children_pairs((n, n4t, nothing), true) == res - res = tuple(((ch, nothing) for ch in _childsort(children(n)))...) + res = tuple(((ch, nothing) for ch in children(n))...) @test _children_pairs((n, nothing), true) == res @test _children_pairs((n, n4t), true) == res end