view: simplify logical comparator implementation

we remove some duplicate and specific logical comparator specialisations
and instead rely on a base view/view comparator implementation by way of
make_view.
This commit is contained in:
Danny Robson 2017-12-20 12:31:52 +11:00
parent cd69bb684a
commit 202c22eee8
5 changed files with 81 additions and 91 deletions

View File

@ -30,7 +30,7 @@ main (int, char**)
};
for (const auto &[tok, expected]: util::zip (util::make_tokeniser (csv, ','), TESTS))
tap.expect (equal (tok, expected.value), "%s", expected.message);
tap.expect_eq (tok, expected.value, "%s", expected.message);
return tap.status ();
}

View File

@ -144,15 +144,15 @@ main (void)
tap.expect_nothrow ([t] (void) { util::uri foo (t.src); }, "nothrow parsing '%s'", t.src);
util::uri u (t.src);
tap.expect (equal (u.scheme (), t.scheme), "scheme for '%s'", t.src);
tap.expect (equal (u.heirarchical (), t.hierarchical), "hierarchical for '%s'", t.src);
tap.expect (equal (u.authority (), t.authority), "authority for '%s'", t.src);
tap.expect (equal (u.host (), t.host), "host for '%s'", t.src);
tap.expect (equal (u.user (), t.user), "user for '%s'", t.src);
tap.expect (equal (u.port (), t.port), "port for '%s'", t.src);
tap.expect (equal (u.path (), t.path), "path for '%s'", t.src);
tap.expect (equal (u.query (), t.query), "query for '%s'", t.src);
tap.expect (equal (u.fragment (), t.fragment), "fragment for '%s'", t.src);
tap.expect_eq (u.scheme (), t.scheme, "scheme for '%s'", t.src);
tap.expect_eq (u.heirarchical (), t.hierarchical, "hierarchical for '%s'", t.src);
tap.expect_eq (u.authority (), t.authority, "authority for '%s'", t.src);
tap.expect_eq (u.host (), t.host, "host for '%s'", t.src);
tap.expect_eq (u.user (), t.user, "user for '%s'", t.src);
tap.expect_eq (u.port (), t.port, "port for '%s'", t.src);
tap.expect_eq (u.path (), t.path, "path for '%s'", t.src);
tap.expect_eq (u.query (), t.query, "query for '%s'", t.src);
tap.expect_eq (u.fragment (), t.fragment, "fragment for '%s'", t.src);
}
static const char* BAD[] = {

View File

@ -1,6 +1,10 @@
#include "tap.hpp"
#include "view.hpp"
#include <algorithm>
///////////////////////////////////////////////////////////////////////////////
int
main (int, char**)
{
@ -9,10 +13,35 @@ main (int, char**)
const std::string s = "this is a test string";
const std::string t = "not the same string";
tap.expect (equal (s, util::make_view(s)), "string/view equality");
tap.expect (!equal (s, util::make_view(t)), "string/view inequality");
tap.expect (equal (s.data (), util::make_view (s)), "c-str/view equality");
tap.expect (equal (s, util::view<const char*> (&*s.cbegin (), &*s.cend ())), "string/pointer-view equality");
tap.expect_eq (s, util::make_view(s), "string/view equality");
tap.expect_neq (s, util::make_view(t), "string/view inequality");
tap.expect_eq (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");
{
// comparator tests
static const struct {
util::view<const char*> a;
util::view<const char*> b;
bool less;
} TESTS[] = {
{ "b", "b", false },
{ "b", "a", false },
{ "b", "c", true },
{ "foo", "bar", false },
{ "bar", "foo", true },
{ "banana", "banana", false },
{ "banana", "banan", false },
{ "banan", "banana", true },
};
tap.expect (std::all_of (std::begin (TESTS), std::end (TESTS), [] (const auto &x) {
return (x.a < x.b) == x.less;
}), "comparator less-than");
};
return tap.status ();
}

View File

@ -20,32 +20,6 @@
#include <iterator>
///////////////////////////////////////////////////////////////////////////////
#define EQUALITY(A,B) \
bool \
util::equal (A a, B b) \
{ \
return a.size () == b.size () && \
std::equal (a.cbegin (), \
a.cend (), \
b.cbegin ()); \
}
//-----------------------------------------------------------------------------
EQUALITY(const std::string&, view<const char*>)
EQUALITY(const std::string&, view<char*>)
EQUALITY(const std::string&, view<std::string::const_iterator>)
EQUALITY(const std::string&, view<std::string::iterator>)
EQUALITY(view<const char*>, const std::string&)
EQUALITY(view<char*>, const std::string&)
EQUALITY(view<std::string::const_iterator>, const std::string&)
EQUALITY(view<std::string::iterator>, const std::string&)
#undef EQUALITY
///////////////////////////////////////////////////////////////////////////////
namespace util {
#define OSTREAM(A) \

View File

@ -46,6 +46,12 @@ namespace util {
{ ; }
//---------------------------------------------------------------------
template <std::size_t N, typename ValueT>
view (const ValueT(&value)[N]):
view (std::begin (value), std::end (value))
{ ; }
//---------------------------------------------------------------------
constexpr
view (const view &rhs) noexcept:
@ -291,55 +297,50 @@ namespace util {
///////////////////////////////////////////////////////////////////////////
template <typename IteratorT>
template <typename IteratorA, typename IteratorB>
constexpr bool
operator== (const view<IteratorT> &a, const view<IteratorT> &b)
operator== (const view<IteratorA> &a, const view<IteratorB> &b)
{
return std::cbegin (a) == std::cbegin (b) &&
std::cend (a) == std::cend (b);
return a.size () == b.size () &&
std::equal (std::begin (a), std::end (a), std::begin (b));
}
//-------------------------------------------------------------------------
template <typename IteratorT>
// defer equality to the view/view operator by way of make_view
template <typename IteratorT, typename ValueT>
constexpr bool
operator!= (const view<IteratorT> &a, const view<IteratorT> &b)
{
return !(a == b);
}
//-------------------------------------------------------------------------
template <typename IteratorT, typename ContainerT>
constexpr bool
operator== (const ContainerT &a, const view<IteratorT> &b)
{
return make_view (a) == b;
}
//-------------------------------------------------------------------------
template <typename IteratorT, typename ContainerT>
constexpr bool
operator!= (const ContainerT &b, const view<IteratorT> &a)
{
return !(a == b);
}
//-------------------------------------------------------------------------
template <typename IteratorT, typename ContainerT>
constexpr bool
operator== (const view<IteratorT> &a, const ContainerT &b)
operator== (const view<IteratorT> &a, const ValueT &b)
{
return a == make_view (b);
}
//-------------------------------------------------------------------------
template <typename IteratorT, typename ContainerT>
// reverse the arguments and forward to the above operator. we formumlate
// equality this way to avoid implementing the operator twice for each
// weird case.
template <typename IteratorT, typename ValueT>
constexpr bool
operator!= (const view<IteratorT> &a, const ContainerT &b)
operator== (const ValueT &a, const view<IteratorT> &b)
{
return b == a;
}
///////////////////////////////////////////////////////////////////////////
template <typename IteratorT, typename ValueT>
constexpr bool
operator!= (const util::view<IteratorT> &a, const ValueT &b)
{
return !(a == b);
}
//-------------------------------------------------------------------------
template <typename IteratorT, typename ValueT>
constexpr bool
operator!= (const ValueT &a, const util::view<IteratorT> &b)
{
return !(a == b);
}
@ -363,24 +364,10 @@ namespace util {
util::min (la, lb)
);
return res < 0 || la < lb;
return res < 0 || (res == 0 && la < lb);
}
//-------------------------------------------------------------------------
// 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&