From 8629755f6940ca9d65730a802d5223a38b9e7cc0 Mon Sep 17 00:00:00 2001 From: Mark Olesen Date: Fri, 31 Jan 2020 17:05:57 +0100 Subject: [PATCH] STYLE: code consistency in find/rfind methods --- .../containers/Lists/FixedList/FixedList.C | 41 ++++++++++++++----- .../containers/Lists/FixedList/FixedList.H | 15 +++++-- .../containers/Lists/FixedList/FixedListI.H | 6 +-- src/OpenFOAM/containers/Lists/UList/UList.C | 35 +++++++++------- src/OpenFOAM/containers/Lists/UList/UList.H | 14 +++---- src/OpenFOAM/containers/Lists/UList/UListI.H | 4 +- .../predicates/scalar/scalarPredicates.C | 35 +++++++++------- .../predicates/scalar/scalarPredicates.H | 14 +++---- .../predicates/scalar/scalarPredicatesI.H | 8 ++-- 9 files changed, 108 insertions(+), 64 deletions(-) diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.C b/src/OpenFOAM/containers/Lists/FixedList/FixedList.C index f76c95fa52..15b629a112 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.C +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -32,21 +32,17 @@ License // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template -Foam::label Foam::FixedList::find -( - const T& val, - const label start -) const +Foam::label Foam::FixedList::find(const T& val, label pos) const { - if (start >= 0) + if (pos >= 0) { List_CONST_ACCESS(T, *this, list); - for (label i = start; i < label(N); ++i) + while (pos < label(N)) { - if (list[i] == val) + if (list[pos] == val) { - return i; + return pos; } } } @@ -55,6 +51,31 @@ Foam::label Foam::FixedList::find } +template +Foam::label Foam::FixedList::rfind(const T& val, label pos) const +{ + // pos == -1 has same meaning as std::string::npos - search from end + if (pos < 0 || pos >= label(N)) + { + pos = label(N)-1; + } + + List_CONST_ACCESS(T, *this, list); + + while (pos >= 0) + { + if (list[pos] == val) + { + return pos; + } + + --pos; + } + + return -1; +} + + template void Foam::FixedList::moveFirst(const label i) { diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H index 5c7ef7f958..0edc4f5fea 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedList.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedList.H @@ -249,12 +249,21 @@ public: // Search //- Find index of the first occurence of the value. + // Any occurences before the start pos are ignored. // Linear search. // \return -1 if not found. - label find(const T& val, const label start=0) const; + label find(const T& val, label pos = 0) const; - //- True if the value if found in the list. Linear search. - inline bool found(const T& val, const label start=0) const; + //- Find index of the last occurrence of the value. + // Any occurrences after the end pos are ignored. + // Linear search. + // \return position in list or -1 if not found. + label rfind(const T& val, label pos = -1) const; + + //- True if the value if found in the list. + // Any occurences before the start pos are ignored. + // Linear search. + inline bool found(const T& val, label pos = 0) const; // Edit diff --git a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H index 47057ec995..f2ee9bfc4d 100644 --- a/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H +++ b/src/OpenFOAM/containers/Lists/FixedList/FixedListI.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -304,10 +304,10 @@ template inline bool Foam::FixedList::found ( const T& val, - const label start + label pos ) const { - return (this->find(val, start) >= 0); + return (this->find(val, pos) >= 0); } diff --git a/src/OpenFOAM/containers/Lists/UList/UList.C b/src/OpenFOAM/containers/Lists/UList/UList.C index 5d259379bd..1d17f2dc5f 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.C +++ b/src/OpenFOAM/containers/Lists/UList/UList.C @@ -196,20 +196,22 @@ std::streamsize Foam::UList::byteSize() const template -Foam::label Foam::UList::find(const T& val, const label start) const +Foam::label Foam::UList::find(const T& val, label pos) const { const label len = this->size(); - if (start >= 0 && len) + if (pos >= 0 && len) { - List_CONST_ACCESS(T, (*this), vp); + List_CONST_ACCESS(T, (*this), list); - for (label i = start; i < len; ++i) + while (pos < len) { - if (vp[i] == val) + if (list[pos] == val) { - return i; + return pos; } + + ++pos; } } @@ -218,19 +220,24 @@ Foam::label Foam::UList::find(const T& val, const label start) const template -Foam::label Foam::UList::rfind(const T& val, const label pos) const +Foam::label Foam::UList::rfind(const T& val, label pos) const { - List_CONST_ACCESS(T, (*this), vp); - - const label len1 = (this->size()-1); - // pos == -1 has same meaning as std::string::npos - search from end - for (label i = ((pos >= 0 && pos < len1) ? pos : len1); i >= 0; --i) + if (pos < 0 || pos >= this->size()) { - if (vp[i] == val) + pos = this->size()-1; + } + + List_CONST_ACCESS(T, (*this), list); + + while (pos >= 0) + { + if (list[pos] == val) { - return i; + return pos; } + + --pos; } return -1; diff --git a/src/OpenFOAM/containers/Lists/UList/UList.H b/src/OpenFOAM/containers/Lists/UList/UList.H index c8c131ba85..259aa076c9 100644 --- a/src/OpenFOAM/containers/Lists/UList/UList.H +++ b/src/OpenFOAM/containers/Lists/UList/UList.H @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2019 OpenCFD Ltd. + Copyright (C) 2017-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -295,22 +295,22 @@ public: // Search //- Find index of the first occurrence of the value. - // When start is specified, any occurrences before start are ignored. + // Any occurrences before the start pos are ignored. // Linear search. // \return position in list or -1 if not found. - label find(const T& val, const label start=0) const; + label find(const T& val, label pos = 0) const; //- Find index of the last occurrence of the value. - // When pos is specified, any occurrences after pos are ignored. + // Any occurrences after the end pos are ignored. // Linear search. // \return position in list or -1 if not found. - label rfind(const T& val, const label pos=-1) const; + label rfind(const T& val, label pos = -1) const; //- True if the value if found in the list. - // When start is specified, any occurences before start are ignored. + // Any occurrences before the start pos are ignored. // Linear search. // \return true if found. - inline bool found(const T& val, const label start=0) const; + inline bool found(const T& val, label pos = 0) const; // Edit diff --git a/src/OpenFOAM/containers/Lists/UList/UListI.H b/src/OpenFOAM/containers/Lists/UList/UListI.H index 4a8452487f..cb1da2dd30 100644 --- a/src/OpenFOAM/containers/Lists/UList/UListI.H +++ b/src/OpenFOAM/containers/Lists/UList/UListI.H @@ -209,9 +209,9 @@ inline T* Foam::UList::data() template -inline bool Foam::UList::found(const T& val, const label start) const +inline bool Foam::UList::found(const T& val, label pos) const { - return (this->find(val, start) >= 0); + return (this->find(val, pos) >= 0); } diff --git a/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.C b/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.C index ba3f2308da..0be0a0400a 100644 --- a/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.C +++ b/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -252,22 +252,24 @@ Foam::predicates::scalars::scalars(Istream& is) Foam::label Foam::predicates::scalars::find ( - const scalar& value, - const label start + const scalar value, + label pos ) const { const label len = this->size(); - if (start >= 0 && len) + if (pos >= 0 && len) { // auto iter = this->cbegin(); - for (label i = start; i < len; ++i) + while (pos < len) { - if ((*this)[i](value)) + if ((*this)[pos](value)) { - return i; + return pos; } + + ++pos; } } @@ -277,19 +279,24 @@ Foam::label Foam::predicates::scalars::find Foam::label Foam::predicates::scalars::rfind ( - const scalar& value, - const label pos + const scalar value, + label pos ) const { - const label len1 = (this->size()-1); - // pos == -1 has same meaning as std::string::npos - search from end - for (label i = ((pos >= 0 && pos < len1) ? pos : len1); i >= 0; --i) + if (pos < 0 || pos >= this->size()) { - if ((*this)[i](value)) + pos = this->size()-1; + } + + while (pos >= 0) + { + if ((*this)[pos](value)) { - return i; + return pos; } + + --pos; } return -1; diff --git a/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.H b/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.H index 406bcc431b..16664f4aca 100644 --- a/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.H +++ b/src/OpenFOAM/primitives/predicates/scalar/scalarPredicates.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -259,22 +259,22 @@ public: // Search //- Index of the first match for the value. - // When start is specified, any occurrences before start are ignored. + // Any occurences before the start pos are ignored. // Linear search. // \return position in list or -1 if not found. - label find(const scalar& value, const label start=0) const; + label find(const scalar value, label pos = 0) const; //- Index of the last match for the value. - // When pos is specified, any occurrences after pos are ignored. + // Any occurences after the end pos are ignored. // Linear search. // \return position in list or -1 if not found. - label rfind(const scalar& value, const label pos=-1) const; + label rfind(const scalar value, label pos = -1) const; //- True if the value matches any in the list. - // When start is specified, any occurences before start are ignored. + // Any occurences before the start pos are ignored. // Linear search. // \return true if found. - inline bool found(const scalar& value, const label start=0) const; + inline bool found(const scalar value, label pos=0) const; //- Match any condition in the list. // diff --git a/src/OpenFOAM/primitives/predicates/scalar/scalarPredicatesI.H b/src/OpenFOAM/primitives/predicates/scalar/scalarPredicatesI.H index 8e61614961..207fe59de5 100644 --- a/src/OpenFOAM/primitives/predicates/scalar/scalarPredicatesI.H +++ b/src/OpenFOAM/primitives/predicates/scalar/scalarPredicatesI.H @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2018 OpenCFD Ltd. + Copyright (C) 2018-2020 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -62,11 +62,11 @@ inline Foam::predicates::scalars::unary Foam::predicates::scalars::operation inline bool Foam::predicates::scalars::found ( - const scalar& value, - const label start + const scalar value, + label pos ) const { - return (this->find(value, start) >= 0); + return (this->find(value, pos) >= 0); }