3 #ifndef INCLUDE_NN_NLIB_NQUEUE_H_
4 #define INCLUDE_NN_NLIB_NQUEUE_H_
7 #include "nn/nlib/Swap.h"
13 extern void* nqueue_enabler;
15 template <
class T,
class AL = std::allocator<
char> >
20 NLIB_MOVE_MEMBER_HELPER_5(
Nqueue, m_Beg1, m_End1, m_Beg2, m_End2, m_List)
21 size_t size() const
NLIB_NOEXCEPT {
return (m_End1 - m_Beg1) + (m_End2 - m_Beg2); }
25 #ifdef NLIB_CXX11_RVALUE_REFERENCES
30 return this->pop_front_swap(&tmp);
33 T* tmp = this->push_back();
34 if (!tmp || !v)
return false;
42 swap(m_Beg1, rhs.m_Beg1);
43 swap(m_End1, rhs.m_End1);
44 swap(m_Beg2, rhs.m_Beg2);
45 swap(m_End2, rhs.m_End2);
46 swap(m_List, rhs.m_List);
58 template <
class T,
class AL>
60 if (m_End1 != m_Beg2 && m_Beg2 != m_End2) {
62 swap(m_List[m_End1++], m_List[m_Beg2++]);
63 if (m_Beg2 != m_End2) {
64 swap(m_List[m_End1++], m_List[m_Beg2++]);
66 if (m_Beg2 == m_End2) {
72 if (m_List.size() == m_End2) {
73 T* tmp = m_List.push_back();
77 return &m_List[m_End2++];
81 #ifdef NLIB_CXX11_RVALUE_REFERENCES
82 template <
class T,
class AL>
84 if (m_End1 != m_Beg2 && m_Beg2 != m_End2) {
86 swap(m_List[m_End1++], m_List[m_Beg2++]);
87 if (m_Beg2 != m_End2) {
88 swap(m_List[m_End1++], m_List[m_Beg2++]);
90 if (m_Beg2 == m_End2) {
96 if (m_List.size() == m_End2) {
97 #ifdef NLIB_HAS_NATIVE_TYPETRAITS
100 T* tmp = m_List.push_back(std::move(rhs));
104 #ifdef NLIB_HAS_NATIVE_TYPETRAITS
107 m_List[m_End2] = std::move(rhs);
108 return &m_List[m_End2++];
113 template <
class T,
class AL>
115 if (!v)
return false;
117 if (m_Beg1 != m_End1) {
118 swap(*v, m_List[m_Beg1++]);
119 if (m_Beg1 == m_End1) {
121 }
else if ((m_End1 - m_Beg1) * 8 < m_End1) {
122 uint32_t cnt =
static_cast<uint32_t
>(m_End1 - m_Beg1);
123 for (uint32_t i = 0; i < cnt; ++i) {
124 swap(m_List[i], m_List[m_Beg1 + i]);
129 }
else if (m_Beg2 != m_End2) {
130 NLIB_ASSERT(m_Beg1 + m_End1 == 0);
131 swap(*v, m_List[m_Beg2++]);
132 if (m_Beg2 == m_End2) m_Beg2 = m_End2 = 0;
141 #ifndef NLIB_STD_SWAP_WORKAROUND
144 NLIB_DEFINE_STD_SWAP_T_BEGIN1(
std)
147 NLIB_DEFINE_STD_SWAP_T1(AL, NLIB_NS::Nqueue)
149 #ifndef NLIB_STD_SWAP_WORKAROUND
152 NLIB_DEFINE_STD_SWAP_T_END1(
std)
155 #endif // INCLUDE_NN_NLIB_NQUEUE_H_
~Nqueue() noexcept
Destructor.
#define NLIB_NOEXCEPT
Defines noexcept geared to the environment, or the equivalent.
bool push_back_swap(T *v) noexcept
Adds an element to the queue, and then swaps that element with v using std:: swap.
size_t capacity() const noexcept
Returns the number of allocated elements.
Substitute definitions for the C++11 standard header type_traits. These substitute definitions are us...
#define NLIB_DISALLOW_COPY_AND_ASSIGN(TypeName)
Prohibits use of the copy constructor and assignment operator for the class specified by TypeName...
A container-like class similar to std::queue that can store objects that do not have copy constructor...
Nqueue() noexcept
Instantiates the object with default parameters (default constructor). Creates an empty queue...
void swap(Nqueue &rhs) noexcept
Swaps the container.
bool empty() const noexcept
Checks whether the container is empty.
Defines the class that resembles std::vector but can store objects that cannot be copied...
bool pop_front() noexcept
Removes the first element from the front of the queue.
A container-like class similar to std::vector that can store objects that do not have copy constructo...
#define NLIB_STATIC_ASSERT(exp)
Defines a static assertion. Uses static_assert if it is available for use.