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:
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);
T end (void);
constexpr T begin (void) const noexcept;
constexpr T end (void) const noexcept;
bool empty (void) const;
size_t size (void) const;
constexpr T cbegin (void) const noexcept;
constexpr T cend (void) const noexcept;
value_type& operator[] (size_t);
const value_type& operator[] (size_t) const;
constexpr T find (value_type) const noexcept;
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:
T m_begin;

View File

@ -25,9 +25,21 @@
#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>
util::view<T>::view (T _begin, T _end):
constexpr
util::view<T>::view (T _begin, T _end) noexcept:
m_begin (_begin),
m_end (_end)
{ ; }
@ -35,8 +47,26 @@ util::view<T>::view (T _begin, T _end):
//-----------------------------------------------------------------------------
template <typename T>
T
util::view<T>::begin (void)
constexpr T
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;
}
@ -44,8 +74,8 @@ util::view<T>::begin (void)
//-----------------------------------------------------------------------------
template <typename T>
T
util::view<T>::end (void)
constexpr T
util::view<T>::cend (void) const noexcept
{
return m_end;
}
@ -53,8 +83,20 @@ util::view<T>::end (void)
///////////////////////////////////////////////////////////////////////////////
template <typename T>
bool
util::view<T>::empty (void) const
constexpr T
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;
}
@ -62,17 +104,17 @@ util::view<T>::empty (void) const
//-----------------------------------------------------------------------------
template <typename T>
size_t
util::view<T>::size (void) const
constexpr size_t
util::view<T>::size (void) const noexcept
{
return std::distance (m_begin, m_end);
return m_end - m_begin;
}
///////////////////////////////////////////////////////////////////////////////
template <typename T>
const typename util::view<T>::value_type&
util::view<T>::operator[] (size_t idx) const
constexpr const typename util::view<T>::value_type&
util::view<T>::operator[] (size_t idx) const noexcept
{
CHECK_LT (m_begin + idx, m_end);
return m_begin[idx];
@ -82,7 +124,7 @@ util::view<T>::operator[] (size_t idx) const
///////////////////////////////////////////////////////////////////////////////
template <typename T>
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 &&
rhs.m_end == m_end;