/* * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * Copyright 2015 Danny Robson */ #ifdef __UTIL_VIEW_IPP #error #endif #define __UTIL_VIEW_IPP #include "./debug.hpp" #include "./iterator.hpp" /////////////////////////////////////////////////////////////////////////////// template constexpr util::view::view (T _begin, T _end) noexcept: m_begin (_begin), m_end (_end) { ; } //----------------------------------------------------------------------------- template template constexpr util::view::view (K &data): m_begin (std::begin (data)), m_end (std::end (data)) { ; } //----------------------------------------------------------------------------- template template constexpr util::view::view (const K &data): m_begin (std::begin (data)), m_end (std::end (data)) { ; } /////////////////////////////////////////////////////////////////////////////// template constexpr T& util::view::begin (void) noexcept { return m_begin; } //----------------------------------------------------------------------------- template constexpr T& util::view::end (void) noexcept { return m_end; } //----------------------------------------------------------------------------- template constexpr const T& util::view::begin (void) const noexcept { return cbegin (); } //----------------------------------------------------------------------------- template constexpr const T& util::view::end (void) const noexcept { return cend (); } //----------------------------------------------------------------------------- template constexpr const T& util::view::cbegin (void) const noexcept { return m_begin; } //----------------------------------------------------------------------------- template constexpr const T& util::view::cend (void) const noexcept { return m_end; } /////////////////////////////////////////////////////////////////////////////// template constexpr T util::view::find (const 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; } //----------------------------------------------------------------------------- template constexpr size_t util::view::size (void) const noexcept { return m_end - m_begin; } /////////////////////////////////////////////////////////////////////////////// template constexpr const typename util::view::value_type& util::view::operator[] (size_t idx) const noexcept { return m_begin[idx]; } //----------------------------------------------------------------------------- template constexpr typename util::view::value_type& util::view::operator[] (size_t idx) noexcept { return m_begin[idx]; } /////////////////////////////////////////////////////////////////////////////// template bool util::view::operator== (const view rhs) const noexcept { return rhs.m_begin == m_begin && rhs.m_end == m_end; } /////////////////////////////////////////////////////////////////////////////// template auto util::make_view (const T (&arr)[N]) { return util::view { arr, arr + N }; } //----------------------------------------------------------------------------- template auto util::make_view (T &t) { return util::view { std::begin (t), std::end (t) }; } //----------------------------------------------------------------------------- template auto util::make_view (const T &t) { return util::view { std::cbegin (t), std::cend (t) }; } //----------------------------------------------------------------------------- template auto util::make_cview (const T &t) { return util::view { std::cbegin (t), std::cend (t) }; }