STYLE: code consistency in find/rfind methods

This commit is contained in:
Mark Olesen
2020-01-31 17:05:57 +01:00
parent 822d052e32
commit 8629755f69
9 changed files with 108 additions and 64 deletions

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2015 OpenFOAM Foundation Copyright (C) 2011-2015 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -32,21 +32,17 @@ License
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T, unsigned N> template<class T, unsigned N>
Foam::label Foam::FixedList<T, N>::find Foam::label Foam::FixedList<T, N>::find(const T& val, label pos) const
(
const T& val,
const label start
) const
{ {
if (start >= 0) if (pos >= 0)
{ {
List_CONST_ACCESS(T, *this, list); 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<T, N>::find
} }
template<class T, unsigned N>
Foam::label Foam::FixedList<T, N>::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<class T, unsigned N> template<class T, unsigned N>
void Foam::FixedList<T, N>::moveFirst(const label i) void Foam::FixedList<T, N>::moveFirst(const label i)
{ {

View File

@ -249,12 +249,21 @@ public:
// Search // Search
//- Find index of the first occurence of the value. //- Find index of the first occurence of the value.
// Any occurences before the start pos are ignored.
// Linear search. // Linear search.
// \return -1 if not found. // \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. //- Find index of the last occurrence of the value.
inline bool found(const T& val, const label start=0) const; // 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 // Edit

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -304,10 +304,10 @@ template<class T, unsigned N>
inline bool Foam::FixedList<T, N>::found inline bool Foam::FixedList<T, N>::found
( (
const T& val, const T& val,
const label start label pos
) const ) const
{ {
return (this->find(val, start) >= 0); return (this->find(val, pos) >= 0);
} }

View File

@ -196,20 +196,22 @@ std::streamsize Foam::UList<T>::byteSize() const
template<class T> template<class T>
Foam::label Foam::UList<T>::find(const T& val, const label start) const Foam::label Foam::UList<T>::find(const T& val, label pos) const
{ {
const label len = this->size(); 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<T>::find(const T& val, const label start) const
template<class T> template<class T>
Foam::label Foam::UList<T>::rfind(const T& val, const label pos) const Foam::label Foam::UList<T>::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 // 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; return -1;

View File

@ -6,7 +6,7 @@
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2019 OpenCFD Ltd. Copyright (C) 2017-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -295,22 +295,22 @@ public:
// Search // Search
//- Find index of the first occurrence of the value. //- 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. // Linear search.
// \return position in list or -1 if not found. // \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. //- 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. // Linear search.
// \return position in list or -1 if not found. // \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. //- 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. // Linear search.
// \return true if found. // \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 // Edit

View File

@ -209,9 +209,9 @@ inline T* Foam::UList<T>::data()
template<class T> template<class T>
inline bool Foam::UList<T>::found(const T& val, const label start) const inline bool Foam::UList<T>::found(const T& val, label pos) const
{ {
return (this->find(val, start) >= 0); return (this->find(val, pos) >= 0);
} }

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -252,22 +252,24 @@ Foam::predicates::scalars::scalars(Istream& is)
Foam::label Foam::predicates::scalars::find Foam::label Foam::predicates::scalars::find
( (
const scalar& value, const scalar value,
const label start label pos
) const ) const
{ {
const label len = this->size(); const label len = this->size();
if (start >= 0 && len) if (pos >= 0 && len)
{ {
// auto iter = this->cbegin(); // 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 Foam::label Foam::predicates::scalars::rfind
( (
const scalar& value, const scalar value,
const label pos label pos
) const ) const
{ {
const label len1 = (this->size()-1);
// pos == -1 has same meaning as std::string::npos - search from end // 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; return -1;

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. This file is part of OpenFOAM.
@ -259,22 +259,22 @@ public:
// Search // Search
//- Index of the first match for the value. //- 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. // Linear search.
// \return position in list or -1 if not found. // \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. //- 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. // Linear search.
// \return position in list or -1 if not found. // \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. //- 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. // Linear search.
// \return true if found. // \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. //- Match any condition in the list.
// //

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com \\ / A nd | www.openfoam.com
\\/ M anipulation | \\/ M anipulation |
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd. Copyright (C) 2018-2020 OpenCFD Ltd.
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
License License
This file is part of OpenFOAM. 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 inline bool Foam::predicates::scalars::found
( (
const scalar& value, const scalar value,
const label start label pos
) const ) const
{ {
return (this->find(value, start) >= 0); return (this->find(value, pos) >= 0);
} }