Преглед на файлове

Patch up CircularQueue

- Use `size` and `count` instead of `length` and `size`
- Fix bug: `peek` should return the item
- Clean up comments, spacing, spelling
Scott Lahteine преди 8 години
родител
ревизия
f26d5425bf
променени са 1 файла, в които са добавени 44 реда и са изтрити 45 реда
  1. 44
    45
      Marlin/circularqueue.h

+ 44
- 45
Marlin/circularqueue.h Целия файл

@@ -26,120 +26,119 @@
26 26
 #include <Arduino.h>
27 27
 
28 28
 /**
29
- * @brief Circular Queue class
29
+ * @brief   Circular Queue class
30 30
  * @details Implementation of the classic ring buffer data structure
31 31
  */
32
-template<typename T, int N>
32
+template<typename T, uint8_t N>
33 33
 class CircularQueue {
34 34
   private:
35 35
 
36 36
     /**
37
-     * @brief Buffer structure
37
+     * @brief   Buffer structure
38 38
      * @details This structure consolidates all the overhead required to handle
39
-     * a circular queue such as the pointers and the buffer vector.
39
+     *          a circular queue such as the pointers and the buffer vector.
40 40
      */
41 41
     struct buffer_t {
42 42
       uint8_t head;
43 43
       uint8_t tail;
44
+      uint8_t count;
44 45
       uint8_t size;
45
-      uint8_t length;
46 46
       T queue[N];
47 47
     } buffer;
48 48
 
49 49
   public:
50 50
     /**
51
-     * @brief Class constructor
51
+     * @brief   Class constructor
52 52
      * @details This class requires two template parameters, T defines the type
53
-     * of the items this queue will handle and N defines the maximum number of
54
-     * items that can be stored on the queue.
53
+     *          of item this queue will handle and N defines the maximum number of
54
+     *          items that can be stored on the queue.
55 55
      */
56 56
     CircularQueue<T, N>() {
57
-      this->buffer.length = N;
58
-      this->buffer.size = this->buffer.head = this->buffer.tail = 0;
57
+      this->buffer.size = N;
58
+      this->buffer.count = this->buffer.head = this->buffer.tail = 0;
59 59
     }
60 60
 
61 61
     /**
62
-     * @brief Removes and returns a item from the queue
63
-     * @details Removes the oldest item on the queue which is pointed by the
64
-     * buffer_t head variable, this item is then returned to the caller.
65
-     * @return type T item
62
+     * @brief   Removes and returns a item from the queue
63
+     * @details Removes the oldest item on the queue, pointed to by the
64
+     *          buffer_t head field. The item is returned to the caller.
65
+     * @return  type T item
66 66
      */
67 67
     T dequeue() {
68 68
       if (this->isEmpty()) return T();
69 69
 
70
-      T const item = this->buffer.queue[this->buffer.head++];
71
-      --this->buffer.size;
70
+      uint8_t index = this->buffer.head;
72 71
 
73
-      if (this->buffer.head == this->buffer.length)
72
+      --this->buffer.count;
73
+      if (++this->buffer.head == this->buffer.size)
74 74
         this->buffer.head = 0;
75 75
 
76
-      return item;
76
+      return this->buffer.queue[index];
77 77
     }
78 78
 
79 79
     /**
80
-     * @brief Adds an item to the queue
81
-     * @details Adds a item to the queue on the location pointed by the buffer_t
82
-     * tail vairable, will return false if there is no queue space available.
83
-     *
84
-     * @param item Item to be added to the queue
85
-     * @return true if the operation was successfull
80
+     * @brief   Adds an item to the queue
81
+     * @details Adds an item to the queue on the location pointed by the buffer_t
82
+     *          tail variable. Returns false if no queue space is available.
83
+     * @param   item Item to be added to the queue
84
+     * @return  true if the operation was successful
86 85
      */
87 86
     bool enqueue(T const &item) {
88 87
       if (this->isFull()) return false;
89 88
 
90
-      this->buffer.queue[this->buffer.tail++] = item;
91
-      ++this->buffer.size;
89
+      this->buffer.queue[this->buffer.tail] = item;
92 90
 
93
-      if (this->buffer.tail == this->buffer.length)
91
+      ++this->buffer.count;
92
+      if (++this->buffer.tail == this->buffer.size)
94 93
         this->buffer.tail = 0;
95 94
 
96 95
       return true;
97 96
     }
98 97
 
99 98
     /**
100
-     * @brief Checks if the queue has no items
99
+     * @brief   Checks if the queue has no items
101 100
      * @details Returns true if there are no items on the queue, false otherwise.
102
-     * @return true if queue is empty
101
+     * @return  true if queue is empty
103 102
      */
104 103
     bool isEmpty() {
105
-      return this->buffer.size == 0;
104
+      return this->buffer.count == 0;
106 105
     }
107 106
 
108 107
     /**
109
-     * @brief Checks if the queue is full
108
+     * @brief   Checks if the queue is full
110 109
      * @details Returns true if the queue is full, false otherwise.
111
-     * @return true if queue is full
110
+     * @return  true if queue is full
112 111
      */
113 112
     bool isFull() {
114
-      return this->buffer.size == this->buffer.length;
113
+      return this->buffer.count == this->buffer.size;
115 114
     }
116 115
 
117 116
     /**
118
-     * @brief Gets the queue size
117
+     * @brief   Gets the queue size
119 118
      * @details Returns the maximum number of items a queue can have.
120
-     * @return the queue lenght
119
+     * @return  the queue size
121 120
      */
122
-    uint8_t length() {
123
-      return this->buffer.length;
121
+    uint8_t size() {
122
+      return this->buffer.size;
124 123
     }
125 124
 
126 125
     /**
127
-     * @brief Gets the next item from the queue without removing it
128
-     * @details Returns the next item on the queue but the item is not removed
129
-     * from the queue nor the pointers updated.
130
-     * @return the queue size
126
+     * @brief   Gets the next item from the queue without removing it
127
+     * @details Returns the next item in the queue without removing it
128
+     *          or updating the pointers.
129
+     * @return  first item in the queue
131 130
      */
132
-    uint8_t peek() {
131
+    T peek() {
133 132
       return this->buffer.queue[this->buffer.head];
134 133
     }
135 134
 
136 135
     /**
137 136
      * @brief Gets the number of items on the queue
138 137
      * @details Returns the current number of items stored on the queue.
139
-     * @return type T item
138
+     * @return number of items in the queue
140 139
      */
141
-    uint8_t size() {
142
-      return this->buffer.size;
140
+    uint8_t count() {
141
+      return this->buffer.count;
143 142
     }
144 143
 };
145 144
 

Loading…
Отказ
Запис