From c5da9a74522967446d796dce5cf9d30ff7b8cec3 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 18 Aug 2018 11:07:42 +0200 Subject: Add comparison operators --- chilbert/BigBitVec.hpp | 15 +++++++++++++++ chilbert/FixBitVec.hpp | 2 ++ test/test_bitvec.cpp | 20 ++++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/chilbert/BigBitVec.hpp b/chilbert/BigBitVec.hpp index 30b8f82..dcc2c99 100644 --- a/chilbert/BigBitVec.hpp +++ b/chilbert/BigBitVec.hpp @@ -108,6 +108,21 @@ public: bool operator!=(const CBigBitVec& vec) const { return !(*this == vec); } + bool operator<(const CBigBitVec& vec) const + { + assert(size() == vec.size()); + + for (size_t ri = 0; ri < m_iRacks; ++ri) { + size_t i = m_iRacks - ri - 1; + if (m_pcRacks[i] < vec.m_pcRacks[i]) { + return true; + } else if (m_pcRacks[i] > vec.m_pcRacks[i]) { + return false; + } + } + return false; + } + /// Non-resizing assignment CBigBitVec& operator=(const CBigBitVec& vec) { diff --git a/chilbert/FixBitVec.hpp b/chilbert/FixBitVec.hpp index 05dae1f..d098956 100644 --- a/chilbert/FixBitVec.hpp +++ b/chilbert/FixBitVec.hpp @@ -115,6 +115,8 @@ public: return m_rack != vec.m_rack; } + bool operator<(const CFixBitVec& vec) const { return m_rack < vec.m_rack; } + CFixBitVec& operator=(const FBV_UINT i) { m_rack = i; diff --git a/test/test_bitvec.cpp b/test/test_bitvec.cpp index 61685d1..8305675 100644 --- a/test/test_bitvec.cpp +++ b/test/test_bitvec.cpp @@ -246,6 +246,25 @@ test_gray_code() } } +template +void +test_comparison() +{ + T a = make_zero_bitvec(); + T b = make_zero_bitvec(); + + for (size_t bit = 1; bit < N; ++bit) { + setBit(a, bit, 1); + + for (size_t i = 0; i < bit; ++i) { + setBit(a, i, rand() % 2 == 0); + setBit(b, i, rand() % 2 == 0); + } + + assert(b < a); + } +} + template void test() @@ -264,6 +283,7 @@ test() test_right_shift(); test_find_first(); test_gray_code(); + test_comparison(); } int -- cgit v1.2.1