3
3
struct IndexSet{IndexT <: Index }
4
4
data:: Vector{IndexT}
5
5
6
- function IndexSet {IndexT} (data) where {IndexT}
6
+ function IndexSet {IndexT} (data:: Vector{IndexT} ) where {IndexT}
7
7
@debug_check begin
8
8
if ! allunique (data)
9
9
error (" Trying to create IndexSet with collection of indices $data . Indices must be unique." )
10
10
end
11
11
end
12
- return new {IndexT} ([ data... ] )
12
+ return new {IndexT} (data)
13
13
end
14
14
15
15
"""
@@ -19,9 +19,15 @@ struct IndexSet{IndexT <: Index}
19
19
20
20
This is used as the `IndexSet` of an `emptyITensor()`, an ITensor with `NDTensors.Empty` storage and any number of indices.
21
21
"""
22
- IndexSet () = new {Union{}} ([])
22
+ IndexSet () = new {Union{}} (Any [])
23
23
end
24
24
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
+
25
31
@eval struct Order{N}
26
32
(OrderT:: Type{ <: Order} )() = $ (Expr (:new , :OrderT ))
27
33
end
@@ -38,6 +44,18 @@ the order of an ITensor.
38
44
"""
39
45
Order (N) = Order {N} ()
40
46
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
+
41
59
IndexSet (f:: Function , :: Order{N} ) where {N} =
42
60
IndexSet (ntuple (f, Val (N)))
43
61
@@ -72,7 +90,7 @@ IndexSet(inds::Index...) = IndexSet(inds)
72
90
73
91
IndexSet (is:: IndexSet ) = is
74
92
75
- IndexSet (:: Tuple{} ) = IndexSet {Union{}} ([])
93
+ IndexSet (:: Tuple{} ) = IndexSet () # IndexSet {Union{}}(Any [])
76
94
77
95
"""
78
96
convert(::Type{IndexSet}, t)
@@ -88,7 +106,11 @@ Base.convert(::Type{IndexSet{IndexT}}, t) where {IndexT} = IndexSet{IndexT}(t)
88
106
89
107
Base. convert (:: Type{IndexSet{IndexT}} , is:: IndexSet{IndexT} ) where {IndexT} = is
90
108
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))
92
114
93
115
"""
94
116
IndexSet(inds::Vector{<:Index})
@@ -178,7 +200,7 @@ the proper Arrow directions.
178
200
function Base. setindex (is:: IndexSet ,
179
201
i:: Index ,
180
202
n:: Int )
181
- return IndexSet (setindex! (data (is), i, n))
203
+ return IndexSet (setindex! (copy ( data (is) ), i, n))
182
204
end
183
205
184
206
"""
@@ -561,8 +583,8 @@ Note that this function is not type stable, since the number
561
583
of output indices is not known at compile time.
562
584
"""
563
585
Base. filter (f:: Function ,
564
- is:: IndexSet ) =
565
- IndexSet (filter (f, Tuple (is)))
586
+ is:: IndexSet ) =
587
+ IndexSet (filter (f, Tuple (is)))
566
588
567
589
Base. filter (is:: IndexSet , args... ; kwargs... ) =
568
590
filter (fmatch (args... ; kwargs... ), is)
0 commit comments