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

ENH: improve consistency of ListOps and stringListOps

- subsetList, inplaceSubsetList with optional inverted logic.

- use moveable elements where possible.

- allow optional starting offset for the identity global function.
  Eg,  'identity(10, start)' vs 'identity(10) + start'
parent 3ee2f329
......@@ -37,6 +37,12 @@ Description
using namespace Foam;
void printCompare(const face& a, const face& b)
{
Info<< "Compare " << a << " and " << b
<< " Match = " << face::compare(a, b) << endl;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
// Main program:
......@@ -96,44 +102,35 @@ int main(int argc, char *argv[])
Info<< nl << nl << "Compare two faces: " << endl;
face a(identity(5));
Info<< "Compare " << a << " and " << a << " Match = " << face::compare(a, a)
<< endl;
printCompare(a, a);
face b(reverseList(a));
Info<< "Compare " << a << " and " << b << " Match = " << face::compare(a, b)
<< endl;
printCompare(a, b);
face c(a);
c[4] = 3;
Info<< "Compare " << a << " and " << c << " Match = " << face::compare(a, c)
<< endl;
printCompare(a, c);
face d(rotateList(a, 2));
Info<< "Compare " << a << " and " << d << " Match = " << face::compare(a, d)
<< endl;
printCompare(a, d);
face g(labelList(5, 1));
face h(g);
Info<< "Compare " << g << " and " << h << " Match = " << face::compare(g, h)
<< endl;
printCompare(g, h);
g[0] = 2;
h[3] = 2;
Info<< "Compare " << g << " and " << h << " Match = " << face::compare(g, h)
<< endl;
printCompare(g, h);
g[4] = 3;
h[4] = 3;
Info<< "Compare " << g << " and " << h << " Match = " << face::compare(g, h)
<< endl;
printCompare(g, h);
face face1(identity(1));
Info<< "Compare " << face1 << " and " << face1
<< " Match = " << face::compare(face1, face1) << endl;
printCompare(face1, face1);
face face2(identity(1)+1);
Info<< "Compare " << face1 << " and " << face2
<< " Match = " << face::compare(face1, face2) << endl;
face face2(identity(1, 2));
printCompare(face1, face2);
Info<< nl << nl << "Zero face" << nl << endl;
......
......@@ -261,7 +261,7 @@ int main(int argc, char *argv[])
// (
// mesh,
// pp,
// identity(pp.size())+pp.start()
// identity(pp.size(), pp.start())
// )
// );
// forAll(pn, pointi)
......@@ -278,7 +278,7 @@ int main(int argc, char *argv[])
// (
// mesh,
// pp,
// identity(pp.size())+pp.start()
// identity(pp.size(), pp.start())
// )
// );
// forAll(pn, pointi)
......
......@@ -247,7 +247,7 @@ int main(int argc, char *argv[])
<< " by this amount" << nl << endl;
patch1Map = identity(surface1.patches().size());
patch2Map = identity(surface2.patches().size()) + patch1Map.size();
patch2Map = identity(surface2.patches().size(), patch1Map.size());
nNewPatches = surface1.patches().size()+surface2.patches().size();
}
......
......@@ -165,10 +165,10 @@ void createBoundaryEdgeTrees
// Boundary edges
treeBoundaryEdges[surfI] =
labelList
identity
(
identity(surf.nEdges() - surf.nInternalEdges())
+ surf.nInternalEdges()
surf.nEdges() - surf.nInternalEdges(),
surf.nInternalEdges()
);
Random rndGen(17301893);
......
......@@ -84,14 +84,7 @@ Foam::scalar Foam::Distribution<Type>::totalWeight(direction cmpt) const
template<class Type>
Foam::List<Foam::label> Foam::Distribution<Type>::keys(direction cmpt) const
{
List<label> keys = identity((*this)[cmpt].size());
forAll(keys, k)
{
keys[k] += listStarts_[cmpt];
}
return keys;
return identity((*this)[cmpt].size(), listStarts_[cmpt]);
}
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation
\\/ M anipulation |
\\/ M anipulation | Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -42,22 +42,23 @@ Foam::labelList Foam::invert
forAll(map, i)
{
const label newPos = map[i];
const label newIdx = map[i];
if (newPos >= 0)
if (newIdx >= 0)
{
if (inverse[newPos] >= 0)
if (inverse[newIdx] >= 0)
{
FatalErrorInFunction
<< "Map is not one-to-one. At index " << i
<< " element " << newPos << " has already occurred before"
<< " element " << newIdx << " has already occurred before"
<< nl << "Please use invertOneToMany instead"
<< abort(FatalError);
}
inverse[newPos] = i;
inverse[newIdx] = i;
}
}
return inverse;
}
......@@ -68,31 +69,31 @@ Foam::labelListList Foam::invertOneToMany
const labelUList& map
)
{
labelList nElems(len, 0);
labelList sizes(len, 0);
forAll(map, i)
for (const label newIdx : map)
{
if (map[i] >= 0)
if (newIdx >= 0)
{
nElems[map[i]]++;
sizes[newIdx]++;
}
}
labelListList inverse(len);
forAll(nElems, i)
for (label i=0; i<len; ++i)
{
inverse[i].setSize(nElems[i]);
nElems[i] = 0;
inverse[i].setSize(sizes[i]);
sizes[i] = 0; // reset size counter
}
forAll(map, i)
{
const label newI = map[i];
const label newIdx = map[i];
if (newI >= 0)
if (newIdx >= 0)
{
inverse[newI][nElems[newI]++] = i;
inverse[newIdx][sizes[newIdx]++] = i;
}
}
......@@ -100,13 +101,13 @@ Foam::labelListList Foam::invertOneToMany
}
Foam::labelList Foam::identity(const label len)
Foam::labelList Foam::identity(const label len, const label start)
{
labelList map(len);
for (label i = 0; i < len; ++i)
{
map[i] = i;
map[i] = i + start;
}
return map;
......
......@@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2017 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2017 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2017-2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
......@@ -59,12 +59,12 @@ static const List<Type>& emptyList()
//- Renumber the values (not the indices) of a list.
// Negative ListType elements are left untouched.
template<class ListType>
ListType renumber(const labelUList& oldToNew, const ListType& lst);
ListType renumber(const labelUList& oldToNew, const ListType& input);
//- Inplace renumber the values (not the indices) of a list.
// Negative ListType elements are left untouched.
template<class ListType>
void inplaceRenumber(const labelUList& oldToNew, ListType& lst);
void inplaceRenumber(const labelUList& oldToNew, ListType& input);
//- Reorder the elements of a list.
......@@ -76,7 +76,7 @@ template<class ListType>
ListType reorder
(
const labelUList& oldToNew,
const ListType& lst,
const ListType& input,
const bool prune = false
);
......@@ -89,7 +89,7 @@ template<class ListType>
void inplaceReorder
(
const labelUList& oldToNew,
ListType& lst,
ListType& input,
const bool prune = false
);
......@@ -99,22 +99,22 @@ void inplaceReorder
//- Map values. Do not map negative values.
template<class Container>
void inplaceMapValue(const labelUList& oldToNew, Container& lst);
void inplaceMapValue(const labelUList& oldToNew, Container& input);
//- Recreate with mapped keys. Do not map elements with negative key.
template<class Container>
void inplaceMapKey(const labelUList& oldToNew, Container& lst);
void inplaceMapKey(const labelUList& oldToNew, Container& input);
//- Generate the (stable) sort order for the list
template<class T>
void sortedOrder(const UList<T>& lst, labelList& order);
void sortedOrder(const UList<T>& input, labelList& order);
//- Sort using specified list compare predicate
template<class T, class ListComparePredicate>
void sortedOrder
(
const UList<T>& lst,
const UList<T>& input,
labelList& order,
const ListComparePredicate& comp
);
......@@ -122,14 +122,14 @@ void sortedOrder
//- Generate (sorted) indices corresponding to duplicate list values
template<class T>
void duplicateOrder(const UList<T>& lst, labelList& order);
void duplicateOrder(const UList<T>& input, labelList& order);
//- Generate (sorted) indices corresponding to duplicate list values
// sort using specified list compare predicate
template<class T, class ListComparePredicate>
void duplicateOrder
(
const UList<T>& lst,
const UList<T>& input,
labelList& order,
const ListComparePredicate& comp
);
......@@ -137,14 +137,14 @@ void duplicateOrder
//- Generate (sorted) indices corresponding to unique list values
template<class T>
void uniqueOrder(const UList<T>& lst, labelList& order);
void uniqueOrder(const UList<T>& input, labelList& order);
//- Generate (sorted) indices corresponding to unique list values
// sort using specified list compare predicate
template<class T, class ListComparePredicate>
void uniqueOrder
(
const UList<T>& lst,
const UList<T>& input,
labelList& order,
const ListComparePredicate& comp
);
......@@ -153,14 +153,14 @@ void uniqueOrder
//- Inplace sorting and removal of duplicates.
// Do not use FixedList for the input list, since it doesn't resize.
template<class ListType>
void inplaceUniqueSort(ListType& lst);
void inplaceUniqueSort(ListType& input);
//- Inplace sorting and removal of duplicates.
// Do not use FixedList for the input list, since it doesn't resize.
template<class ListType, class ListComparePredicate>
void inplaceUniqueSort
(
ListType& lst,
ListType& input,
const ListComparePredicate& comp
);
......@@ -178,19 +178,19 @@ void inplaceSubset(const UList<T>& select, const T& value, ListType&);
//- Extract elements of List when select is true
// eg, to extract all selected elements:
// subset<boolList, labelList>(selectedElems, lst);
// subset<boolList, labelList>(selectedElems, list);
// Note a labelHashSet can also be used as the bool-list.
// Do not use FixedList for the input list, since it doesn't resize.
template<class BoolListType, class ListType>
ListType subset(const BoolListType& select, const ListType& lst);
ListType subset(const BoolListType& select, const ListType& input);
//- Inplace extract elements of List when select is true
// eg, to extract all selected elements:
// inplaceSubset<boolList, labelList>(selectedElems, lst);
// inplaceSubset<boolList, labelList>(selectedElems, list);
// Note a labelHashSet can also be used as the bool-list.
// Do not use FixedList for the input list, since it doesn't resize.
template<class BoolListType, class ListType>
void inplaceSubset(const BoolListType& select, ListType& lst);
void inplaceSubset(const BoolListType& select, ListType& input);
//- Copy a subset of the input list when predicate is true.
......@@ -199,7 +199,8 @@ template<class ListType, class UnaryPredicate>
ListType subsetList
(
const ListType& input,
const UnaryPredicate& pred
const UnaryPredicate& pred,
const bool invert=false
);
//- Inplace subset of the list when predicate is true.
......@@ -208,7 +209,8 @@ template<class ListType, class UnaryPredicate>
void inplaceSubsetList
(
ListType& input,
const UnaryPredicate& pred
const UnaryPredicate& pred,
const bool invert=false
);
......@@ -242,7 +244,8 @@ List<OutputIntListType> invertManyToMany
}
//- Create identity map of the given length with (map[i] == i)
labelList identity(const label len);
// Optionally with an alternative start index, so that (map[i] == i+start)
labelList identity(const label len, const label start=0);
//- Find first occurence of given element and return index,
// return -1 if not found. Linear search.
......@@ -265,23 +268,23 @@ labelList findIndices
(
const ListType& input,
typename ListType::const_reference val,
const label start=0
label start=0
);
//- Opposite of findIndices: set values at indices to given value
template<class ListType>
void setValues
(
ListType& l,
ListType& input,
const labelUList& indices,
typename ListType::const_reference t
typename ListType::const_reference val
);
//- Opposite of findIndices: set values at indices to given value
template<class ListType>
ListType createWithValues
(
const label sz,
const label len,
typename ListType::const_reference initValue,
const labelUList& indices,
typename ListType::const_reference setValue
......@@ -290,13 +293,13 @@ ListType createWithValues
//- Find index of max element (and larger than given element).
// return -1 if not found. Linear search.
template<class ListType>
label findMax(const ListType& l, const label start=0);
label findMax(const ListType& input, const label start=0);
//- Find index of min element (and less than given element).
// return -1 if not found. Linear search.
template<class ListType>
label findMin(const ListType& l, const label start=0);
label findMin(const ListType& input, const label start=0);
//- Find first occurrence of given element in sorted list and return index,
......@@ -304,21 +307,21 @@ label findMin(const ListType& l, const label start=0);
template<class ListType>
label findSortedIndex
(
const ListType& l,
typename ListType::const_reference t,
const ListType& input,
typename ListType::const_reference val,
const label start=0
);
//- Find last element < given value in sorted list and return index,
// return -1 if not found. Binary search.
template<class ListType, class BinaryOp>
template<class ListType, class BinaryPredicate>
label findLower
(
const ListType& l,
typename ListType::const_reference t,
const ListType& input,
typename ListType::const_reference val,
const label start,
const BinaryOp& bop
const BinaryPredicate& pred
);
......@@ -327,8 +330,8 @@ label findLower
template<class ListType>
label findLower
(
const ListType& l,
typename ListType::const_reference t,
const ListType& input,
typename ListType::const_reference val,
const label start=0
);
......@@ -369,12 +372,12 @@ public:
//- Reverse a list. First element becomes last element etc.
template<class ListType>
ListType reverseList(const ListType& list);
ListType reverseList(const ListType& input);
//- Inplace reversal of a list using Swap.
template<class ListType>
void inplaceReverseList(ListType& list);
void inplaceReverseList(ListType& input);
//- Rotate a list by n places. If n is positive rotate clockwise/right/down.
......
......@@ -369,7 +369,7 @@ inline void Foam::UList<T>::swap(UList<T>& lst)
template<class T>
inline void Foam::reverse(UList<T>& lst, const label n)
{
for (int i=0; i<n/2; ++i)
for (label i=0; i<n/2; ++i)
{
Foam::Swap(lst[i], lst[n-1-i]);
}
......
......@@ -54,7 +54,7 @@ namespace Foam
labelList findMatchingStrings
(
const UnaryMatchPredicate& matcher,
const UList<StringType>& lst,
const UList<StringType>& input,
const bool invert=false
);
......@@ -65,11 +65,11 @@ namespace Foam
labelList findStrings
(
const regExp& matcher,
const UList<StringType>& lst,
const UList<StringType>& input,
const bool invert=false
)
{
return findMatchingStrings(matcher, lst, invert);
return findMatchingStrings(matcher, input, invert);
}
......@@ -79,11 +79,11 @@ namespace Foam
labelList findStrings
(
const char* re,
const UList<StringType>& lst,
const UList<StringType>& input,
const bool invert=false
)
{
return findMatchingStrings(regExp(re), lst, invert);
return findMatchingStrings(regExp(re), input, invert);
}
......@@ -93,11 +93,11 @@ namespace Foam
labelList findStrings
(
const std::string& re,
const UList<StringType>& lst,
const UList<StringType>& input,
const bool invert=false
)
{
return findMatchingStrings(regExp(re), lst, invert);
return findMatchingStrings(regExp(re), input, invert);
}
......@@ -107,11 +107,11 @@ namespace Foam
labelList findStrings
(
const wordRe& matcher,
const UList<StringType>& lst,
const UList<StringType>& input,
const bool invert=false
)
{
return findMatchingStrings(matcher, lst, invert);
return findMatchingStrings(matcher, input, invert);
}
......@@ -121,11 +121,11 @@ namespace Foam
labelList findStrings
(
const wordRes& matcher,
const UList<StringType>& lst,
const UList<StringType>& input,
const bool invert=false
)
{
return findMatchingStrings(matcher, lst, invert);
return findMatchingStrings(matcher, input, invert);
}
//- Return list indices for strings matching one of the regular expression
......@@ -148,13 +148,13 @@ namespace Foam
// optionally invert the match
// eg, to extract all selected elements:
// \code
// subsetMatchingStrings<regExp, stringList>(myRegExp, lst);
// subsetMatchingStrings<regExp, stringList>(myRegExp, list);
// \endcode
template<class UnaryMatchPredicate, class StringListType>
StringListType subsetMatchingStrings
(
const UnaryMatchPredicate& matcher,
const StringListType& lst,
const StringListType& input,
const bool invert=false
);
......@@ -165,11 +165,11 @@ namespace Foam
StringListType subsetStrings
(
const regExp& matcher,
const StringListType& lst,
const StringListType& input,
const bool invert=false
)
{
return subsetMatchingStrings(matcher, lst, invert);
return subsetMatchingStrings(matcher, input, invert);
}
......@@ -179,11 +179,11 @@ namespace Foam
StringListType subsetStrings
(