2015-02-11 16:18:43 +11:00
|
|
|
/*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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
|
2015-02-11 16:18:43 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2015-02-11 16:18:43 +11:00
|
|
|
*
|
2015-04-13 18:05:28 +10:00
|
|
|
* 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.
|
2015-02-11 16:18:43 +11:00
|
|
|
*
|
|
|
|
* Copyright 2015 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-09-22 18:31:47 +10:00
|
|
|
#ifdef __UTIL_VIEW_IPP
|
|
|
|
#error
|
|
|
|
#endif
|
|
|
|
#define __UTIL_VIEW_IPP
|
|
|
|
|
2015-02-11 16:18:43 +11:00
|
|
|
|
2016-03-17 18:12:34 +11:00
|
|
|
#include "./debug.hpp"
|
|
|
|
#include "./iterator.hpp"
|
2015-02-11 16:18:43 +11:00
|
|
|
|
2015-09-25 13:41:04 +10:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
util::view<T>::view (T _begin, T _end) noexcept:
|
2015-02-11 16:41:09 +11:00
|
|
|
m_begin (_begin),
|
|
|
|
m_end (_end)
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-09-21 15:36:05 +10:00
|
|
|
template <typename T>
|
2016-06-28 14:09:35 +10:00
|
|
|
template <typename K>
|
|
|
|
constexpr
|
|
|
|
util::view<T>::view (K &data):
|
|
|
|
m_begin (std::begin (data)),
|
|
|
|
m_end (std::end (data))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
template <typename K>
|
|
|
|
constexpr
|
|
|
|
util::view<T>::view (const K &data):
|
|
|
|
m_begin (std::begin (data)),
|
|
|
|
m_end (std::end (data))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2016-03-17 18:12:34 +11:00
|
|
|
constexpr T&
|
|
|
|
util::view<T>::begin (void) noexcept
|
|
|
|
{
|
|
|
|
return m_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr T&
|
|
|
|
util::view<T>::end (void) noexcept
|
|
|
|
{
|
|
|
|
return m_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr const T&
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::begin (void) const noexcept
|
|
|
|
{
|
|
|
|
return cbegin ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2016-03-17 18:12:34 +11:00
|
|
|
constexpr const T&
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::end (void) const noexcept
|
|
|
|
{
|
|
|
|
return cend ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2016-03-17 18:12:34 +11:00
|
|
|
constexpr const T&
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::cbegin (void) const noexcept
|
2015-02-11 16:41:09 +11:00
|
|
|
{
|
|
|
|
return m_begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-09-21 15:36:05 +10:00
|
|
|
template <typename T>
|
2016-03-17 18:12:34 +11:00
|
|
|
constexpr const T&
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::cend (void) const noexcept
|
2015-02-11 16:41:09 +11:00
|
|
|
{
|
2015-09-25 13:41:04 +10:00
|
|
|
return m_end;
|
2015-02-11 16:41:09 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-22 18:31:47 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-21 15:36:05 +10:00
|
|
|
template <typename T>
|
2015-09-25 13:41:04 +10:00
|
|
|
constexpr T
|
2016-11-14 21:29:43 +11:00
|
|
|
util::view<T>::find (const value_type &v) const noexcept
|
2015-09-25 13:41:04 +10:00
|
|
|
{
|
|
|
|
for (T i = cbegin (); i != cend (); ++i)
|
|
|
|
if (*i == v)
|
|
|
|
return i;
|
|
|
|
return cend ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
template <typename T>
|
2016-02-10 13:07:03 +11:00
|
|
|
constexpr
|
|
|
|
bool
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::empty (void) const noexcept
|
2015-02-11 16:18:43 +11:00
|
|
|
{
|
2015-09-21 15:36:05 +10:00
|
|
|
return m_begin == m_end;
|
2015-02-11 16:18:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-09-21 15:36:05 +10:00
|
|
|
template <typename T>
|
2016-02-10 13:07:03 +11:00
|
|
|
constexpr
|
|
|
|
size_t
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::size (void) const noexcept
|
2015-02-11 16:18:43 +11:00
|
|
|
{
|
2015-09-25 13:41:04 +10:00
|
|
|
return m_end - m_begin;
|
2015-02-11 16:18:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-22 18:31:47 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-21 15:36:05 +10:00
|
|
|
template <typename T>
|
2016-02-10 13:07:03 +11:00
|
|
|
constexpr
|
2016-12-06 15:18:49 +11:00
|
|
|
const typename util::view<T>::value_type&
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::operator[] (size_t idx) const noexcept
|
2015-02-11 16:18:43 +11:00
|
|
|
{
|
2015-02-11 16:41:09 +11:00
|
|
|
return m_begin[idx];
|
2015-02-11 16:18:43 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-14 21:30:35 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
constexpr
|
|
|
|
typename util::view<T>::value_type&
|
|
|
|
util::view<T>::operator[] (size_t idx) noexcept
|
|
|
|
{
|
|
|
|
return m_begin[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-22 18:31:47 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-09-21 15:36:05 +10:00
|
|
|
template <typename T>
|
2015-02-11 16:42:32 +11:00
|
|
|
bool
|
2015-09-25 13:41:04 +10:00
|
|
|
util::view<T>::operator== (const view<T> rhs) const noexcept
|
2015-02-11 16:42:32 +11:00
|
|
|
{
|
2015-09-21 15:36:05 +10:00
|
|
|
return rhs.m_begin == m_begin &&
|
|
|
|
rhs.m_end == m_end;
|
2015-02-11 16:42:32 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-22 18:31:47 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2016-11-14 21:29:16 +11:00
|
|
|
template <typename T, size_t N>
|
|
|
|
auto
|
|
|
|
util::make_view (const T (&arr)[N])
|
|
|
|
{
|
2017-01-04 22:39:18 +11:00
|
|
|
return util::view<const T*> (arr + 0, arr + N);
|
2016-11-14 21:29:16 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2015-09-21 15:36:05 +10:00
|
|
|
template <typename T>
|
2016-06-28 14:06:02 +10:00
|
|
|
auto
|
2016-03-17 18:12:34 +11:00
|
|
|
util::make_view (T &t)
|
2015-02-11 16:18:43 +11:00
|
|
|
{
|
2016-12-06 15:18:49 +11:00
|
|
|
return util::view<decltype(std::begin (t))> { std::begin (t), std::end (t) };
|
2016-03-17 18:12:34 +11:00
|
|
|
}
|
2016-02-10 13:08:02 +11:00
|
|
|
|
|
|
|
|
2016-03-17 18:12:34 +11:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
2016-06-28 14:06:02 +10:00
|
|
|
auto
|
2016-03-17 18:12:34 +11:00
|
|
|
util::make_view (const T &t)
|
|
|
|
{
|
2016-12-06 15:18:49 +11:00
|
|
|
return util::view<decltype(std::cbegin (t))> { std::cbegin (t), std::cend (t) };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
template <typename T>
|
|
|
|
auto
|
|
|
|
util::make_cview (const T &t)
|
|
|
|
{
|
|
|
|
return util::view<decltype(std::cbegin (t))> { std::cbegin (t), std::cend (t) };
|
2015-02-11 16:18:43 +11:00
|
|
|
}
|