Skip to content

Commit a6cce85

Browse files
authored
Improve randname and Index constructors (#86)
1 parent 05b8816 commit a6cce85

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

Project.toml

Lines changed: 3 additions & 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.2.11"
4+
version = "0.2.12"
55

66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
@@ -10,6 +10,7 @@ FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b"
1010
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1111
MapBroadcast = "ebd9b9da-f48d-417c-9660-449667d60261"
1212
NamedDimsArrays = "60cbd0c0-df58-4cb7-918c-6f5607b73fde"
13+
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
1314
TensorAlgebra = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
1415
UnallocatedArrays = "43c9e47c-e622-40fb-bf18-a09fc8c466b6"
1516
UnspecifiedTypes = "42b3faec-625b-4613-8ddc-352bf9672b8d"
@@ -34,6 +35,7 @@ GradedArrays = "0.4"
3435
LinearAlgebra = "1.10"
3536
MapBroadcast = "0.1.5"
3637
NamedDimsArrays = "0.7"
38+
Random = "1.10"
3739
SparseArraysBase = "0.7"
3840
TensorAlgebra = "0.3, 0.4"
3941
UnallocatedArrays = "0.1.1"

src/index.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using NamedDimsArrays:
99
name,
1010
randname,
1111
setname
12+
using Random: Random, AbstractRNG
1213

1314
tagpairstring(pair::Pair) = repr(first(pair)) * "=>" * repr(last(pair))
1415
function tagsstring(tags::Dict{String,String})
@@ -24,10 +25,15 @@ struct IndexName <: AbstractName
2425
tags::Dict{String,String}
2526
plev::Int
2627
end
27-
function IndexName(; id::UInt64=rand(UInt64), tags=Dict{String,String}(), plev::Int=0)
28+
function IndexName(
29+
rng::AbstractRNG=Random.default_rng();
30+
id::UInt64=rand(rng, UInt64),
31+
tags=Dict{String,String}(),
32+
plev::Int=0,
33+
)
2834
return IndexName(id, Dict{String,String}(tags), plev)
2935
end
30-
NamedDimsArrays.randname(n::IndexName) = IndexName()
36+
NamedDimsArrays.randname(rng::AbstractRNG, ::Type{<:IndexName}) = IndexName(rng)
3137

3238
id(n::IndexName) = n.id
3339
tags(n::IndexName) = n.tags
@@ -81,8 +87,16 @@ struct Index{T,Value<:AbstractUnitRange{T}} <: AbstractNamedUnitRange{T,Value,In
8187
name::IndexName
8288
end
8389

90+
function Index{T,Value}(
91+
r::AbstractUnitRange{T}; kwargs...
92+
) where {T,Value<:AbstractUnitRange{T}}
93+
return Index{T,Value}(r, IndexName(; kwargs...))
94+
end
95+
function Index{T}(r::AbstractUnitRange{T}; kwargs...) where {T}
96+
return Index{T,typeof(r)}(r; kwargs...)
97+
end
8498
function Index(r::AbstractUnitRange; kwargs...)
85-
return Index(r, IndexName(; kwargs...))
99+
return Index{eltype(r)}(r; kwargs...)
86100
end
87101

88102
function Index(length::Int; kwargs...)

0 commit comments

Comments
 (0)