Skip to content

Commit

Permalink
Add two-iterator erase to IndexedVector (p4lang#5082)
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimír Štill <[email protected]>
  • Loading branch information
vlstill authored Jan 2, 2025
1 parent 1dc0afa commit f554010
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions ir/indexed_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ class IndexedVector : public Vector<T> {
Util::Enumerator<const IDeclaration *> *getDeclarations() const {
return Util::enumerate(Values(declarations));
}
iterator erase(iterator from, iterator to) {
for (auto it = from; it != to; ++it) {
removeFromMap(*it);
}
return Vector<T>::erase(from, to);
}
iterator erase(iterator i) {
removeFromMap(*i);
return Vector<T>::erase(i);
Expand Down
45 changes: 45 additions & 0 deletions test/gtest/indexed_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,49 @@ TEST(IndexedVector, Vector_ctor) {
EXPECT_EQ(vec[1]->name.name, "bar");
}

TEST(IndexedVector, erase) {
TestVector vec(Vector<StructField>{
testItem("a"_cs),
testItem("b"_cs),
testItem("c"_cs),
testItem("d"_cs),
testItem("e"_cs),
testItem("f"_cs),
testItem("g"_cs),
});
EXPECT_EQ(vec.size(), 7);
vec.erase(std::next(vec.begin(), 1)); // a c d e f g
EXPECT_EQ(vec.size(), 6);
EXPECT_FALSE(vec.getDeclaration("b"));
vec.validate();

vec.erase(std::next(vec.begin(), 2), std::next(vec.begin(), 4)); // a c f g
EXPECT_EQ(vec.size(), 4);
EXPECT_EQ(vec[0]->name.name, "a");
EXPECT_EQ(vec[1]->name.name, "c");
EXPECT_EQ(vec[2]->name.name, "f");
EXPECT_EQ(vec[3]->name.name, "g");
EXPECT_FALSE(vec.getDeclaration("d"));
vec.validate();

vec.erase(vec.end(), vec.end());
EXPECT_EQ(vec.size(), 4);
EXPECT_EQ(vec[0]->name.name, "a");
EXPECT_EQ(vec[1]->name.name, "c");
EXPECT_EQ(vec[2]->name.name, "f");
EXPECT_EQ(vec[3]->name.name, "g");
EXPECT_FALSE(vec.getDeclaration("e"));
vec.validate();

vec.erase(std::next(vec.begin(), 2), vec.end()); // a c
EXPECT_EQ(vec.size(), 2);
EXPECT_EQ(vec[0]->name.name, "a");
EXPECT_EQ(vec[1]->name.name, "c");

EXPECT_TRUE(vec.getDeclaration("a"));
EXPECT_TRUE(vec.getDeclaration("c"));
EXPECT_FALSE(vec.getDeclaration("f"));
vec.validate();
}

} // namespace P4::Test

0 comments on commit f554010

Please sign in to comment.