Core/Bitset: Add comparison operators

Former-commit-id: 7ca04f36fa001f322182c5e9457127f05432d373
This commit is contained in:
Lynix 2015-05-19 14:31:04 +02:00
parent f82446dda3
commit 656290d089
3 changed files with 59 additions and 2 deletions

View File

@ -42,8 +42,8 @@ return {
{
Title = "Stockage et gestion de champs de bits dynamiques (Bitset)",
Description = "Classe permettant le stockage et la manipulation optimisée d'un nombre élevé et dynamique de bits (=booléens, opérations de comptage, recherche de bits activés, opérations entre champs de bits, etc.)",
Note = "Il manque des opérateurs de comparaison et une spécialisation de la fonction de hachage",
Progress = 90
Note = "Il manque une spécialisation de la fonction de hachage",
Progress = 95
}
}
},

View File

@ -110,6 +110,18 @@ bool operator==(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, All
template<typename Block, class Allocator>
bool operator!=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);
template<typename Block, class Allocator>
bool operator<(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);
template<typename Block, class Allocator>
bool operator<=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);
template<typename Block, class Allocator>
bool operator>(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);
template<typename Block, class Allocator>
bool operator>=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);
template<typename Block, class Allocator>
NzBitset<Block, Allocator> operator&(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs);

View File

@ -555,6 +555,51 @@ bool operator!=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, All
return !(lhs == rhs);
}
template<typename Block, class Allocator>
bool operator<(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{
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();
// If the greatest bitset has a single bit active in a block outside the lesser bitset range, then it is greater
for (unsigned int i = maxBlockCount; i > minBlockCount; ++i)
{
if (greater.GetBlock(i))
return lhs.GetBlockCount() < rhs.GetBlockCount();
}
// Compare the common blocks
for (unsigned int i = 0; i < minBlockCount; ++i)
{
unsigned int index = (minBlockCount - i - 1); // Compare from the most significant block to the less significant block
if (lhs.GetBlock(index) < rhs.GetBlock(index))
return true;
}
return false; // They are equal
}
template<typename Block, class Allocator>
bool operator<=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{
return lhs < rhs || lhs == rhs;
}
template<typename Block, class Allocator>
bool operator>(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{
return rhs < lhs;
}
template<typename Block, class Allocator>
bool operator>=(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{
return rhs <= lhs;
}
template<typename Block, class Allocator>
NzBitset<Block, Allocator> operator&(const NzBitset<Block, Allocator>& lhs, const NzBitset<Block, Allocator>& rhs)
{