From 36f9607c63091517c4ef08eb081c697e454a35aa Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 1 Oct 2017 14:36:30 +0800 Subject: [PATCH] mempool: add operator== for std::vector/mempool::vector comparison Signed-off-by: Kefu Chai --- src/include/mempool.h | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/include/mempool.h b/src/include/mempool.h index 2cd61ad2afc34..e4c8c32b68b06 100644 --- a/src/include/mempool.h +++ b/src/include/mempool.h @@ -431,7 +431,40 @@ DEFINE_MEMORY_POOLS_HELPER(P) }; - +// the elements allocated by mempool is in the same memory space as the ones +// allocated by the default allocator. so compare them in an efficient way: +// libstdc++'s std::equal is specialized to use memcmp if T is integer or +// pointer. this is good enough for our usecase. use +// std::is_trivially_copyable to expand the support to more types if +// nececssary. +template +bool operator==(const std::vector>& lhs, + const std::vector>& rhs) +{ + return (lhs.size() == rhs.size() && + std::equal(lhs.begin(), lhs.end(), rhs.begin())); +} + +template +bool operator!=(const std::vector>& lhs, + const std::vector>& rhs) +{ + return !(lhs == rhs); +} + +template +bool operator==(const std::vector>& lhs, + const std::vector>& rhs) +{ + return rhs == lhs; +} + +template +bool operator!=(const std::vector>& lhs, + const std::vector>& rhs) +{ + return !(lhs == rhs); +} // Use this for any type that is contained by a container (unless it // is a class you defined; see below). -- 2.39.5