esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
simple-queue.hpp
Go to the documentation of this file.
1#ifndef NDNPH_CORE_SIMPLE_QUEUE_HPP
2#define NDNPH_CORE_SIMPLE_QUEUE_HPP
3
4#include "common.hpp"
5
6namespace ndnph {
7
9template<typename T>
11public:
12 static_assert(std::is_default_constructible<T>::value, "");
13 static_assert(std::is_move_assignable<T>::value, "");
14 static_assert(std::is_move_constructible<T>::value, "");
15 using Item = T;
16
17 bool push(Item item) {
18 size_t newTail = nextIndex(m_tail);
19 if (newTail == m_head) {
20 return false;
21 }
22
23 m_arr[m_tail] = std::move(item);
24 m_tail = newTail;
25 return true;
26 }
27
28 std::tuple<Item, bool> pop() {
29 if (m_head == m_tail) {
30 return std::make_tuple(T(), false);
31 }
32 Item item = std::move(m_arr[m_head]);
33 m_arr[m_head] = T();
34 m_head = nextIndex(m_head);
35 return std::make_tuple(std::move(item), true);
36 }
37
38 size_t capacity() const {
39 return m_cap1 - 1;
40 }
41
42 size_t size() const {
43 return (m_tail - m_head + m_cap1) % m_cap1;
44 }
45
46 size_t available() const {
47 return capacity() - size();
48 }
49
50protected:
56 explicit SimpleQueue(Item* arr, size_t cap)
57 : m_arr(arr)
58 , m_cap1(cap + 1) {}
59
61 return m_arr;
62 }
63
64private:
65 size_t nextIndex(size_t i) const {
66 return (i + 1) % m_cap1;
67 }
68
69private:
70 Item* m_arr;
71 size_t m_cap1 = 0;
72 size_t m_head = 0;
73 size_t m_tail = 0;
74};
75
80template<typename T, size_t C>
81class StaticSimpleQueue : public SimpleQueue<T> {
82public:
84 : SimpleQueue<T>(m_array, C) {}
85
86private:
87 T m_array[C + 1];
88};
89
91template<typename T>
93public:
95 : SimpleQueue<T>(new T[capacity + 1], capacity) {}
96
98 delete[] this->getArray();
99 }
100};
101
102} // namespace ndnph
103
104#endif // NDNPH_CORE_SIMPLE_QUEUE_HPP
SimpleQueue with dynamically allocated memory.
Definition simple-queue.hpp:92
DynamicSimpleQueue(size_t capacity)
Definition simple-queue.hpp:94
~DynamicSimpleQueue()
Definition simple-queue.hpp:97
Generic non-thread-safe queue.
Definition simple-queue.hpp:10
size_t capacity() const
Definition simple-queue.hpp:38
size_t available() const
Definition simple-queue.hpp:46
std::tuple< Item, bool > pop()
Definition simple-queue.hpp:28
size_t size() const
Definition simple-queue.hpp:42
Item * getArray()
Definition simple-queue.hpp:60
bool push(Item item)
Definition simple-queue.hpp:17
SimpleQueue(Item *arr, size_t cap)
Constructor.
Definition simple-queue.hpp:56
T Item
Definition simple-queue.hpp:15
SimpleQueue with statically allocated memory.
Definition simple-queue.hpp:81
StaticSimpleQueue()
Definition simple-queue.hpp:83
Definition fs.hpp:33