Commit d7a80eba authored by Mark Olesen's avatar Mark Olesen

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
#define VRWGraph_H
#include "labelLongList.H"
#include "graphRow.H"
#include "DynList.H"
#include "bool.H"
#include "error.H"
#include "graphRow.H"
#include "graphConstRow.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -108,7 +109,7 @@ public:
// Useful typedefs
typedef const graphRow<const VRWGraph> constRow;
typedef const graphConstRow<VRWGraph> constRow;
typedef graphRow<VRWGraph> row;
......
......@@ -25,19 +25,21 @@ License
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
<< "Row index " << i
<< " is not in range " << 0
<< " and " << rows_.size() << abort(FatalError);
<< " is not in range [0.." << rows_.size() << ")"
<< abort(FatalError);
}
if ((j < 0) || (j >= rows_[i].size()))
if (j < 0 || j >= rows_[i].size())
{
FatalErrorInFunction
<< "Column index " << j
<< " is not in range " << 0
<< " and " << rows_[i].size() << abort(FatalError);
<< " is not in range [0.." << rows_[i].size() << ")"
<< 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
Foam::graphRow
Description
This class provides access to a row of a graph
This class provides non-const access to a row of a graph
SourceFiles
graphRowI.H
......@@ -35,21 +35,18 @@ SourceFiles
#ifndef graphRow_H
#define graphRow_H
#include "bool.H"
#include "Ostream.H"
#include "error.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
template<class graphType>
class graphRow;
class Ostream;
template<class graphType> class graphRow;
template<class graphType> class graphConstRow;
template<class graphType>
Ostream& operator<<(Ostream&, const graphRow<graphType>&);
/*---------------------------------------------------------------------------*\
Class graphRow Declaration
\*---------------------------------------------------------------------------*/
......@@ -59,17 +56,14 @@ class graphRow
{
// Private data
//- reference to the graph
//- Reference to the graph
graphType& data_;
//- row number
//- Row number
const label rowI_;
// Private member functions
//- check index
inline void checkIndex(const label i) const;
// Allow copy construct from non-const to const version
friend class graphConstRow<graphType>;
public:
......@@ -80,7 +74,7 @@ public:
inline graphRow(graphType& g, const label i);
//- Copy contructor
inline graphRow(const graphRow<graphType>&);
inline graphRow(const graphRow<graphType>& r);
//- Destructor
......@@ -89,7 +83,7 @@ public:
// Member Functions
//- Returns the number of elements in the row
//- Return the number of elements in the row
inline label size() const;
//- Reset the number of elements in the row
......@@ -116,20 +110,21 @@ public:
inline label operator[](const label) const;
inline label& operator[](const label);
//- Assignment operator
inline void operator=(const graphRow<graphType>&);
//- Copy contents from another row
inline void operator=(const graphRow<graphType>& rhs);
template<class listType>
inline void operator=(const listType&);
//- Copy contents from a list
template<class ListType>
inline void operator=(const ListType& rhs);
// IOstream operators
// Write graphRow to Ostream.
//- Write graphRow contents to Ostream.
friend Ostream& operator<< <graphType>
(
Ostream&,
const graphRow<graphType>&
Ostream& os,
const graphRow<graphType>& r
);
};
......
......@@ -23,21 +23,7 @@ License
\*---------------------------------------------------------------------------*/
namespace Foam
{
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);
}
}
#include "Ostream.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -52,11 +38,11 @@ inline Foam::graphRow<graphType>::graphRow(graphType& g, const label i)
template<class graphType>
inline Foam::graphRow<graphType>::graphRow
(
const graphRow<graphType>& ol
const graphRow<graphType>& r
)
:
data_(ol.data_),
rowI_(ol.rowI_)
data_(r.data_),
rowI_(r.rowI_)
{}
......@@ -140,49 +126,52 @@ inline Foam::label& Foam::graphRow<graphType>::operator[](const label i)
template<class graphType>
inline void Foam::graphRow<graphType>::operator=
(
const graphRow<graphType>& l
const graphRow<graphType>& rhs
)
{
data_.setRowSize(rowI_, l.size());
for (label i = 0; i < l.size(); ++i)
const label len = rhs.size();
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 listType>
inline void Foam::graphRow<graphType>::operator=(const listType& l)
template<class ListType>
inline void Foam::graphRow<graphType>::operator=(const ListType& rhs)
{
data_.setRowSize(rowI_, l.size());
for (label i = 0; i < l.size(); ++i)
const label len = rhs.size();
data_.setRowSize(rowI_, len);
for (label i = 0; i < len; ++i)
{
data_(rowI_, i) = l[i];
data_(rowI_, i) = rhs[i];
}
}
template<class graphType>
inline Foam::Ostream& operator<<
inline Foam::Ostream& Foam::operator<<
(
Foam::Ostream& os,
Ostream& os,
const Foam::graphRow<graphType>& r
)
{
os << r.size() << "(";
for (Foam::label i = 0; i < r.size(); ++i)
const label len = r.size();
os << len << '(';
for (label i = 0; i < len; ++i)
{
os << r[i] << " ";
if (i) os << ' ';
os << r[i];
}
os << ")";
os << ')';
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// ************************************************************************* //
......@@ -39,6 +39,7 @@ SourceFiles
#include "Ostream.H"
#include "error.H"
#include "graphRow.H"
#include "graphConstRow.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -116,7 +117,7 @@ public:
//- set and get operators
inline label operator()(const label i, const label j) const;
inline label& operator()(const label i, const label j);
inline const graphRow<const graphType> operator[](const label) const;
inline const graphConstRow<graphType> operator[](const label) const;
inline graphRow<graphType> operator[](const label);
......
......@@ -163,7 +163,7 @@ inline Foam::label& Foam::subGraph<graphType>::operator()
template<class graphType>
inline const Foam::graphRow<const graphType>
inline const Foam::graphConstRow<graphType>
Foam::subGraph<graphType>::operator[]
(
const label i
......
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