|
@@ -114,29 +114,58 @@ public:
|
|
|
class Iterator
|
|
|
{
|
|
|
public:
|
|
|
- Iterator(T* ptr, long capacity) : m_ptr(ptr), m_capacity(capacity) {}
|
|
|
-
|
|
|
- T& operator*() { return *m_ptr; }
|
|
|
- T* operator->() { return m_ptr; }
|
|
|
+ Iterator(RingQueueManualMutex<T>* queue) : m_ptr(queue), m_index(m_ptr->m_front) {}
|
|
|
+ Iterator(RingQueueManualMutex<T>* queue, long index) : m_ptr(queue), m_index(index) {}
|
|
|
+
|
|
|
+ T& operator*() {
|
|
|
+ if(m_index == -1)
|
|
|
+ {
|
|
|
+ throw std::out_of_range("Iterator out of range");
|
|
|
+ }
|
|
|
+ return m_ptr->m_queue[m_index];
|
|
|
+ }
|
|
|
+ T* operator->() {
|
|
|
+ if(m_index == -1)
|
|
|
+ {
|
|
|
+ throw std::out_of_range("Iterator out of range");
|
|
|
+ }
|
|
|
+ return &m_ptr->m_queue[m_index];
|
|
|
+ }
|
|
|
|
|
|
Iterator& operator++()
|
|
|
{
|
|
|
- m_ptr = (m_ptr + 1) % m_capacity;
|
|
|
+ m_index = (m_index + 1) % m_ptr->m_capacity;
|
|
|
+ if(m_index == m_ptr->m_rear)
|
|
|
+ {
|
|
|
+ m_index = -1; // 到达末尾
|
|
|
+ }
|
|
|
return *this;
|
|
|
}
|
|
|
|
|
|
bool operator!=(const Iterator& other) const
|
|
|
{
|
|
|
- return m_ptr != other.m_ptr;
|
|
|
+ if(m_ptr != other.m_ptr)
|
|
|
+ return true;
|
|
|
+ if(m_index == other.m_index)
|
|
|
+ return false;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ bool operator==(const Iterator& other) const
|
|
|
+ {
|
|
|
+ if(m_ptr != other.m_ptr)
|
|
|
+ return false;
|
|
|
+ if(m_index != other.m_index)
|
|
|
+ return false;
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
private:
|
|
|
- T* m_ptr;
|
|
|
- long m_capacity = 0;
|
|
|
+ RingQueueManualMutex<T>* m_ptr = nullptr;
|
|
|
+ long m_index = -1;
|
|
|
};
|
|
|
|
|
|
- Iterator begin() { return Iterator(m_queue + m_front, m_capacity); }
|
|
|
- Iterator end() { return Iterator(m_queue + m_rear, m_capacity); }
|
|
|
+ Iterator begin() { return Iterator(this); }
|
|
|
+ Iterator end() { return Iterator(this, -1); }
|
|
|
};
|
|
|
|
|
|
|
|
@@ -247,10 +276,9 @@ RingQueueManualMutex<T>::~RingQueueManualMutex()
|
|
|
template<typename T>
|
|
|
T RingQueueManualMutex<T>::push(const T& value)
|
|
|
{
|
|
|
- T ret = _DefaultValue; // 默认值
|
|
|
+ T ret = _DefaultValue;
|
|
|
if(_isFull())
|
|
|
{
|
|
|
- // std::cout << "RingQueueManualMutex is full, pop one element." << std::endl;
|
|
|
/* 队列已满,先出队一个元素 */
|
|
|
ret = std::move(m_queue[m_front]);
|
|
|
/* 出队后,前进一个位置 */
|
|
@@ -265,7 +293,7 @@ T RingQueueManualMutex<T>::push(const T& value)
|
|
|
m_queue[m_rear] = value;
|
|
|
m_rear = (m_rear + 1) % m_capacity;
|
|
|
|
|
|
- return ret;
|
|
|
+ return std::move(ret);
|
|
|
}
|
|
|
|
|
|
template<typename T>
|
|
@@ -287,7 +315,7 @@ T RingQueueManualMutex<T>::push(T&& value)
|
|
|
m_queue[m_rear] = std::move(value);
|
|
|
m_rear = (m_rear + 1) % m_capacity;
|
|
|
|
|
|
- return ret;
|
|
|
+ return std::move(ret);
|
|
|
}
|
|
|
|
|
|
|
|
@@ -313,8 +341,8 @@ T RingQueueManualMutex<T>::front_pop()
|
|
|
{
|
|
|
return _DefaultValue; // 如果队列为空,返回默认值
|
|
|
}
|
|
|
- int ret = m_front;
|
|
|
- // T ret = std::move(m_queue[m_front]);
|
|
|
+ // int ret = m_front;
|
|
|
+ T ret = std::move(m_queue[m_front]);
|
|
|
/* 临时记录索引 */
|
|
|
m_front = (m_front + 1) % m_capacity;
|
|
|
if(m_front == m_rear)
|
|
@@ -322,8 +350,8 @@ T RingQueueManualMutex<T>::front_pop()
|
|
|
m_front = -1;
|
|
|
m_rear = -1;
|
|
|
}
|
|
|
-
|
|
|
- return std::move(m_queue[ret]);
|
|
|
+
|
|
|
+ return std::move(ret);
|
|
|
}
|
|
|
|
|
|
/* 获取最后一个值,最后一个值不会出队 */
|