diff options
author | vng <viktor.govako@gmail.com> | 2015-08-18 17:30:38 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-09-23 03:01:34 +0300 |
commit | fb7e49b8285f92eb796a8a156461eeba5bbcbb83 (patch) | |
tree | 347be7653e973b1ef4833c91eb241bd93e1885bf /base | |
parent | 6f4b88973b598abc92adf919587cdb6d7c41f929 (diff) |
Added buffer_vector::erase_if.
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/buffer_vector_test.cpp | 20 | ||||
-rw-r--r-- | base/buffer_vector.hpp | 10 |
2 files changed, 30 insertions, 0 deletions
diff --git a/base/base_tests/buffer_vector_test.cpp b/base/base_tests/buffer_vector_test.cpp index 40a34b5615..b63f3c929f 100644 --- a/base/base_tests/buffer_vector_test.cpp +++ b/base/base_tests/buffer_vector_test.cpp @@ -331,3 +331,23 @@ UNIT_TEST(BufferVectorMove) v1 = move(v2); TestVector(v1, 3); } + +UNIT_TEST(BufferVector_EraseIf) +{ + buffer_vector<int, 2> v; + v.push_back(1); + v.push_back(2); + v.erase_if([] (int x) { return x == 1; }); + TEST_EQUAL(v.size(), 1, ()); + TEST_EQUAL(v[0], 2, ()); + + v.push_back(3); + v.push_back(4); + v.erase_if([] (int x) { return x == 3; }); + TEST_EQUAL(v.size(), 2, ()); + TEST_EQUAL(v[0], 2, ()); + TEST_EQUAL(v[1], 4, ()); + + v.erase_if([] (int x) { return true; }); + TEST_EQUAL(v.size(), 0, ()); +} diff --git a/base/buffer_vector.hpp b/base/buffer_vector.hpp index 0d012209dd..df303b12a5 100644 --- a/base/buffer_vector.hpp +++ b/base/buffer_vector.hpp @@ -373,6 +373,16 @@ public: insert(where, &value, &value + 1); } + template <class TFn> + void erase_if(TFn fn) + { + iterator b = begin(); + iterator e = end(); + iterator i = remove_if(b, e, fn); + if (i != e) + resize(distance(b, i)); + } + private: void SwitchToDynamic() { |