further HashSet improvements

- added global operator|, operator& and operator^
 - can construct from table of contents of another HashTable
This commit is contained in:
Mark Olesen
2008-11-19 12:37:37 +01:00
parent cf7762c2b2
commit 110e9989b9
3 changed files with 188 additions and 31 deletions

View File

@ -29,6 +29,26 @@ License
#include "HashSet.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class Key, class Hash>
template<class T>
Foam::HashSet<Key, Hash>::HashSet(const HashTable<T, Key, Hash>& ht)
:
HashTable<empty, Key, Hash>(ht.size())
{
for
(
typename HashTable<T, Key, Hash>::const_iterator iter = ht.begin();
iter != ht.end();
++iter
)
{
insert(iter.key());
}
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Key, class Hash>
@ -64,9 +84,9 @@ bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator+=(const HashSet<Key, Hash>& rhs)
void Foam::HashSet<Key, Hash>::operator|=(const HashSet<Key, Hash>& rhs)
{
// Add in rhs elements into lhs
// Add rhs elements into lhs
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
insert(iter.key());
@ -74,6 +94,38 @@ void Foam::HashSet<Key, Hash>::operator+=(const HashSet<Key, Hash>& rhs)
}
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs)
{
// Remove elements not also found in rhs
for (iterator iter = this->begin(); iter != this->end(); ++iter)
{
if (!rhs.found(iter.key()))
{
erase(iter);
}
}
}
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator^=(const HashSet<Key, Hash>& rhs)
{
// Add missed rhs elements, remove duplicate elements
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
if (found(iter.key()))
{
erase(iter.key());
}
else
{
insert(iter.key());
}
}
}
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
{
@ -85,23 +137,51 @@ void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
/* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs)
Foam::HashSet<Key, Hash>
Foam::operator|
(
const HashSet<Key, Hash>& hash1,
const HashSet<Key, Hash>& hash2
)
{
// Remove elements not found in rhs as well
for (iterator iter = this->begin(); iter != this->end(); ++iter)
{
if (!rhs.found(iter.key()))
{
erase(iter);
}
}
HashSet<Key, Hash> out(hash1);
out |= hash2;
return out;
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
template<class Key, class Hash>
Foam::HashSet<Key, Hash>
Foam::operator&
(
const HashSet<Key, Hash>& hash1,
const HashSet<Key, Hash>& hash2
)
{
HashSet<Key, Hash> out(hash1);
out &= hash2;
return out;
}
template<class Key, class Hash>
Foam::HashSet<Key, Hash>
Foam::operator^
(
const HashSet<Key, Hash>& hash1,
const HashSet<Key, Hash>& hash2
)
{
HashSet<Key, Hash> out(hash1);
out ^= hash2;
return out;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif