mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
further HashSet improvements
- added global operator|, operator& and operator^ - can construct from table of contents of another HashTable
This commit is contained in:
@ -27,6 +27,7 @@ Description
|
|||||||
\*---------------------------------------------------------------------------*/
|
\*---------------------------------------------------------------------------*/
|
||||||
|
|
||||||
#include "HashSet.H"
|
#include "HashSet.H"
|
||||||
|
#include "Map.H"
|
||||||
|
|
||||||
using namespace Foam;
|
using namespace Foam;
|
||||||
|
|
||||||
@ -35,12 +36,46 @@ using namespace Foam;
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
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("kjhk");
|
||||||
setA.insert("kjhk2");
|
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<< 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);
|
labelHashSet setB(1);
|
||||||
setB.insert(11);
|
setB.insert(11);
|
||||||
@ -71,9 +106,10 @@ int main(int argc, char *argv[])
|
|||||||
setB &= setD;
|
setB &= setD;
|
||||||
Info<< "setB &= setD : " << setB << endl;
|
Info<< "setB &= setD : " << setB << endl;
|
||||||
|
|
||||||
setB += setC;
|
Info<< "setB : " << setB << endl;
|
||||||
setB -= setD;
|
Info<< "setC : " << setC << endl;
|
||||||
Info<< "setB += setC -= setD : " << setB << endl;
|
Info<< "setD : " << setD << endl;
|
||||||
|
Info<< "setB ^ setC ^ setD : " << (setB ^ setC ^ setD) << endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -29,6 +29,26 @@ License
|
|||||||
|
|
||||||
#include "HashSet.H"
|
#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 * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
template<class Key, class Hash>
|
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>
|
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)
|
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
|
||||||
{
|
{
|
||||||
insert(iter.key());
|
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>
|
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)
|
||||||
{
|
{
|
||||||
@ -85,23 +137,51 @@ void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
/* * * * * * * * * * * * * * * * Global operators * * * * * * * * * * * * * */
|
||||||
|
|
||||||
template<class Key, class Hash>
|
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
|
HashSet<Key, Hash> out(hash1);
|
||||||
for (iterator iter = this->begin(); iter != this->end(); ++iter)
|
out |= hash2;
|
||||||
{
|
return out;
|
||||||
if (!rhs.found(iter.key()))
|
|
||||||
{
|
|
||||||
erase(iter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * 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
|
#endif
|
||||||
|
|||||||
@ -106,22 +106,25 @@ public:
|
|||||||
HashTable<empty, Key, Hash>(hs)
|
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
|
// Member Functions
|
||||||
|
|
||||||
// Edit
|
// Edit
|
||||||
|
|
||||||
//- Insert a new entry
|
//- Insert a new entry
|
||||||
bool insert(const Key& key)
|
bool insert(const Key& key)
|
||||||
{
|
{
|
||||||
return HashTable<empty, Key, Hash>::insert(key, empty());
|
return HashTable<empty, Key, Hash>::insert(key, empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
//- Same as insert (cannot overwrite empty content)
|
//- Same as insert (cannot overwrite empty content)
|
||||||
bool set(const Key& key)
|
bool set(const Key& key)
|
||||||
{
|
{
|
||||||
return HashTable<empty, Key, Hash>::insert(key, empty());
|
return HashTable<empty, Key, Hash>::insert(key, empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Member Operators
|
// Member Operators
|
||||||
@ -134,18 +137,56 @@ public:
|
|||||||
bool operator!=(const HashSet<Key, Hash>&) const;
|
bool operator!=(const HashSet<Key, Hash>&) const;
|
||||||
|
|
||||||
|
|
||||||
//- Add entries listed in the given HashSet to this HashSet
|
//- Combine entries from HashSets
|
||||||
void operator+=(const HashSet<Key, Hash>&);
|
void operator|=(const HashSet<Key, Hash>&);
|
||||||
|
|
||||||
//- Remove entries listed in the given HashSet from this HashSet
|
|
||||||
void operator-=(const HashSet<Key, Hash>&);
|
|
||||||
|
|
||||||
//- Only retain entries found in both HashSets
|
//- Only retain entries found in both HashSets
|
||||||
void operator&=(const HashSet<Key, Hash>&);
|
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.
|
//- A HashSet with word keys.
|
||||||
typedef HashSet<> wordHashSet;
|
typedef HashSet<> wordHashSet;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user