Browse Source

V1.4.5
1、修改了环形队列,手动管理的添加了迭代器

Apple 1 week ago
parent
commit
4df2fb6e80
2 changed files with 67 additions and 8 deletions
  1. 4 3
      module/RingQueue/RingQueue.hpp
  2. 63 5
      module/RingQueue/RingQueueManualMutex.hpp

+ 4 - 3
module/RingQueue/RingQueue.hpp

@@ -45,7 +45,7 @@ public:
     RingQueue(long size, T defaultValue);
     RingQueue(long size, T defaultValue);
     ~RingQueue();
     ~RingQueue();
 
 
-    /* 入队,默认是阻塞入队 */
+    /* 入队,默认是阻塞入队,队列满就阻塞住,直到有位置 */
     void push(const T& value);
     void push(const T& value);
     void push(T&& value);
     void push(T&& value);
     bool push_noBlock(const T& value);
     bool push_noBlock(const T& value);
@@ -111,6 +111,7 @@ private:
     long m_rear = -1;                           /* 队尾 */
     long m_rear = -1;                           /* 队尾 */
     std::condition_variable m_cond_NoFull;      /* 非满条件变量 */
     std::condition_variable m_cond_NoFull;      /* 非满条件变量 */
     std::condition_variable m_cond_NoEmpty;     /* 非空条件变量 */
     std::condition_variable m_cond_NoEmpty;     /* 非空条件变量 */
+
 };
 };
 
 
 
 
@@ -414,7 +415,7 @@ T RingQueue<T>::front_pop()
             m_rear = -1;
             m_rear = -1;
         }
         }
         m_cond_NoFull.notify_all();
         m_cond_NoFull.notify_all();
-        return m_queue[front];
+        return std::move(m_queue[front]);
     }
     }
 }
 }
 
 
@@ -464,7 +465,7 @@ T RingQueue<T>::front_pop_noBlock()
             m_rear = -1;
             m_rear = -1;
         }
         }
         m_cond_NoFull.notify_all();
         m_cond_NoFull.notify_all();
-        return m_queue[front];
+        return std::move(m_queue[front]);
     }
     }
 }
 }
 
 

+ 63 - 5
module/RingQueue/RingQueueManualMutex.hpp

@@ -108,6 +108,64 @@ private:
     long m_rear = -1;                           /* 队尾 */
     long m_rear = -1;                           /* 队尾 */
     // std::condition_variable m_cond_NoFull;      /* 非满条件变量 */
     // std::condition_variable m_cond_NoFull;      /* 非满条件变量 */
     // std::condition_variable m_cond_NoEmpty;     /* 非空条件变量 */
     // std::condition_variable m_cond_NoEmpty;     /* 非空条件变量 */
+
+public:
+    /* 定义迭代器 */
+    class Iterator
+    {
+    public:
+        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_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
+        {
+            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:
+        RingQueueManualMutex<T>* m_ptr = nullptr;
+        long m_index = -1;
+    };
+
+    Iterator begin() { return Iterator(this); }
+    Iterator end() { return Iterator(this, -1); }
 };
 };
 
 
 
 
@@ -218,10 +276,9 @@ RingQueueManualMutex<T>::~RingQueueManualMutex()
 template<typename T>
 template<typename T>
 T  RingQueueManualMutex<T>::push(const T& value)
 T  RingQueueManualMutex<T>::push(const T& value)
 {
 {
-    T ret = _DefaultValue; // 默认值
+    T ret = _DefaultValue;
     if(_isFull())
     if(_isFull())
     {
     {
-        // std::cout << "RingQueueManualMutex is full, pop one element." << std::endl;
         /* 队列已满,先出队一个元素 */
         /* 队列已满,先出队一个元素 */
         ret = std::move(m_queue[m_front]);
         ret = std::move(m_queue[m_front]);
         /* 出队后,前进一个位置 */
         /* 出队后,前进一个位置 */
@@ -236,7 +293,7 @@ T  RingQueueManualMutex<T>::push(const T& value)
     m_queue[m_rear] = value;
     m_queue[m_rear] = value;
     m_rear = (m_rear + 1) % m_capacity;
     m_rear = (m_rear + 1) % m_capacity;
 
 
-    return ret;
+    return std::move(ret);
 }
 }
 
 
 template<typename T>
 template<typename T>
@@ -258,7 +315,7 @@ T  RingQueueManualMutex<T>::push(T&& value)
     m_queue[m_rear] = std::move(value);
     m_queue[m_rear] = std::move(value);
     m_rear = (m_rear + 1) % m_capacity;
     m_rear = (m_rear + 1) % m_capacity;
 
 
-    return ret;
+    return std::move(ret);
 }
 }
 
 
 
 
@@ -284,6 +341,7 @@ T RingQueueManualMutex<T>::front_pop()
     {
     {
         return _DefaultValue; // 如果队列为空,返回默认值
         return _DefaultValue; // 如果队列为空,返回默认值
     }
     }
+    // int ret = m_front;
     T ret = std::move(m_queue[m_front]);
     T ret = std::move(m_queue[m_front]);
     /* 临时记录索引 */
     /* 临时记录索引 */
     m_front = (m_front + 1) % m_capacity;
     m_front = (m_front + 1) % m_capacity;
@@ -292,7 +350,7 @@ T RingQueueManualMutex<T>::front_pop()
         m_front = -1;
         m_front = -1;
         m_rear = -1;
         m_rear = -1;
     }
     }
-    
+
     return std::move(ret);
     return std::move(ret);
 }
 }