diff --git a/view.hpp b/view.hpp index c0e31bf3..84f38edd 100644 --- a/view.hpp +++ b/view.hpp @@ -27,18 +27,25 @@ namespace util { public: using value_type = typename std::iterator_traits::value_type; - view (T first, T last); + template + 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; diff --git a/view.ipp b/view.ipp index 9621cf2a..9c1a043f 100644 --- a/view.ipp +++ b/view.ipp @@ -25,9 +25,21 @@ #include + +/////////////////////////////////////////////////////////////////////////////// +template +template +constexpr +util::view::view (const value_type(&arr)[S]) noexcept: + m_begin (arr), + m_end (arr + S) +{ ; } + + //----------------------------------------------------------------------------- template -util::view::view (T _begin, T _end): +constexpr +util::view::view (T _begin, T _end) noexcept: m_begin (_begin), m_end (_end) { ; } @@ -35,8 +47,26 @@ util::view::view (T _begin, T _end): //----------------------------------------------------------------------------- template -T -util::view::begin (void) +constexpr T +util::view::begin (void) const noexcept +{ + return cbegin (); +} + + +//----------------------------------------------------------------------------- +template +constexpr T +util::view::end (void) const noexcept +{ + return cend (); +} + + +//----------------------------------------------------------------------------- +template +constexpr T +util::view::cbegin (void) const noexcept { return m_begin; } @@ -44,17 +74,29 @@ util::view::begin (void) //----------------------------------------------------------------------------- template -T -util::view::end (void) +constexpr T +util::view::cend (void) const noexcept { - return m_end; + return m_end; } /////////////////////////////////////////////////////////////////////////////// template -bool -util::view::empty (void) const +constexpr T +util::view::find (value_type v) const noexcept +{ + for (T i = cbegin (); i != cend (); ++i) + if (*i == v) + return i; + return cend (); +} + + +/////////////////////////////////////////////////////////////////////////////// +template +constexpr bool +util::view::empty (void) const noexcept { return m_begin == m_end; } @@ -62,17 +104,17 @@ util::view::empty (void) const //----------------------------------------------------------------------------- template -size_t -util::view::size (void) const +constexpr size_t +util::view::size (void) const noexcept { - return std::distance (m_begin, m_end); + return m_end - m_begin; } /////////////////////////////////////////////////////////////////////////////// template -const typename util::view::value_type& -util::view::operator[] (size_t idx) const +constexpr const typename util::view::value_type& +util::view::operator[] (size_t idx) const noexcept { CHECK_LT (m_begin + idx, m_end); return m_begin[idx]; @@ -82,7 +124,7 @@ util::view::operator[] (size_t idx) const /////////////////////////////////////////////////////////////////////////////// template bool -util::view::operator== (const view rhs) const +util::view::operator== (const view rhs) const noexcept { return rhs.m_begin == m_begin && rhs.m_end == m_end;