Skip to content

Commit a872b48

Browse files
authored
Improve printing, more general tags inputs (#35)
1 parent 3c68dd4 commit a872b48

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ITensorBase"
22
uuid = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.1.17"
4+
version = "0.1.18"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"

src/index.jl

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,24 @@ using NamedDimsArrays:
1010
randname,
1111
setname
1212

13-
tagsstring(tags::Dict{String,String}) = string(tags)
13+
tagpairstring(pair::Pair) = repr(first(pair)) * "=>" * repr(last(pair))
14+
function tagsstring(tags::Dict{String,String})
15+
tagpairs = sort(collect(tags); by=first)
16+
tagpair1, tagpair_rest = Iterators.peel(tagpairs)
17+
return mapreduce(*, tagpair_rest; init=tagpairstring(tagpair1)) do tagpair
18+
return "," * tagpairstring(tagpair)
19+
end
20+
end
1421

15-
@kwdef struct IndexName <: AbstractName
16-
id::UInt64 = rand(UInt64)
17-
tags::Dict{String,String} = Dict{String,String}()
18-
plev::Int = 0
22+
struct IndexName <: AbstractName
23+
id::UInt64
24+
tags::Dict{String,String}
25+
plev::Int
26+
end
27+
function IndexName(; id::UInt64=rand(UInt64), tags=Dict{String,String}(), plev::Int=0)
28+
return IndexName(id, Dict{String,String}(tags), plev)
1929
end
20-
NamedDimsArrays.randname(n::IndexName) = IndexName(; tags=tags(n), plev=plev(n))
30+
NamedDimsArrays.randname(n::IndexName) = IndexName()
2131

2232
id(n::IndexName) = n.id
2333
tags(n::IndexName) = n.tags
@@ -47,7 +57,7 @@ sim(n::IndexName) = randname(n)
4757

4858
function Base.show(io::IO, i::IndexName)
4959
idstr = "id=$(id(i) % 1000)"
50-
tagsstr = !isempty(tags(i)) ? "|\"$(tagsstring(tags(i)))\"" : ""
60+
tagsstr = !isempty(tags(i)) ? "|$(tagsstring(tags(i)))" : ""
5161
primestr = primestring(plev(i))
5262
str = "IndexName($(idstr)$(tagsstr))$(primestr)"
5363
print(io, str)
@@ -125,7 +135,7 @@ end
125135
function Base.show(io::IO, i::Index)
126136
lenstr = "length=$(dename(length(i)))"
127137
idstr = "|id=$(id(i) % 1000)"
128-
tagsstr = !isempty(tags(i)) ? "|\"$(tagsstring(tags(i)))\"" : ""
138+
tagsstr = !isempty(tags(i)) ? "|$(tagsstring(tags(i)))" : ""
129139
primestr = primestring(plev(i))
130140
str = "Index($(lenstr)$(idstr)$(tagsstr))$(primestr)"
131141
print(io, str)

test/test_basics.jl

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,28 @@ using Test: @test, @test_broken, @test_throws, @testset
6060
@test dename(i) == 1:2
6161
@test plev(i) == 0
6262
@test length(tags(i)) == 0
63+
64+
for i in (
65+
Index(2; tags=Dict(["X" => "Y"])),
66+
Index(2; tags=["X" => "Y"]),
67+
Index(2; tags=("X" => "Y",)),
68+
Index(2; tags="X" => "Y"),
69+
)
70+
@test Int(length(i)) == 2
71+
@test hastag(i, "X")
72+
@test gettag(i, "X") == "Y"
73+
@test plev(i) == 0
74+
@test length(tags(i)) == 1
75+
end
76+
end
77+
@testset "show" begin
78+
id = rand(UInt64)
79+
i = Index(2; id)
80+
@test sprint(show, "text/plain", i) == "Index(length=2|id=$(id % 1000))"
81+
82+
id = rand(UInt64)
83+
i = Index(2; id, tags=["X" => "Y"])
84+
@test sprint(show, "text/plain", i) == "Index(length=2|id=$(id % 1000)|\"X\"=>\"Y\")"
6385
end
6486
@testset "delta" begin
6587
i, j = Index.((2, 2))

0 commit comments

Comments
 (0)