Fix size count in TSLQueue when using iterator
This commit is contained in:
parent
3f338be365
commit
a5873624aa
2 changed files with 18 additions and 4 deletions
|
@ -62,7 +62,8 @@ class TSLQueue {
|
||||||
class TSLQIter {
|
class TSLQIter {
|
||||||
public:
|
public:
|
||||||
TSLQIter(std::mutex &mutex,
|
TSLQIter(std::mutex &mutex,
|
||||||
std::weak_ptr<TSLQNode> currentNode);
|
std::weak_ptr<TSLQNode> currentNode,
|
||||||
|
unsigned long long *msize);
|
||||||
~TSLQIter();
|
~TSLQIter();
|
||||||
|
|
||||||
std::optional<T> current();
|
std::optional<T> current();
|
||||||
|
@ -73,6 +74,8 @@ class TSLQueue {
|
||||||
private:
|
private:
|
||||||
std::lock_guard<std::mutex> lock;
|
std::lock_guard<std::mutex> lock;
|
||||||
std::weak_ptr<TSLQNode> currentNode;
|
std::weak_ptr<TSLQNode> currentNode;
|
||||||
|
unsigned long long *msize;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -273,9 +276,11 @@ bool TSLQueue<T>::TSLQNode::isNormal() const {
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
TSLQueue<T>::TSLQIter::TSLQIter(std::mutex &mutex,
|
TSLQueue<T>::TSLQIter::TSLQIter(std::mutex &mutex,
|
||||||
std::weak_ptr<TSLQNode> currentNode) :
|
std::weak_ptr<TSLQNode> currentNode,
|
||||||
|
unsigned long long *msize) :
|
||||||
lock(mutex),
|
lock(mutex),
|
||||||
currentNode(currentNode)
|
currentNode(currentNode),
|
||||||
|
msize(msize)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -335,12 +340,15 @@ bool TSLQueue<T>::TSLQIter::remove() {
|
||||||
currentNode->next->prev = parent;
|
currentNode->next->prev = parent;
|
||||||
parent->next = currentNode->next;
|
parent->next = currentNode->next;
|
||||||
|
|
||||||
|
assert(*msize > 0);
|
||||||
|
--(*msize);
|
||||||
|
|
||||||
return parent->next->isNormal();
|
return parent->next->isNormal();
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename TSLQueue<T>::TSLQIter TSLQueue<T>::begin() {
|
typename TSLQueue<T>::TSLQIter TSLQueue<T>::begin() {
|
||||||
return TSLQIter(mutex, head->next);
|
return TSLQIter(mutex, head->next, &msize);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -123,6 +123,7 @@ TEST(TSLQueue, Iterator) {
|
||||||
for(int i = 0; i < 10; ++i) {
|
for(int i = 0; i < 10; ++i) {
|
||||||
q.push(i);
|
q.push(i);
|
||||||
}
|
}
|
||||||
|
EXPECT_EQ(q.size(), 10);
|
||||||
|
|
||||||
{
|
{
|
||||||
// iteration
|
// iteration
|
||||||
|
@ -175,6 +176,7 @@ TEST(TSLQueue, Iterator) {
|
||||||
EXPECT_TRUE(op.has_value());
|
EXPECT_TRUE(op.has_value());
|
||||||
EXPECT_EQ(op.value(), 2);
|
EXPECT_EQ(op.value(), 2);
|
||||||
}
|
}
|
||||||
|
EXPECT_EQ(q.size(), 9);
|
||||||
|
|
||||||
// check that "3" was removed from queue
|
// check that "3" was removed from queue
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
@ -194,10 +196,12 @@ TEST(TSLQueue, Iterator) {
|
||||||
q.push(1);
|
q.push(1);
|
||||||
q.push(2);
|
q.push(2);
|
||||||
q.push(3);
|
q.push(3);
|
||||||
|
EXPECT_EQ(q.size(), 4);
|
||||||
{
|
{
|
||||||
auto iter = q.begin();
|
auto iter = q.begin();
|
||||||
EXPECT_TRUE(iter.remove());
|
EXPECT_TRUE(iter.remove());
|
||||||
}
|
}
|
||||||
|
EXPECT_EQ(q.size(), 3);
|
||||||
i = 1;
|
i = 1;
|
||||||
while(!q.empty()) {
|
while(!q.empty()) {
|
||||||
op = q.top();
|
op = q.top();
|
||||||
|
@ -211,6 +215,7 @@ TEST(TSLQueue, Iterator) {
|
||||||
q.push(1);
|
q.push(1);
|
||||||
q.push(2);
|
q.push(2);
|
||||||
q.push(3);
|
q.push(3);
|
||||||
|
EXPECT_EQ(q.size(), 4);
|
||||||
{
|
{
|
||||||
auto iter = q.begin();
|
auto iter = q.begin();
|
||||||
while(true) {
|
while(true) {
|
||||||
|
@ -223,6 +228,7 @@ TEST(TSLQueue, Iterator) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
EXPECT_EQ(q.size(), 3);
|
||||||
i = 0;
|
i = 0;
|
||||||
while(!q.empty()) {
|
while(!q.empty()) {
|
||||||
op = q.top();
|
op = q.top();
|
||||||
|
|
Loading…
Reference in a new issue