Skip to content

Commit 2c6a5ef

Browse files
committed
No fallback implementation for indexOf, use std::optional
1 parent 63b7866 commit 2c6a5ef

2 files changed

Lines changed: 15 additions & 22 deletions

File tree

include/openPMD/Iteration.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ namespace internal
136136
* of the owning map entry instead of a linear scan in
137137
* <code>Series::indexOf()</code>.
138138
*/
139-
uint64_t m_iterationIndex = 0;
139+
std::optional<uint64_t> m_iterationIndex = 0;
140140

141141
/**
142142
* Information on a parsing request that has not yet been executed.
@@ -265,7 +265,7 @@ class Iteration : public Attributable
265265
*/
266266
uint64_t getCachedIterationIndex() const
267267
{
268-
return get().m_iterationIndex;
268+
return *get().m_iterationIndex;
269269
}
270270
/**
271271
* @brief Has the iteration been parsed yet?

src/Series.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2606,30 +2606,23 @@ Series::iterations_iterator Series::indexOf(Iteration const &iteration)
26062606
auto &series = get();
26072607
// first try the cached index; if it points to the correct entry return it
26082608
auto idx = iteration.get().m_iterationIndex;
2609-
if (idx !=
2610-
0) // zero is default/unset but index 0 is valid, so we still check
2609+
if (!idx.has_value())
26112610
{
2612-
auto it = series.iterations.find(idx);
2613-
if (it != series.iterations.end() &&
2614-
&it->second.Attributable::get() == &iteration.Attributable::get())
2615-
{
2616-
return it;
2617-
}
2618-
// if the cached index is stale (shouldn't happen), fall back
2611+
throw error::Internal("Iteration index not known.");
26192612
}
2620-
// fallback to linear scan for safety
2621-
for (auto it = series.iterations.begin(); it != series.iterations.end();
2622-
++it)
2613+
2614+
auto it = series.iterations.find(*idx);
2615+
if (it != series.iterations.end() &&
2616+
&it->second.Attributable::get() == &iteration.Attributable::get())
26232617
{
2624-
if (&it->second.Attributable::get() == &iteration.Attributable::get())
2625-
{
2626-
// update cache so future calls are fast
2627-
it->second.get().m_iterationIndex = it->first;
2628-
return it;
2629-
}
2618+
return it;
2619+
}
2620+
else
2621+
{
2622+
throw error::Internal(
2623+
"Iteration " + std::to_string(*idx) +
2624+
" no longer known by the Series?");
26302625
}
2631-
throw std::runtime_error(
2632-
"[Iteration::close] Iteration not found in Series.");
26332626
}
26342627

26352628
AdvanceStatus Series::advance(

0 commit comments

Comments
 (0)