Add remove() to TSLQueue's iterator wrapper
Minor additions to UnitTest.
This commit is contained in:
parent
78862dc29d
commit
3dc12683e8
2 changed files with 89 additions and 8 deletions
|
@ -50,6 +50,8 @@ class TSLQueue {
|
||||||
bool next();
|
bool next();
|
||||||
bool prev();
|
bool prev();
|
||||||
std::optional<T> current();
|
std::optional<T> current();
|
||||||
|
/// can only be used when isRev is false
|
||||||
|
bool remove();
|
||||||
|
|
||||||
TSLQIterWrapper<isConst, isRev>& operator++();
|
TSLQIterWrapper<isConst, isRev>& operator++();
|
||||||
TSLQIterWrapper<isConst, isRev>& operator--();
|
TSLQIterWrapper<isConst, isRev>& operator--();
|
||||||
|
@ -329,20 +331,34 @@ template <bool isConst, bool isRev>
|
||||||
std::optional<T> TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::current() {
|
std::optional<T> TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::current() {
|
||||||
if(!isValid()) {
|
if(!isValid()) {
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
} else if constexpr (isRev) {
|
||||||
|
if(containerPtr->rend() == iter) {
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if constexpr (isRev) {
|
if(containerPtr->end() == iter) {
|
||||||
if(containerPtr->rend() == iter) {
|
return std::nullopt;
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if(containerPtr->end() == iter) {
|
|
||||||
return std::nullopt;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return *iter;
|
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 <typename T>
|
||||||
template <bool isConst, bool isRev>
|
template <bool isConst, bool isRev>
|
||||||
typename TSLQueue<T>::template TSLQIterWrapper<isConst, isRev>& TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::operator++() {
|
typename TSLQueue<T>::template TSLQIterWrapper<isConst, isRev>& TSLQueue<T>::TSLQIterWrapper<isConst, isRev>::operator++() {
|
||||||
|
|
|
@ -149,4 +149,69 @@ TEST(TSLQueue, Usage) {
|
||||||
ASSERT_TRUE(opt.has_value());
|
ASSERT_TRUE(opt.has_value());
|
||||||
EXPECT_EQ(opt.value(), 1);
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue