From 2a17ac0f8af5f3f7dac0780756a73dc3392d264e Mon Sep 17 00:00:00 2001 From: mattijs Date: Thu, 19 Apr 2012 16:46:14 +0100 Subject: [PATCH] ENH: findLower: have user-defined < operator --- .../containers/Lists/ListOps/ListOps.H | 15 ++++++++++- .../Lists/ListOps/ListOpsTemplates.C | 25 ++++++++++++++----- src/OpenFOAM/primitives/ops/ops.H | 6 ++++- 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H index 90d70cc26b..d0689b0eb1 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOps.H +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOps.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -37,6 +37,7 @@ SourceFiles #define ListOps_H #include "labelList.H" +#include "ops.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -209,6 +210,18 @@ label findSortedIndex ); +//- Find last element < given value in sorted list and return index, +// return -1 if not found. Binary search. +template +label findLower +( + const ListType&, + typename ListType::const_reference, + const label stary, + const BinaryOp& bop +); + + //- Find last element < given value in sorted list and return index, // return -1 if not found. Binary search. template diff --git a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C index 41e28c3f64..be6337a428 100644 --- a/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C +++ b/src/OpenFOAM/containers/Lists/ListOps/ListOpsTemplates.C @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -592,12 +592,13 @@ Foam::label Foam::findSortedIndex } -template +template Foam::label Foam::findLower ( const ListType& l, typename ListType::const_reference t, - const label start + const label start, + const BinaryOp& bop ) { if (start >= l.size()) @@ -612,7 +613,7 @@ Foam::label Foam::findLower { label mid = (low + high)/2; - if (l[mid] < t) + if (bop(l[mid], t)) { low = mid; } @@ -622,13 +623,13 @@ Foam::label Foam::findLower } } - if (l[high] < t) + if (bop(l[high], t)) { return high; } else { - if (l[low] < t) + if (bop(l[low], t)) { return low; } @@ -640,6 +641,18 @@ Foam::label Foam::findLower } +template +Foam::label Foam::findLower +( + const ListType& l, + typename ListType::const_reference t, + const label start +) +{ + return findLower(l, t, start, lessOp()); +} + + template Foam::List Foam::initList(const T elems[nRows]) { diff --git a/src/OpenFOAM/primitives/ops/ops.H b/src/OpenFOAM/primitives/ops/ops.H index c68face927..66d7930849 100644 --- a/src/OpenFOAM/primitives/ops/ops.H +++ b/src/OpenFOAM/primitives/ops/ops.H @@ -2,7 +2,7 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation + \\ / A nd | Copyright (C) 2011-2012 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- License @@ -142,6 +142,10 @@ Op(minMod, minMod(x, y)) Op(and, x && y) Op(or, x || y) Op(eqEq, x == y) +Op(less, x < y) +Op(lessEq, x <= y) +Op(greater, x > y) +Op(greaterEq, x >= y) #undef Op