Add remove() to TSLQueue's iterator wrapper

Minor additions to UnitTest.
This commit is contained in:
Stephen Seo 2019-10-28 11:09:48 +09:00
parent 78862dc29d
commit 3dc12683e8
2 changed files with 89 additions and 8 deletions

View file

@ -50,6 +50,8 @@ class TSLQueue {
bool next();
bool prev();
std::optional<T> current();
/// can only be used when isRev is false
bool remove();
TSLQIterWrapper<isConst, isRev>& operator++();
TSLQIterWrapper<isConst, isRev>& operator--();
@ -329,8 +331,7 @@ template <bool isConst, bool isRev>
std::optional<T> TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::current() {
if(!isValid()) {
return std::nullopt;
} else {
if constexpr (isRev) {
} else if constexpr (isRev) {
if(containerPtr->rend() == iter) {
return std::nullopt;
}
@ -339,10 +340,25 @@ std::optional<T> TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::current() {
return std::nullopt;
}
}
}
return *iter;
}
template <typename T>
template <bool isConst, bool isRev>
bool TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::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 <typename T>
template <bool isConst, bool isRev>
typename TSLQueue<T>::template TSLQIterWrapper<isConst, isRev>& TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::operator++() {

View file

@ -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());
}
}