view: make methods constexpr

the class is used in some constexpr parsing routines, so constexpr for
many methods is required.
This commit is contained in:
Danny Robson 2015-09-25 13:41:04 +10:00
parent c7637fd627
commit 4310366987
2 changed files with 71 additions and 22 deletions

View File

@ -27,18 +27,25 @@ namespace util {
public: public:
using value_type = typename std::iterator_traits<T>::value_type; using value_type = typename std::iterator_traits<T>::value_type;
view (T first, T last); template <size_t S>
constexpr view (const value_type (&arr)[S]) noexcept;
constexpr view (T first, T last) noexcept;
T begin (void); constexpr T begin (void) const noexcept;
T end (void); constexpr T end (void) const noexcept;
bool empty (void) const; constexpr T cbegin (void) const noexcept;
size_t size (void) const; constexpr T cend (void) const noexcept;
value_type& operator[] (size_t); constexpr T find (value_type) const noexcept;
const value_type& operator[] (size_t) const;
bool operator== (view) const; constexpr bool empty (void) const noexcept;
constexpr size_t size (void) const noexcept;
constexpr value_type& operator[] (size_t) noexcept;
constexpr const value_type& operator[] (size_t) const noexcept;
bool operator== (view) const noexcept;
private: private:
T m_begin; T m_begin;

View File

@ -25,9 +25,21 @@
#include <iterator> #include <iterator>
///////////////////////////////////////////////////////////////////////////////
template <typename T>
template <size_t S>
constexpr
util::view<T>::view (const value_type(&arr)[S]) noexcept:
m_begin (arr),
m_end (arr + S)
{ ; }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
util::view<T>::view (T _begin, T _end): constexpr
util::view<T>::view (T _begin, T _end) noexcept:
m_begin (_begin), m_begin (_begin),
m_end (_end) m_end (_end)
{ ; } { ; }
@ -35,8 +47,26 @@ util::view<T>::view (T _begin, T _end):
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T constexpr T
util::view<T>::begin (void) util::view<T>::begin (void) const noexcept
{
return cbegin ();
}
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
util::view<T>::end (void) const noexcept
{
return cend ();
}
//-----------------------------------------------------------------------------
template <typename T>
constexpr T
util::view<T>::cbegin (void) const noexcept
{ {
return m_begin; return m_begin;
} }
@ -44,17 +74,29 @@ util::view<T>::begin (void)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
T constexpr T
util::view<T>::end (void) util::view<T>::cend (void) const noexcept
{ {
return m_end; return m_end;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
bool constexpr T
util::view<T>::empty (void) const util::view<T>::find (value_type v) const noexcept
{
for (T i = cbegin (); i != cend (); ++i)
if (*i == v)
return i;
return cend ();
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
constexpr bool
util::view<T>::empty (void) const noexcept
{ {
return m_begin == m_end; return m_begin == m_end;
} }
@ -62,17 +104,17 @@ util::view<T>::empty (void) const
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
template <typename T> template <typename T>
size_t constexpr size_t
util::view<T>::size (void) const util::view<T>::size (void) const noexcept
{ {
return std::distance (m_begin, m_end); return m_end - m_begin;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
const typename util::view<T>::value_type& constexpr const typename util::view<T>::value_type&
util::view<T>::operator[] (size_t idx) const util::view<T>::operator[] (size_t idx) const noexcept
{ {
CHECK_LT (m_begin + idx, m_end); CHECK_LT (m_begin + idx, m_end);
return m_begin[idx]; return m_begin[idx];
@ -82,7 +124,7 @@ util::view<T>::operator[] (size_t idx) const
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
template <typename T> template <typename T>
bool bool
util::view<T>::operator== (const view<T> rhs) const util::view<T>::operator== (const view<T> rhs) const noexcept
{ {
return rhs.m_begin == m_begin && return rhs.m_begin == m_begin &&
rhs.m_end == m_end; rhs.m_end == m_end;