@@ -9,19 +9,60 @@ abstract type AbstractPropagationCache end
99# # The interface to hook into with each library
1010PropagationCache (thing:: AbstractTermSum ) = _thrownotimplemented (thing, :PropagationCache )
1111
12+ """
13+ mainsum(prop_cache::AbstractPropagationCache)
14+
15+ Returns the current main term sum from the propagation cache.
16+ It may be over-allocated and filled with trash terms.
17+ Use `extractsum!(prop_cache)` to get a properly-sized and valid term sum.
18+ """
1219mainsum (prop_cache:: AbstractPropagationCache ) = _thrownotimplemented (prop_cache, :mainsum )
20+
21+ """
22+ auxsum(prop_cache::AbstractPropagationCache)
23+
24+ Returns the current auxiliary term sum from the propagation cache.
25+ It may be over-allocated and filled with trash terms.
26+ """
1327auxsum (prop_cache:: AbstractPropagationCache ) = _thrownotimplemented (prop_cache, :auxsum )
1428
1529setmainsum! (prop_cache:: AbstractPropagationCache , new_mainsum) = _thrownotimplemented (prop_cache, :setmainsum! )
1630setauxsum! (prop_cache:: AbstractPropagationCache , new_auxsum) = _thrownotimplemented (prop_cache, :setauxsum! )
1731
32+
33+ # An optional interface for returning the mainsum when it is safe to return it as a whole or as a view.
34+ # Should leave the sum linked to the cache, and the cache unperturbed.
35+ # This is concrete type dependent.
36+ function activesum (prop_cache:: AbstractPropagationCache )
37+ _thrownotimplemented (prop_cache, :activesum )
38+ end
39+
40+ """
41+ swapsums!(prop_cache::AbstractPropagationCache)
42+
43+ Swaps the mainsum and auxsum pointers on the propagation cache.
44+ """
1845function swapsums! (prop_cache:: AbstractPropagationCache )
1946 temp = mainsum (prop_cache)
2047 setmainsum! (prop_cache, auxsum (prop_cache))
2148 setauxsum! (prop_cache, temp)
2249 return prop_cache
2350end
2451
52+ """
53+ copyswapsums!(prop_cache::AbstractPropagationCache)
54+
55+ Copies the active contents of the mainsum into the auxsum and then calls swapsums!(prop_cache).
56+ This is useful for when the current auxsum is meant to carry the final result.
57+ """
58+ function copyswapsums! (prop_cache:: AbstractPropagationCache )
59+ # instead of just swapping auxsum(prop_cache) and mainsum(prop_cache),
60+ # we need to copy the mainsum into the aux sum and then swap the sums
61+ copy! (auxsum (prop_cache), mainsum (prop_cache))
62+ swapsums! (prop_cache)
63+ return prop_cache
64+ end
65+
2566StorageType (prop_cache:: AbstractPropagationCache ) = StorageType (mainsum (prop_cache))
2667
2768nsites (prop_cache:: AbstractPropagationCache ) = nsites (mainsum (prop_cache))
@@ -69,25 +110,62 @@ activeindices(prop_cache::AbstractPropagationCache) = view(indices(prop_cache),
69110lastactiveindex (prop_cache:: AbstractPropagationCache ) = activeindices (prop_cache)[end ]
70111
71112
113+ mult! (prop_cache:: AbstractPropagationCache , scalar:: Number ) = mult! (mainsum (prop_cache), scalar)
72114
73115function Base. resize! (prop_cache:: AbstractPropagationCache , new_size:: Int )
74116 _thrownotimplemented (prop_cache, :resize! )
75117end
76118
77119# # Back-conversions
120+
121+ # effectively a out-of-place version of extractsum!()
78122function (:: Type{TS} )(prop_cache:: AbstractPropagationCache ) where TS<: AbstractTermSum
79- return convert (TS, _cachetosum! (StorageType (prop_cache), prop_cache))
123+ return convert (TS, extractsum! (deepcopy (prop_cache)))
124+ end
125+
126+ """
127+ extractsum!(prop_cache::AbstractPropagationCache, which_sum::AbstractTermSum)
128+
129+ Extracts the indicated sum `which_sum` from the propagation cache.
130+ This resizes the cache to the active size and returns the indicated sum.
131+ Further manipulating prop_cache or the returned sum may invalidate the other.
132+ If the indicated sum is the auxsum, active contents will be copied over from mainsum.
133+ If neither `mainsum(prop_cache)` nor `auxsum(prop_cache)` is indicated, an error is thrown.
134+ """
135+ function extractsum! (prop_cache:: AbstractPropagationCache , which_sum:: AbstractTermSum )
136+ if which_sum != = mainsum (prop_cache) && which_sum != = auxsum (prop_cache)
137+ throw (ArgumentError (" Indicated sum is not part of the propagation cache." ))
138+ end
139+
140+ # if the indicated sum is not the mainsum, it is the auxsum
141+ # copy contents over and extract
142+ if which_sum != = mainsum (prop_cache)
143+ copyswapsums! (prop_cache)
144+ end
145+
146+ return extractsum! (prop_cache)
80147end
81148
82- _cachetosum! (:: DictStorage , prop_cache:: AbstractPropagationCache ) = mainsum (prop_cache)
149+ """
150+ extractsum!(prop_cache::AbstractPropagationCache)
83151
84- function _cachetosum! (:: ArrayStorage , prop_cache:: AbstractPropagationCache )
85- # convert back to TermSum
86- term_sum = mainsum (prop_cache)
87- term_sum = resize! (term_sum, activesize (prop_cache))
88- return term_sum
152+ Extracts the mainsum from the propagation cache.
153+ This resizes the cache to the active size and returns the mainsum.
154+ Further manipulating prop_cache or the returned sum may invalidate the other.
155+ """
156+ function extractsum! (prop_cache:: AbstractPropagationCache )
157+ return _extractsum! (StorageType (prop_cache), prop_cache)
89158end
90159
91- function _cachetosum! (:: Type{ST} , :: PC ) where {ST<: StorageType ,PC<: AbstractPropagationCache }
92- throw (ErrorException (" cachetosum!(::$(ST) , ::$(PC) ) not implemented." ))
160+
161+ _extractsum! (:: DictStorage , prop_cache:: AbstractPropagationCache ) = mainsum (prop_cache)
162+
163+ function _extractsum! (:: ArrayStorage , prop_cache:: AbstractPropagationCache )
164+ # resize the entire cache to retain validity
165+ resize! (prop_cache, activesize (prop_cache))
166+ return mainsum (prop_cache)
93167end
168+
169+ function _extractsum! (:: Type{ST} , :: PC ) where {ST<: StorageType ,PC<: AbstractPropagationCache }
170+ throw (ErrorException (" extractsum!(::$(ST) , ::$(PC) ) not implemented." ))
171+ end
0 commit comments