Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/support/parent_index_iterator.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ namespace wasm {

// A helper for defining iterators that contain references to some parent object
// and indices into that parent object. Provides operator implementations for
// equality, inequality, index updates, and index differences, but not for
// dereferencing.
// equality, inequality, index updates, index differences, and subscripting,
// but not for dereferencing (`operator*` or `operator->`).
//
// Users of this utility should subclass ParentIndexIterator<...> and define the
// following members in the subclass:
Expand Down Expand Up @@ -96,6 +96,10 @@ template<typename Parent, typename Iterator> struct ParentIndexIterator {
Iterator operator+(difference_type off) const {
return Iterator(self()) += off;
}
friend Iterator operator+(difference_type off,
const ParentIndexIterator& it) {
return it + off;
}
Iterator& operator-=(difference_type off) {
index -= off;
return self();
Expand All @@ -107,6 +111,9 @@ template<typename Parent, typename Iterator> struct ParentIndexIterator {
assert(parent == other.parent);
return index - other.index;
}
decltype(auto) operator[](difference_type off) const {
return *(self() + off);
}
};

} // namespace wasm
Expand Down
12 changes: 12 additions & 0 deletions test/gtest/inplace_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,15 @@ TEST_F(InplaceVectorTest, Insert) {
EXPECT_EQ(vec.size(), 6u);
EXPECT_EQ(vec[5], 50);
}

TEST_F(InplaceVectorTest, SortAndIteratorOps) {
inplace_vector<int, 5> vec{40, 10, 50, 20, 30};

// Test iterator subscripting and friend operator+
EXPECT_EQ(vec.begin()[1], 10);
EXPECT_EQ(*(2 + vec.begin()), 50);

// Test std::sort on inplace_vector iterators
std::sort(vec.begin(), vec.end());
EXPECT_EQ(vec, (inplace_vector<int, 5>{10, 20, 30, 40, 50}));
}
Loading