Skip to content

Commit 690679e

Browse files
committed
Touch up
1 parent 76958e4 commit 690679e

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/indexset.jl

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
struct IndexSet{IndexT <: Index}
44
data::Vector{IndexT}
55

6-
function IndexSet{IndexT}(data) where {IndexT}
6+
function IndexSet{IndexT}(data::Vector{IndexT}) where {IndexT}
77
@debug_check begin
88
if !allunique(data)
99
error("Trying to create IndexSet with collection of indices $data. Indices must be unique.")
1010
end
1111
end
12-
return new{IndexT}([data...])
12+
return new{IndexT}(data)
1313
end
1414

1515
"""
@@ -19,9 +19,15 @@ struct IndexSet{IndexT <: Index}
1919
2020
This is used as the `IndexSet` of an `emptyITensor()`, an ITensor with `NDTensors.Empty` storage and any number of indices.
2121
"""
22-
IndexSet() = new{Union{}}([])
22+
IndexSet() = new{Union{}}(Any[])
2323
end
2424

25+
function IndexSet{Union{}}(data::Vector{<:Any})
26+
return IndexSet{Union{}}(copy(data))
27+
end
28+
IndexSet{Union{}}(()) = IndexSet{Union{}}(Any[])
29+
IndexSet{IndexT}(data) where {IndexT} = IndexSet{IndexT}(collect(data))
30+
2531
@eval struct Order{N}
2632
(OrderT::Type{ <: Order})() = $(Expr(:new, :OrderT))
2733
end
@@ -38,6 +44,18 @@ the order of an ITensor.
3844
"""
3945
Order(N) = Order{N}()
4046

47+
"""
48+
IndexSet(::Function, ::Order{N})
49+
Construct an IndexSet of length N from a function that accepts
50+
an integer between 1:N and returns an Index.
51+
# Examples
52+
```julia
53+
IndexSet(n -> Index(1, "i\$n"), Order(4))
54+
```
55+
"""
56+
IndexSet(f::Function, N::Int) =
57+
IndexSet(ntuple(f, N))
58+
4159
IndexSet(f::Function, ::Order{N}) where {N} =
4260
IndexSet(ntuple(f, Val(N)))
4361

@@ -72,7 +90,7 @@ IndexSet(inds::Index...) = IndexSet(inds)
7290

7391
IndexSet(is::IndexSet) = is
7492

75-
IndexSet(::Tuple{}) = IndexSet{Union{}}([])
93+
IndexSet(::Tuple{}) = IndexSet()#IndexSet{Union{}}(Any[])
7694

7795
"""
7896
convert(::Type{IndexSet}, t)
@@ -88,7 +106,11 @@ Base.convert(::Type{IndexSet{IndexT}}, t) where {IndexT} = IndexSet{IndexT}(t)
88106

89107
Base.convert(::Type{IndexSet{IndexT}}, is::IndexSet{IndexT}) where {IndexT} = is
90108

91-
Base.Tuple(is::IndexSet) = Tuple(data(is))
109+
const ValCacheLength = 100
110+
const ValCache = Dict([n=>Val(n) for n in 0:ValCacheLength])
111+
_NTuple(::Val{N}, v::Vector{T}) where {N, T} = ntuple(n -> v[n], Val(N))
112+
_Tuple(v::Vector{T}) where {T} = _NTuple(ValCache[length(v)], v)
113+
Base.Tuple(is::IndexSet) = _Tuple(data(is))
92114

93115
"""
94116
IndexSet(inds::Vector{<:Index})
@@ -178,7 +200,7 @@ the proper Arrow directions.
178200
function Base.setindex(is::IndexSet,
179201
i::Index,
180202
n::Int)
181-
return IndexSet(setindex!(data(is), i, n))
203+
return IndexSet(setindex!(copy(data(is)), i, n))
182204
end
183205

184206
"""
@@ -561,8 +583,8 @@ Note that this function is not type stable, since the number
561583
of output indices is not known at compile time.
562584
"""
563585
Base.filter(f::Function,
564-
is::IndexSet) =
565-
IndexSet(filter(f, Tuple(is)))
586+
is::IndexSet) =
587+
IndexSet(filter(f, Tuple(is)))
566588

567589
Base.filter(is::IndexSet, args...; kwargs...) =
568590
filter(fmatch(args...; kwargs...), is)

src/itensor.jl

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,13 @@ has the same indices.
207207
"""
208208
itensor(T::Tensor) = itensor(store(T), inds(T))
209209

210-
const ValCacheLength = 100
211-
const ValCache = Dict([n=>Val(n) for n in 0:ValCacheLength])
212-
_NTuple(::Val{N}, v::Vector{T}) where {N, T} = ntuple(n -> v[n], Val(N))
213-
_Tuple(v::Vector{T}) where {T} = _NTuple(ValCache[length(v)], v)
214-
215210
"""
216211
tensor(::ITensor)
217212
218213
Convert the ITensor to a Tensor that shares the same
219214
storage and indices as the ITensor.
220215
"""
221-
tensor(A::ITensor) = tensor(store(A), _Tuple(inds(A).data))
216+
tensor(A::ITensor) = tensor(store(A), Tuple(inds(A)))
222217

223218
"""
224219
ITensor([::Type{ElT} = Float64, ]inds)

0 commit comments

Comments
 (0)