Commit e3ae44f3 authored by laurence's avatar laurence
Browse files

ENH: ListOps: Add reverse and rotate functions

reverseList
inplaceReverseList
rotateList
inplaceRotateList

Remove reverse function from UList and call the ListOps from the other
reverse function.
parent 23f04e33
Test-ListOps.C
EXE = $(FOAM_USER_APPBIN)/Test-ListOps
EXE_INC = /*-DFULLDEBUG -O0 -g*/ \
EXE_LIBS = \
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2012 OpenFOAM Foundation
\\/ 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/>.
Application
Test-ListOps
Description
\*---------------------------------------------------------------------------*/
#include "argList.H"
#include "List.H"
#include "SubList.H"
#include "ListOps.H"
#include "face.H"
using namespace Foam;
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
int main(int argc, char *argv[])
{
Info<< "Test Rotations:" << nl << endl;
List<label> forwardRotate(identity(5));
face testFace(identity(4));
for (label i = 0; i < 8; ++i)
{
Info<< "Rotate forward by " << i << " : "
<< rotateList(forwardRotate, i) << endl;
}
for (label i = 0; i < 8; ++i)
{
Info<< "Rotate backward by " << i << " : "
<< rotateList(forwardRotate, -i) << endl;
}
Info<< nl << "Face : " << testFace << endl;
Info<< "Rotate by 2 : " << rotateList(testFace, 2) << endl;
inplaceRotateList(testFace, -6);
Info<< "Rotate inplace by -6 : " << testFace << nl << endl;
Info<< "Test inplace rotate : " << forwardRotate << endl;
inplaceRotateList(forwardRotate, 2);
Info<< "Rotate to the right by 2 : " << forwardRotate << endl;
inplaceRotateList(forwardRotate, -2);
Info<< "Rotate to the left by 2 : " << forwardRotate << endl;
List<label> subRotate(identity(10));
SubList<label> subL(subRotate, 5, 3);
Info<< "Test inplace rotate on sublist : " << subRotate << endl;
inplaceRotateList(subL, 3);
Info<< "Rotate to the right by 3 : " << subRotate << endl;
inplaceRotateList(subL, -8);
Info<< "Rotate to the left by 3 : " << subRotate << endl;
Info<< nl << nl << "Test Reversing:" << nl << endl;
Info<< "List : " << identity(5) << endl;
Info<< "Reverse : " << reverseList(identity(5)) << endl;
Info<< "List : " << identity(6) << endl;
Info<< "Reverse : " << reverseList(identity(6)) << nl << endl;
List<label> test1(identity(5));
Info<< "List : " << test1 << endl;
inplaceReverseList(test1);
Info<< "Inplace Reverse : " << test1 << nl << endl;
List<label> test2(identity(6));
Info<< "List : " << test2 << endl;
inplaceReverseList(test2);
Info<< "Inplace Reverse : " << test2 << nl << endl;
face test3(identity(6));
Info<< "Face : " << test3 << endl;
inplaceReverseList(test3);
Info<< "Inplace Reverse : " << test3 << nl << endl;
FixedList<label, 6> test4(identity(6));
Info<< "FixedList : " << test4 << endl;
inplaceReverseList(test4);
Info<< "Inplace Reverse : " << test4 << nl << endl;
List<label> test5(identity(9));
SubList<label> test5SubList(test5, 4, 3);
Info<< "List : " << test5 << endl;
inplaceReverseList(test5SubList);
Info<< "Reverse Sublist between 3 and 6 : " << test5 << endl;
Info<< "\nEnd\n" << endl;
return 0;
}
// ************************************************************************* //
......@@ -254,6 +254,27 @@ public:
};
//- Reverse a list. First element becomes last element etc.
template <class ListType>
ListType reverseList(const ListType& list);
//- Inplace reversal of a list using Swap.
template <class ListType>
void inplaceReverseList(ListType& list);
//- Rotate a list by n places. If n is positive rotate clockwise/right/down.
// If n is negative rotate anti-clockwise/left/up.
template <class ListType>
ListType rotateList(const ListType& list, const label n);
//- Inplace reversal of a list using the Reversal Block Swapping algorithm.
template <template <typename> class ListType, class DataType>
void inplaceRotateList(ListType<DataType>& list, label n);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
......
......@@ -706,4 +706,83 @@ void Foam::ListAppendEqOp<T>::operator()(List<T>& x, const List<T>& y) const
}
template <class ListType>
ListType Foam::reverseList(const ListType& list)
{
const label listSize = list.size();
const label lastIndex = listSize - 1;
ListType tmpList(listSize);
forAll(tmpList, elemI)
{
tmpList[elemI] = list[lastIndex - elemI];
}
return tmpList;
}
template <class ListType>
void Foam::inplaceReverseList(ListType& list)
{
const label listSize = list.size();
const label lastIndex = listSize - 1;
const label nIterations = listSize >> 1;
label elemI = 0;
while (elemI < nIterations)
{
Swap(list[elemI], list[lastIndex - elemI]);
elemI++;
}
}
template <class ListType>
ListType Foam::rotateList(const ListType& list, const label n)
{
const label listSize = list.size();
ListType tmpList(listSize);
forAll(tmpList, elemI)
{
label index = (elemI - n) % listSize;
if (index < 0)
{
index += listSize;
}
tmpList[elemI] = list[index];
}
return tmpList;
}
template <template <typename> class ListType, class DataType>
void Foam::inplaceRotateList(ListType<DataType>& list, label n)
{
const label listSize = list.size();
n = (listSize - n) % listSize;
if (n < 0)
{
n += listSize;
}
SubList<DataType> firstHalf(list, n, 0);
SubList<DataType> secondHalf(list, listSize - n, n);
inplaceReverseList(firstHalf);
inplaceReverseList(secondHalf);
inplaceReverseList(list);
}
// ************************************************************************* //
......@@ -348,10 +348,6 @@ void stableSort(UList<T>&, const Cmp&);
template<class T>
void shuffle(UList<T>&);
// Reverse the first n elements of the list
template<class T>
inline void reverse(UList<T>&, const label n);
// Reverse all the elements of the list
template<class T>
inline void reverse(UList<T>&);
......
......@@ -25,6 +25,7 @@ License
#include "error.H"
#include "pTraits.H"
#include "ListOps.H"
#include "Swap.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
......@@ -318,19 +319,10 @@ inline bool Foam::UList<T>::empty() const
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class T>
inline void Foam::reverse(UList<T>& ul, const label n)
{
for (int i=0; i<n/2; i++)
{
Swap(ul[i], ul[n-1-i]);
}
}
template<class T>
inline void Foam::reverse(UList<T>& ul)
{
reverse(ul, ul.size());
inplaceReverseList(ul);
}
......
Supports Markdown
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