Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ITensorBase"
uuid = "4795dd04-0d67-49bb-8f44-b89c448a1dc7"
authors = ["ITensor developers <support@itensor.org> and contributors"]
version = "0.1.17"
version = "0.1.18"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
26 changes: 18 additions & 8 deletions src/index.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,24 @@
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)

Check warning on line 18 in src/index.jl

View check run for this annotation

Codecov / codecov/patch

src/index.jl#L18

Added line #L18 was not covered by tests
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
Expand Down Expand Up @@ -47,7 +57,7 @@

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)))" : ""

Check warning on line 60 in src/index.jl

View check run for this annotation

Codecov / codecov/patch

src/index.jl#L60

Added line #L60 was not covered by tests
primestr = primestring(plev(i))
str = "IndexName($(idstr)$(tagsstr))$(primestr)"
print(io, str)
Expand Down Expand Up @@ -125,7 +135,7 @@
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)
Expand Down
22 changes: 22 additions & 0 deletions test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Loading