Browse Source

constexpr all the... deques

master
parent
commit
a354ded3de
4 changed files with 57 additions and 53 deletions
  1. +29
    -29
      include/cont/deque.h
  2. +3
    -3
      include/cont/edeque.h
  3. +3
    -3
      include/cont/queue.h
  4. +22
    -18
      include/core/ring_iterator.h

+ 29
- 29
include/cont/deque.h View File

@@ -76,13 +76,13 @@ class deque {
//! @{ //! @{
public: public:
//! Default constructor //! Default constructor
deque () noexcept :
constexpr deque () noexcept :
data_{}, data_{},
f{data_.data(), N}, f{data_.data(), N},
r{data_.data()} { } r{data_.data()} { }
//! fill contructor //! fill contructor
deque(const Data_t& value) noexcept {
constexpr deque(const Data_t& value) noexcept {
data_.fill(value); data_.fill(value);
f = iterator(data_.data(), N); f = iterator(data_.data(), N);
r = iterator(data_.data(), N); r = iterator(data_.data(), N);
@@ -90,7 +90,7 @@ class deque {
//! Initializer list contructor //! Initializer list contructor
template <typename ...It> template <typename ...It>
deque(It&& ...it) noexcept :
constexpr deque(It&& ...it) noexcept :
data_{{std::forward<It>(it)...}}, data_{{std::forward<It>(it)...}},
f(data_.data(), N), f(data_.data(), N),
r(data_.data(), sizeof...(It)) { } r(data_.data(), sizeof...(It)) { }
@@ -103,38 +103,38 @@ class deque {
//! \name Iterators //! \name Iterators
//! @{ //! @{
public: public:
iterator begin() noexcept { return f+1; }
const_iterator begin() const noexcept { return f+1; }
const_iterator cbegin() const noexcept { return f+1; }
constexpr iterator begin() noexcept { return f+1; }
constexpr const_iterator begin() const noexcept { return f+1; }
constexpr const_iterator cbegin() const noexcept { return f+1; }
iterator end() noexcept { return r; }
const_iterator end() const noexcept { return r; }
const_iterator cend() const noexcept { return r; }
constexpr iterator end() noexcept { return r; }
constexpr const_iterator end() const noexcept { return r; }
constexpr const_iterator cend() const noexcept { return r; }
reverse_iterator rbegin() noexcept { return r; }
const_reverse_iterator rbegin() const noexcept { return r; }
const_reverse_iterator crbegin() const noexcept { return r; }
constexpr reverse_iterator rbegin() noexcept { return r; }
constexpr const_reverse_iterator rbegin() const noexcept { return r; }
constexpr const_reverse_iterator crbegin() const noexcept { return r; }
reverse_iterator rend() noexcept { return f+1; }
const_reverse_iterator rend() const noexcept { return f+1; }
const_reverse_iterator crend() const noexcept { return f+1; }
constexpr reverse_iterator rend() noexcept { return f+1; }
constexpr const_reverse_iterator rend() const noexcept { return f+1; }
constexpr const_reverse_iterator crend() const noexcept { return f+1; }
//! @} //! @}
//! \name Capacity //! \name Capacity
//! @{ //! @{
public: public:
//! \return The size of the deque. The items currently in queue. //! \return The size of the deque. The items currently in queue.
size_t size() noexcept {
constexpr size_t size() noexcept {
return full() ? N: (r - f) -1; return full() ? N: (r - f) -1;
} }
//! \return The maximum size of the deque. The items the queue can hold. //! \return The maximum size of the deque. The items the queue can hold.
size_t max_size() noexcept { return N; }
constexpr size_t max_size() noexcept { return N; }
//! \return The capacity of the deque. The items the queue can hold. //! \return The capacity of the deque. The items the queue can hold.
size_t capacity() noexcept { return N; }
constexpr size_t capacity() noexcept { return N; }
//! \return True if the deque is empty //! \return True if the deque is empty
bool empty() noexcept { return size() == 0 ? true : false; }
constexpr bool empty() noexcept { return size() == 0 ? true : false; }
//! \return True if the deque is full //! \return True if the deque is full
bool full() noexcept { return (r == f) ? true : false; }
constexpr bool full() noexcept { return (r == f) ? true : false; }
//! @} //! @}
//! \name Member access //! \name Member access
@@ -142,44 +142,44 @@ class deque {
public: public:
//! \brief Clears-empty the deque and return it to init state, without //! \brief Clears-empty the deque and return it to init state, without
//! really deleting the contents. //! really deleting the contents.
void clear() noexcept {
constexpr void clear() noexcept {
f = iterator_t(data_.data(), N); f = iterator_t(data_.data(), N);
r = iterator_t(data_.data()); r = iterator_t(data_.data());
} }
//! \brief Push an item in the front of the deque //! \brief Push an item in the front of the deque
//! \param it The item to push //! \param it The item to push
void push_front (const Data_t& it) {
constexpr void push_front (const Data_t& it) {
if (full()) return; if (full()) return;
*f-- = it; *f-- = it;
} }
//! \brief Extract an item from the front of the deque and remove it from the deque //! \brief Extract an item from the front of the deque and remove it from the deque
//! \param it The item to push //! \param it The item to push
Data_t pop_front () {
constexpr Data_t pop_front () {
if (empty()) return Data_t{}; if (empty()) return Data_t{};
return *++f; return *++f;
} }
//! \brief Push an item in the back of the deque //! \brief Push an item in the back of the deque
//! \param it The item to push //! \param it The item to push
void push_back (const Data_t& it) {
constexpr void push_back (const Data_t& it) {
if (full()) return; if (full()) return;
*r++ = it; *r++ = it;
} }
//! \brief Extract an item from the back of the deque and remove it from the deque //! \brief Extract an item from the back of the deque and remove it from the deque
//! \param it The item to push //! \param it The item to push
Data_t pop_back () {
constexpr Data_t pop_back () {
if (empty()) return Data_t{}; if (empty()) return Data_t{};
return *--r; return *--r;
} }
//! \brief Get a reference to the item in the front of the deque without extracting it. //! \brief Get a reference to the item in the front of the deque without extracting it.
//! \return Reference to the item //! \return Reference to the item
Data_t& front() noexcept { return *(f+1); }
const Data_t& front() const noexcept { return *(f+1); }
constexpr Data_t& front() noexcept { return *(f+1); }
constexpr const Data_t& front() const noexcept { return *(f+1); }
//! \brief Get a reference to the item in the front of the deque without extracting it. //! \brief Get a reference to the item in the front of the deque without extracting it.
//! \return Reference to the item //! \return Reference to the item
Data_t& back() noexcept { return *(r-1); }
const Data_t& back() const noexcept { return *(r-1); }
constexpr Data_t& back() noexcept { return *(r-1); }
constexpr const Data_t& back() const noexcept { return *(r-1); }
//! @} //! @}
private: private:


+ 3
- 3
include/cont/edeque.h View File

@@ -117,11 +117,11 @@ class edeque : public deque<Data_t, N> {
//! @{ //! @{
public: public:
//! Default constructor //! Default constructor
edeque () noexcept :
constexpr edeque () noexcept :
base_type() { } base_type() { }
//! //!
edeque (size_match match, size_t size, callable_t&& fn) :
constexpr edeque (size_match match, size_t size, callable_t&& fn) :
base_type(), base_type(),
mode_{match_mode::SIZE}, mode_{match_mode::SIZE},
callback_{std::forward<callable_t>(fn)} { callback_{std::forward<callable_t>(fn)} {
@@ -129,7 +129,7 @@ class edeque : public deque<Data_t, N> {
trigger_.tsize.size = size; trigger_.tsize.size = size;
} }
edeque (data_match match, Data_t value, callable_t&& fn) :
constexpr edeque (data_match match, Data_t value, callable_t&& fn) :
base_type(), base_type(),
mode_{match_mode::DATA}, mode_{match_mode::DATA},
callback_{std::forward<callable_t>(fn)} { callback_{std::forward<callable_t>(fn)} {


+ 3
- 3
include/cont/queue.h View File

@@ -73,14 +73,14 @@ class queue : public deque<Data_t, N> {
//! @{ //! @{
public: public:
//! Default constructor //! Default constructor
queue () noexcept : base_type() { }
constexpr queue () noexcept : base_type() { }
//! fill contructor //! fill contructor
queue(const Data_t& value) noexcept : base_type(value) { }
constexpr queue(const Data_t& value) noexcept : base_type(value) { }
//! Initializer list contructor //! Initializer list contructor
template <typename ...It> template <typename ...It>
queue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
constexpr queue(It&& ...it) noexcept : base_type(std::forward<It>(it)...) { }
//! @} //! @}


+ 22
- 18
include/core/ring_iterator.h View File

@@ -58,16 +58,16 @@ class ring_iterator {
//! \name Constructor / Destructor //! \name Constructor / Destructor
//! @{ //! @{
public: public:
ring_iterator(const Iter_t base =nullptr) noexcept :
constexpr ring_iterator(const Iter_t base =nullptr) noexcept :
base_(base), iter_(base) { } base_(base), iter_(base) { }
ring_iterator(const Iter_t base, size_t elem) noexcept :
constexpr ring_iterator(const Iter_t base, size_t elem) noexcept :
base_(base), iter_(base + elem) { } base_(base), iter_(base + elem) { }
ring_iterator(const ring_iterator& it) noexcept :
constexpr ring_iterator(const ring_iterator& it) noexcept :
base_(it.base_), iter_(it.iter_) { } base_(it.base_), iter_(it.iter_) { }
ring_iterator& operator= (const ring_iterator& it) noexcept {
constexpr ring_iterator& operator= (const ring_iterator& it) noexcept {
base_ = it.base_; base_ = it.base_;
iter_ = it.iter_; iter_ = it.iter_;
return *this; return *this;
@@ -77,18 +77,21 @@ class ring_iterator {
//! \name Forward iterator requirements //! \name Forward iterator requirements
//! @{ //! @{
public: public:
reference operator*() const noexcept {
constexpr reference operator*() const noexcept {
return *iter_; return *iter_;
} }
pointer operator->() const noexcept {
constexpr pointer operator->() const noexcept {
return iter_; return iter_;
} }
ring_iterator& operator++() noexcept {
constexpr ring_iterator& operator++() noexcept {
if (static_cast<size_t>(++iter_ - base_) >= N) if (static_cast<size_t>(++iter_ - base_) >= N)
iter_ = base_; iter_ = base_;
return *this; return *this;
} }
ring_iterator operator++(int) noexcept {
constexpr ring_iterator operator++(int) noexcept {
ring_iterator it = *this; ring_iterator it = *this;
if (static_cast<size_t>(++iter_ - base_) >= N) if (static_cast<size_t>(++iter_ - base_) >= N)
iter_ = base_; iter_ = base_;
@@ -99,12 +102,12 @@ class ring_iterator {
//! \name Bidirectional iterator requirements //! \name Bidirectional iterator requirements
//! @{ //! @{
public: public:
ring_iterator& operator--() noexcept {
constexpr ring_iterator& operator--() noexcept {
if (--iter_ < base_) if (--iter_ < base_)
iter_ = base_ + N -1; iter_ = base_ + N -1;
return *this; return *this;
} }
ring_iterator operator--(int) noexcept {
constexpr ring_iterator operator--(int) noexcept {
ring_iterator it = *this; ring_iterator it = *this;
if (--iter_ < base_) if (--iter_ < base_)
iter_ = base_ + N -1; iter_ = base_ + N -1;
@@ -114,33 +117,34 @@ class ring_iterator {
//! \name Random access iterator requirements //! \name Random access iterator requirements
//! @{ //! @{
reference operator[](difference_type n) const noexcept {
constexpr reference operator[](difference_type n) const noexcept {
difference_type k = iter_ - base_; // ptrdiff from base_ difference_type k = iter_ - base_; // ptrdiff from base_
return (static_cast<size_t>(k + n) < N) ? return (static_cast<size_t>(k + n) < N) ?
base_[k + n] : // on range base_[k + n] : // on range
base_[k + n - N]; // out of range, loop base_[k + n - N]; // out of range, loop
} }
ring_iterator& operator+=(difference_type n) noexcept {
constexpr ring_iterator& operator+=(difference_type n) noexcept {
difference_type k = iter_ - base_; // ptrdiff from base_ difference_type k = iter_ - base_; // ptrdiff from base_
iter_ += (static_cast<size_t>(k + n) < N) ? iter_ += (static_cast<size_t>(k + n) < N) ?
n : // on range n : // on range
n - N; // out of range, loop n - N; // out of range, loop
return *this; return *this;
} }
ring_iterator operator+(difference_type n) const noexcept {
constexpr ring_iterator operator+(difference_type n) const noexcept {
difference_type k = iter_ - base_; // ptrdiff from base_ difference_type k = iter_ - base_; // ptrdiff from base_
return (static_cast<size_t>(k + n) < N) ? return (static_cast<size_t>(k + n) < N) ?
ring_iterator(base_, k + n) : // on range ring_iterator(base_, k + n) : // on range
ring_iterator(base_, k + n - N); // out of range, loop ring_iterator(base_, k + n - N); // out of range, loop
} }
ring_iterator& operator-=(difference_type n) noexcept {
constexpr ring_iterator& operator-=(difference_type n) noexcept {
difference_type k = iter_ - base_; // ptrdiff from base_ difference_type k = iter_ - base_; // ptrdiff from base_
iter_ -= ((k - n) < 0)? iter_ -= ((k - n) < 0)?
n - N: // out of range, loop n - N: // out of range, loop
n; // on range n; // on range
return *this; return *this;
} }
ring_iterator operator-(difference_type n) const noexcept {
constexpr ring_iterator operator-(difference_type n) const noexcept {
difference_type k = iter_ - base_; // ptrdiff from base_ difference_type k = iter_ - base_; // ptrdiff from base_
return ((k - n) < 0) ? return ((k - n) < 0) ?
ring_iterator(base_, k - n + N) : // out of range, loop ring_iterator(base_, k - n + N) : // out of range, loop
@@ -150,13 +154,13 @@ class ring_iterator {
//! \name Data members and access //! \name Data members and access
//! @{ //! @{
const Iter_t& base() const noexcept {
constexpr const Iter_t& base() const noexcept {
return base_; return base_;
} }
const Iter_t& iter() const noexcept {
constexpr const Iter_t& iter() const noexcept {
return iter_; return iter_;
} }
size_t size() noexcept {
constexpr size_t size() noexcept {
return N; return N;
} }


Loading…
Cancel
Save