(Bitset) Added equality operator

Former-commit-id: b5145a1a0ad8031001927c29c49e0f1faae2d7af
This commit is contained in:
Lynix 2015-03-16 18:02:17 +01:00
parent 17961f42fd
commit 458cdd7cab
2 changed files with 39 additions and 0 deletions

View File

@ -99,6 +99,12 @@ class NzBitset
unsigned int m_bitCount;
};
template<typename Block, typename Allocator>
bool operator==(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);
template<typename Block, typename Allocator>
bool operator!=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);
template<typename Block, typename Allocator>
NzBitset<Block, Allocator> operator&(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);

View File

@ -493,6 +493,39 @@ template<typename Block, class Allocator>
unsigned int NzBitset<Block, Allocator>::npos = std::numeric_limits<unsigned int>::max();
template<typename Block, typename Allocator>
bool operator==(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{
// La comparaison part du principe que (uint8) 00001100 == (uint16) 00000000 00001100
// et conserve donc cette propriété
const NzBitset<Block, Allocator>& greater = (lhs.GetBlockCount() > rhs.GetBlockCount()) ? lhs : rhs;
const NzBitset<Block, Allocator>& lesser = (lhs.GetBlockCount() > rhs.GetBlockCount()) ? rhs : lhs;
unsigned int maxBlockCount = greater.GetBlockCount();
unsigned int minBlockCount = lesser.GetBlockCount();
// Nous testons les blocs en commun pour vérifier l'égalité des bits
for (unsigned int i = 0; i < minBlockCount; ++i)
{
if (lhs.GetBlock(i) != rhs.GetBlock(i))
return false;
}
// Nous vérifions maintenant les blocs que seul le plus grand bitset possède, pour prétendre à l'égalité
// ils doivent tous être nuls
for (unsigned int i = minBlockCount; i < maxBlockCount; ++i)
if (greater.GetBlock(i))
return false;
return true;
}
template<typename Block, typename Allocator>
bool operator!=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{
return !(lhs == rhs);
}
template<typename Block, typename Allocator>
NzBitset<Block, Allocator> operator&(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{