Skip to content

Commit

Permalink
fixed ordering for Dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
simonmandlik committed Jun 3, 2024
1 parent 5c0dd05 commit 1b23776
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
7 changes: 2 additions & 5 deletions src/HierarchicalUtils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/maps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 9 additions & 17 deletions src/utilities.jl
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
_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
_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))
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 13 additions & 8 deletions test/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 1b23776

Please sign in to comment.