2020 */
2121
2222#include " openPMD/backend/Container.hpp"
23-
23+ # include " openPMD/Iteration.hpp " // needed for index caching in operator[]
2424/*
2525 * Instantiations in src/backend/Container.cpp
2626 * This file exists so that our tests can include the Container class with
@@ -149,6 +149,12 @@ auto Container<T, T_key, T_container>::operator[](key_type const &key)
149149 {
150150 ret.writable ().ownKeyWithinParent = std::to_string (key);
151151 }
152+ // remember our key inside the iteration object itself so that
153+ // Series::indexOf becomes constant/ log-time instead of linear.
154+ if constexpr (std::is_same_v<T, Iteration>)
155+ {
156+ ret.get ().m_iterationIndex = key;
157+ }
152158 traits::GenerationPolicy<T> gen;
153159 gen (ret);
154160 return ret;
@@ -181,6 +187,10 @@ auto Container<T, T_key, T_container>::operator[](key_type &&key)
181187 {
182188 ret.writable ().ownKeyWithinParent = std::to_string (std::move (key));
183189 }
190+ if constexpr (std::is_same_v<T, Iteration>)
191+ {
192+ ret.get ().m_iterationIndex = key;
193+ }
184194 traits::GenerationPolicy<T> gen;
185195 gen (ret);
186196 return ret;
@@ -201,13 +211,23 @@ template <typename T, typename T_key, typename T_container>
201211auto Container<T, T_key, T_container>::insert(value_type const &value)
202212 -> std::pair<iterator, bool >
203213{
204- return container ().insert (value);
214+ auto res = container ().insert (value);
215+ if constexpr (std::is_same_v<T, Iteration>)
216+ {
217+ res.first ->second .get ().m_iterationIndex = res.first ->first ;
218+ }
219+ return res;
205220}
206221template <typename T, typename T_key, typename T_container>
207222auto Container<T, T_key, T_container>::insert(value_type &&value)
208223 -> std::pair<iterator, bool >
209224{
210- return container ().insert (value);
225+ auto res = container ().insert (value);
226+ if constexpr (std::is_same_v<T, Iteration>)
227+ {
228+ res.first ->second .get ().m_iterationIndex = res.first ->first ;
229+ }
230+ return res;
211231}
212232template <typename T, typename T_key, typename T_container>
213233auto Container<T, T_key, T_container>::insert(
0 commit comments