Commit 8629755f authored by Mark Olesen's avatar Mark Olesen
Browse files

STYLE: code consistency in find/rfind methods

parent 822d052e
......@@ -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<class T, unsigned N>
Foam::label Foam::FixedList<T, N>::find
(
const T& val,
const label start
) const
Foam::label Foam::FixedList<T, N>::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<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>
void Foam::FixedList<T, N>::moveFirst(const label i)
{
......
......@@ -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
......
......@@ -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<class T, unsigned N>
inline bool Foam::FixedList<T, N>::found
(
const T& val,
const label start
label pos
) const
{
return (this->find(val, start) >= 0);
return (this->find(val, pos) >= 0);
}
......
......@@ -196,20 +196,22 @@ std::streamsize Foam::UList<T>::byteSize() const
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();
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>
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);
// pos == -1 has same meaning as std::string::npos - search from end
if (pos < 0 || pos >= this->size())
{
pos = this->size()-1;
}
const label len1 = (this->size()-1);
List_CONST_ACCESS(T, (*this), list);
// pos == -1 has same meaning as std::string::npos - search from end
for (label i = ((pos >= 0 && pos < len1) ? pos : len1); i >= 0; --i)
while (pos >= 0)
{
if (vp[i] == val)
if (list[pos] == val)
{
return i;
return pos;
}
--pos;
}
return -1;
......
......@@ -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
......
......@@ -209,9 +209,9 @@ inline T* Foam::UList<T>::data()
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);
}
......
......@@ -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())
{
pos = this->size()-1;
}
while (pos >= 0)
{
if ((*this)[i](value))
if ((*this)[pos](value))
{
return i;
return pos;
}
--pos;
}
return -1;
......
......@@ -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.
//
......
......@@ -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);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment