From f0757e986e55c94bd6b25e342ba11d55fcbe801b Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Tue, 14 Jan 2025 12:20:04 +0100 Subject: [PATCH] added erase() method to yarp::sig::Vector --- src/libYARP_sig/src/yarp/sig/Vector.h | 23 +++++++++++++++++++++-- src/libYARP_sig/tests/VectorTest.cpp | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/libYARP_sig/src/yarp/sig/Vector.h b/src/libYARP_sig/src/yarp/sig/Vector.h index 1376033f8b7..e287afcce3a 100644 --- a/src/libYARP_sig/src/yarp/sig/Vector.h +++ b/src/libYARP_sig/src/yarp/sig/Vector.h @@ -224,7 +224,26 @@ class yarp::sig::VectorOf : public VectorBase } /** - * Resize the vector and initilize the element to a default value. + * Remove an element from the vector. + * @param pos iterator pointing at the element to remove + */ + void erase(iterator pos) + { + bytes.erase(pos); + } + + /** + * Remove one or more elements from the vector. + * @param first iterator pointing at the first element to remove + * @param last iterator pointing at the last element to remove + */ + void erase(iterator first, iterator last) + { + bytes.erase(first, last); + } + + /** + * Resize the vector and initialize the element to a default value. * @param s the new size * @param def the default value */ @@ -263,7 +282,7 @@ class yarp::sig::VectorOf : public VectorBase /** * @brief Construct a new element in the vector: size is changed * @param args, arguments to be forwarded for constructing the new element. - * @return the reference to the new element contructed. + * @return the reference to the new element constructed. */ template inline T& emplace_back(_Args&&... args) diff --git a/src/libYARP_sig/tests/VectorTest.cpp b/src/libYARP_sig/tests/VectorTest.cpp index 0992de91409..aa6d875f643 100644 --- a/src/libYARP_sig/tests/VectorTest.cpp +++ b/src/libYARP_sig/tests/VectorTest.cpp @@ -380,7 +380,20 @@ TEST_CASE("sig::VectorTest", "[yarp::sig]") CHECK(v[2] == 3.0); // Checking data consistency } - SECTION("Checking the the for range based") + SECTION("Checking the erase method") + { + Vector v{0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0}; + Vector vtest1{0.0, 1.0, 3.0, 4.0, 5.0, 6.0}; + Vector vtest2{0.0, 1.0, 5.0, 6.0}; + + v.erase(v.begin() + 2); + CHECK(v == vtest1); + + v.erase(v.begin() + 2, v.begin()+4); + CHECK(v == vtest2); + } + + SECTION("Checking the for range based") { Vector v{0.0, 1.0, 2.0, 3.0, 4.0}; double i = 0.0;