/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2011-2026 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 . InNamespace Foam Description Various functions to operate on Lists. SourceFiles ListOps.C ListOpsTemplates.C \*---------------------------------------------------------------------------*/ #ifndef ListOps_H #define ListOps_H #include "labelList.H" #include "ops.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { extern const labelList emptyLabelList; //- Return reference to zero-sized list. Compare to List::null() which returns // null pointer cast as list reference. template static const List& emptyList() { return *reinterpret_cast*>(&emptyLabelList); } //- Renumber the values (not the indices) of a list. // Negative ListType elements are left as is. template ListType renumber(const labelUList& oldToNew, const ListType&); //- As above, but given a pre-constructed result list template void renumber(const labelUList& oldToNew, const ListType&, ListType&); //- As above, but for a single label. Termination condition. inline void renumber(const labelUList& oldToNew, const label, label&); //- Inplace renumber the values of a list. // Negative ListType elements are left as is. template void inplaceRenumber(const labelUList& oldToNew, ListType&); //- As above, but for a single label. Termination condition. inline void inplaceRenumber(const labelUList& oldToNew, label&); //- Reorder the elements (indices, not values) of a list. // Negative ListType elements are left as is. template ListType reorder(const labelUList& oldToNew, const ListType&); //- Inplace reorder the elements of a list. // Negative ListType elements are left as is. template void inplaceReorder(const labelUList& oldToNew, ListType&); //- Map values. Do not map negative values. template void inplaceMapValue(const labelUList& oldToNew, Container&); //- Recreate with mapped keys. Do not map elements with negative key. template void inplaceMapKey(const labelUList& oldToNew, Container&); //- Generate the (stable) sort order for the list template void sortedOrder(const UList&, labelList& order); //- Generate the (stable) sort order for the list, for a given comparison // operator template void sortedOrder(const UList&, labelList& order, const Cmp& cmp); //- Generate (sorted) indices corresponding to duplicate list values template void duplicateOrder(const UList&, labelList& order); //- Generate (sorted) indices corresponding to duplicate list values, for a // given comparison operator template void duplicateOrder(const UList&, labelList& order, const Cmp& cmp); //- Generate (sorted) indices corresponding to unique list values template void uniqueOrder(const UList&, labelList& order); //- Generate (sorted) indices corresponding to unique list values, for a given // comparison operator template void uniqueOrder(const UList&, labelList& order, const Cmp& cmp); //- Extract elements of List when select is a certain value. // eg, to extract all selected elements: // subset(selectedElems, true, lst); template ListType subset(const UList& select, const T& value, const ListType&); //- Inplace extract elements of List when select is a certain value. // eg, to extract all selected elements: // inplaceSubset(selectedElems, true, lst); template void inplaceSubset(const UList& select, const T& value, ListType&); //- Extract elements of List when select is true // eg, to extract all selected elements: // subset(selectedElems, lst); // Note a labelHashSet could also be used for the bool-list template ListType subset(const BoolListType& select, const ListType&); //- Inplace extract elements of List when select is true // eg, to extract all selected elements: // inplaceSubset(selectedElems, lst); // Note a labelHashSet could also be used for the bool-list template void inplaceSubset(const BoolListType& select, ListType&); //- Invert one-to-one map. Unmapped elements will be -1. labelList invert(const label len, const labelUList&); //- Invert one-to-many map. Unmapped elements will be size 0. labelListList invertOneToMany(const label len, const labelUList&); //- Invert many-to-many. // Input and output types need to be inherited from List. // eg, faces to pointFaces. template void invertManyToMany(const label len, const UList&, List&); //- Invert many-to-many. template List invertManyToMany(const label len, const UList& in) { List out; invertManyToMany(len, in, out); return out; } //- Create identity map (map[i] == i) of given length labelList identityMap(const label len); //- Create identity map (map[i] == start + i) of given length labelList identityMap(const label start, const label len); //- Count the number of occurrences of a value in a list template label count(const ListType& l, typename ListType::const_reference x); //- Find first occurrence of given element and return index, // return -1 if not found. Linear search. template label findIndex ( const ListType&, typename ListType::const_reference, const label start=0 ); //- Find all occurrences of given element. Linear search. template labelList findIndices ( const ListType&, typename ListType::const_reference, const label start=0 ); //- Select all occurrences given a boolean criteria. Linear search. template labelList selectIndices ( const ListType&, const BoolOp& bop, const label start=0 ); //- Opposite of findIndices: set values at indices to given value template void setValues ( ListType&, const labelUList& indices, typename ListType::const_reference ); //- Opposite of findIndices: set values at indices to given value template ListType createWithValues ( const label sz, typename ListType::const_reference initValue, const labelUList& indices, typename ListType::const_reference setValue ); //- Return the largest element of the list // or pTraits::min if the list is empty template Type max(const UList&); //- Return the smallest element of the list // or pTraits::max if the list is empty template Type min(const UList&); //- Return the sum of the element of the list // or zero if the list is empty template Type sum(const UList&); //- Find index of max element (and larger than given element). // return -1 if not found. Linear search. template label findMax(const ListType&, const label start=0); //- Find index of min element (and less than given element). // return -1 if not found. Linear search. template label findMin(const ListType&, const label start=0); //- Find first occurrence of given element in sorted list and return index, // return -1 if not found. Binary search. template label findSortedIndex ( const ListType&, typename ListType::const_reference, const label start=0 ); //- Find last element < given value in sorted list and return index, // return -1 if not found. Binary search. template label findLower ( const ListType&, typename ListType::const_reference, const label stary, const BinaryOp& bop ); //- Find last element < given value in sorted list and return index, // return -1 if not found. Binary search. template label findLower ( const ListType&, typename ListType::const_reference, 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*. template List 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. template List initListList(const T[mRows][nColumns]); //- Reverse a list. First element becomes last element etc. template ListType reverseList(const ListType& list); //- Inplace reversal of a list using Swap. template 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 ListType rotateList(const ListType& list, const label n); //- Inplace reversal of a list using the Reversal Block Swapping algorithm. template class ListType, class DataType> void inplaceRotateList(ListType& list, label n); //- Operator to apply a binary operation to a pair of lists template struct ListOp { template List operator()(const List& a, const List& b) const; }; //- Operator to apply a binary-equals operation to a pair of lists template struct ListEqOp { template void operator()(List& a, const List& b) const; }; //- List operator to append one list onto another struct ListAppendEqOp { template void operator()(List& x, const List& y) const; }; // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #ifdef NoRepository #include "ListOpsTemplates.C" #endif // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // #endif // ************************************************************************* //