container/mn_deque.hpp
Go to the documentation of this file.
1 
18 #ifndef _MINILIB_19dfc60c_16c2_4ec9_8b2d_8c9f1617631a_H_
19 #define _MINILIB_19dfc60c_16c2_4ec9_8b2d_8c9f1617631a_H_
20 
21 #include "../mn_config.hpp"
22 #include "../queue/mn_deque.hpp"
23 #include "../mn_algorithm.hpp"
24 
25 namespace mn {
26  namespace container {
27  template <class T, class TQUEUE>
29  public:
31  using value_type = T;
32  using pointer = value_type*;
35 
36  using deque_type = TQUEUE;
38  using pointer_deque = TQUEUE*;
39 
40  explicit basic_deque_iterator(pointer_deque _pQueue, bool isEnd = false)
41  : m_pValue(NULL), m_pQueue(_pQueue), m_bIsEnd(isEnd) { }
42 
43  explicit basic_deque_iterator(pointer value, pointer_deque _pQueue, bool isEnd = false)
44  : m_pValue(value), m_pQueue(_pQueue), m_bIsEnd(isEnd) { }
45 
46  template<class U, class UQUEUE>
48  : m_pValue(rhs.m_pValue), m_pQueue(rhs._pQueue), m_bIsEnd(rhs.isEnd) { }
49 
50  pointer get() const { return m_pValue; }
51 
52  reference operator*() const { return *m_pValue; }
53  pointer operator->() const { return m_pValue; }
54 
56  if(m_bIsEnd) return *this; _pQueue.dequeue(m_pValue); return *this; }
57 
59  self_type copy(*this); ++(*this); return copy; }
60 
61  bool operator == (const self_type& rhs) const {
62  return rhs.m_pValue == m_pValue; }
63 
64  bool operator != (const self_type& rhs) const {
65  return !(rhs == *this); }
66  private:
69  bool m_bIsEnd;
70  };
71 
79  template <class T, mn::size_t TMAXITEMS, mn::size_t TITEMSIZE = sizeof(T) >
80  class basic_deque {
81  public:
83  using value_type = T;
84  using pointer = T*;
85  using reference = T&;
86  using const_reference = const T&;
89  using const_iterator = const iterator;
91 
95  basic_deque() : m_deque(TMAXITEMS, TITEMSIZE) { m_deque.create(); }
96 
97  basic_deque(const self_type& other)
98  : m_deque(other.m_deque), m_pEnd(other.m_pEnd), m_pFront(other.m_pFront) { }
99 
104 
105 
112  }
118  return basic_deque_iterator(m_pEnd, &m_deque, true);
119  }
120 
127  }
132  const_iterator end() const {
133  return basic_deque_iterator(m_pEnd, &m_deque, true);
134  }
136  assert(!empty()); return *m_pFront; }
137 
139  assert(!empty()); return *m_pFront; }
140 
142  assert(!empty()); return *m_pEnd; }
143 
145  assert(!empty()); return *m_pEnd; }
146 
151  void push_back(reference _Element) {
152  if( m_deque.enqueue(&_Element) == ERR_QUEUE_OK) {
153  m_pEnd = &_Element;
154  }
155  intern_getfront();
156  }
161  void push_front(reference _Element) {
162  if( m_deque.enqueue_front(&_Element) == ERR_QUEUE_OK) {
163  m_pFront = &_Element;
164  }
165  }
170  inline pointer pop_front() {
171  pointer tmp = NULL;
172  m_deque.dequeue(tmp);
173 
174  intern_getfront();
175 
176  return tmp;
177  }
181  inline void clear() {
182  m_deque.clear();
183  }
189  inline const bool empty() const {
190  return m_deque.is_empty();
191  }
196  inline const mn::size_t length() const {
197  return TMAXITEMS;
198  }
203  inline const mn::size_t size() const {
204  return m_deque.get_num_items();
205  }
206 
211  inline const mn::size_t left() const {
212  return m_deque.get_left();
213  }
214 
215  void swap(const self_type& other) {
216  self_type _temp(this);
217  other = this;
218  this = _temp;
219  }
220 
221  self_type& operator = (const self_type& other) {
222  m_deque = other.m_deque;
223  m_pEnd = other.m_pEnd;
224  m_pFront = other.m_pFront;
225  return *this;
226  }
227  private:
230  return m_pFront;
231  }
232  private:
236  };
237 
238  template <class T, mn::size_t TMAXITEMS, mn::size_t TITEMSIZE>
241  a.swap(b);
242  }
243 
244  template <class T, mn::size_t TMAXITEMS, mn::size_t TITEMSIZE = sizeof(T)>
246  }
247 }
248 
249 #endif
Definition: container/mn_deque.hpp:28
pointer_deque m_pQueue
Definition: container/mn_deque.hpp:68
self_type & operator++()
Definition: container/mn_deque.hpp:55
pointer get() const
Definition: container/mn_deque.hpp:50
bool operator!=(const self_type &rhs) const
Definition: container/mn_deque.hpp:64
basic_deque_iterator(pointer_deque _pQueue, bool isEnd=false)
Definition: container/mn_deque.hpp:40
T value_type
Definition: container/mn_deque.hpp:31
self_type operator++(int)
Definition: container/mn_deque.hpp:58
bool m_bIsEnd
Definition: container/mn_deque.hpp:69
bool operator==(const self_type &rhs) const
Definition: container/mn_deque.hpp:61
value_type * pointer
Definition: container/mn_deque.hpp:32
TQUEUE deque_type
Definition: container/mn_deque.hpp:36
ptrdiff_t difference_type
Definition: container/mn_deque.hpp:34
basic_deque_iterator(const basic_deque_iterator< U, UQUEUE > &rhs)
Definition: container/mn_deque.hpp:47
value_type & reference
Definition: container/mn_deque.hpp:33
pointer * m_pValue
Definition: container/mn_deque.hpp:67
TQUEUE * pointer_deque
Definition: container/mn_deque.hpp:38
reference operator*() const
Definition: container/mn_deque.hpp:52
pointer operator->() const
Definition: container/mn_deque.hpp:53
basic_deque_iterator(pointer value, pointer_deque _pQueue, bool isEnd=false)
Definition: container/mn_deque.hpp:43
A basic wrapper for the MiniLib deque's as Template deque.
Definition: container/mn_deque.hpp:80
void push_back(reference _Element)
Push a element to the end of the queue.
Definition: container/mn_deque.hpp:151
deque_type m_deque
Definition: container/mn_deque.hpp:233
T * pointer
Definition: container/mn_deque.hpp:84
const_iterator end() const
Get the end iterator.
Definition: container/mn_deque.hpp:132
basic_deque()
Construct a new basic queue object.
Definition: container/mn_deque.hpp:95
const_reference front() const
Definition: container/mn_deque.hpp:135
reference front()
Definition: container/mn_deque.hpp:138
reference back()
Definition: container/mn_deque.hpp:144
const mn::size_t size() const
Definition: container/mn_deque.hpp:203
T & reference
Definition: container/mn_deque.hpp:85
basic_deque(const self_type &other)
Definition: container/mn_deque.hpp:97
~basic_deque()
Destroy the basic queue object.
Definition: container/mn_deque.hpp:103
const mn::size_t length() const
How many items can queue.
Definition: container/mn_deque.hpp:196
iterator end()
Get the end iterator.
Definition: container/mn_deque.hpp:117
pointer pop_front()
Pop the oldest entry from the queue.
Definition: container/mn_deque.hpp:170
basic_deque_iterator< T, deque_type > iterator
Definition: container/mn_deque.hpp:88
void push_front(reference _Element)
Push a element to the front of the queue.
Definition: container/mn_deque.hpp:161
iterator begin()
Get the begin iterator.
Definition: container/mn_deque.hpp:110
void swap(const self_type &other)
Definition: container/mn_deque.hpp:215
T * intern_getfront()
Definition: container/mn_deque.hpp:228
const_iterator begin() const
Get the begin iterator.
Definition: container/mn_deque.hpp:125
T value_type
Definition: container/mn_deque.hpp:83
void clear()
Clear the queue.
Definition: container/mn_deque.hpp:181
mn::size_t size_type
Definition: container/mn_deque.hpp:90
const_reference back() const
Definition: container/mn_deque.hpp:141
const bool empty() const
Check, if queue is empty.
Definition: container/mn_deque.hpp:189
pointer m_pFront
Definition: container/mn_deque.hpp:235
const mn::size_t left() const
Definition: container/mn_deque.hpp:211
self_type & operator=(const self_type &other)
Definition: container/mn_deque.hpp:221
pointer m_pEnd
Definition: container/mn_deque.hpp:234
const T & const_reference
Definition: container/mn_deque.hpp:86
Definition: queue/mn_deque.hpp:30
virtual int enqueue_front(void *item, unsigned int timeout=MN_THREAD_CONFIG_TIMEOUT_QUEUE_DEFAULT)
Definition: mn_deque.cpp:34
virtual int enqueue(void *item, unsigned int timeout=MN_THREAD_CONFIG_TIMEOUT_QUEUE_DEFAULT)
Definition: mn_queue.cpp:50
virtual unsigned int get_num_items()
Definition: mn_queue.cpp:117
virtual int peek(void *item, unsigned int timeout=MN_THREAD_CONFIG_TIMEOUT_QUEUE_DEFAULT)
Definition: mn_queue.cpp:86
virtual int destroy()
Definition: mn_queue.cpp:41
virtual bool is_empty()
Definition: mn_queue.cpp:100
virtual int clear()
Definition: mn_queue.cpp:111
virtual int create()
Definition: mn_queue.cpp:34
virtual unsigned int get_left()
Definition: mn_queue.cpp:122
virtual int dequeue(void *item, unsigned int timeout=MN_THREAD_CONFIG_TIMEOUT_QUEUE_DEFAULT)
Definition: mn_queue.cpp:68
#define MN_THREAD_CONFIG_TIMEOUT_QUEUE_DEFAULT
Definition: mn_config.hpp:387
#define ERR_QUEUE_OK
Definition: mn_error.hpp:137
void swap(basic_any &x, basic_any &y) noexcept
Definition: mn_any.hpp:272
struct mn::memory::detail::ptr_difference T
Definition: mn_atomic_singleton.hpp:38
basic_deque deque_t
Definition: queue/mn_deque.hpp:57
Definition: mn_allocator_typetraits.hpp:25
long ptrdiff_t
Definition: mn_def.hpp:49
void copy(const T *src, const T *last, T *dest)
Definition: mn_algorithm.hpp:79
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48
Definition: mn_iterator.hpp:34