From 9e669c8837be1d3b6fa24e1d49d31a5a04abe6ea Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Wed, 14 Dec 2016 11:03:48 -0500 Subject: [PATCH] Move rest of CAmountMap stuff --- src/amount.cpp | 104 ++++++++++++++++++++++++++++++++++++++++++ src/amount.h | 29 ++++++++++++ src/wallet/wallet.cpp | 104 ------------------------------------------ src/wallet/wallet.h | 28 ------------ 4 files changed, 133 insertions(+), 132 deletions(-) diff --git a/src/amount.cpp b/src/amount.cpp index 9d4a22893f..e48365dd70 100644 --- a/src/amount.cpp +++ b/src/amount.cpp @@ -75,3 +75,107 @@ CAmountMap operator-(const CAmountMap& a, const CAmountMap& b) c[it->first] -= it->second; return c; } + +bool operator<(const CAmountMap& a, const CAmountMap& b) +{ + bool smallerElement = false; + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { + CAmount aValue = a.count(it->first) ? a.find(it->first)->second : 0; + if (aValue > it->second) + return false; + if (aValue < it->second) + smallerElement = true; + } + for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { + CAmount bValue = b.count(it->first) ? b.find(it->first)->second : 0; + if (it->second > bValue) + return false; + if (it->second < bValue) + smallerElement = true; + } + return smallerElement; +} + +bool operator<=(const CAmountMap& a, const CAmountMap& b) +{ + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { + CAmount aValue = a.count(it->first) ? a.find(it->first)->second : 0; + if (aValue > it->second) + return false; + } + for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { + CAmount bValue = b.count(it->first) ? b.find(it->first)->second : 0; + if (it->second > bValue) + return false; + } + return true; +} + +bool operator>(const CAmountMap& a, const CAmountMap& b) +{ + bool largerElement = false; + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { + CAmount aValue = a.count(it->first) ? a.find(it->first)->second : 0; + if (aValue < it->second) + return false; + if (aValue > it->second) + largerElement = true; + } + for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { + CAmount bValue = b.count(it->first) ? b.find(it->first)->second : 0; + if (it->second < bValue) + return false; + if (it->second > bValue) + largerElement = true; + } + return largerElement; +} + +bool operator>=(const CAmountMap& a, const CAmountMap& b) +{ + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { + if ((a.count(it->first) ? a.find(it->first)->second : 0) < it->second) + return false; + } + for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { + if (it->second < (b.count(it->first) ? b.find(it->first)->second : 0)) + return false; + } + return true; +} + +bool operator==(const CAmountMap& a, const CAmountMap& b) +{ + for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { + if ((b.count(it->first) ? b.find(it->first)->second : 0) != it->second) + return false; + } + for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { + if ((a.count(it->first) ? a.find(it->first)->second : 0) != it->second) + return false; + } + return true; +} + +bool operator!=(const CAmountMap& a, const CAmountMap& b) +{ + return !(a == b); +} + +bool hasNegativeValue(const CAmountMap& amount) +{ + for(std::map::const_iterator it = amount.begin(); it != amount.end(); ++it) { + if (it->second < 0) + return true; + } + return false; +} + +bool hasNonPostiveValue(const CAmountMap& amount) +{ + for(std::map::const_iterator it = amount.begin(); it != amount.end(); ++it) { + if (it->second <= 0) + return true; + } + return false; +} diff --git a/src/amount.h b/src/amount.h index 2aea97c8cd..8d9b708bfd 100644 --- a/src/amount.h +++ b/src/amount.h @@ -41,6 +41,35 @@ CAmountMap& operator-=(CAmountMap& a, const CAmountMap& b); CAmountMap operator+(const CAmountMap& a, const CAmountMap& b); CAmountMap operator-(const CAmountMap& a, const CAmountMap& b); +// WARNING: Comparisons are only looking for *complete* ordering. +// For strict inequality checks, if any entry would fail the non-strict +// inequality, the comparison will fail. Therefore it is possible +// that all inequality comparison checks may fail. +// Therefore if >/< fails against a CAmountMap(), this means there +// are all zeroes or one or more negative values. +// +// Examples: 1A + 2B <= 1A + 2B + 1C +// and 1A + 2B < 1A + 2B + 1C +// but +// !(1A + 2B == 1A + 2B + 1C) +//------------------------------------- +// 1A + 2B == 1A + 2B +// and 1A + 2B <= 1A + 2B +// but +// !(1A + 2B < 1A + 2B) +//------------------------------------- +// !(1A + 2B == 2B - 1C) +// !(1A + 2B >= 2B - 1C) +// ... +// !(1A + 2B < 2B - 1C) +// and 1A + 2B != 2B - 1C +bool operator<(const CAmountMap& a, const CAmountMap& b); +bool operator<=(const CAmountMap& a, const CAmountMap& b); +bool operator>(const CAmountMap& a, const CAmountMap& b); +bool operator>=(const CAmountMap& a, const CAmountMap& b); +bool operator==(const CAmountMap& a, const CAmountMap& b); +bool operator!=(const CAmountMap& a, const CAmountMap& b); + /** No amount larger than this (in satoshi) is valid. * * Note that this constant is *not* the total money supply, which in Bitcoin diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index f15154f1a8..ff66176c1f 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4428,107 +4428,3 @@ void CWalletTx::WipeUnknownBlindingData() const } } } - -bool operator<(const CAmountMap& a, const CAmountMap& b) -{ - bool smallerElement = false; - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { - CAmount aValue = a.count(it->first) ? a.find(it->first)->second : 0; - if (aValue > it->second) - return false; - if (aValue < it->second) - smallerElement = true; - } - for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { - CAmount bValue = b.count(it->first) ? b.find(it->first)->second : 0; - if (it->second > bValue) - return false; - if (it->second < bValue) - smallerElement = true; - } - return smallerElement; -} - -bool operator<=(const CAmountMap& a, const CAmountMap& b) -{ - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { - CAmount aValue = a.count(it->first) ? a.find(it->first)->second : 0; - if (aValue > it->second) - return false; - } - for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { - CAmount bValue = b.count(it->first) ? b.find(it->first)->second : 0; - if (it->second > bValue) - return false; - } - return true; -} - -bool operator>(const CAmountMap& a, const CAmountMap& b) -{ - bool largerElement = false; - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { - CAmount aValue = a.count(it->first) ? a.find(it->first)->second : 0; - if (aValue < it->second) - return false; - if (aValue > it->second) - largerElement = true; - } - for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { - CAmount bValue = b.count(it->first) ? b.find(it->first)->second : 0; - if (it->second < bValue) - return false; - if (it->second > bValue) - largerElement = true; - } - return largerElement; -} - -bool operator>=(const CAmountMap& a, const CAmountMap& b) -{ - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { - if ((a.count(it->first) ? a.find(it->first)->second : 0) < it->second) - return false; - } - for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { - if (it->second < (b.count(it->first) ? b.find(it->first)->second : 0)) - return false; - } - return true; -} - -bool operator==(const CAmountMap& a, const CAmountMap& b) -{ - for(std::map::const_iterator it = a.begin(); it != a.end(); ++it) { - if ((b.count(it->first) ? b.find(it->first)->second : 0) != it->second) - return false; - } - for(std::map::const_iterator it = b.begin(); it != b.end(); ++it) { - if ((a.count(it->first) ? a.find(it->first)->second : 0) != it->second) - return false; - } - return true; -} - -bool operator!=(const CAmountMap& a, const CAmountMap& b) -{ - return !(a == b); -} - -bool hasNegativeValue(const CAmountMap& amount) -{ - for(std::map::const_iterator it = amount.begin(); it != amount.end(); ++it) { - if (it->second < 0) - return true; - } - return false; -} - -bool hasNonPostiveValue(const CAmountMap& amount) -{ - for(std::map::const_iterator it = amount.begin(); it != amount.end(); ++it) { - if (it->second <= 0) - return true; - } - return false; -} diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index ea022af91e..23cd65266c 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -75,34 +75,6 @@ static const bool DEFAULT_USE_HD_WALLET = true; extern const char * DEFAULT_WALLET_DAT; -// WARNING: Comparisons are only looking for *complete* ordering. -// For strict inequality checks, if any entry would fail the non-strict -// inequality, the comparison will fail. Therefore it is possible -// that all inequality comparison checks may fail. -// Therefore if >/< fails against a CAmountMap(), this means there -// are all zeroes or one or more negative values. -// -// Examples: 1A + 2B <= 1A + 2B + 1C -// and 1A + 2B < 1A + 2B + 1C -// but -// !(1A + 2B == 1A + 2B + 1C) -//------------------------------------- -// 1A + 2B == 1A + 2B -// and 1A + 2B <= 1A + 2B -// but -// !(1A + 2B < 1A + 2B) -//------------------------------------- -// !(1A + 2B == 2B - 1C) -// !(1A + 2B >= 2B - 1C) -// ... -// !(1A + 2B < 2B - 1C) -// and 1A + 2B != 2B - 1C -bool operator<(const CAmountMap& a, const CAmountMap& b); -bool operator<=(const CAmountMap& a, const CAmountMap& b); -bool operator>(const CAmountMap& a, const CAmountMap& b); -bool operator>=(const CAmountMap& a, const CAmountMap& b); -bool operator==(const CAmountMap& a, const CAmountMap& b); -bool operator!=(const CAmountMap& a, const CAmountMap& b); bool hasNegativeValue(const CAmountMap& amount); bool hasNonPositiveValue(const CAmountMap& amount);