diff --git a/Project.toml b/Project.toml index 220b83b..bb8971b 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "ITensorBase" uuid = "4795dd04-0d67-49bb-8f44-b89c448a1dc7" authors = ["ITensor developers and contributors"] -version = "0.1.17" +version = "0.1.18" [deps] Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697" diff --git a/src/index.jl b/src/index.jl index 39f145a..1e280e6 100644 --- a/src/index.jl +++ b/src/index.jl @@ -10,14 +10,24 @@ using NamedDimsArrays: randname, setname -tagsstring(tags::Dict{String,String}) = string(tags) +tagpairstring(pair::Pair) = repr(first(pair)) * "=>" * repr(last(pair)) +function tagsstring(tags::Dict{String,String}) + tagpairs = sort(collect(tags); by=first) + tagpair1, tagpair_rest = Iterators.peel(tagpairs) + return mapreduce(*, tagpair_rest; init=tagpairstring(tagpair1)) do tagpair + return "," * tagpairstring(tagpair) + end +end -@kwdef struct IndexName <: AbstractName - id::UInt64 = rand(UInt64) - tags::Dict{String,String} = Dict{String,String}() - plev::Int = 0 +struct IndexName <: AbstractName + id::UInt64 + tags::Dict{String,String} + plev::Int +end +function IndexName(; id::UInt64=rand(UInt64), tags=Dict{String,String}(), plev::Int=0) + return IndexName(id, Dict{String,String}(tags), plev) end -NamedDimsArrays.randname(n::IndexName) = IndexName(; tags=tags(n), plev=plev(n)) +NamedDimsArrays.randname(n::IndexName) = IndexName() id(n::IndexName) = n.id tags(n::IndexName) = n.tags @@ -47,7 +57,7 @@ sim(n::IndexName) = randname(n) function Base.show(io::IO, i::IndexName) idstr = "id=$(id(i) % 1000)" - tagsstr = !isempty(tags(i)) ? "|\"$(tagsstring(tags(i)))\"" : "" + tagsstr = !isempty(tags(i)) ? "|$(tagsstring(tags(i)))" : "" primestr = primestring(plev(i)) str = "IndexName($(idstr)$(tagsstr))$(primestr)" print(io, str) @@ -125,7 +135,7 @@ end function Base.show(io::IO, i::Index) lenstr = "length=$(dename(length(i)))" idstr = "|id=$(id(i) % 1000)" - tagsstr = !isempty(tags(i)) ? "|\"$(tagsstring(tags(i)))\"" : "" + tagsstr = !isempty(tags(i)) ? "|$(tagsstring(tags(i)))" : "" primestr = primestring(plev(i)) str = "Index($(lenstr)$(idstr)$(tagsstr))$(primestr)" print(io, str) diff --git a/test/test_basics.jl b/test/test_basics.jl index 01b5612..9b5dd11 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -60,6 +60,28 @@ using Test: @test, @test_broken, @test_throws, @testset @test dename(i) == 1:2 @test plev(i) == 0 @test length(tags(i)) == 0 + + for i in ( + Index(2; tags=Dict(["X" => "Y"])), + Index(2; tags=["X" => "Y"]), + Index(2; tags=("X" => "Y",)), + Index(2; tags="X" => "Y"), + ) + @test Int(length(i)) == 2 + @test hastag(i, "X") + @test gettag(i, "X") == "Y" + @test plev(i) == 0 + @test length(tags(i)) == 1 + end + end + @testset "show" begin + id = rand(UInt64) + i = Index(2; id) + @test sprint(show, "text/plain", i) == "Index(length=2|id=$(id % 1000))" + + id = rand(UInt64) + i = Index(2; id, tags=["X" => "Y"]) + @test sprint(show, "text/plain", i) == "Index(length=2|id=$(id % 1000)|\"X\"=>\"Y\")" end @testset "delta" begin i, j = Index.((2, 2))