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
This commit is contained in:
Mark Olesen
2008-11-18 23:11:09 +01:00
parent 4e700e62b0
commit 1c9102dada
46 changed files with 190 additions and 154 deletions

View File

@ -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;
}

View File

@ -45,7 +45,7 @@ SourceFiles
#ifndef regionSide_H
#define regionSide_H
#include "labelHashSet.H"
#include "HashSet.H"
#include "typeInfo.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -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);
}
}
}
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * Friend Operators * * * * * * * * * * * * * //
#endif
// ************************************************************************* //

View File

@ -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
@ -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;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -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()

View File

@ -1,54 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / 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
// ************************************************************************* //

View File

@ -39,7 +39,7 @@ SourceFiles
#define faceMapper_H
#include "morphFieldMapper.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -143,7 +143,7 @@ SourceFiles
#include "labelList.H"
#include "objectMap.H"
#include "pointField.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -39,7 +39,7 @@ SourceFiles
#include "List.H"
#include "IndirectList.H"
#include "regIOobject.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "pointFieldsFwd.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -57,7 +57,7 @@ SourceFiles
#include "edgeList.H"
#include "point.H"
#include "intersection.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -27,7 +27,7 @@ Description
\*---------------------------------------------------------------------------*/
#include "PrimitivePatch.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -40,7 +40,7 @@ SourceFiles
#define polyMeshGeometry_H
#include "pointFields.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -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"

View File

@ -37,7 +37,7 @@ SourceFiles
#include "faceStencil.H"
#include "boolList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
#include "EdgeMap.H"

View File

@ -37,7 +37,7 @@ SourceFiles
#include "faceStencil.H"
#include "boolList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -38,7 +38,7 @@ SourceFiles
#include "faceStencil.H"
#include "boolList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
#include "EdgeMap.H"

View File

@ -38,7 +38,7 @@ SourceFiles
#include "globalIndex.H"
#include "boolList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "indirectPrimitivePatch.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -39,7 +39,7 @@ SourceFiles
#include "morphFieldMapper.H"
#include "fvMesh.H"
#include "faceMapper.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "mapPolyMesh.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -60,7 +60,7 @@ SourceFiles
#include "fvPatchFieldMapper.H"
#include "pointPatchFieldMapper.H"
#include "GeometricField.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "surfaceMesh.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -27,7 +27,7 @@ License
#include "inverseDistanceDiffusivity.H"
#include "addToRunTimeSelectionTable.H"
#include "patchWave.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "surfaceInterpolate.H"
#include "zeroGradientFvPatchFields.H"

View File

@ -26,7 +26,7 @@ License
#include "inverseFaceDistanceDiffusivity.H"
#include "addToRunTimeSelectionTable.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "wallPoint.H"
#include "MeshWave.H"

View File

@ -26,7 +26,7 @@ License
#include "inversePointDistanceDiffusivity.H"
#include "addToRunTimeSelectionTable.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "pointEdgePoint.H"
#include "PointEdgeWave.H"

View File

@ -27,7 +27,7 @@ License
#include "inverseVolumeDiffusivity.H"
#include "addToRunTimeSelectionTable.H"
#include "patchWave.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "surfaceInterpolate.H"
#include "zeroGradientFvPatchFields.H"

View File

@ -37,7 +37,7 @@ SourceFiles
#define cellDistFuncs_H
#include "scalarField.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
#include "wordList.H"
#include "scalarField.H"

View File

@ -28,7 +28,7 @@ Description
#include "cellFeatures.H"
#include "primitiveMesh.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
#include "demandDrivenData.H"
#include "ListOps.H"

View File

@ -44,7 +44,7 @@ SourceFiles
#include "faceList.H"
#include "labelList.H"
#include "boolList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Map.H"
#include "DynamicList.H"

View File

@ -40,7 +40,7 @@ SourceFiles
#include "pointIndexHit.H"
#include "FixedList.H"
#include "Ostream.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "labelBits.H"
#include "PackedList.H"

View File

@ -30,7 +30,7 @@ Description
#include "treeNode.H"
#include "treeBoundBox.H"
#include "octree.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -42,7 +42,7 @@ SourceFiles
#include "treeElem.H"
#include "boolList.H"
#include "linePointRef.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -50,7 +50,7 @@ SourceFiles
#include "treeBoundBoxList.H"
#include "treeElem.H"
#include "linePointRef.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -37,7 +37,7 @@ SourceFiles
#define primitiveMeshGeometry_H
#include "pointFields.H"
#include "labelHashSet.H"
#include "HashSet.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -39,7 +39,7 @@ SourceFiles
#ifndef topoSet_H
#define topoSet_H
#include "labelHashSet.H"
#include "HashSet.H"
#include "regIOobject.H"
#include "labelList.H"
#include "typeInfo.H"

View File

@ -31,7 +31,6 @@ Description
#include "labelPairLookup.H"
#include "OFstream.H"
#include "HashSet.H"
#include "labelHashSet.H"
#include "triSurface.H"
#include "pointIndexHit.H"
#include "treeDataTriSurface.H"

View File

@ -31,7 +31,6 @@ Description
#include "labelPairLookup.H"
#include "OFstream.H"
#include "HashSet.H"
#include "labelHashSet.H"
#include "triSurface.H"
#include "pointIndexHit.H"
#include "octreeDataTriSurface.H"

View File

@ -31,7 +31,6 @@ Description
#include "labelPairLookup.H"
#include "OFstream.H"
#include "HashSet.H"
#include "labelHashSet.H"
#include "triSurface.H"
#include "pointIndexHit.H"
#include "octreeDataTriSurface.H"

View File

@ -50,7 +50,7 @@ SourceFiles
#include "pointField.H"
#include "Map.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "pointIndexHit.H"
#include "edgeList.H"
#include "typeInfo.H"

View File

@ -39,7 +39,7 @@ SourceFiles
#include "boolList.H"
#include "pointField.H"
#include "DynamicList.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "FixedList.H"
#include "vector2D.H"
#include "triPointRef.H"

View File

@ -46,7 +46,7 @@ SourceFiles
#include "primitiveFieldsFwd.H"
#include "volFieldsFwd.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "Tuple2.H"
#include "OFstream.H"
#include "Switch.H"

View File

@ -45,7 +45,7 @@ SourceFiles
#include "primitiveFieldsFwd.H"
#include "volFieldsFwd.H"
#include "labelHashSet.H"
#include "HashSet.H"
#include "OFstream.H"
#include "Switch.H"
#include "pointFieldFwd.H"