Commit 1c9102da authored by Mark Olesen's avatar Mark Olesen
Browse files

HashSet gets additional operators

 - operator+=  : add in the listed keys
 - operator-=  : remove the listed keys
 - operator&=  : intersection of keys
 - added xfer constructor (just in case)
 - moved labelHashSet typedef to HashSet.H, for consistency with the
   wordHashSet typedef being there and since it is used so often
parent 4e700e62
......@@ -26,7 +26,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "labelHashSet.H"
#include "HashSet.H"
using namespace Foam;
......@@ -35,19 +35,45 @@ using namespace Foam;
int main(int argc, char *argv[])
{
HashSet<Foam::string> testSet(0);
HashSet<string> setA(0);
testSet.insert("kjhk");
testSet.insert("kjhk2");
setA.insert("kjhk");
setA.insert("kjhk2");
Info<< testSet << endl;
Info<< setA << endl;
labelHashSet testLabelHashSet(1);
labelHashSet setB(1);
setB.insert(11);
setB.insert(42);
testLabelHashSet.insert(11);
testLabelHashSet.insert(42);
Info<< "setB : " << setB << endl;
Info<< testLabelHashSet << endl;
labelHashSet setC(1);
setC.insert(2008);
setC.insert(1984);
Info<< "setC : " << setC << endl;
labelHashSet setD(1);
setD.insert(11);
setD.insert(100);
setD.insert(2008);
Info<< "setD : " << setD << endl;
Info<< "setB == setC: " << (setB == setC) << endl;
Info<< "setC != setD: " << (setC != setD) << endl;
// test operations
setB += setC;
Info<< "setB += setC : " << setB << endl;
setB &= setD;
Info<< "setB &= setD : " << setB << endl;
setB += setC;
setB -= setD;
Info<< "setB += setC -= setD : " << setB << endl;
return 0;
}
......
......@@ -45,7 +45,7 @@ SourceFiles
#ifndef regionSide_H
#define regionSide_H
#include "labelHashSet.H"
#include "HashSet.H"
#include "typeInfo.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -29,62 +29,81 @@ License
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class Key, class Hash>
bool HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& ht) const
bool Foam::HashSet<Key, Hash>::operator==(const HashSet<Key, Hash>& rhs) const
{
const HashTable<empty, Key, Hash>& a = *this;
// Are all my elements in ht?
for
(
typename HashTable<empty, Key, Hash>::const_iterator iter = a.begin();
iter != a.end();
++iter
)
// Are all lhs elements in rhs?
for (const_iterator iter = this->begin(); iter != this->end(); ++iter)
{
if (!ht.found(iter.key()))
if (!rhs.found(iter.key()))
{
return false;
}
}
// Are all ht elements in me?
for
(
typename HashTable<empty, Key, Hash>::const_iterator iter = ht.begin();
iter != ht.end();
++iter
)
// Are all rhs elements in lhs?
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
if (!found(iter.key()))
{
return false;
}
}
return true;
}
template<class Key, class Hash>
bool HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& ht) const
bool Foam::HashSet<Key, Hash>::operator!=(const HashSet<Key, Hash>& rhs) const
{
return !(operator==(ht));
return !(operator==(rhs));
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
template<class Key, class Hash>
void Foam::HashSet<Key, Hash>::operator+=(const HashSet<Key, Hash>& rhs)
{
// Add in rhs elements into lhs
for (const_iterator iter = rhs.begin(); iter != rhs.end(); ++iter)
{
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());
}
}
template<class Key, class Hash>
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)
{
if (!rhs.found(iter.key()))
{
erase(iter);
}
}
}
} // End namespace Foam
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
......@@ -26,7 +26,7 @@ Class
Foam::HashSet
Description
A HashTable with word keys but without contents.
A HashTable with keys but without contents.
Typedef
Foam::wordHashSet
......@@ -34,6 +34,12 @@ Typedef
Description
A HashSet with (the default) word keys.
Typedef
Foam::labelHashSet
Description
A HashSet with label keys.
\*---------------------------------------------------------------------------*/
#ifndef HashSet_H
......@@ -48,7 +54,7 @@ namespace Foam
{
/*---------------------------------------------------------------------------*\
Class HashSet Declaration
Class HashSet Declaration
\*---------------------------------------------------------------------------*/
template<class Key=word, class Hash=string::hash>
......@@ -59,9 +65,13 @@ class HashSet
public:
typedef typename HashTable<empty, Key, Hash>::iterator iterator;
typedef typename HashTable<empty, Key, Hash>::const_iterator const_iterator;
// Constructors
//- Construct given initial map size
//- Construct given initial size
HashSet(label size = 100)
:
HashTable<empty, Key, Hash>(size)
......@@ -74,13 +84,13 @@ public:
{}
//- Construct from UList of Key
HashSet(const UList<Key>& ul)
HashSet(const UList<Key>& lst)
:
HashTable<empty, Key, Hash>(2*ul.size())
HashTable<empty, Key, Hash>(2*lst.size())
{
forAll (ul, i)
forAll(lst, i)
{
insert(ul[i]);
insert(lst[i]);
}
}
......@@ -90,32 +100,58 @@ public:
HashTable<empty, Key, Hash>(hs)
{}
//- Construct by transferring the parameter contents
HashSet(const xfer<HashSet<Key, Hash> >& hs)
:
HashTable<empty, Key, Hash>(hs)
{}
// Member Functions
// Edit
//- Insert a new hashedEntry
//- 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());
}
// Member Operators
//- Equality. Two hashtables are equal if all contents of first are
// also in second and vice versa. So does not depend on table size or
// order!
//- Equality. Two hashtables are equal when their contents are equal.
// Independent of table size or order.
bool operator==(const HashSet<Key, Hash>&) const;
//- The opposite of the equality operation.
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>&);
//- Only retain entries found in both HashSets
void operator&=(const HashSet<Key, Hash>&);
};
//- A HashSet with word keys.
typedef HashSet<> wordHashSet;
//- A HashSet with label keys.
typedef HashSet<label, Hash<label> > labelHashSet;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -54,9 +54,15 @@ class Map
{
public:
typedef typename HashTable<T, label, Hash<label> >::iterator iterator;
typedef typename HashTable<T, label, Hash<label> >::const_iterator
const_iterator;
// Constructors
//- Construct given initial map size
//- Construct given initial size
Map(label size = 100)
:
HashTable<T, label, Hash<label> >(size)
......@@ -74,6 +80,12 @@ public:
HashTable<T, label, Hash<label> >(map)
{}
//- Construct by transferring the parameter contents
Map(const xfer<Map<T> >& map)
:
HashTable<T, label, Hash<label> >(map)
{}
//- Return a null Map
static const Map<T>& null()
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2008 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM; if not, write to the Free Software Foundation,
Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Typedef
Foam::labelHashSet
Description
A HashSet with label keys.
See Also
wordHashSet
\*---------------------------------------------------------------------------*/
#ifndef labelHashSet_H
#define labelHashSet_H
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef HashSet<label, Hash<label> > labelHashSet;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
......@@ -39,7 +39,7 @@ SourceFiles
#define faceMapper_H
#include "morphFieldMapper.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -143,7 +143,7 @@ SourceFiles
#include "labelList.H"
#include "objectMap.H"
#include "pointField.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -39,7 +39,7 @@ SourceFiles
#include "List.H"
#include "IndirectList.H"
#include "regIOobject.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "pointFieldsFwd.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -57,7 +57,7 @@ SourceFiles
#include "edgeList.H"
#include "point.H"
#include "intersection.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -27,7 +27,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "PrimitivePatch.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -64,7 +64,7 @@ SourceFiles
#include "cellShapeList.H"
#include "labelList.H"
#include "boolList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
#include "EdgeMap.H"
......
......@@ -31,7 +31,7 @@ License
#include "meshTools.H"
#include "SortableList.H"
#include "triSurfaceTools.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "ListOps.H"
#include "transform.H"
......
......@@ -75,7 +75,7 @@ SourceFiles
#define motionSmoother_H
#include "pointFields.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "PackedList.H"
#include "indirectPrimitivePatch.H"
#include "className.H"
......
......@@ -40,7 +40,7 @@ SourceFiles
#define polyMeshGeometry_H
#include "pointFields.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......
......@@ -56,7 +56,7 @@ SourceFiles
#include "labelList.H"
#include "point.H"
#include "Map.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "typeInfo.H"
#include "edgeList.H"
......
......@@ -38,7 +38,7 @@ SourceFiles
#include "labelIOList.H"
#include "face.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "DynamicList.H"
#include "primitivePatch.H"
#include "removeFaces.H"
......
......@@ -46,7 +46,7 @@ SourceFiles
#include "typeInfo.H"
#include "Map.H"
#include "labelList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "faceList.H"
#include "boolList.H"
......
......@@ -73,7 +73,7 @@ SourceFiles
#include "PtrList.H"
#include "cellList.H"
#include "Map.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "mapPolyMesh.H"
#include "CompactListList.H"
#include "PackedList.H"
......
......@@ -40,7 +40,7 @@ SourceFiles
#define removeFaces_H
#include "Pstream.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
#include "boolList.H"
#include "indirectPrimitivePatch.H"
......
Markdown is supported
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