From 5ec44cd51f7da8f1e781891b13fbfdef8777b034 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 25 Jan 2019 10:47:34 +0100 Subject: [PATCH] ENH: add range check on findIndices (#1182) - add compile-time detection of deprecated findIndex() function - replace occurrences of findIndex() with the equivalent container method --- .../test/IndirectList/Test-IndirectList.C | 3 +-- applications/test/List/Test-List.C | 3 +-- .../test/UIndirectList/Test-UIndirectList.C | 3 +-- .../test/prefixOSstream/Test-prefixOSstream.C | 2 +- .../containers/Lists/ListOps/ListOps.H | 4 ++-- .../Lists/ListOps/ListOpsTemplates.C | 14 +++++++++----- .../collatedFileOperation.C | 18 +++++++----------- .../hostCollatedFileOperation.C | 2 +- .../AMIMethod/mapNearestAMI/mapNearestAMI.C | 2 +- 9 files changed, 24 insertions(+), 27 deletions(-) diff --git a/applications/test/IndirectList/Test-IndirectList.C b/applications/test/IndirectList/Test-IndirectList.C index 25b95990fb..19bd5587c6 100644 --- a/applications/test/IndirectList/Test-IndirectList.C +++ b/applications/test/IndirectList/Test-IndirectList.C @@ -60,8 +60,7 @@ void testFind(const T& val, const ListType& lst) <<" find() = " << lst.find(val) <<" rfind() = " << lst.rfind(val) <<" find(2) = " << lst.find(val, 2) - <<" rfind(2) = " << lst.rfind(val, 2) - <<" findIndex = " << findIndex(lst, val) << nl + <<" rfind(2) = " << lst.rfind(val, 2) << nl << nl; } diff --git a/applications/test/List/Test-List.C b/applications/test/List/Test-List.C index 5d3d5164de..1c0ebff0e0 100644 --- a/applications/test/List/Test-List.C +++ b/applications/test/List/Test-List.C @@ -92,8 +92,7 @@ void testFind(const T& val, const ListType& lst) <<" find() = " << lst.find(val) <<" rfind() = " << lst.rfind(val) <<" find(2) = " << lst.find(val, 2) - <<" rfind(2) = " << lst.rfind(val, 2) - <<" findIndex = " << findIndex(lst, val) << nl + <<" rfind(2) = " << lst.rfind(val, 2) << nl << nl; } diff --git a/applications/test/UIndirectList/Test-UIndirectList.C b/applications/test/UIndirectList/Test-UIndirectList.C index 7749a2b18f..acf1974253 100644 --- a/applications/test/UIndirectList/Test-UIndirectList.C +++ b/applications/test/UIndirectList/Test-UIndirectList.C @@ -50,8 +50,7 @@ void testFind(const T& val, const ListType& lst) <<" find() = " << lst.find(val) <<" rfind() = " << lst.rfind(val) <<" find(2) = " << lst.find(val, 2) - <<" rfind(2) = " << lst.rfind(val, 2) - <<" findIndex = " << findIndex(lst, val) << nl + <<" rfind(2) = " << lst.rfind(val, 2) << nl << nl; } diff --git a/applications/test/prefixOSstream/Test-prefixOSstream.C b/applications/test/prefixOSstream/Test-prefixOSstream.C index 363b4ccd3b..c12981a677 100644 --- a/applications/test/prefixOSstream/Test-prefixOSstream.C +++ b/applications/test/prefixOSstream/Test-prefixOSstream.C @@ -70,7 +70,7 @@ int main(int argc, char *argv[]) }; Pout<< list2 << endl; - Info<< findIndex(list2, vector(3, 4, 5)) << endl; + Info<< list2.find(vector(3, 4, 5)) << endl; return 0; } diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H index a7b5bd7fb6..57749742d0 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation - \\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd. + \\/ M anipulation | Copyright (C) 2017-2019 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -366,7 +366,7 @@ List invertManyToMany // \return The index found or return -1 if not found. // \deprecated(2017-10) - use the UList find/found methods template -label findIndex +label FOAM_DEPRECATED(2017-10) findIndex ( const ListType& input, typename ListType::const_reference val, diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C index 805d1b31ba..97e3327663 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C @@ -740,12 +740,16 @@ Foam::labelList Foam::findIndices // Pass 1: count occurrences label count = 0; - for (label i = start; i < len; ++i) + + if (start >= 0) { - if (input[i] == val) + for (label i = start; i < len; ++i) { - if (!count) start = i; // adjust start for second pass - ++count; + if (input[i] == val) + { + if (!count) start = i; // adjust start for second pass + ++count; + } } } @@ -761,7 +765,7 @@ Foam::labelList Foam::findIndices if (input[i] == val) { indices[count] = i; - if (++count == total) + if (++count == total) // early termination { break; } diff --git a/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.C b/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.C index 67d1985ec0..18078862b2 100644 --- a/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/collatedFileOperation/collatedFileOperation.C @@ -97,19 +97,15 @@ const { return Pstream::master(comm_); } + else if (ioRanks_.size()) + { + // Found myself in IO rank + return ioRanks_.found(proci); + } else { - // Use any IO ranks - if (ioRanks_.size()) - { - // Find myself in IO rank - return findIndex(ioRanks_, proci) != -1; - } - else - { - // Assume all in single communicator - return proci == 0; - } + // Assume all in single communicator + return proci == 0; } } diff --git a/src/OpenFOAM/global/fileOperations/collatedFileOperation/hostCollatedFileOperation.C b/src/OpenFOAM/global/fileOperations/collatedFileOperation/hostCollatedFileOperation.C index 2a3da281fd..ba2622b8f4 100644 --- a/src/OpenFOAM/global/fileOperations/collatedFileOperation/hostCollatedFileOperation.C +++ b/src/OpenFOAM/global/fileOperations/collatedFileOperation/hostCollatedFileOperation.C @@ -69,7 +69,7 @@ Foam::labelList Foam::fileOperations::hostCollatedFileOperation::subRanks IStringStream is(ioRanksString); labelList ioRanks(is); - if (findIndex(ioRanks, 0) == -1) + if (!ioRanks.found(0)) { FatalErrorInFunction << "Rank 0 (master) should be in the IO ranks. Currently " diff --git a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C index 27e455bc56..8e9baeb116 100644 --- a/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C +++ b/src/meshTools/AMIInterpolation/AMIInterpolation/AMIMethod/mapNearestAMI/mapNearestAMI.C @@ -150,7 +150,7 @@ Foam::label Foam::mapNearestAMI::findMappedSrcFace for (const label nbrFacei : nbrFaces) { - if (findIndex(visitedFaces, nbrFacei) == -1) + if (!visitedFaces.found(nbrFacei)) { testFaces.append(nbrFacei); }