ENH: relocate sortedOrder from ListOps.H to List.H

- commonly used, only depends on routines defined in UList
  (don't need the rest of ListOps for it).

ENH: implement boolList::operator() const

- allows use as a predicate functor, as per bitSet and labelHashSet

GIT: combine SubList, UList into List directory (intertwined concepts)

STYLE: default initialize DynamicList instead of with size 0
This commit is contained in:
Mark Olesen 2022-05-04 00:33:53 +02:00
parent cb6f908798
commit bf0b3d8872
26 changed files with 200 additions and 147 deletions

View File

@ -157,18 +157,34 @@ int main(int argc, char *argv[])
{
boolList list2(5, true);
list2.unset(2);
list2.unset(3);
Info<< "Test wrapper idea" << nl;
bitSetOrBoolList wrapper(list2);
if (wrapper.test(1))
// Use predicate, or test() method
if (list2(1))
{
Info<< "1 is on" << nl;
Info<< "1 is on (original)" << nl;
}
if (!wrapper.test(2))
if (!list2(2))
{
Info<< "2 is off" << nl;
Info<< "2 is off (original)" << nl;
}
if (!list2(100))
{
Info<< "100 is off (original)" << nl;
}
if (wrapper(1))
{
Info<< "1 is on (wrapped)" << nl;
}
if (!wrapper(2))
{
Info<< "2 is off (wrapped)" << nl;
}
}

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2020 OpenCFD Ltd.
Copyright (C) 2020-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -32,8 +32,8 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef bitSetOrBoolList_H
#define bitSetOrBoolList_H
#ifndef Foam_bitSetOrBoolList_H
#define Foam_bitSetOrBoolList_H
#include "bitSet.H"
#include "boolList.H"
@ -88,6 +88,13 @@ public:
{
return bits_.test(i) || bools_.test(i);
}
//- Test predicate
bool operator()(const label i) const
{
// Can also use test(i) etc...
return bits_(i) || bools_(i);
}
};

View File

@ -41,8 +41,8 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef disabledBoolList_H
#define disabledBoolList_H
#ifndef Foam_disabledBoolList_H
#define Foam_disabledBoolList_H
/*---------------------------------------------------------------------------*\
Class disabledBoolList Declaration
@ -60,6 +60,9 @@ struct disabledBoolList
bool test(int) const { return true; }
void set(bool) {}
// Perhaps not?
/// bool operator()(const label i) const { return true; }
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -31,7 +31,7 @@ Description
#include "string.H"
#include "macros.H"
#include "IOstreams.H"
#include "UList.H"
#include "List.H"
#include "HashSet.H"
#include <typeinfo>

View File

@ -163,9 +163,8 @@ void Foam::conformalVoronoiMesh::calcTetMesh
label nPatches = patchNames.size();
List<DynamicList<face>> patchFaces(nPatches, DynamicList<face>(0));
List<DynamicList<label>> patchOwners(nPatches, DynamicList<label>(0));
List<DynamicList<face>> patchFaces(nPatches);
List<DynamicList<label>> patchOwners(nPatches);
faces.setSize(number_of_finite_facets());
@ -1711,12 +1710,11 @@ void Foam::conformalVoronoiMesh::createFacesOwnerNeighbourAndPatches
patchDicts[patchi].getOrDefault<label>("neighbProcNo", -1);
}
List<DynamicList<face>> patchFaces(nPatches, DynamicList<face>(0));
List<DynamicList<label>> patchOwners(nPatches, DynamicList<label>(0));
List<DynamicList<face>> patchFaces(nPatches);
List<DynamicList<label>> patchOwners(nPatches);
// Per patch face the index of the slave node of the point pair
List<DynamicList<label>> patchPPSlaves(nPatches, DynamicList<label>(0));
List<DynamicList<bool>> indirectPatchFace(nPatches, DynamicList<bool>(0));
List<DynamicList<label>> patchPPSlaves(nPatches);
List<DynamicList<bool>> indirectPatchFace(nPatches);
faces.setSize(number_of_finite_edges());

View File

@ -32,8 +32,8 @@ License
#include "JobInfo.H"
#include "OSspecific.H"
#include "IOstreams.H"
#include "List.H"
#include "Switch.H"
#include "UList.H"
#include <float.h> // For *fp functions
#include <limits>

View File

@ -31,8 +31,8 @@ License
#include "JobInfo.H"
#include "OSspecific.H"
#include "IOstreams.H"
#include "List.H"
#include "Switch.H"
#include "UList.H"
#include <limits>

View File

@ -25,7 +25,7 @@ License
\*---------------------------------------------------------------------------*/
#include "ListOps.H"
#include "ListOps.H" // For identity
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //

View File

@ -636,4 +636,52 @@ void Foam::List<T>::operator=(SLList<T>&& list)
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class T>
Foam::labelList Foam::sortedOrder
(
const UList<T>& list
)
{
labelList order;
Foam::sortedOrder(list, order, typename UList<T>::less(list));
return order;
}
template<class T>
void Foam::sortedOrder
(
const UList<T>& list,
labelList& order
)
{
Foam::sortedOrder(list, order, typename UList<T>::less(list));
}
template<class T, class ListComparePredicate>
void Foam::sortedOrder
(
const UList<T>& list,
labelList& order,
const ListComparePredicate& comp
)
{
// List lengths must be identical. Old content is overwritten
order.resize_nocopy(list.size());
// Same as std::iota and ListOps::identity
label value = 0;
for (label& item : order)
{
item = value;
++value;
}
Foam::stableSort(order, comp);
}
// ************************************************************************* //

View File

@ -373,10 +373,27 @@ Istream& operator>>(Istream& is, List<T>& list)
// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
//- Create identity map of the given length with (map[i] == i)
//- Return an identity map of the given length with (map[i] == i)
// Optionally with an alternative start index, so that (map[i] == i+start)
labelList identity(const label len, label start=0);
//- Return the (stable) sort order for the list
template<class T>
labelList sortedOrder(const UList<T>& input);
//- Generate the (stable) sort order for the list
template<class T>
void sortedOrder(const UList<T>& input, labelList& order);
//- Sort using specified list compare predicate
template<class T, class ListComparePredicate>
void sortedOrder
(
const UList<T>& input,
labelList& order,
const ListComparePredicate& comp
);
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -39,11 +39,10 @@ SourceFiles
\*---------------------------------------------------------------------------*/
#ifndef SubList_H
#define SubList_H
#ifndef Foam_SubList_H
#define Foam_SubList_H
#include "List.H"
#include "FixedList.H"
#include "labelRange.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -52,6 +51,7 @@ namespace Foam
{
// Forward Declarations
template<class T, unsigned N> class FixedList;
template<class T> class SubList;
// Common list types

View File

@ -26,6 +26,8 @@ License
\*---------------------------------------------------------------------------*/
#include "FixedList.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2016 OpenFOAM Foundation
Copyright (C) 2017-2021 OpenCFD Ltd.
Copyright (C) 2017-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -257,41 +257,6 @@ Foam::label Foam::UList<T>::rfind(const T& val, label pos) const
}
template<class T>
void Foam::sort(UList<T>& a)
{
std::sort(a.begin(), a.end());
}
template<class T, class Compare>
void Foam::sort(UList<T>& a, const Compare& comp)
{
std::sort(a.begin(), a.end(), comp);
}
template<class T>
void Foam::stableSort(UList<T>& a)
{
std::stable_sort(a.begin(), a.end());
}
template<class T, class Compare>
void Foam::stableSort(UList<T>& a, const Compare& comp)
{
std::stable_sort(a.begin(), a.end(), comp);
}
template<class T>
void Foam::shuffle(UList<T>& a)
{
std::shuffle(a.begin(), a.end(), std::default_random_engine());
}
// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
template<class T>
@ -371,4 +336,41 @@ bool Foam::UList<T>::operator>=(const UList<T>& list) const
}
// * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * //
template<class T>
void Foam::sort(UList<T>& list)
{
std::sort(list.begin(), list.end());
}
template<class T, class Compare>
void Foam::sort(UList<T>& list, const Compare& comp)
{
std::sort(list.begin(), list.end(), comp);
}
template<class T>
void Foam::stableSort(UList<T>& list)
{
std::stable_sort(list.begin(), list.end());
}
template<class T, class Compare>
void Foam::stableSort(UList<T>& list, const Compare& comp)
{
std::stable_sort(list.begin(), list.end(), comp);
}
template<class T>
void Foam::shuffle(UList<T>& list)
{
std::shuffle(list.begin(), list.end(), std::default_random_engine());
}
// ************************************************************************* //

View File

@ -74,6 +74,7 @@ class labelRange;
template<class T> class List;
template<class T> class SubList;
template<class T> class UList;
template<class T> class IndirectList;
template<class T, class Addr> class IndirectListBase;
template<class T> Istream& operator>>(Istream&, UList<T>&);
@ -510,9 +511,19 @@ public:
// Special Methods
//- A bitSet::test() method for a list of bool
//
// \return The element value, or false for out-of-range access
//- Test \c bool value at specified position,
//- always false for out-of-range access. Same as test().
// \note Predicate definition as per bitSet, HashSet
template<class TypeT = T>
typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
inline operator()(const label i) const
{
return (i >= 0 && i < size_ && v_[i]);
}
//- Test \c bool value at specified position,
//- always false for out-of-range access.
// \note Method name compatibility with bitSet, HashSet
template<class TypeT = T>
typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
inline test(const label i) const
@ -520,9 +531,9 @@ public:
return (i >= 0 && i < size_ && v_[i]);
}
//- A bitSet::get() method for a list of bool
//
// \return The element value, or false for out-of-range access
//- Return \c bool value at specified position,
//- always false for out-of-range access.
// \note Method name compatibility with bitSet
template<class TypeT = T>
typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
inline get(const label i) const
@ -530,9 +541,10 @@ public:
return (i >= 0 && i < size_ && v_[i]);
}
//- A bitSet::unset() method for a list of bool
//
//- Unset the \c bool entry at specified position,
//- always false for out-of-range access.
// \return True if value changed and was not out-of-range
// \note Method name compatibility with bitSet
template<class TypeT = T>
typename std::enable_if<std::is_same<bool, TypeT>::value, bool>::type
inline unset(const label i)
@ -624,26 +636,31 @@ Ostream& operator<<(Ostream& os, const std::vector<T>& list);
// * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * * //
//- Sort the list
template<class T>
void sort(UList<T>& a);
void sort(UList<T>& list);
//- Sort the list with the specified comparator
template<class T, class Compare>
void sort(UList<T>& a, const Compare& comp);
void sort(UList<T>& list, const Compare& comp);
//- Stable sort the list
template<class T>
void stableSort(UList<T>& a);
void stableSort(UList<T>& list);
//- Stable sort the list with the specified comparator
template<class T, class Compare>
void stableSort(UList<T>& a, const Compare& comp);
void stableSort(UList<T>& list, const Compare& comp);
//- Randomise the list order
template<class T>
void shuffle(UList<T>& a);
void shuffle(UList<T>& list);
// Reverse the first n elements of the list
//- Reverse the first n elements of the list
template<class T>
inline void reverse(UList<T>& list, const label n);
// Reverse all the elements of the list
//- Reverse all elements of the list
template<class T>
inline void reverse(UList<T>& list);

View File

@ -448,7 +448,9 @@ inline void Foam::UList<T>::swap(UList<T>& list)
template<class T>
inline void Foam::reverse(UList<T>& list, const label n)
{
for (label i=0; i<n/2; ++i)
const label nBy2 = n/2;
for (label i = 0; i < nBy2; ++i)
{
Foam::Swap(list[i], list[n-1-i]);
}
@ -458,7 +460,7 @@ inline void Foam::reverse(UList<T>& list, const label n)
template<class T>
inline void Foam::reverse(UList<T>& list)
{
reverse(list, list.size());
Foam::reverse(list, list.size());
}

View File

@ -48,6 +48,7 @@ SourceFiles
#include "FlatOutput.H"
#include "labelPair.H"
#include "labelList.H"
#include "IndirectList.H"
#include "HashSet.H"
#include "Map.H"
#include "bitSet.H"
@ -178,24 +179,6 @@ template<class Container>
label inplaceMapValue(const Map<label>& mapper, Container& input);
//- Return the (stable) sort order for the list
template<class T>
labelList sortedOrder(const UList<T>& input);
//- Generate the (stable) sort order for the list
template<class T>
void sortedOrder(const UList<T>& input, labelList& order);
//- Sort using specified list compare predicate
template<class T, class ListComparePredicate>
void sortedOrder
(
const UList<T>& input,
labelList& order,
const ListComparePredicate& comp
);
//- Return (sorted) indices corresponding to duplicate list values
template<class T>
labelList duplicateOrder(const UList<T>& input);
@ -320,7 +303,7 @@ void inplaceSubset
//- Copy a subset of the input list when predicate is true.
//
// \param[in,out] input the list input values.
// \param[in] input the list input values.
// \param[in] pred the selection predicate
// \param[in] invert set as true to invert the selection logic
template<class T, class UnaryPredicate>

View File

@ -321,46 +321,6 @@ Foam::label Foam::inplaceMapValue
}
template<class T>
Foam::labelList Foam::sortedOrder
(
const UList<T>& input
)
{
labelList order;
sortedOrder(input, order, typename UList<T>::less(input));
return order;
}
template<class T>
void Foam::sortedOrder
(
const UList<T>& input,
labelList& order
)
{
sortedOrder(input, order, typename UList<T>::less(input));
}
template<class T, class ListComparePredicate>
void Foam::sortedOrder
(
const UList<T>& input,
labelList& order,
const ListComparePredicate& comp
)
{
// List lengths must be identical. Old content is overwritten
order.resize_nocopy(input.size());
ListOps::identity(order);
Foam::stableSort(order, comp);
}
template<class T>
Foam::labelList Foam::duplicateOrder
(
@ -398,7 +358,7 @@ void Foam::duplicateOrder
return;
}
sortedOrder(input, order, comp);
Foam::sortedOrder(input, order, comp);
const label last = (order.size()-1);
label count = 0;
@ -445,7 +405,7 @@ void Foam::uniqueOrder
const ListComparePredicate& comp
)
{
sortedOrder(input, order, comp);
Foam::sortedOrder(input, order, comp);
if (order.size() > 1)
{

View File

@ -26,7 +26,7 @@ License
\*---------------------------------------------------------------------------*/
#include "ListOps.H"
#include "ListOps.H" // For identity
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //

View File

@ -31,7 +31,6 @@ License
#include "CircularBuffer.H"
#include "CompactListList.H"
#include "DynamicList.H"
#include "ListOps.H" // sortedOrder
#include "IOstreams.H"
// * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * //

View File

@ -27,7 +27,7 @@ License
\*---------------------------------------------------------------------------*/
#include "IOobject.H"
#include "UList.H"
#include "List.H"
#include "hexRef8Data.H"
#include "mapPolyMesh.H"
#include "mapDistributePolyMesh.H"

View File

@ -1048,8 +1048,8 @@ Foam::autoPtr<Foam::refinementHistory> Foam::refinementHistory::clone
new refinementHistory
(
io,
DynamicList<splitCell8>(0),
labelList(0),
List<splitCell8>(),
labelList(),
false
)
);

View File

@ -28,7 +28,7 @@ License
#include "ensightFile.H"
#include "error.H"
#include "UList.H"
#include "List.H"
#include <cstring>
#include <sstream>

View File

@ -46,7 +46,7 @@ SourceFiles
#include "uint64.H"
#include "direction.H"
#include "word.H"
#include "UList.H"
#include "List.H"
#include "DynamicList.H"
#include "foamVtkCore.H"
#include "foamVtkPTraits.H"

View File

@ -41,7 +41,6 @@ License
#include "CompactListList.H"
#include "objectMap.H"
#include "processorPolyPatch.H"
#include "ListOps.H" // sortedOrder
#include "mapPolyMesh.H"
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //