view: make equality consistent
This commit is contained in:
parent
cfa272a80a
commit
220e499681
@ -9,10 +9,10 @@ main (int, char**)
|
|||||||
const std::string s = "this is a test string";
|
const std::string s = "this is a test string";
|
||||||
const std::string t = "not the same string";
|
const std::string t = "not the same string";
|
||||||
|
|
||||||
tap.expect_eq (s, util::make_view(s), "string/view equality");
|
tap.expect (equal (s, util::make_view(s)), "string/view equality");
|
||||||
tap.expect_neq (s, util::make_view(t), "string/view inequality");
|
tap.expect (!equal (s, util::make_view(t)), "string/view inequality");
|
||||||
tap.expect_eq (s.data (), util::make_view (s), "c-str/view equality");
|
tap.expect (equal (s.data (), util::make_view (s)), "c-str/view equality");
|
||||||
tap.expect_eq (s, util::view<const char*> (&*s.cbegin (), &*s.cend ()), "string/pointer-view equality");
|
tap.expect (equal (s, util::view<const char*> (&*s.cbegin (), &*s.cend ())), "string/pointer-view equality");
|
||||||
|
|
||||||
return tap.status ();
|
return tap.status ();
|
||||||
}
|
}
|
||||||
|
4
view.cpp
4
view.cpp
@ -11,7 +11,7 @@
|
|||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*
|
*
|
||||||
* Copyright 2016 Danny Robson <danny@nerdcruft.net>
|
* Copyright 2016-2017 Danny Robson <danny@nerdcruft.net>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "./view.hpp"
|
#include "./view.hpp"
|
||||||
@ -23,7 +23,7 @@
|
|||||||
///////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////
|
||||||
#define EQUALITY(A,B) \
|
#define EQUALITY(A,B) \
|
||||||
bool \
|
bool \
|
||||||
util::operator== (A a, B b) \
|
util::equal (A a, B b) \
|
||||||
{ \
|
{ \
|
||||||
return a.size () == a.size () && \
|
return a.size () == a.size () && \
|
||||||
std::equal (a.cbegin (), \
|
std::equal (a.cbegin (), \
|
||||||
|
174
view.hpp
174
view.hpp
@ -28,18 +28,24 @@
|
|||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template <typename T>
|
template <typename IteratorT>
|
||||||
struct view {
|
struct view {
|
||||||
public:
|
public:
|
||||||
using value_type = typename std::iterator_traits<remove_restrict_t<T>>::value_type;
|
//---------------------------------------------------------------------
|
||||||
|
using value_type = typename std::iterator_traits<
|
||||||
|
remove_restrict_t<IteratorT>
|
||||||
|
>::value_type;
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
constexpr
|
constexpr
|
||||||
view (const T &first, const T &last) noexcept:
|
view (const IteratorT &first, const IteratorT &last) noexcept:
|
||||||
m_begin (first),
|
m_begin (first),
|
||||||
m_end (last)
|
m_end (last)
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
constexpr
|
constexpr
|
||||||
view (const view &rhs) noexcept:
|
view (const view &rhs) noexcept:
|
||||||
m_begin (rhs.m_begin),
|
m_begin (rhs.m_begin),
|
||||||
@ -47,34 +53,38 @@ namespace util {
|
|||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
// technically we could get away without explicitly defining a move
|
// technically we could get away without explicitly defining a move
|
||||||
// constructor here, but by nulling rhs we can more easily use this
|
// constructor here, but by nulling rhs we can more easily use this
|
||||||
// class as a base for unique owning pointers without exposing
|
// class as a base for unique owning pointers without exposing
|
||||||
// begin/end to them directly.
|
// begin/end to them directly.
|
||||||
constexpr view (view &&rhs) noexcept:
|
constexpr view (view &&rhs) noexcept:
|
||||||
view (T{}, T{})
|
view (IteratorT{}, IteratorT{})
|
||||||
{
|
{
|
||||||
std::swap (m_begin, rhs.m_begin);
|
std::swap (m_begin, rhs.m_begin);
|
||||||
std::swap (m_end, rhs.m_end);
|
std::swap (m_end, rhs.m_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template <typename K>
|
//---------------------------------------------------------------------
|
||||||
|
template <typename ContainerT>
|
||||||
constexpr explicit
|
constexpr explicit
|
||||||
view (K &klass):
|
view (ContainerT &klass):
|
||||||
m_begin (std::begin (klass)),
|
m_begin (std::begin (klass)),
|
||||||
m_end (std::end (klass))
|
m_end (std::end (klass))
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
template <typename K>
|
//---------------------------------------------------------------------
|
||||||
|
template <typename ContainerT>
|
||||||
constexpr explicit
|
constexpr explicit
|
||||||
view (const K &klass):
|
view (const ContainerT &klass):
|
||||||
m_begin (std::begin (klass)),
|
m_begin (std::begin (klass)),
|
||||||
m_end (std::end (klass))
|
m_end (std::end (klass))
|
||||||
{ ; }
|
{ ; }
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
view&
|
view&
|
||||||
operator= (const view &rhs) noexcept
|
operator= (const view &rhs) noexcept
|
||||||
{
|
{
|
||||||
@ -84,43 +94,51 @@ namespace util {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
view&
|
view&
|
||||||
operator= (view &&rhs) noexcept
|
operator= (view &&rhs) noexcept
|
||||||
{
|
{
|
||||||
m_begin = rhs.m_begin;
|
m_begin = rhs.m_begin;
|
||||||
m_end = rhs.m_end;
|
m_end = rhs.m_end;
|
||||||
|
|
||||||
m_begin = T{};
|
rhs.m_begin = IteratorT{};
|
||||||
m_end = T{};
|
rhs.m_end = IteratorT{};
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
constexpr T begin (void) noexcept { return m_begin; }
|
//---------------------------------------------------------------------
|
||||||
constexpr T end (void) noexcept { return m_end; }
|
constexpr IteratorT begin (void) noexcept { return m_begin; }
|
||||||
|
constexpr IteratorT end (void) noexcept { return m_end; }
|
||||||
|
|
||||||
constexpr const T begin (void) const noexcept { return cbegin (); }
|
//---------------------------------------------------------------------
|
||||||
constexpr const T end (void) const noexcept { return cend (); }
|
constexpr const IteratorT begin (void) const noexcept { return cbegin (); }
|
||||||
|
constexpr const IteratorT end (void) const noexcept { return cend (); }
|
||||||
|
|
||||||
constexpr const T cbegin (void) const noexcept { return m_begin; }
|
//---------------------------------------------------------------------
|
||||||
constexpr const T cend (void) const noexcept { return m_end; }
|
constexpr const IteratorT cbegin (void) const noexcept { return m_begin; }
|
||||||
|
constexpr const IteratorT cend (void) const noexcept { return m_end; }
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
auto data (void) { return begin (); }
|
auto data (void) { return begin (); }
|
||||||
auto data (void) const { return begin (); }
|
auto data (void) const { return begin (); }
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
constexpr bool
|
constexpr bool
|
||||||
empty (void) const noexcept
|
empty (void) const noexcept
|
||||||
{
|
{
|
||||||
return m_begin == m_end;
|
return m_begin == m_end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
constexpr auto
|
constexpr auto
|
||||||
size (void) const noexcept
|
size (void) const noexcept
|
||||||
{
|
{
|
||||||
return std::distance (m_begin, m_end);
|
return std::distance (m_begin, m_end);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
constexpr auto
|
constexpr auto
|
||||||
redim (int count) const
|
redim (int count) const
|
||||||
{
|
{
|
||||||
@ -130,94 +148,107 @@ namespace util {
|
|||||||
return view { m_begin, m_begin + count };
|
return view { m_begin, m_begin + count };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
constexpr value_type&
|
constexpr value_type&
|
||||||
operator[] (size_t idx) noexcept
|
operator[] (size_t idx)& noexcept
|
||||||
{
|
{
|
||||||
auto it = begin ();
|
auto it = begin ();
|
||||||
std::advance (it, idx);
|
std::advance (it, idx);
|
||||||
return *it;
|
return *it;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
constexpr const value_type&
|
constexpr const value_type&
|
||||||
operator[] (size_t idx) const noexcept
|
operator[] (size_t idx) const& noexcept
|
||||||
{
|
{
|
||||||
auto it = begin ();
|
auto it = begin ();
|
||||||
std::advance (it, idx);
|
std::advance (it, idx);
|
||||||
return *it;
|
return *it;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
|
||||||
operator== (const view &rhs) const noexcept
|
|
||||||
{
|
|
||||||
return rhs.m_begin == m_begin &&
|
|
||||||
rhs.m_end == m_end;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
T m_begin;
|
//---------------------------------------------------------------------
|
||||||
T m_end;
|
IteratorT m_begin;
|
||||||
|
IteratorT m_end;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
template <typename T, size_t N>
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename ValueT, size_t N>
|
||||||
auto
|
auto
|
||||||
make_view (const T (&arr)[N])
|
make_view (const ValueT (&arr)[N])
|
||||||
{
|
{
|
||||||
return util::view<const T*> (arr + 0, arr + N);
|
return util::view<const ValueT*> (arr + 0, arr + N);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename ContainerT>
|
||||||
auto
|
auto
|
||||||
make_view (T &t)
|
make_view (ContainerT &t)
|
||||||
{
|
{
|
||||||
return util::view<decltype(std::begin (t))> { std::begin (t), std::end (t) };
|
return util::view<decltype(std::begin (t))> { std::begin (t), std::end (t) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename ContainerT>
|
||||||
auto
|
auto
|
||||||
make_view (const T &t)
|
make_view (const ContainerT &t)
|
||||||
{
|
{
|
||||||
return util::view<decltype(std::cbegin (t))> { std::cbegin (t), std::cend (t) };
|
return util::view<decltype(std::cbegin (t))> { std::cbegin (t), std::cend (t) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
auto
|
|
||||||
make_view (T&&) = delete;
|
|
||||||
|
|
||||||
template <typename T>
|
//-------------------------------------------------------------------------
|
||||||
|
// disable the possibility of creating a view to a temporary. note that
|
||||||
|
// this only works if an lval version has already been defined otherwise
|
||||||
|
// universal reference rules will capture both lval and rval here.
|
||||||
|
template <typename ContainerT>
|
||||||
auto
|
auto
|
||||||
make_cview (const T &t)
|
make_view (ContainerT&&) = delete;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename ContainerT>
|
||||||
|
auto
|
||||||
|
make_cview (const ContainerT &t)
|
||||||
{
|
{
|
||||||
return util::view<decltype(std::cbegin (t))> { std::cbegin (t), std::cend (t) };
|
return util::view<decltype(std::cbegin (t))> { std::cbegin (t), std::cend (t) };
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename IteratorT>
|
||||||
auto
|
auto
|
||||||
make_view (T first, T last)
|
make_view (IteratorT first, IteratorT last)
|
||||||
{
|
{
|
||||||
return view<T> {first, last};
|
return view<IteratorT> {first, last};
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename ValueT>
|
||||||
auto
|
auto
|
||||||
make_cview (T *first, T *last)
|
make_cview (ValueT *first, ValueT *last)
|
||||||
{
|
{
|
||||||
return view<const T*> {first, last};
|
return view<const ValueT*> {first, last};
|
||||||
}
|
}
|
||||||
|
|
||||||
// string conversions
|
///////////////////////////////////////////////////////////////////////////
|
||||||
inline
|
inline
|
||||||
view<const char*> make_view (const char *str)
|
view<const char*> make_view (const char *str)
|
||||||
{
|
{
|
||||||
return { str, str + strlen (str) };
|
return { str, str + strlen (str) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
inline
|
inline
|
||||||
view<char*> make_view (char *str)
|
view<char*> make_view (char *str)
|
||||||
{
|
{
|
||||||
return { str, str + strlen (str) };
|
return { str, str + strlen (str) };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
template <typename CharT, typename TraitsT, typename AllocT>
|
template <typename CharT, typename TraitsT, typename AllocT>
|
||||||
view<const CharT*>
|
view<const CharT*>
|
||||||
make_view (const std::basic_string<CharT,TraitsT,AllocT> &str)
|
make_view (const std::basic_string<CharT,TraitsT,AllocT> &str)
|
||||||
@ -228,6 +259,8 @@ namespace util {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
template <typename CharT, typename TraitsT, typename AllocT>
|
template <typename CharT, typename TraitsT, typename AllocT>
|
||||||
view<CharT*>
|
view<CharT*>
|
||||||
make_view (std::basic_string<CharT,TraitsT,AllocT> &str)
|
make_view (std::basic_string<CharT,TraitsT,AllocT> &str)
|
||||||
@ -238,31 +271,56 @@ namespace util {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
template <typename CharT, typename TraitsT, typename AllocT>
|
template <typename CharT, typename TraitsT, typename AllocT>
|
||||||
view<const CharT*>
|
view<const CharT*>
|
||||||
make_view (const std::basic_string<CharT,TraitsT,AllocT>&&) = delete;
|
make_view (const std::basic_string<CharT,TraitsT,AllocT>&&) = delete;
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
template <typename CharT, typename TraitsT, typename AllocT>
|
template <typename CharT, typename TraitsT, typename AllocT>
|
||||||
view<CharT*>
|
view<CharT*>
|
||||||
make_view (std::basic_string<CharT,TraitsT,AllocT>&&) = delete;
|
make_view (std::basic_string<CharT,TraitsT,AllocT>&&) = delete;
|
||||||
|
|
||||||
// boolean operationrs
|
|
||||||
template <typename T>
|
|
||||||
bool operator!= (view<T>, view<T>);
|
|
||||||
|
|
||||||
bool operator== (const std::string&, view<const char*>);
|
///////////////////////////////////////////////////////////////////////////
|
||||||
bool operator== (const std::string&, view<char*>);
|
template <typename IteratorT>
|
||||||
bool operator== (const std::string&, view<std::string::const_iterator>);
|
constexpr bool
|
||||||
bool operator== (const std::string&, view<std::string::iterator>);
|
operator== (const view<IteratorT> &a, const view<IteratorT> &b)
|
||||||
|
{
|
||||||
|
return std::cbegin (a) == std::cbegin (b) &&
|
||||||
|
std::cend (a) == std::cend (b);
|
||||||
|
}
|
||||||
|
|
||||||
bool operator== (view<const char*>, const std::string&);
|
|
||||||
bool operator== (view<char*>, const std::string&);
|
|
||||||
bool operator== (view<std::string::const_iterator>, const std::string&);
|
|
||||||
bool operator== (view<std::string::iterator>, const std::string&);
|
|
||||||
|
|
||||||
template <typename T>
|
//-------------------------------------------------------------------------
|
||||||
|
template <typename IteratorT>
|
||||||
|
constexpr bool
|
||||||
|
operator!= (const view<IteratorT> &a, const view<IteratorT> &b)
|
||||||
|
{
|
||||||
|
return !(a == b);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------
|
||||||
|
// equality operations for string-like views against std::strings
|
||||||
|
bool equal (const std::string&, view<const char*>);
|
||||||
|
bool equal (const std::string&, view<char*>);
|
||||||
|
bool equal (const std::string&, view<std::string::const_iterator>);
|
||||||
|
bool equal (const std::string&, view<std::string::iterator>);
|
||||||
|
|
||||||
|
|
||||||
|
bool equal (view<const char*>, const std::string&);
|
||||||
|
bool equal (view<char*>, const std::string&);
|
||||||
|
bool equal (view<std::string::const_iterator>, const std::string&);
|
||||||
|
bool equal (view<std::string::iterator>, const std::string&);
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////////////////////////
|
||||||
|
template <typename IteratorT>
|
||||||
std::ostream&
|
std::ostream&
|
||||||
operator<< (std::ostream&, view<T>);
|
operator<< (std::ostream&, view<IteratorT>);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user