From 3dc12683e83479f83c14b6380984c5932433dabb Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Mon, 28 Oct 2019 11:09:48 +0900 Subject: [PATCH] Add remove() to TSLQueue's iterator wrapper Minor additions to UnitTest. --- cpp_impl/src/TSLQueue.hpp | 32 +++++++++++---- cpp_impl/src/test/TestTSLQueue.cpp | 65 ++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 8 deletions(-) diff --git a/cpp_impl/src/TSLQueue.hpp b/cpp_impl/src/TSLQueue.hpp index e2b7718..7d03f0c 100644 --- a/cpp_impl/src/TSLQueue.hpp +++ b/cpp_impl/src/TSLQueue.hpp @@ -50,6 +50,8 @@ class TSLQueue { bool next(); bool prev(); std::optional current(); + /// can only be used when isRev is false + bool remove(); TSLQIterWrapper& operator++(); TSLQIterWrapper& operator--(); @@ -329,20 +331,34 @@ template std::optional TSLQueue::TSLQIterWrapper::current() { if(!isValid()) { return std::nullopt; + } else if constexpr (isRev) { + if(containerPtr->rend() == iter) { + return std::nullopt; + } } else { - if constexpr (isRev) { - if(containerPtr->rend() == iter) { - return std::nullopt; - } - } else { - if(containerPtr->end() == iter) { - return std::nullopt; - } + if(containerPtr->end() == iter) { + return std::nullopt; } } return *iter; } +template +template +bool TSLQueue::TSLQIterWrapper::remove() { + if(!isValid()) { + return false; + } else if constexpr(isRev) { + return false; + } else { + if(containerPtr->end() == iter) { + return false; + } + } + iter = containerPtr->erase(iter); + return true; +} + template template typename TSLQueue::template TSLQIterWrapper& TSLQueue::TSLQIterWrapper::operator++() { diff --git a/cpp_impl/src/test/TestTSLQueue.cpp b/cpp_impl/src/test/TestTSLQueue.cpp index e06d75c..d5b97bc 100644 --- a/cpp_impl/src/test/TestTSLQueue.cpp +++ b/cpp_impl/src/test/TestTSLQueue.cpp @@ -149,4 +149,69 @@ TEST(TSLQueue, Usage) { ASSERT_TRUE(opt.has_value()); EXPECT_EQ(opt.value(), 1); } + + { + // remove center (2), result: 1, 3 + auto iter = q.iter(); + opt = iter.current(); + ASSERT_TRUE(opt.has_value()); + EXPECT_EQ(opt.value(), 1); + + EXPECT_TRUE(iter.next()); + opt = iter.current(); + ASSERT_TRUE(opt.has_value()); + EXPECT_EQ(opt.value(), 2); + + EXPECT_TRUE(iter.remove()); + opt = iter.current(); + ASSERT_TRUE(opt.has_value()); + EXPECT_EQ(opt.value(), 3); + + EXPECT_FALSE(iter.next()); + opt = iter.current(); + ASSERT_FALSE(opt.has_value()); + EXPECT_FALSE(iter.remove()); + } + + { + // remove first (1), result: 3 + auto iter = q.iter(); + opt = iter.current(); + ASSERT_TRUE(opt.has_value()); + EXPECT_EQ(opt.value(), 1); + + EXPECT_TRUE(iter.remove()); + opt = iter.current(); + ASSERT_TRUE(opt.has_value()); + EXPECT_EQ(opt.value(), 3); + + EXPECT_FALSE(iter.next()); + opt = iter.current(); + ASSERT_FALSE(opt.has_value()); + EXPECT_FALSE(iter.remove()); + } + + { + // remove (3), result: empty + auto iter = q.iter(); + opt = iter.current(); + ASSERT_TRUE(opt.has_value()); + EXPECT_EQ(opt.value(), 3); + + EXPECT_TRUE(iter.remove()); + opt = iter.current(); + EXPECT_FALSE(opt.has_value()); + EXPECT_FALSE(iter.remove()); + } + + { + auto iter = q.iter(); + opt = iter.current(); + EXPECT_FALSE(opt.has_value()); + } + { + auto riter = q.riter(); + opt = riter.current(); + EXPECT_FALSE(opt.has_value()); + } }