Pothos  0.4.1-gb758ed46
The Pothos dataflow programming software suite
OrderedQueue.hpp
Go to the documentation of this file.
1 
12 #pragma once
13 #include <Pothos/Config.hpp>
15 #include <cstdlib> //size_t
16 #include <utility> //std::forward
17 #include <vector>
18 #include <cassert>
19 
20 namespace Pothos {
21 namespace Util {
22 
27 template <typename T>
29 {
30 public:
32  OrderedQueue(void);
33 
35  OrderedQueue(const size_t capacity);
36 
42  bool empty(void) const;
43 
49  template <typename U>
50  void push(U &&elem, const size_t index);
51 
55  const T &front(void) const;
56 
60  T &front(void);
61 
65  void pop(void);
66 
68  size_t capacity(void) const;
69 
70 private:
71  size_t _indexToAck;
72  std::vector<T> _pushedElems;
73  std::vector<bool> _pushedElemsFlag;
74  Pothos::Util::RingDeque<T> _readyElems;
75 };
76 
77 template <typename T>
79  _indexToAck(0)
80 {
81  return;
82 }
83 
84 template <typename T>
86  _indexToAck(0),
87  _pushedElems(capacity),
88  _pushedElemsFlag(capacity, false),
89  _readyElems(capacity)
90 {
91  return;
92 }
93 
94 template <typename T>
95 bool OrderedQueue<T>::empty(void) const
96 {
97  return _readyElems.empty();
98 }
99 
100 template <typename T>
101 template <typename U>
102 void OrderedQueue<T>::push(U &&elem, const size_t index)
103 {
104  //store the element into its position
105  assert(index < _pushedElems.size());
106  _pushedElems[index] = std::forward<U>(elem);
107  _pushedElemsFlag[index] = true;
108 
109  //look for pushed elements -- but in order
110  while (_pushedElemsFlag[_indexToAck])
111  {
112  //move the element into the queue
113  assert(not _readyElems.full());
114  _readyElems.push_back(_pushedElems[_indexToAck]);
115  _pushedElems[_indexToAck] = T(); //clear reference
116  _pushedElemsFlag[_indexToAck] = false;
117 
118  //increment for the next pushed element
119  if (++_indexToAck == _pushedElems.size()) _indexToAck = 0;
120  }
121 }
122 
123 template <typename T>
124 const T &OrderedQueue<T>::front(void) const
125 {
126  return _readyElems.front();
127 }
128 
129 template <typename T>
131 {
132  return _readyElems.front();
133 }
134 
135 template <typename T>
137 {
138  _readyElems.pop_front();
139 }
140 
141 template <typename T>
142 size_t OrderedQueue<T>::capacity(void) const
143 {
144  return _readyElems.capacity();
145 }
146 
147 } //namespace Util
148 } //namespace Pothos
const T & front(void) const
Definition: OrderedQueue.hpp:124
Definition: Testing.hpp:134
size_t capacity(void) const
How many elements can be stored?
Definition: OrderedQueue.hpp:142
void push(U &&elem, const size_t index)
Definition: OrderedQueue.hpp:102
bool empty(void) const
Definition: OrderedQueue.hpp:95
Definition: RingDeque.hpp:29
void pop(void)
Definition: OrderedQueue.hpp:136
Definition: OrderedQueue.hpp:28
OrderedQueue(void)
Construct a size-zero ordered queue.
Definition: OrderedQueue.hpp:78