mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
HashSet enhancement
- allow insert() and set() from a UList of Key This complements the existing erase(const UList<Key>&) method
This commit is contained in:
@ -92,6 +92,8 @@ int main(int argc, char *argv[])
|
|||||||
labelHashSet setD(1);
|
labelHashSet setD(1);
|
||||||
setD.insert(11);
|
setD.insert(11);
|
||||||
setD.insert(100);
|
setD.insert(100);
|
||||||
|
setD.insert(49);
|
||||||
|
setD.insert(36);
|
||||||
setD.insert(2008);
|
setD.insert(2008);
|
||||||
|
|
||||||
Info<< "setD : " << setD << endl;
|
Info<< "setD : " << setD << endl;
|
||||||
@ -138,6 +140,17 @@ int main(int argc, char *argv[])
|
|||||||
// this doesn't work (yet?)
|
// this doesn't work (yet?)
|
||||||
// setD[12] = true;
|
// setD[12] = true;
|
||||||
|
|
||||||
|
List<label> someLst(10);
|
||||||
|
forAll(someLst, elemI)
|
||||||
|
{
|
||||||
|
someLst[elemI] = elemI*elemI;
|
||||||
|
}
|
||||||
|
|
||||||
|
label added = setD.set(someLst);
|
||||||
|
Info<< "added " << added << " from " << someLst.size() << endl;
|
||||||
|
Info<< "setD : " << setD << endl;
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -31,6 +31,18 @@ License
|
|||||||
|
|
||||||
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
template<class Key, class Hash>
|
||||||
|
Foam::HashSet<Key, Hash>::HashSet(const UList<Key>& lst)
|
||||||
|
:
|
||||||
|
HashTable<nil, Key, Hash>(2*lst.size())
|
||||||
|
{
|
||||||
|
forAll(lst, elemI)
|
||||||
|
{
|
||||||
|
insert(lst[elemI]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class Key, class Hash>
|
template<class Key, class Hash>
|
||||||
template<class AnyType>
|
template<class AnyType>
|
||||||
Foam::HashSet<Key, Hash>::HashSet(const HashTable<AnyType, Key, Hash>& h)
|
Foam::HashSet<Key, Hash>::HashSet(const HashTable<AnyType, Key, Hash>& h)
|
||||||
@ -49,6 +61,24 @@ Foam::HashSet<Key, Hash>::HashSet(const HashTable<AnyType, Key, Hash>& h)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
|
||||||
|
|
||||||
|
template<class Key, class Hash>
|
||||||
|
Foam::label Foam::HashSet<Key, Hash>::insert(const UList<Key>& lst)
|
||||||
|
{
|
||||||
|
label count = 0;
|
||||||
|
forAll(lst, elemI)
|
||||||
|
{
|
||||||
|
if (insert(lst[elemI]))
|
||||||
|
{
|
||||||
|
++count;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
|
||||||
|
|
||||||
template<class Key, class Hash>
|
template<class Key, class Hash>
|
||||||
|
|||||||
@ -84,15 +84,7 @@ public:
|
|||||||
{}
|
{}
|
||||||
|
|
||||||
//- Construct from UList of Key
|
//- Construct from UList of Key
|
||||||
HashSet(const UList<Key>& lst)
|
HashSet(const UList<Key>&);
|
||||||
:
|
|
||||||
HashTable<nil, Key, Hash>(2*lst.size())
|
|
||||||
{
|
|
||||||
forAll(lst, i)
|
|
||||||
{
|
|
||||||
insert(lst[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//- Construct as copy
|
//- Construct as copy
|
||||||
HashSet(const HashSet<Key, Hash>& hs)
|
HashSet(const HashSet<Key, Hash>& hs)
|
||||||
@ -127,10 +119,20 @@ public:
|
|||||||
return HashTable<nil, Key, Hash>::insert(key, nil());
|
return HashTable<nil, Key, Hash>::insert(key, nil());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//- Insert keys from a UList of Key
|
||||||
|
// Return the number of new elements inserted
|
||||||
|
label insert(const UList<Key>&);
|
||||||
|
|
||||||
//- Same as insert (cannot overwrite nil content)
|
//- Same as insert (cannot overwrite nil content)
|
||||||
bool set(const Key& key)
|
bool set(const Key& key)
|
||||||
{
|
{
|
||||||
return HashTable<nil, Key, Hash>::insert(key, nil());
|
return insert(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
//- Same as insert (cannot overwrite nil content)
|
||||||
|
label set(const UList<Key>& lst)
|
||||||
|
{
|
||||||
|
return insert(lst);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Member Operators
|
// Member Operators
|
||||||
|
|||||||
Reference in New Issue
Block a user