further HashSet improvements
- added global operator|, operator& and operator^ - can construct from table of contents of another HashTable
This commit is contained in:
parent
cf7762c2b2
commit
110e9989b9
@ -27,6 +27,7 @@ Description
|
||||
\*---------------------------------------------------------------------------*/
|
||||
|
||||
#include "HashSet.H"
|
||||
#include "Map.H"
|
||||
|
||||
using namespace Foam;
|
||||
|
||||
@ -35,12 +36,46 @@ using namespace Foam;
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
HashSet<string> setA(0);
|
||||
wordHashSet setA(0);
|
||||
HashTable<label, word> tableA;
|
||||
|
||||
HashTable<empty> tableB;
|
||||
Map<label> mapA;
|
||||
|
||||
setA.insert("kjhk");
|
||||
setA.insert("kjhk2");
|
||||
|
||||
tableA.insert("value1", 1);
|
||||
tableA.insert("value2", 2);
|
||||
tableA.insert("value3", 3);
|
||||
|
||||
tableB.insert("value4", empty());
|
||||
tableB.insert("value5", empty());
|
||||
tableB.insert("value6", empty());
|
||||
|
||||
mapA.set(1, 1);
|
||||
mapA.set(2, 2);
|
||||
mapA.set(3, 3);
|
||||
mapA.set(4, 4);
|
||||
|
||||
Info<< setA << endl;
|
||||
Info<< tableA << endl;
|
||||
Info<< mapA << endl;
|
||||
|
||||
Info<< "create from HashSet: ";
|
||||
Info<< wordHashSet(setA) << endl;
|
||||
Info<< "create from HashTable<T>: ";
|
||||
Info<< wordHashSet(tableA) << endl;
|
||||
Info<< "create from HashTable<empty>: ";
|
||||
Info<< wordHashSet(tableB) << endl;
|
||||
|
||||
Info<< "create from Map<label>: ";
|
||||
Info<< labelHashSet(mapA) << endl;
|
||||
|
||||
Info<<"combined toc: "
|
||||
<< (wordHashSet(setA) | wordHashSet(tableA) | wordHashSet(tableB))
|
||||
<< nl;
|
||||
|
||||
|
||||
labelHashSet setB(1);
|
||||
setB.insert(11);
|
||||
@ -71,9 +106,10 @@ int main(int argc, char *argv[])
|
||||
setB &= setD;
|
||||
Info<< "setB &= setD : " << setB << endl;
|
||||
|
||||
setB += setC;
|
||||
setB -= setD;
|
||||
Info<< "setB += setC -= setD : " << setB << endl;
|
||||
Info<< "setB : " << setB << endl;
|
||||
Info<< "setC : " << setC << endl;
|
||||
Info<< "setD : " << setD << endl;
|
||||
Info<< "setB ^ setC ^ setD : " << (setB ^ setC ^ setD) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -106,6 +106,9 @@ public:
|
||||
HashTable<empty, Key, Hash>(hs)
|
||||
{}
|
||||
|
||||
//- Construct from table of contents of the HashTable
|
||||
template<class T>
|
||||
HashSet(const HashTable<T, Key, Hash>& ht);
|
||||
|
||||
// Member Functions
|
||||
|
||||
@ -134,18 +137,56 @@ public:
|
||||
bool operator!=(const HashSet<Key, Hash>&) const;
|
||||
|
||||
|
||||
//- Add entries listed in the given HashSet to this HashSet
|
||||
void operator+=(const HashSet<Key, Hash>&);
|
||||
|
||||
//- Remove entries listed in the given HashSet from this HashSet
|
||||
void operator-=(const HashSet<Key, Hash>&);
|
||||
//- Combine entries from HashSets
|
||||
void operator|=(const HashSet<Key, Hash>&);
|
||||
|
||||
//- Only retain entries found in both HashSets
|
||||
void operator&=(const HashSet<Key, Hash>&);
|
||||
|
||||
//- Only retain unique entries (xor)
|
||||
void operator^=(const HashSet<Key, Hash>&);
|
||||
|
||||
//- Add entries listed in the given HashSet to this HashSet
|
||||
inline void operator+=(const HashSet<Key, Hash>& rhs)
|
||||
{
|
||||
this->operator|=(rhs);
|
||||
}
|
||||
|
||||
//- Remove entries listed in the given HashSet from this HashSet
|
||||
void operator-=(const HashSet<Key, Hash>&);
|
||||
|
||||
};
|
||||
|
||||
|
||||
// Global Operators
|
||||
|
||||
//- Combine entries from HashSets
|
||||
template<class Key, class Hash>
|
||||
HashSet<Key,Hash> operator|
|
||||
(
|
||||
const HashSet<Key,Hash>& hash1,
|
||||
const HashSet<Key,Hash>& hash2
|
||||
);
|
||||
|
||||
|
||||
//- Create a HashSet that only contains entries found in both HashSets
|
||||
template<class Key, class Hash>
|
||||
HashSet<Key,Hash> operator&
|
||||
(
|
||||
const HashSet<Key,Hash>& hash1,
|
||||
const HashSet<Key,Hash>& hash2
|
||||
);
|
||||
|
||||
|
||||
//- Create a HashSet that only contains unique entries (xor)
|
||||
template<class Key, class Hash>
|
||||
HashSet<Key,Hash> operator^
|
||||
(
|
||||
const HashSet<Key,Hash>& hash1,
|
||||
const HashSet<Key,Hash>& hash2
|
||||
);
|
||||
|
||||
|
||||
//- A HashSet with word keys.
|
||||
typedef HashSet<> wordHashSet;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user