Commit 110e9989 authored by Mark Olesen's avatar Mark Olesen
Browse files

further HashSet improvements

 - added global operator|, operator& and operator^
 - can construct from table of contents of another HashTable
parent cf7762c2
......@@ -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());
......@@ -75,32 +95,92 @@ 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)
void Foam::HashSet<Key, Hash>::operator&=(const HashSet<Key, Hash>& rhs)
{
// Remove rhs elements from lhs
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
// Remove elements not also found in rhs
for (iterator iter = this->begin(); iter != this->end(); ++iter)
{
erase(iter.key());
if (!rhs.found(iter.key()))
{
erase(iter);
}
}
}
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)
{
// Remove elements not found in rhs as well
for (iterator iter = this->begin(); iter != this->end(); ++iter)
// Add missed rhs elements, remove duplicate elements
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
if (!rhs.found(iter.key()))
if (found(iter.key()))
{
erase(iter);
erase(iter.key());
}
else
{
insert(iter.key());
}
}
}
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator-=(const HashSet<Key, Hash>& rhs)
{
// Remove rhs elements from lhs
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
erase(iter.key());
}
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
/* * * * * * * * * * * * * * * * Global 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;
}
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;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -106,22 +106,25 @@ 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
// Edit
//- Insert a new entry
bool insert(const Key& key)
{
return HashTable<empty, Key, Hash>::insert(key, empty());
}
//- Insert a new entry
bool insert(const Key& key)
{
return HashTable<empty, Key, Hash>::insert(key, empty());
}
//- Same as insert (cannot overwrite empty content)
bool set(const Key& key)
{
return HashTable<empty, Key, Hash>::insert(key, empty());
}
//- Same as insert (cannot overwrite empty content)
bool set(const Key& key)
{
return HashTable<empty, Key, Hash>::insert(key, empty());
}
// Member Operators
......@@ -134,18 +137,56 @@ public:
bool operator!=(const HashSet<Key, Hash>&) const;
//- 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
void operator+=(const HashSet<Key, Hash>&);
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>&);
//- Only retain entries found in both HashSets
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;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment