Skip to content

Commit

Permalink
tbv2: improve equality for iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeHillion committed Oct 13, 2023
1 parent e867178 commit dcd5da2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
15 changes: 12 additions & 3 deletions include/oi/IntrospectionResult-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static_assert(false,
#define INCLUDED_OI_INTROSPECTIONRESULT_INL_H 1

#include <cassert>
#include <limits>

#include "IntrospectionResult.h"

Expand Down Expand Up @@ -72,9 +73,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 {
Expand Down
6 changes: 6 additions & 0 deletions include/oi/IntrospectionResult.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,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);
Expand Down
6 changes: 5 additions & 1 deletion oi/IntrospectionResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit dcd5da2

Please sign in to comment.