ENH: add ListOps find/found accepting a unary predicate (#1182)

- can also be used for other purposes.
  Eg,

      if (ListOps::found(list, matcher))
      {
         ...
      }

  vs.

      if (!findStrings(matcher, list).empty())
      {
         ...
      }
This commit is contained in:
Mark Olesen
2019-01-25 11:45:03 +01:00
parent 5ec44cd51f
commit 87523acad3
9 changed files with 114 additions and 21 deletions

View File

@ -582,6 +582,32 @@ struct greater
};
//- Find index of the first occurrence that satisfies the predicate.
// When start is specified, any occurrences before start are ignored.
// Linear search.
// \return position in list or -1 if not found.
template<class ListType, class UnaryPredicate>
label find
(
const ListType& input,
const UnaryPredicate& pred,
const label start=0
);
//- True if there is a value in the list that satisfies the predicate.
// When start is specified, any occurences before start are ignored.
// Linear search.
// \return true if found.
template<class ListType, class UnaryPredicate>
bool found
(
const ListType& input,
const UnaryPredicate& pred,
const label start=0
);
//- Set various locations of the list with a specified value.
//
// \param list the list to modify

View File

@ -1097,6 +1097,43 @@ void Foam::ListOps::uniqueEqOp<T>::operator()
}
template<class ListType, class UnaryPredicate>
Foam::label Foam::ListOps::find
(
const ListType& input,
const UnaryPredicate& pred,
const label start
)
{
const label len = input.size();
if (start >= 0)
{
for (label i = start; i < len; ++i)
{
if (pred(input[i]))
{
return i;
}
}
}
return -1;
}
template<class ListType, class UnaryPredicate>
bool Foam::ListOps::found
(
const ListType& input,
const UnaryPredicate& pred,
const label start
)
{
return (ListOps::find(input, pred, start) >= 0);
}
template<class T>
void Foam::ListOps::setValue
(

View File

@ -111,6 +111,7 @@ namespace Foam
template<class T> class MinMax;
// Common min/max types
typedef MinMax<label> labelMinMax;
typedef MinMax<scalar> scalarMinMax;

View File

@ -425,7 +425,7 @@ Foam::label Foam::voxelMeshSearch::findCell(const point& p) const
label nei = mesh_.faceNeighbour()[facei];
nextCell = (own == celli ? nei : own);
if (findIndex(track_, nextCell, startOfTrack) != -1)
if (track_.found(nextCell, startOfTrack))
{
return celli;
}
@ -438,7 +438,7 @@ Foam::label Foam::voxelMeshSearch::findCell(const point& p) const
{
return nextCell;
}
else if (findIndex(track_, nextCell, startOfTrack) != -1)
else if (track_.found(nextCell, startOfTrack))
{
return -1; // point is really out
}

View File

@ -26,8 +26,8 @@ License
#include "sampledSets.H"
#include "volFields.H"
#include "IOobjectList.H"
#include "stringListOps.H"
#include "UIndirectList.H"
#include "ListOps.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -69,7 +69,7 @@ Foam::label Foam::sampledSets::classifyFields()
// Detect missing fields
forAll(fieldSelection_, i)
{
if (findStrings(fieldSelection_[i], allFields).empty())
if (!ListOps::found(allFields, fieldSelection_[i]))
{
missed.append(i);
}

View File

@ -25,8 +25,8 @@ License
#include "sampledSurfaces.H"
#include "IOobjectList.H"
#include "stringListOps.H"
#include "UIndirectList.H"
#include "ListOps.H"
// * * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * //
@ -57,7 +57,7 @@ Foam::label Foam::sampledSurfaces::classifyFields()
// Detect missing fields
forAll(fieldSelection_, i)
{
if (findStrings(fieldSelection_[i], allFields).empty())
if (!ListOps::found(allFields, fieldSelection_[i]))
{
missed.append(i);
}

View File

@ -641,7 +641,7 @@ void Foam::isoSurfaceTopo::generateTriPoints
forAll(f1, fp)
{
oppositeI = f1[fp];
if (findIndex(f0, oppositeI) == -1)
if (!f0.found(oppositeI))
{
break;
}