From 12957038f798646effc6cec6a2339401f7fc0eb3 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 17 Apr 2018 13:42:30 -0700 Subject: [PATCH] ircd::db: Create template for integer comparators. --- include/ircd/db/comparator.h | 54 ++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/include/ircd/db/comparator.h b/include/ircd/db/comparator.h index f449a40eb..a39fa04dd 100644 --- a/include/ircd/db/comparator.h +++ b/include/ircd/db/comparator.h @@ -15,9 +15,15 @@ namespace ircd::db { struct comparator; + template struct cmp_integer; + template struct reverse_cmp_integer; + struct cmp_int64_t; struct reverse_cmp_int64_t; + struct cmp_uint64_t; + struct reverse_cmp_uint64_t; + struct cmp_string_view; struct reverse_cmp_string_view; } @@ -74,42 +80,66 @@ struct ircd::db::reverse_cmp_string_view {} }; -struct ircd::db::cmp_int64_t +template +struct ircd::db::cmp_integer :db::comparator { static bool less(const string_view &sa, const string_view &sb) { - assert(sa.size() == sizeof(int64_t) && sb.size() == sizeof(int64_t)); - const byte_view a{sa}, b{sb}; + const byte_view a{sa}, b{sb}; return a < b; } static bool equal(const string_view &sa, const string_view &sb) { - assert(sa.size() == sizeof(int64_t) && sb.size() == sizeof(int64_t)); - const byte_view a{sa}, b{sb}; + const byte_view a{sa}, b{sb}; return a == b; } - cmp_int64_t() - :db::comparator{"int64_t", &less, &equal} + cmp_integer() + :db::comparator{"integer", &less, &equal} {} }; -struct ircd::db::reverse_cmp_int64_t +template +struct ircd::db::reverse_cmp_integer :db::comparator { static bool less(const string_view &a, const string_view &b) { - return !cmp_int64_t::less(a, b); + return !cmp_integer::less(a, b); } static bool equal(const string_view &a, const string_view &b) { - return cmp_int64_t::equal(a, b); + return cmp_integer::equal(a, b); } - reverse_cmp_int64_t() - :db::comparator{"reverse_int64_t", &less, &equal} + reverse_cmp_integer() + :db::comparator{"reverse_integer", &less, &equal} {} }; + +struct ircd::db::cmp_int64_t +:cmp_integer +{ + using cmp_integer::cmp_integer; +}; + +struct ircd::db::reverse_cmp_int64_t +:reverse_cmp_integer +{ + using reverse_cmp_integer::reverse_cmp_integer; +}; + +struct ircd::db::cmp_uint64_t +:cmp_integer +{ + using cmp_integer::cmp_integer; +}; + +struct ircd::db::reverse_cmp_uint64_t +:reverse_cmp_integer +{ + using reverse_cmp_integer::reverse_cmp_integer; +};