Skip to content
Snippets Groups Projects
Commit d01a47ae authored by Mark Olesen's avatar Mark Olesen
Browse files

ENH: replace IndirectList with version that inherits from UIndirectList

parent f2c3ba65
Branches
Tags
No related merge requests found
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -30,6 +30,15 @@ Description
using namespace Foam;
template<class ListType>
void printInfo(const ListType& lst)
{
Info<< "addr: " << lst.addressing() << nl
<< "list: " << lst << nl
<< endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -42,8 +51,7 @@ int main(int argc, char *argv[])
completeList[i] = 0.1*i;
}
Info<< "raw : " << completeList << nl
<< endl;
Info<< "raw : " << completeList << nl << endl;
List<label> addresses(5);
......@@ -53,11 +61,9 @@ int main(int argc, char *argv[])
addresses[3] = 8;
addresses[4] = 5;
IndirectList<double> idl(completeList, addresses);
IndirectList<double> idl1(completeList, addresses);
Info<< "addr: " << idl.addressing() << nl
<< "list: " << idl() << nl
<< endl;
printInfo(idl1);
addresses[4] = 1;
addresses[3] = 0;
......@@ -65,11 +71,26 @@ int main(int argc, char *argv[])
addresses[1] = 8;
addresses[0] = 5;
idl.resetAddressing(addresses.xfer());
idl1.resetAddressing(addresses.xfer());
Info<< "addr: " << idl.addressing() << nl
<< "list: " << idl() << nl
<< endl;
printInfo(idl1);
// test copying
UIndirectList<double> uidl1(idl1);
IndirectList<double> idl2(uidl1);
IndirectList<double> idl3(idl2);
printInfo(uidl1);
idl1.resetAddressing(List<label>());
// idl2.resetAddressing(List<label>());
Info<<"after resetAddressing:" << nl << endl;
printInfo(uidl1);
printInfo(idl1);
printInfo(idl2);
printInfo(idl3);
Info<< "End\n" << endl;
......
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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::IndirectList2
Description
A List with indirect addressing.
SourceFiles
IndirectListI.H
\*---------------------------------------------------------------------------*/
#ifndef IndirectList2_H
#define IndirectList2_H
#include "UIndirectList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
/*---------------------------------------------------------------------------*\
Class IndirectListAddressing Declaration
\*---------------------------------------------------------------------------*/
//- A helper class for storing addresses.
class IndirectListAddressing
{
// Private data
//- Storage for the list addressing
List<label> addressing_;
// Private Member Functions
//- Disallow default bitwise copy construct
IndirectListAddressing(const IndirectListAddressing&);
//- Disallow default bitwise assignment
void operator=(const IndirectListAddressing&);
protected:
// Constructors
//- Construct by copying the addressing array
explicit inline IndirectListAddressing(const UList<label>& addr);
//- Construct by transferring addressing array
explicit inline IndirectListAddressing(const Xfer<List<label> >& addr);
// Member Functions
// Access
//- Return the list addressing
inline const List<label>& addressing() const;
// Edit
//- Reset addressing
inline void resetAddressing(const UList<label>&);
inline void resetAddressing(const Xfer<List<label> >&);
};
/*---------------------------------------------------------------------------*\
Class IndirectList2 Declaration
\*---------------------------------------------------------------------------*/
template<class T>
class IndirectList2
:
private IndirectListAddressing,
public UIndirectList<T>
{
// Private Member Functions
//- Disable default assignment operator
void operator=(const IndirectList2<T>&);
//- Disable assignment from UIndirectList
void operator=(const UIndirectList<T>&);
public:
// Constructors
//- Construct given the complete list and the addressing array
inline IndirectList2(const UList<T>&, const UList<label>&);
//- Construct given the complete list and by transferring addressing
inline IndirectList2(const UList<T>&, const Xfer<List<label> >&);
//- Copy constructor
inline IndirectList2(const IndirectList2<T>&);
//- Construct from UIndirectList
explicit inline IndirectList2(const UIndirectList<T>&);
// Member Functions
// Access
//- Return the list addressing
using UIndirectList<T>::addressing;
// Edit
//- Reset addressing
using IndirectListAddressing::resetAddressing;
// Member Operators
//- Assignment operator
using UIndirectList<T>::operator=;
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#include "IndirectList2I.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
\*---------------------------------------------------------------------------*/
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
inline Foam::IndirectListAddressing::IndirectListAddressing
(
const UList<label>& addr
)
:
addressing_(addr)
{}
inline Foam::IndirectListAddressing::IndirectListAddressing
(
const Xfer<List<label> >& addr
)
:
addressing_(addr)
{}
template<class T>
inline Foam::IndirectList2<T>::IndirectList2
(
const UList<T>& completeList,
const UList<label>& addr
)
:
IndirectListAddressing(addr),
UIndirectList<T>
(
completeList,
IndirectListAddressing::addressing()
)
{}
template<class T>
inline Foam::IndirectList2<T>::IndirectList2
(
const UList<T>& completeList,
const Xfer<List<label> >& addr
)
:
IndirectListAddressing(addr),
UIndirectList<T>
(
completeList,
IndirectListAddressing::addressing()
)
{}
template<class T>
inline Foam::IndirectList2<T>::IndirectList2
(
const IndirectList2<T>& lst
)
:
IndirectListAddressing(lst.addressing()),
UIndirectList<T>
(
lst.completeList(),
IndirectListAddressing::addressing()
)
{}
template<class T>
inline Foam::IndirectList2<T>::IndirectList2
(
const UIndirectList<T>& lst
)
:
IndirectListAddressing(lst.addressing()),
UIndirectList<T>
(
lst.completeList(),
IndirectListAddressing::addressing()
)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
inline const Foam::List<Foam::label>&
Foam::IndirectListAddressing::addressing() const
{
return addressing_;
}
inline void Foam::IndirectListAddressing::resetAddressing
(
const UList<label>& addr
)
{
addressing_ = addr;
}
inline void Foam::IndirectListAddressing::resetAddressing
(
const Xfer<List<label> >& addr
)
{
addressing_.transfer(addr());
}
// ************************************************************************* //
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
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/>.
Description
\*---------------------------------------------------------------------------*/
#include "IndirectList2.H"
#include "IOstreams.H"
using namespace Foam;
template<class ListType>
void printInfo(const ListType& lst)
{
Info<< "addr: " << lst.addressing() << nl
<< "list: " << lst << nl
<< endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
List<double> completeList(10);
forAll(completeList, i)
{
completeList[i] = 0.1*i;
}
Info<< "raw : " << completeList << nl << endl;
List<label> addresses(5);
addresses[0] = 1;
addresses[1] = 0;
addresses[2] = 7;
addresses[3] = 8;
addresses[4] = 5;
IndirectList2<double> idl1(completeList, addresses);
printInfo(idl1);
addresses[4] = 1;
addresses[3] = 0;
addresses[2] = 7;
addresses[1] = 8;
addresses[0] = 5;
idl1.resetAddressing(addresses.xfer());
printInfo(idl1);
// test copying
UIndirectList<double> uidl1(idl1);
IndirectList2<double> idl2(uidl1);
IndirectList2<double> idl3(idl2);
printInfo(uidl1);
idl1.resetAddressing(List<label>());
// idl2.resetAddressing(List<label>());
Info<<"after resetAddressing:" << nl << endl;
printInfo(uidl1);
printInfo(idl1);
printInfo(idl2);
printInfo(idl3);
Info<< "End\n" << endl;
return 0;
}
// ************************************************************************* //
IndirectListTest2.C
EXE = $(FOAM_USER_APPBIN)/IndirectListTest2
/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */
/* EXE_LIBS = -lfiniteVolume */
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -25,7 +25,11 @@ Class
Foam::IndirectList
Description
A List with indirect addressing
A List with indirect addressing.
SeeAlso
Foam::UIndirectList for a version without any allocation for the
addressing.
SourceFiles
IndirectListI.H
......@@ -35,7 +39,7 @@ SourceFiles
#ifndef IndirectList_H
#define IndirectList_H
#include "List.H"
#include "UIndirectList.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
......@@ -43,81 +47,109 @@ namespace Foam
{
/*---------------------------------------------------------------------------*\
Class IndirectList Declaration
Class IndirectListAddressing Declaration
\*---------------------------------------------------------------------------*/
template<class T>
class IndirectList
//- A helper class for storing addresses.
class IndirectListAddressing
{
// Private data
UList<T>& completeList_;
//- Storage for the list addressing
List<label> addressing_;
public:
// Private Member Functions
//- Disallow default bitwise copy construct
IndirectListAddressing(const IndirectListAddressing&);
//- Disallow default bitwise assignment
void operator=(const IndirectListAddressing&);
protected:
// Constructors
//- Construct given the complete list and the addressing array
inline IndirectList(const UList<T>&, const UList<label>&);
//- Construct by copying the addressing array
explicit inline IndirectListAddressing(const UList<label>& addr);
//- Construct given the complete list and by transferring addressing
inline IndirectList(const UList<T>&, const Xfer<List<label> >&);
//- Construct by transferring addressing array
explicit inline IndirectListAddressing(const Xfer<List<label> >& addr);
// Member Functions
// Access
//- Return the number of elements in the list
inline label size() const;
//- Return the list addressing
inline const List<label>& addressing() const;
//- Return true if the list is empty (ie, size() is zero).
inline bool empty() const;
// Edit
//- Return the first element of the list.
inline T& first();
//- Reset addressing
inline void resetAddressing(const UList<label>&);
inline void resetAddressing(const Xfer<List<label> >&);
//- Return first element of the list.
inline const T& first() const;
};
//- Return the last element of the list.
inline T& last();
//- Return the last element of the list.
inline const T& last() const;
/*---------------------------------------------------------------------------*\
Class IndirectList Declaration
\*---------------------------------------------------------------------------*/
//- Return the complete list
inline const UList<T>& completeList() const;
template<class T>
class IndirectList
:
private IndirectListAddressing,
public UIndirectList<T>
{
// Private Member Functions
//- Return the list addressing
inline const List<label>& addressing() const;
//- Disable default assignment operator
void operator=(const IndirectList<T>&);
//- Disable assignment from UIndirectList
void operator=(const UIndirectList<T>&);
// Edit
//- Reset addressing
inline void resetAddressing(const UList<label>&);
inline void resetAddressing(const Xfer<List<label> >&);
public:
// Constructors
//- Construct given the complete list and the addressing array
inline IndirectList(const UList<T>&, const UList<label>&);
// Member Operators
//- Construct given the complete list and by transferring addressing
inline IndirectList(const UList<T>&, const Xfer<List<label> >&);
//- Return the addressed elements as a List
inline List<T> operator()() const;
//- Copy constructor
inline IndirectList(const IndirectList<T>&);
//- Return non-const access to an element
inline T& operator[](const label);
//- Construct from UIndirectList
explicit inline IndirectList(const UIndirectList<T>&);
//- Return const access to an element
inline const T& operator[](const label) const;
//- Assignment from UList of addressed elements
inline void operator=(const UList<T>&);
// Member Functions
// Access
//- Return the list addressing
using UIndirectList<T>::addressing;
// Edit
//- Reset addressing
using IndirectListAddressing::resetAddressing;
// Member Operators
//- Assignment of all entries to the given value
inline void operator=(const T&);
//- Assignment operator
using UIndirectList<T>::operator=;
};
......
......@@ -2,7 +2,7 @@
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 1991-2010 OpenCFD Ltd.
\\ / A nd | Copyright (C) 2010-2010 OpenCFD Ltd.
\\/ M anipulation |
-------------------------------------------------------------------------------
License
......@@ -25,90 +25,97 @@ License
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>
inline Foam::IndirectList<T>::IndirectList
inline Foam::IndirectListAddressing::IndirectListAddressing
(
const UList<T>& completeList,
const UList<label>& addr
)
:
completeList_(const_cast<UList<T>&>(completeList)),
addressing_(addr)
{}
template<class T>
inline Foam::IndirectList<T>::IndirectList
inline Foam::IndirectListAddressing::IndirectListAddressing
(
const UList<T>& completeList,
const Xfer<List<label> >& addr
)
:
completeList_(const_cast<UList<T>&>(completeList)),
addressing_(addr)
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline Foam::label Foam::IndirectList<T>::size() const
{
return addressing_.size();
}
template<class T>
inline bool Foam::IndirectList<T>::empty() const
{
return addressing_.empty();
}
template<class T>
inline T& Foam::IndirectList<T>::first()
{
return completeList_[addressing_.first()];
}
inline Foam::IndirectList<T>::IndirectList
(
const UList<T>& completeList,
const UList<label>& addr
)
:
IndirectListAddressing(addr),
UIndirectList<T>
(
completeList,
IndirectListAddressing::addressing()
)
{}
template<class T>
inline const T& Foam::IndirectList<T>::first() const
{
return completeList_[addressing_.first()];
}
inline Foam::IndirectList<T>::IndirectList
(
const UList<T>& completeList,
const Xfer<List<label> >& addr
)
:
IndirectListAddressing(addr),
UIndirectList<T>
(
completeList,
IndirectListAddressing::addressing()
)
{}
template<class T>
inline T& Foam::IndirectList<T>::last()
{
return completeList_[addressing_.last()];
}
inline Foam::IndirectList<T>::IndirectList
(
const IndirectList<T>& lst
)
:
IndirectListAddressing(lst.addressing()),
UIndirectList<T>
(
lst.completeList(),
IndirectListAddressing::addressing()
)
{}
template<class T>
inline const T& Foam::IndirectList<T>::last() const
{
return completeList_[addressing_.last()];
}
inline Foam::IndirectList<T>::IndirectList
(
const UIndirectList<T>& lst
)
:
IndirectListAddressing(lst.addressing()),
UIndirectList<T>
(
lst.completeList(),
IndirectListAddressing::addressing()
)
{}
template<class T>
inline const Foam::UList<T>& Foam::IndirectList<T>::completeList() const
{
return completeList_;
}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
inline const Foam::List<Foam::label>& Foam::IndirectList<T>::addressing() const
inline const Foam::List<Foam::label>&
Foam::IndirectListAddressing::addressing() const
{
return addressing_;
}
template<class T>
inline void Foam::IndirectList<T>::resetAddressing
inline void Foam::IndirectListAddressing::resetAddressing
(
const UList<label>& addr
)
......@@ -117,8 +124,7 @@ inline void Foam::IndirectList<T>::resetAddressing
}
template<class T>
inline void Foam::IndirectList<T>::resetAddressing
inline void Foam::IndirectListAddressing::resetAddressing
(
const Xfer<List<label> >& addr
)
......@@ -127,63 +133,4 @@ inline void Foam::IndirectList<T>::resetAddressing
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
inline Foam::List<T> Foam::IndirectList<T>::operator()() const
{
List<T> result(size());
forAll(*this, i)
{
result[i] = operator[](i);
}
return result;
}
template<class T>
inline T& Foam::IndirectList<T>::operator[](const label i)
{
return completeList_[addressing_[i]];
}
template<class T>
inline const T& Foam::IndirectList<T>::operator[](const label i) const
{
return completeList_[addressing_[i]];
}
template<class T>
inline void Foam::IndirectList<T>::operator=(const UList<T>& ae)
{
if (addressing_.size() != ae.size())
{
FatalErrorIn("IndirectList<T>::operator=(const UList<T>&)")
<< "Addressing and list of addressed elements "
"have different sizes: "
<< addressing_.size() << " " << ae.size()
<< abort(FatalError);
}
forAll(addressing_, i)
{
completeList_[addressing_[i]] = ae[i];
}
}
template<class T>
inline void Foam::IndirectList<T>::operator=(const T& t)
{
forAll(addressing_, i)
{
completeList_[addressing_[i]] = t;
}
}
// ************************************************************************* //
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