Bläddra i källkod

V1.7.3
1、环形队列 RingQueueManualMutex.hpp 完善了迭代器,新增了 自减,支持倒叙遍历

Apple 2 dagar sedan
förälder
incheckning
e23100681b
2 ändrade filer med 70 tillägg och 34 borttagningar
  1. 26 24
      module/RingQueue/RingQueue.hpp
  2. 44 10
      module/RingQueue/RingQueueManualMutex.hpp

+ 26 - 24
module/RingQueue/RingQueue.hpp

@@ -8,28 +8,30 @@
 #include <atomic>
 
 /**
- * @brief 这里采用零公摊的方式,设置多大的空间,就有多大的空间可以使用
- *        1、m_rear指向最新入队的元素的下一个位置,就是下个将要入队的元素位置
- *        2、m_front指向需要出队的第一个元素
- *        3、环形队列自带互斥锁
- *        
- * 注意:
- *      使用时要注意,不带NoBlock的都是阻塞函数
- *
- * 判断队列满:
- *        m_rear == m_front,并且此时都不等于 -1
- *
- * 判断队列空:
- *        m_rear == m_front,并且都等于 -1
- *
- * 获取队列大小:
- *       基本原则就是m_rear后面跟着的是有效值,m_front后面跟着的是已经出队的大小
- *       m_rear > m_front,返回 m_rear - m_front
- *       m_front > m_rear,返回 m_capacity - (m_front - m_rear)
- *       m_rear == m_front,且不等于-1,返回 m_capacity
- *       m_rear == m_front,且等于-1,返回 0
- * 
- * @tparam T 模版类型
+    @brief 这里采用零公摊的方式,设置多大的空间,就有多大的空间可以使用
+            1、m_rear指向最新入队的元素的下一个位置,就是下个将要入队的元素位置
+            2、m_front指向需要出队的第一个元素
+            3、环形队列自带互斥锁
+            
+    注意:
+        使用时要注意,不带NoBlock的都是阻塞函数
+
+    判断队列满:
+            m_rear == m_front,并且此时都不等于 -1
+
+    判断队列空:
+            m_rear == m_front,并且都等于 -1
+
+    获取队列大小:
+        基本原则就是m_rear后面跟着的是有效值,m_front后面跟着的是已经出队的大小
+        m_rear > m_front,返回 m_rear - m_front
+        m_front > m_rear,返回 m_capacity - (m_front - m_rear)
+        m_rear == m_front,且不等于-1,返回 m_capacity
+        m_rear == m_front,且等于-1,返回 0
+    
+    @tparam T 模版类型
+        关于迭代器
+            因为这里会自动上锁,所以没有提供迭代器,这个环形队列适用于生产者消费者模型
  */
 
 #define _DefaultValue (m_isUseDefaultValue.load() ? m_defaultValue : T{})
@@ -57,7 +59,7 @@ public:
      * 注意,如果存储的是指针,需要手动释放该指针指向的内存区域,不然会造成内存泄漏 */
     void pop();
 
-    /* 获取队列中第一个值,但是不出队
+    /* 获取队列中第一个值,但是不出队
      * 阻塞的方式获取,如果队列为空,会一直阻塞住,直到获取到数据为止 */
     T front();
     /* 非阻塞的方式获取,队列为空返回false */
@@ -65,7 +67,7 @@ public:
     /* 非阻塞方式获取第一个值,如果对列为空,则会返回设置的默认值 */
     T front_noBlock();
 
-    /* 获取对立第一个数据,获取完立刻出队
+    /* 获取队列有一个数据,获取完立刻出队
      * 如果队列为空,会阻塞住,直到有数据为止
      * 如果删除了拷贝构造函数,使用会报错 */
     T front_pop();

+ 44 - 10
module/RingQueue/RingQueueManualMutex.hpp

@@ -114,7 +114,7 @@ public:
     class Iterator
     {
     public:
-        Iterator(RingQueueManualMutex<T>* queue) : m_ptr(queue), m_index(m_ptr->m_front) {}
+        // 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*() { 
@@ -124,16 +124,15 @@ public:
             }
             return m_ptr->m_queue[m_index]; 
         }
-        T* operator->() { 
+        T* operator->() {
             if(m_index == -1)
             {
                 throw std::out_of_range("Iterator out of range");
             }
-            return &m_ptr->m_queue[m_index]; 
+            return &m_ptr->m_queue[m_index];
         }
 
-        Iterator& operator++()
-        {
+        Iterator& operator++() {
             m_index = (m_index + 1) % m_ptr->m_capacity;
             if(m_index == m_ptr->m_rear)
             {
@@ -141,17 +140,52 @@ public:
             }
             return *this;
         }
+        Iterator operator++(int) {
+            Iterator tmp = *this;  // 先保存旧值
+            ++(*this);             // 复用前置 ++
+            return tmp;            // 返回修改前的迭代器副本
+        }
+        /* 自减比较复杂,迭代器使用 -1 表示 end(),如果 end() 自减,则需要判断是空还是满
+         */
+        Iterator& operator--() {
+            /* 等于 -1 说明是结尾,判断是空还是满 */
+            if(m_index == -1)
+            {
+                if(m_ptr->isEmpty())
+                {
+                    /* 是空,返回自身 */
+                    return *this;
+                }
+                else if(m_ptr->isFull())
+                {
+                    m_index = (m_ptr->m_rear - 1 + m_ptr->m_capacity) % m_ptr->m_capacity;
+                }
+            }else 
+            {
+                if(m_ptr->m_front == m_index)
+                {
+                    m_index = -1; // 到达开头前面
+                } else
+                {
+                    m_index = (m_index - 1 + m_ptr->m_capacity) % m_ptr->m_capacity;
+                }
+            }
+            return *this;
+        }
+        Iterator operator--(int) {
+            Iterator tmp = *this;  // 先保存旧值
+            --(*this);             // 复用前置 --
+            return tmp;            // 返回修改前的迭代器副本
+        }
 
-        bool operator!=(const Iterator& other) const
-        {
+        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
-        {
+        bool operator==(const Iterator& other) const {
             if(m_ptr != other.m_ptr)
                 return false;
             if(m_index != other.m_index)
@@ -164,7 +198,7 @@ public:
         long m_index = -1;
     };
 
-    Iterator begin() { return Iterator(this); }
+    Iterator begin() { return Iterator(this, m_front); }
     Iterator end() { return Iterator(this, -1); }
 };