1 #ifndef __XY_CIRCULAR_ARRAY_QUEUE_HPP_47831C93_EDAA_44B9_94F2_6FF36839D1A6__
2 #define __XY_CIRCULAR_ARRAY_QUEUE_HPP_47831C93_EDAA_44B9_94F2_6FF36839D1A6__
4 template <typename ElementType
>
5 class XYCircularArrayQueue
8 typedef ElementType value_type
;
9 typedef ElementType
& reference
;
10 typedef const ElementType
& const_reference
;
13 * @brief Default constructor creates no elements.
15 explicit XYCircularArrayQueue()
23 ~XYCircularArrayQueue()
28 inline int init(size_t capacity
)
31 _elements
= new value_type
[_CAPACITY
];
32 return _elements
? 0 : -1;
35 inline size_t capacity()
40 inline bool full() const
42 return size() >= (_CAPACITY
-1);
45 inline bool empty() const
47 return _head
== _tail
;
51 * @return: the number of elements in the @queue.
53 inline size_t size() const
55 return (_CAPACITY
-_head
+_tail
) %_CAPACITY
;
59 * @return: the number of free elements in the @queue.
61 inline size_t free() const
63 return _CAPACITY
- (_CAPACITY
-_head
+_tail
) %_CAPACITY
;
67 * @return: a read/write reference to the i-th element.
69 inline reference
get_at(int i
)
77 * @return: a read-only reference to the i-th element.
79 inline const_reference
get_at(int i
) const
87 * @return: a read/write reference to the last element
89 inline reference
back()
91 return _elements
[(_CAPACITY
+_tail
-1) % _CAPACITY
];
95 * @return: a read-only reference to the last element
97 inline const_reference
back() const
99 return _elements
[(_CAPACITY
+_tail
-1) % _CAPACITY
];
103 * @return: 0 if succeeded, -1 if failed
105 inline int push_back(const_reference value
)
107 if(size() == (_CAPACITY
-1))
113 _elements
[_tail
] = value
;
114 _tail
= (_tail
+1) % _CAPACITY
;
119 inline reference
inc_1_at_tail()
124 return _elements
[(_CAPACITY
+_tail
-1) % _CAPACITY
];
127 inline void pop_front()
135 _head
= (_head
+ 1) % _CAPACITY
;
139 inline void pop_back()
147 _tail
= (_tail
- 1 + _CAPACITY
) % _CAPACITY
;
151 inline void pop_last_n(int n
)
165 value_type
* _elements
;
166 volatile uint32_t _head
;
167 volatile uint32_t _tail
;
172 #endif /* #ifndef __XY_CIRCULAR_ARRAY_QUEUE_HPP_47831C93_EDAA_44B9_94F2_6FF36839D1A6__ */