ENH: add subsetList/inplaceSubsetList functions with unary predicate

- these are suitable for use with lambda functions.

- Deprecate the unused 3-parameter version of subset/inplaceSubset.

- Deprecate initList and initListList in favour of initializer_list

STYLE: adjust some comments, remove dead code in regionSizeDistribution.C
This commit is contained in:
Mark Olesen
2017-03-07 17:00:30 +01:00
parent 865f09e623
commit b7dc6d0441
6 changed files with 135 additions and 82 deletions

View File

@ -109,7 +109,21 @@ int main(int argc, char *argv[])
SubList<label> test5SubList(test5, 4, 3);
Info<< "List : " << test5 << endl;
inplaceReverseList(test5SubList);
Info<< "Reverse Sublist between 3 and 6 : " << test5 << endl;
Info<< "Reverse Sublist between 3 and 6 : " << test5 << nl << endl;
Info<< nl << "Test lambda predicates:" << nl << endl;
List<label> test6(identity(19));
// shift range for general testing
std::for_each(test6.begin(), test6.end(), [](label& x){ x -= 10; });
Info<< "Subset of non-zero, even values: "
<< subsetList
(
test6,
[](const label& x){ return x && !(x % 2); }
) << nl
<< endl;
Info<< "\nEnd\n" << endl;

View File

@ -28,7 +28,7 @@ License
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
const Foam::labelList Foam::emptyLabelList = Foam::labelList(0);
const Foam::labelList Foam::emptyLabelList;
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
@ -43,7 +43,7 @@ Foam::labelList Foam::invert
forAll(map, i)
{
label newPos = map[i];
const label newPos = map[i];
if (newPos >= 0)
{
@ -89,7 +89,7 @@ Foam::labelListList Foam::invertOneToMany
forAll(map, i)
{
label newI = map[i];
const label newI = map[i];
if (newI >= 0)
{
@ -105,10 +105,11 @@ Foam::labelList Foam::identity(const label len)
{
labelList map(len);
forAll(map, i)
for (label i = 0; i < len; ++i)
{
map[i] = i;
}
return map;
}

View File

@ -55,26 +55,27 @@ static const List<Type>& emptyList()
return *reinterpret_cast<const List<Type>*>(&emptyLabelList);
}
//- Renumber the values (not the indices) of a list.
// Negative ListType elements are left as is.
template<class ListType>
ListType renumber(const labelUList& oldToNew, const ListType&);
ListType renumber(const labelUList& oldToNew, const ListType& lst);
//- Inplace renumber the values of a list.
// Negative ListType elements are left as is.
template<class ListType>
void inplaceRenumber(const labelUList& oldToNew, ListType&);
void inplaceRenumber(const labelUList& oldToNew, ListType& lst);
//- Reorder the elements (indices, not values) of a list.
// Negative ListType elements are left as is.
template<class ListType>
ListType reorder(const labelUList& oldToNew, const ListType&);
ListType reorder(const labelUList& oldToNew, const ListType& lst);
//- Inplace reorder the elements of a list.
// Negative ListType elements are left as is.
template<class ListType>
void inplaceReorder(const labelUList& oldToNew, ListType&);
void inplaceReorder(const labelUList& oldToNew, ListType& lst);
// Variants to work with iterators and sparse tables.
@ -82,65 +83,81 @@ void inplaceReorder(const labelUList& oldToNew, ListType&);
//- Map values. Do not map negative values.
template<class Container>
void inplaceMapValue(const labelUList& oldToNew, Container&);
void inplaceMapValue(const labelUList& oldToNew, Container& lst);
//- Recreate with mapped keys. Do not map elements with negative key.
template<class Container>
void inplaceMapKey(const labelUList& oldToNew, Container&);
void inplaceMapKey(const labelUList& oldToNew, Container& lst);
//- Generate the (stable) sort order for the list
template<class T>
void sortedOrder(const UList<T>&, labelList& order);
void sortedOrder(const UList<T>& lst, labelList& order);
template<class T, class Cmp>
void sortedOrder(const UList<T>&, labelList& order, const Cmp& cmp);
void sortedOrder(const UList<T>& lst, labelList& order, const Cmp& cmp);
//- Generate (sorted) indices corresponding to duplicate list values
template<class T>
void duplicateOrder(const UList<T>&, labelList& order);
void duplicateOrder(const UList<T>& lst, labelList& order);
template<class T, class Cmp>
void duplicateOrder(const UList<T>&, labelList& order, const Cmp& cmp);
void duplicateOrder(const UList<T>& lst, labelList& order, const Cmp& cmp);
//- Generate (sorted) indices corresponding to unique list values
template<class T>
void uniqueOrder(const UList<T>&, labelList& order);
void uniqueOrder(const UList<T>& lst, labelList& order);
template<class T, class Cmp>
void uniqueOrder(const UList<T>&, labelList& order, const Cmp& cmp);
void uniqueOrder(const UList<T>& lst, labelList& order, const Cmp& cmp);
//- Extract elements of List when select is a certain value.
// eg, to extract all selected elements:
// subset<bool, labelList>(selectedElems, true, lst);
// \deprecated use subsetList instead (deprecated Mar 2017)
template<class T, class ListType>
ListType subset(const UList<T>& select, const T& value, const ListType&);
//- Inplace extract elements of List when select is a certain value.
// eg, to extract all selected elements:
// inplaceSubset<bool, labelList>(selectedElems, true, lst);
// \deprecated use inplaceSubsetList instead (deprecated Mar 2017)
template<class T, class ListType>
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);
// Note a labelHashSet could also be used for the bool-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&);
ListType subset(const BoolListType& select, const ListType& lst);
//- Inplace extract elements of List when select is true
// eg, to extract all selected elements:
// inplaceSubset<boolList, labelList>(selectedElems, lst);
// Note a labelHashSet could also be used for the bool-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&);
void inplaceSubset(const BoolListType& select, ListType& lst);
//- Copy a subset of the input list when predicate is true.
// Do not use FixedList for the input list, since it doesn't resize.
template<class ListType, class UnaryPredicate>
ListType subsetList(const ListType& input, UnaryPredicate pred);
//- Inplace subset of the list when predicate is true.
// Do not use FixedList for the input list, since it doesn't resize.
template<class ListType, class UnaryPredicate>
void inplaceSubsetList(ListType& input, UnaryPredicate pred);
//- Invert one-to-one map. Unmapped elements will be -1.
labelList invert(const label len, const labelUList&);
labelList invert(const label len, const labelUList& map);
//- Invert one-to-many map. Unmapped elements will be size 0.
labelListList invertOneToMany(const label len, const labelUList&);
labelListList invertOneToMany(const label len, const labelUList& map);
//- Invert many-to-many.
// Input and output types need to be inherited from List.
@ -156,7 +173,7 @@ List<OutList> invertManyToMany(const label len, const UList<InList>& in)
return out;
}
//- Create identity map (map[i] == i) of given length
//- Create identity map of the given length with (map[i] == i)
labelList identity(const label len);
//- Find first occurence of given element and return index,
@ -173,8 +190,8 @@ label findIndex
template<class ListType>
labelList findIndices
(
const ListType&,
typename ListType::const_reference,
const ListType& l,
typename ListType::const_reference t,
const label start=0
);
@ -182,9 +199,9 @@ labelList findIndices
template<class ListType>
void setValues
(
ListType&,
ListType& l,
const labelUList& indices,
typename ListType::const_reference
typename ListType::const_reference t
);
//- Opposite of findIndices: set values at indices to given value
@ -200,13 +217,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&, const label start=0);
label findMax(const ListType& l, 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&, const label start=0);
label findMin(const ListType& l, const label start=0);
//- Find first occurrence of given element in sorted list and return index,
@ -214,8 +231,8 @@ label findMin(const ListType&, const label start=0);
template<class ListType>
label findSortedIndex
(
const ListType&,
typename ListType::const_reference,
const ListType& l,
typename ListType::const_reference t,
const label start=0
);
@ -225,8 +242,8 @@ label findSortedIndex
template<class ListType, class BinaryOp>
label findLower
(
const ListType&,
typename ListType::const_reference,
const ListType& l,
typename ListType::const_reference t,
const label start,
const BinaryOp& bop
);
@ -237,20 +254,24 @@ label findLower
template<class ListType>
label findLower
(
const ListType&,
typename ListType::const_reference,
const ListType& l,
typename ListType::const_reference t,
const label start=0
);
//- To construct a List from a C array. Has extra Container type
// to initialise e.g. wordList from arrays of char*.
//
// \deprecated can often use initializer_list instead (deprecated Mar 2017)
template<class Container, class T, int mRows>
List<Container> initList(const T[mRows]);
//- To construct a (square) ListList from a C array. Has extra Container type
// to initialise e.g. faceList from arrays of labels.
//
// \deprecated can often use initializer_list instead (deprecated Mar 2017)
template<class Container, class T, int mRows, int nColumns>
List<Container> initListList(const T[mRows][nColumns]);

View File

@ -3,7 +3,7 @@
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | Copyright (C) 2011-2016 OpenFOAM Foundation
\\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd.
\\/ M anipulation | Copyright (C) 2015-2017 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -34,11 +34,8 @@ ListType Foam::renumber
const ListType& lst
)
{
// Create copy
ListType newLst(lst.size());
// ensure consistent addressable size (eg, DynamicList)
newLst.setSize(lst.size());
newLst.setSize(lst.size()); // Consistent sizing (eg, DynamicList)
forAll(lst, elemI)
{
@ -76,11 +73,8 @@ ListType Foam::reorder
const ListType& lst
)
{
// Create copy
ListType newLst(lst.size());
// ensure consistent addressable size (eg, DynamicList)
newLst.setSize(lst.size());
newLst.setSize(lst.size()); // Consistent sizes (eg, DynamicList)
forAll(lst, elemI)
{
@ -104,11 +98,8 @@ void Foam::inplaceReorder
ListType& lst
)
{
// Create copy
ListType newLst(lst.size());
// ensure consistent addressable size (eg, DynamicList)
newLst.setSize(lst.size());
newLst.setSize(lst.size()); // Consistent sizing (eg, DynamicList)
forAll(lst, elemI)
{
@ -303,9 +294,7 @@ ListType Foam::subset
}
ListType newLst(lst.size());
// ensure consistent addressable size (eg, DynamicList)
newLst.setSize(lst.size());
newLst.setSize(lst.size()); // Consistent sizing (eg, DynamicList)
label nElem = 0;
forAll(lst, elemI)
@ -366,9 +355,7 @@ ListType Foam::subset
// eg, when it is a PackedBoolList or a labelHashSet
ListType newLst(lst.size());
// ensure consistent addressable size (eg, DynamicList)
newLst.setSize(lst.size());
newLst.setSize(lst.size()); // Consistent sizing (eg, DynamicList)
label nElem = 0;
forAll(lst, elemI)
@ -411,6 +398,54 @@ void Foam::inplaceSubset
}
template<class ListType, class UnaryPredicate>
ListType Foam::subsetList
(
const ListType& lst,
UnaryPredicate pred
)
{
ListType newLst(lst.size());
newLst.setSize(lst.size()); // Consistent sizing (eg, DynamicList)
label nElem = 0;
forAll(lst, elemI)
{
if (pred(lst[elemI]))
{
newLst[nElem++] = lst[elemI];
}
}
newLst.setSize(nElem);
return newLst;
}
template<class ListType, class UnaryPredicate>
void Foam::inplaceSubsetList
(
ListType& lst,
UnaryPredicate pred
)
{
label nElem = 0;
forAll(lst, elemI)
{
if (pred(lst[elemI]))
{
if (nElem != elemI)
{
lst[nElem] = lst[elemI];
}
++nElem;
}
}
lst.setSize(nElem);
}
template<class InList, class OutList>
void Foam::invertManyToMany
(

View File

@ -45,7 +45,7 @@ SourceFiles
namespace Foam
{
// single-string matches:
// Single-string matches:
//- Return true if string matches one of the regular expressions
inline bool findStrings
@ -57,14 +57,14 @@ namespace Foam
return matcher.match(str);
}
// multi-string matches:
// Multi-string matches:
//- Return list indices for matching strings
template<class Matcher, class StringType>
labelList findMatchingStrings
(
const Matcher&,
const UList<StringType>&,
const Matcher& matcher,
const UList<StringType>& lst,
const bool invert=false
);
@ -92,7 +92,7 @@ namespace Foam
)
{
const regExp re(rePattern);
return findStrings(re, lst, invert);
return findMatchingStrings(re, lst, invert);
}
//- Return list indices for strings matching the regular expression

View File

@ -42,24 +42,6 @@ namespace Foam
dictionary
);
}
//- Plus op for FixedList<scalar>
template<class T, unsigned Size>
class ListPlusEqOp
{
public:
void operator()
(
FixedList<T, Size>& x,
const FixedList<T, Size>& y
) const
{
forAll(x, i)
{
x[i] += y[i];
}
}
};
}