Commit d7a80eba authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH/COMP: provide graphConstRow accessor

- the previous code used a dual-purposed graphRow for non-const/const
  access. However, clang 4.0 rightly identifies there being no
  constructor path from "graphRow<T>" to a "graphRow<const T>".

  Since the row-access is a somewhat similar concept to an STL-iterator,
  now provide non-const and const versions of the row access.
parent 1e8698bc
...@@ -38,10 +38,11 @@ SourceFiles ...@@ -38,10 +38,11 @@ SourceFiles
#define VRWGraph_H #define VRWGraph_H
#include "labelLongList.H" #include "labelLongList.H"
#include "graphRow.H"
#include "DynList.H" #include "DynList.H"
#include "bool.H" #include "bool.H"
#include "error.H" #include "error.H"
#include "graphRow.H"
#include "graphConstRow.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
...@@ -108,7 +109,7 @@ public: ...@@ -108,7 +109,7 @@ public:
// Useful typedefs // Useful typedefs
typedef const graphRow<const VRWGraph> constRow; typedef const graphConstRow<VRWGraph> constRow;
typedef graphRow<VRWGraph> row; typedef graphRow<VRWGraph> row;
......
...@@ -25,19 +25,21 @@ License ...@@ -25,19 +25,21 @@ License
inline void Foam::VRWGraph::checkIndex(const label i, const label j) const inline void Foam::VRWGraph::checkIndex(const label i, const label j) const
{ {
if ((i < 0) || (i >= rows_.size())) if (i < 0 || i >= rows_.size())
{ {
FatalErrorInFunction FatalErrorInFunction
<< "Row index " << i << "Row index " << i
<< " is not in range " << 0 << " is not in range [0.." << rows_.size() << ")"
<< " and " << rows_.size() << abort(FatalError); << abort(FatalError);
} }
if ((j < 0) || (j >= rows_[i].size())) if (j < 0 || j >= rows_[i].size())
{
FatalErrorInFunction FatalErrorInFunction
<< "Column index " << j << "Column index " << j
<< " is not in range " << 0 << " is not in range [0.." << rows_[i].size() << ")"
<< " and " << rows_[i].size() << abort(FatalError); << abort(FatalError);
}
} }
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | cfMesh: A library for mesh generation
\\ / O peration |
\\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
\\/ M anipulation | Copyright (C) Creative Fields, Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
Class
Foam::graphConstRow
Description
This class provides const access to a row of a graph
SourceFiles
graphConstRowI.H
\*---------------------------------------------------------------------------*/
#ifndef graphConstRow_H
#define graphConstRow_H
#include "Ostream.H"
#include "error.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
class Ostream;
template<class graphType> class graphRow;
template<class graphType> class graphConstRow;
template<class graphType>
Ostream& operator<<(Ostream&, const graphConstRow<graphType>&);
/*---------------------------------------------------------------------------*\
Class graphConstRow Declaration
\*---------------------------------------------------------------------------*/
template<class graphType>
class graphConstRow
{
// Private data
//- Const reference to the graph
const graphType& data_;
//- Row number
const label rowI_;
// Private Member Functions
//- Avoid possible confusion
void operator=(const graphConstRow<graphType>&) = delete;
public:
// Constructors
//- Construct from graph and row number
inline graphConstRow(const graphType& g, const label i);
//- Copy contructor
inline graphConstRow(const graphConstRow<graphType>& r);
//- Copy contructor from non-const version
inline graphConstRow(const graphRow<graphType>& r);
//- Destructor
~graphConstRow() = default;
// Member Functions
//- Return the number of elements in the row
inline label size() const;
// Member Operators
//- check if the element is in the given row (takes linear time)
inline bool contains(const label e) const;
inline bool found(const label e) const;
inline label find(const label e) const;
//- Get operator
inline label operator[](const label) const;
// IOstream operators
//- Write graphConstRow contents to Ostream.
friend Ostream& operator<< <graphType>
(
Ostream& os,
const graphConstRow<graphType>& r
);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "graphConstRowI.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | cfMesh: A library for mesh generation
\\ / O peration |
\\ / A nd | Author: Franjo Juretic (franjo.juretic@c-fields.com)
\\/ M anipulation | Copyright (C) Creative Fields, Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "Ostream.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class graphType>
inline Foam::graphConstRow<graphType>::graphConstRow
(
const graphType& g,
const label i
)
:
data_(g),
rowI_(i)
{}
template<class graphType>
inline Foam::graphConstRow<graphType>::graphConstRow
(
const graphConstRow<graphType>& r
)
:
data_(r.data_),
rowI_(r.rowI_)
{}
template<class graphType>
inline Foam::graphConstRow<graphType>::graphConstRow
(
const graphRow<graphType>& r
)
:
data_(r.data_),
rowI_(r.rowI_)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class graphType>
inline Foam::label Foam::graphConstRow<graphType>::size() const
{
return data_.sizeOfRow(rowI_);
}
template<class graphType>
inline bool Foam::graphConstRow<graphType>::contains(const label e) const
{
return data_.contains(rowI_, e);
}
template<class graphType>
inline bool Foam::graphConstRow<graphType>::found(const label e) const
{
return data_.found(rowI_, e);
}
template<class graphType>
inline Foam::label Foam::graphConstRow<graphType>::find
(
const label e
) const
{
return data_.find(rowI_, e);
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class graphType>
inline Foam::label Foam::graphConstRow<graphType>::operator[]
(
const label i
) const
{
return data_(rowI_, i);
}
template<class graphType>
inline Foam::Ostream& Foam::operator<<
(
Ostream& os,
const Foam::graphConstRow<graphType>& r
)
{
const label len = r.size();
os << len << '(';
for (label i = 0; i < len; ++i)
{
if (i) os << ' ';
os << r[i];
}
os << ')';
return os;
}
// ************************************************************************* //
...@@ -25,7 +25,7 @@ Class ...@@ -25,7 +25,7 @@ Class
Foam::graphRow Foam::graphRow
Description Description
This class provides access to a row of a graph This class provides non-const access to a row of a graph
SourceFiles SourceFiles
graphRowI.H graphRowI.H
...@@ -35,21 +35,18 @@ SourceFiles ...@@ -35,21 +35,18 @@ SourceFiles
#ifndef graphRow_H #ifndef graphRow_H
#define graphRow_H #define graphRow_H
#include "bool.H"
#include "Ostream.H"
#include "error.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam namespace Foam
{ {
class Ostream;
template<class graphType> template<class graphType> class graphRow;
class graphRow; template<class graphType> class graphConstRow;
template<class graphType> template<class graphType>
Ostream& operator<<(Ostream&, const graphRow<graphType>&); Ostream& operator<<(Ostream&, const graphRow<graphType>&);
/*---------------------------------------------------------------------------*\ /*---------------------------------------------------------------------------*\
Class graphRow Declaration Class graphRow Declaration
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
...@@ -59,17 +56,14 @@ class graphRow ...@@ -59,17 +56,14 @@ class graphRow
{ {
// Private data // Private data
//- reference to the graph //- Reference to the graph
graphType& data_; graphType& data_;
//- row number //- Row number
const label rowI_; const label rowI_;
// Allow copy construct from non-const to const version
// Private member functions friend class graphConstRow<graphType>;
//- check index
inline void checkIndex(const label i) const;
public: public:
...@@ -80,7 +74,7 @@ public: ...@@ -80,7 +74,7 @@ public:
inline graphRow(graphType& g, const label i); inline graphRow(graphType& g, const label i);
//- Copy contructor //- Copy contructor
inline graphRow(const graphRow<graphType>&); inline graphRow(const graphRow<graphType>& r);
//- Destructor //- Destructor
...@@ -89,7 +83,7 @@ public: ...@@ -89,7 +83,7 @@ public:
// Member Functions // Member Functions
//- Returns the number of elements in the row //- Return the number of elements in the row
inline label size() const; inline label size() const;
//- Reset the number of elements in the row //- Reset the number of elements in the row
...@@ -116,20 +110,21 @@ public: ...@@ -116,20 +110,21 @@ public:
inline label operator[](const label) const; inline label operator[](const label) const;
inline label& operator[](const label); inline label& operator[](const label);
//- Assignment operator //- Copy contents from another row
inline void operator=(const graphRow<graphType>&); inline void operator=(const graphRow<graphType>& rhs);
template<class listType> //- Copy contents from a list
inline void operator=(const listType&); template<class ListType>
inline void operator=(const ListType& rhs);
// IOstream operators // IOstream operators
// Write graphRow to Ostream. //- Write graphRow contents to Ostream.
friend Ostream& operator<< <graphType> friend Ostream& operator<< <graphType>
( (
Ostream&, Ostream& os,
const graphRow<graphType>& const graphRow<graphType>& r
); );
}; };
......
...@@ -23,21 +23,7 @@ License ...@@ -23,21 +23,7 @@ License
\*---------------------------------------------------------------------------*/ \*---------------------------------------------------------------------------*/
namespace Foam #include "Ostream.H"
{
template<class graphType>
inline void Foam::graphRow<graphType>::checkIndex(const label i) const
{
if ((i < 0) || (i >=data_.sizeOfRow(rowI_)))
{
FatalErrorInFunction
<< "Row index " << rowI_
<< " is not in range " << Foam::label(0)
<< " and " << data_.sizeOfRow(rowI_) << abort(FatalError);
}
}
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
...@@ -52,11 +38,11 @@ inline Foam::graphRow<graphType>::graphRow(graphType& g, const label i) ...@@ -52,11 +38,11 @@ inline Foam::graphRow<graphType>::graphRow(graphType& g, const label i)
template<class graphType> template<class graphType>
inline Foam::graphRow<graphType>::graphRow inline Foam::graphRow<graphType>::graphRow
( (
const graphRow<graphType>& ol const graphRow<graphType>& r
) )
: :
data_(ol.data_), data_(r.data_),
rowI_(ol.rowI_) rowI_(r.rowI_)
{} {}
...@@ -140,49 +126,52 @@ inline Foam::label& Foam::graphRow<graphType>::operator[](const label i) ...@@ -140,49 +126,52 @@ inline Foam::label& Foam::graphRow<graphType>::operator[](const label i)
template<class graphType> template<class graphType>
inline void Foam::graphRow<graphType>::operator= inline void Foam::graphRow<graphType>::operator=
( (
const graphRow<graphType>& l const graphRow<graphType>& rhs
) )
{ {
data_.setRowSize(rowI_, l.size()); const label len = rhs.size();
for (label i = 0; i < l.size(); ++i)
data_.setRowSize(rowI_, len);
for (label i = 0; i < len; ++i)
{ {
data_(rowI_, i) = l[i]; data_(rowI_, i) = rhs[i];
} }
} }
template<class graphType> template<class graphType>
template<class listType> template<class ListType>
inline void Foam::graphRow<graphType>::operator=(const listType& l) inline void Foam::graphRow<graphType>::operator=(const ListType& rhs)
{ {
data_.setRowSize(rowI_, l.size()); const label len = rhs.size();
for (label i = 0; i < l.size(); ++i)
data_.setRowSize(rowI_, len);
for (label i = 0; i < len; ++i)
{ {
data_(rowI_, i) = l[i]; data_(rowI_, i) = rhs[i];
} }
} }
template<class graphType> template<class graphType>
inline Foam::Ostream& operator<< inline Foam::Ostream& Foam::operator<<
( (
Foam::Ostream& os, Ostream& os,
const Foam::graphRow<graphType>& r const Foam::graphRow<graphType>& r
) )
{ {
os << r.size() << "("; const label len = r.size();
for (Foam::label i = 0; i < r.size(); ++i)
os << len << '(';
for (label i = 0; i < len; ++i)
{ {
os << r[i] << " "; if (i) os << ' ';
os << r[i];
} }
os << ")"; os << ')';
return os; return os;
}