tbv2: improve equality for iterator

This commit is contained in:
Jake Hillion 2023-10-12 21:33:39 -07:00 committed by Jake Hillion
parent e7581ad915
commit 4563cbe713
3 changed files with 22 additions and 4 deletions

View File

@ -72,9 +72,17 @@ inline const result::Element* IntrospectionResult::const_iterator::operator->()
inline bool IntrospectionResult::const_iterator::operator==(
const IntrospectionResult::const_iterator& that) const {
return this->data_ == that.data_ && !this->next_.has_value() &&
!that.next_.has_value(); // TODO: is this sufficient? kind of hacky as
// this only works for comparing to .end()
// Case 1: The next data to read differs, thus the iterators are different.
if (this->data_ != that.data_)
return false;
// Case 2: Both iterators have no next value, thus they are both complete. It
// is insufficient to check increments as the number of increments is unknown
// when constructing `end()`.
if (!this->next_.has_value() && !that.next_.has_value())
return true;
// Case 3: The iterators are reading the same data. If they have produced the
// same number of elements they are equal, else they are not.
return this->increments_ == that.increments_;
}
inline bool IntrospectionResult::const_iterator::operator!=(
const IntrospectionResult::const_iterator& that) const {

View File

@ -63,6 +63,12 @@ class IntrospectionResult {
// improvement but it isn't copyable. A string type with size fixed at
// construction would also be good.
std::list<std::string> dynamic_type_path_;
// We cannot track the position in the iteration solely by the underlying
// iterator as some fields do not extract data (for example, primitives).
// Track the number of increment operations as well to get an accurate
// equality check.
uint64_t increments_ = 0;
};
IntrospectionResult(std::vector<uint8_t> buf, exporters::inst::Inst inst);

View File

@ -30,9 +30,13 @@ namespace oi {
IntrospectionResult::const_iterator&
IntrospectionResult::const_iterator::operator++() {
if (stack_.empty()) {
next_ = std::nullopt;
if (next_ != std::nullopt) {
++increments_;
next_ = std::nullopt;
}
return *this;
}
++increments_;
auto el = stack_.top();
stack_.pop();