ListOps.H
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2011-2017 OpenFOAM Foundation
9  Copyright (C) 2017-2025 OpenCFD Ltd.
10 -------------------------------------------------------------------------------
11 License
12  This file is part of OpenFOAM.
13 
14  OpenFOAM is free software: you can redistribute it and/or modify it
15  under the terms of the GNU General Public License as published by
16  the Free Software Foundation, either version 3 of the License, or
17  (at your option) any later version.
18 
19  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
20  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  for more details.
23 
24  You should have received a copy of the GNU General Public License
25  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
26 
27 InNamespace
28  Foam
29 
30 Description
31  Various functions to operate on Lists.
32 
33 Namespace
34  Foam::ListOps
35 
36 Description
37  Various utility functions to work on Lists.
38 
39 SourceFiles
40  ListOps.C
41  ListOpsTemplates.C
42 
43 \*---------------------------------------------------------------------------*/
44 
45 #ifndef Foam_ListOps_H
46 #define Foam_ListOps_H
47 
48 #include "FlatOutput.H"
49 #include "labelPair.H"
50 #include "labelList.H"
51 #include "IndirectList.H"
52 #include "HashSet.H"
53 #include "Map.H"
54 #include "bitSet.H"
55 #include "ops.H"
56 
57 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
58 
59 namespace Foam
60 {
61 
62 // Forward Declarations
63 template<class T> class CompactListList;
64 
65 
66 //- Renumber the values within a list.
67 // Negative input values are left untouched.
68 template<class IntListType>
69 IntListType renumber(const labelUList& oldToNew, const IntListType& input);
70 
71 //- Inplace renumber the values within a list.
72 // Negative input values are left untouched.
73 template<class IntListType>
74 void inplaceRenumber(const labelUList& oldToNew, IntListType& input);
75 
76 //- Inplace renumber the values of a list.
77 // Values that are not mapped by oldToNew are left untouched.
78 template<class IntListType>
79 void inplaceRenumber(const Map<label>& oldToNew, IntListType& input);
80 
81 
82 //- Reorder the elements of a list.
83 // Locations with negative oldToNew values are left as is (copy-through).
84 // However, if pruning is activated, these negative oldToNew values are
85 // instead skipped over and the resulting list shrunk to the max index
86 // actually used.
87 template<class ListType>
88 ListType reorder
89 (
90  const labelUList& oldToNew,
91  const ListType& input,
92  const bool prune = false
93 );
94 
95 //- Inplace reorder the elements of a list.
96 // Locations with negative oldToNew values are left as is (copy-through).
97 // However, if pruning is activated, these negative oldToNew values are
98 // instead skipped over and the resulting list shrunk to the max index
99 // actually used.
100 template<class ListType>
101 void inplaceReorder
102 (
103  const labelUList& oldToNew,
104  ListType& input,
105  const bool prune = false
106 );
107 
108 
109 //- Reorder the elements of a packed list.
110 // Similar to the general templated form, but with auto-vivify
111 // for PackedList.
112 template<unsigned Width>
113 PackedList<Width> reorder
114 (
115  const labelUList& oldToNew,
116  const PackedList<Width>& input,
117  const bool prune = false
118 );
119 
120 //- Inplace reorder the elements of a packed list.
121 // Similar to the general templated form, but with auto-vivify
122 // for PackedList.
123 template<unsigned Width>
124 void inplaceReorder
125 (
126  const labelUList& oldToNew,
127  PackedList<Width>& input,
128  const bool prune = false
129 );
130 
131 
132 //- Reorder the elements of a list.
133 // Similar to the general templated form, but with auto-vivify for Bitset.
134 bitSet reorder
135 (
136  const labelUList& oldToNew,
137  const bitSet& input,
138  const bool prune = false
139 );
140 
141 //- Inplace reorder the elements of a list.
142 // Similar to the general templated form, but with auto-vivify for bitSet.
143 void inplaceReorder
144 (
145  const labelUList& oldToNew,
146  bitSet& input,
147  const bool prune = false
148 );
149 
150 
151 //- Rewrite with mapped keys. Ignore elements with negative key.
152 // The Container is some type of HashTable or Map with a label for its key.
153 template<class Container>
154 void inplaceMapKey(const labelUList& oldToNew, Container& input);
155 
156 //- Map values. Ignore negative values.
157 // \return number of values changed
158 template<class Container>
159 label inplaceMapValue(const labelUList& oldToNew, Container& input);
160 
161 //- Use mapper as a lookup to modify the values of input.
162 //
163 // \return number of values changed
164 //
165 // \code
166 // Map<label> mapper({{1, 3}, {2, 6}, {3, 12}, {5, 8}});
167 //
168 // HashTable<label> models with
169 // {
170 // model0 => 1,
171 // model1 => 4,
172 // model2 => 5,
173 // }
174 //
175 // inplaceMapValue(mapper, models);
176 //
177 // Now contains
178 // {
179 // model0 => 3,
180 // model1 => 4,
181 // model2 => 8,
182 // }
183 // \endcode
184 //
185 // \note the modification occurs in a single pass and will not
186 // remap more than once.
187 template<class Container>
188 label inplaceMapValue(const Map<label>& mapper, Container& input);
189 
190 
191 //- Return (sorted) indices corresponding to duplicate list values
192 template<class T>
193 labelList duplicateOrder(const UList<T>& input);
194 
195 //- Generate (sorted) indices corresponding to duplicate list values
196 template<class T>
197 void duplicateOrder(const UList<T>& input, labelList& order);
198 
199 //- Generate (sorted) indices corresponding to duplicate list values
200 // sort using specified list compare predicate
201 template<class T, class ListComparePredicate>
202 void duplicateOrder
203 (
204  const UList<T>& input,
205  labelList& order,
206  const ListComparePredicate& comp
207 );
208 
209 
210 //- Return (sorted) indices corresponding to unique list values
211 template<class T>
212 labelList uniqueOrder(const UList<T>& input);
213 
214 //- Generate (sorted) indices corresponding to unique list values
215 template<class T>
216 void uniqueOrder(const UList<T>& input, labelList& order);
217 
218 //- Generate (sorted) indices corresponding to unique list values
219 // sort using specified list compare predicate
220 template<class T, class ListComparePredicate>
221 void uniqueOrder
222 (
223  const UList<T>& input,
224  labelList& order,
225  const ListComparePredicate& comp
226 );
227 
228 
229 //- Return sorted list with removal of duplicates
230 template<class T>
231 List<T> uniqueSort(const UList<T>& input);
232 
233 //- Inplace sorting and removal of duplicates.
234 // Do not use FixedList for the input list, since it doesn't resize.
235 template<class ListType>
236 void inplaceUniqueSort(ListType& input);
237 
238 //- Inplace sorting and removal of duplicates.
239 // Do not use FixedList for the input list, since it doesn't resize.
240 template<class ListType, class ListComparePredicate>
242 (
243  ListType& input,
244  const ListComparePredicate& comp
245 );
246 
247 
248 //- Extract elements of the input list when select is true.
249 //
250 // \param[in] select the bool-list selector, for which the operator[]
251 // returns true or false. A labelHashSet can also be used since
252 // it satisfies these requirements
253 // \param[in] input the list input values.
254 // \param[in] invert set as true to invert the selection logic
255 //
256 // Eg, to extract all selected elements:
257 // \code
258 // subset<boolList, labelList>(selectedElems, list);
259 // \endcode
260 //
261 // \return The subsetted list
262 // \see IndirectList::subset
263 template<class BoolListType, class T>
264 List<T> subset
265 (
266  const BoolListType& select,
267  const UList<T>& input,
268  const bool invert=false
269 );
270 
271 //- Extract elements of the input list when select is true.
272 //
273 // \param[in] select the selection as a bitSet.
274 // \param[in] input the list input values.
275 // \param[in] invert set as true to invert the selection logic
276 //
277 // \return The subsetted list
278 // \see IndirectList::subset
279 template<class T>
280 List<T> subset
281 (
282  const bitSet& select,
283  const UList<T>& input,
284  const bool invert=false
285 );
286 
287 //- Inplace extract elements of the input list when select is true.
288 //
289 // \param[in] select the bool-list selector, for which the operator[]
290 // returns true or false. A labelHashSet can also be used since
291 // it satisfies these requirements
292 // \param[in,out] input the list input values.
293 // Cannot be a FixedList since it doesn't resize.
294 // \param[in] invert set as true to invert the selection logic
295 template<class BoolListType, class ListType>
296 void inplaceSubset
297 (
298  const BoolListType& select,
299  ListType& input,
300  const bool invert=false
301 );
302 
303 //- Inplace extract elements of the input list when select is true.
304 //
305 // \param[in] select the selection as a bitSet.
306 // \param[in,out] input the list input values.
307 // Cannot be a FixedList since it doesn't resize.
308 // \param[in] invert set as true to invert the selection logic
309 //
310 // \note Includes optimized handling of bitSet when invert=false.
311 template<class ListType>
312 void inplaceSubset
313 (
314  const bitSet& select,
315  ListType& input,
316  const bool invert=false
317 );
318 
319 //- Copy a subset of the input list when predicate is true.
320 //
321 // \param[in] input the list input values.
322 // \param[in] pred the selection predicate
323 // \param[in] invert set as true to invert the selection logic
324 //
325 // \return The subsetted list
326 // \see IndirectList::subset_if
327 template<class T, class UnaryPredicate>
328 List<T> subsetList
329 (
330  const UList<T>& input,
331  const UnaryPredicate& pred,
332  const bool invert=false
333 );
334 
335 
336 //- Inplace subset of the list when predicate is true.
337 //
338 // \param[in,out] input the list input/output values.
339 // Cannot be a FixedList since it doesn't resize.
340 // \param[in] pred the selection predicate
341 // \param[in] invert set as true to invert the selection logic
342 template<class ListType, class UnaryPredicate>
344 (
345  ListType& input,
346  const UnaryPredicate& pred,
347  const bool invert=false
348 );
349 
350 
351 //- Create an inverse one-to-one mapping.
352 // \param len the output size
353 // \param map the unique indices to map, in a [0..len) range.
354 // Any negative indices are ignored.
355 //
356 // \return the inverse mapping with -1 for unmapped elements.
357 labelList invert(const label len, const labelUList& map);
358 
359 //- Create an inverse one-to-one mapping for all 'on' bits of the map.
360 // \param len the output size
361 // \param map the 'on' bits to use for the mapping indices.
362 // The last on bit shall be less than len.
363 //
364 // \return the inverse mapping with -1 for unmapped elements.
365 labelList invert(const label len, const bitSet& map);
366 
367 //- Create an inverse one-to-one mapping for all 'on' bits of the map.
368 // The output size is dictated by the map size.
369 // \param map the unique indices to map.
370 //
371 // \return the inverse mapping with -1 for unmapped elements.
372 labelList invert(const bitSet& map);
373 
374 //- Create inverse mapping, which is a lookup table into the given list
375 // \param values the values (likely unique).
376 //
377 // Negative values are allowed, duplicates are "first wins"
378 //
379 // \return the inverse lookup
380 Map<label> invertToMap(const labelUList& values);
381 
382 //- Invert one-to-many map. Unmapped elements will be size 0.
383 labelListList invertOneToMany(const label len, const labelUList& map);
384 
385 //- Invert one-to-many compact map. Unmapped elements will be size 0.
386 CompactListList<label> invertOneToManyCompact
387 (
388  const label len,
389  const labelUList& map
390 );
391 
392 //- Invert many-to-many.
393 // Input and output types must be inherited from List and also
394 // contain ints/labels. Used, for example, for faces to pointFaces.
395 template<class InputIntListType, class OutputIntListType>
396 void invertManyToMany
397 (
398  const label len,
399  const UList<InputIntListType>& input,
400  List<OutputIntListType>& output
401 );
402 
403 template<class InputIntListType, class OutputIntListType>
404 List<OutputIntListType> invertManyToMany
405 (
406  const label len,
407  const UList<InputIntListType>& input
408 )
409 {
410  List<OutputIntListType> output;
411  invertManyToMany<InputIntListType,OutputIntListType>(len, input, output);
412  return output;
413 }
414 
415 
416 //- Deprecated(2017-10) search for first occurrence of the given element.
417 // \return The index found or return -1 if not found.
418 // \deprecated(2017-10) - use the UList find/found methods
419 template<class ListType>
420 FOAM_DEPRECATED_FOR(2017-10, "UList find/found methods")
421 label findIndex
422 (
423  const ListType& input,
424  typename ListType::const_reference val,
425  const label start=0
426 )
427 {
428  return input.find(val, start);
429 }
430 
431 
432 //- Linear search to find all occurrences of given element.
433 template<class ListType>
435 (
436  const ListType& input,
437  typename ListType::const_reference val,
438  label start=0
439 );
440 
441 
442 //- Linear search for the index of the min element,
443 //- similar to std::min_element but for lists and returns the index.
444 //
445 // \tparam ListType The input list type
446 //
447 // \param input The list to search
448 // \param start The start index in the list (default: 0)
449 //
450 // \return The min index or -1 on error.
451 template<class ListType>
452 label findMin(const ListType& input, label start=0);
453 
454 //- Linear search for the index of the max element,
455 //- similar to std::max_element but for lists and returns the index.
456 //
457 // \tparam ListType The input list type
458 //
459 // \param input The list to search
460 // \param start The start index in the list (default: 0)
461 //
462 // \return The max index or -1 on error.
463 template<class ListType>
464 label findMax(const ListType& input, label start=0);
465 
466 
467 //- Linear search for the index of the min/max element,
468 //- similar to std::minmax_element but for lists and returns the index.
469 //
470 // \tparam ListType The input list type
471 //
472 // \param input The list to search
473 // \param start The start index in the list (default: 0)
474 //
475 // \return The min/max indices as a Pair (min is first max is second)
476 // or (-1,-1) on error.
477 template<class ListType>
478 labelPair findMinMax(const ListType& input, label start=0);
479 
480 
481 //- Binary search to find the index of the last element in a sorted list
482 //- that is less than value.
483 //
484 // Uses the global <code> < </code> operator and thus
485 // <code> (list[i] < val) </code> for the test.
486 //
487 // \tparam ListType The input list type
488 //
489 // \param input The sorted list to search
490 // \param val The value for searching/comparing
491 // \param start The start index in the list (default: 0)
492 //
493 // \return The index found or -1 if not found.
494 template<class ListType>
495 label findSortedIndex
496 (
497  const ListType& input,
498  typename ListType::const_reference val,
499  const label start=0
500 );
501 
502 
503 //- Binary search to find the index of the last element in a sorted list
504 //- that is less than value.
505 //
506 // Uses <code> lessOp<T>() </code> and thus
507 // <code> lessOp<T>(list[i], val) </code> for the test.
508 //
509 // \tparam ListType The input list type
510 // \tparam T The value type (usually the same as ListType::value_type)
511 // \tparam ComparePredicate The type of the comparison functor that
512 // returns true for sorting below.
513 //
514 // \param input The sorted list to search
515 // \param val The value for searching/comparing
516 // \param start The start index in the list
517 // \param comp The comparison functor for testing.
518 // Uses <code> comp(list[i], val) </code> for the test.
519 //
520 // \return The index found or -1 if not found.
521 template<class ListType, class T, class ComparePredicate>
522 label findLower
523 (
524  const ListType& input,
525  const T& val,
526  const label start,
527  const ComparePredicate& comp
528 );
529 
530 
531 //- Binary search to find the index of the last element in a sorted list
532 //- that is less than value.
533 //
534 // Uses <code> lessOp<T>() </code> and thus
535 // <code> lessOp<T>(list[i], val) </code> for the test.
536 //
537 // \tparam ListType The input list type
538 // \tparam T The value type (should normally be ListType::value_type)
539 //
540 // \param input The sorted list to search
541 // \param val The value for searching/comparing
542 // \param start The start index in the list (default: 0)
543 //
544 // \return The index found or -1 if not found.
545 template<class ListType, class T>
546 label findLower
547 (
548  const ListType& input,
549  const T& val,
550  const label start=0
551 );
552 
553 
554 //- Reverse a list. First element becomes last element etc.
555 template<class ListType>
556 ListType reverseList(const ListType& input);
557 
558 
559 //- Inplace reversal of a list using Swap.
560 template<class ListType>
561 void inplaceReverseList(ListType& input);
562 
563 
564 //- Rotate a list by n places.
565 // If n is positive rotate clockwise/right/down.
566 // If n is negative rotate anti-clockwise/left/up.
567 template<class ListType>
568 ListType rotateList(const ListType& list, const label n);
569 
570 
571 //- Inplace reversal of a list using the Reversal Block Swapping algorithm.
572 template<template<typename> class ListType, class DataType>
573 void inplaceRotateList(ListType<DataType>& list, label n);
574 
575 
576 /*---------------------------------------------------------------------------*\
577  Namespace ListOps Declaration
578 \*---------------------------------------------------------------------------*/
579 
580 namespace ListOps
581 {
582 
583 //- List helper to append y elements onto the end of x
584 template<class T>
585 struct appendEqOp
586 {
587  void operator()(List<T>& x, const UList<T>& y) const;
588 };
589 
590 //- List helper to append y unique elements onto the end of x
591 template<class T>
592 struct uniqueEqOp
593 {
594  void operator()(List<T>& x, const UList<T>& y) const;
595 };
596 
597 //- List helper to add y unique elements to x
598 struct unionEqOp
599 {
600  void operator()(labelList& x, const labelUList& y) const;
601 };
602 
603 
604 //- Test for list equality with different but compatible data types.
605 //- Eg, int32 and int64
606 template<class Type1, class Type2>
607 bool equal(const UList<Type1>& a, const UList<Type2>& b);
608 
609 //- Test for list equality with different but compatible data types.
610 template<class Type1, class Type2, class BinaryPredicate>
611 bool equal(const UList<Type1>& a, const UList<Type2>& b, BinaryPredicate pred);
612 
613 
614 // Public classes
615 
616 //- A list compare binary predicate for normal sort
617 template<class ListType>
618 struct less
619 {
620  const ListType& values;
621 
622  less(const ListType& list)
623  :
624  values(list)
625  {}
626 
627  bool operator()(const label a, const label b) const
628  {
629  return (values[a] < values[b]);
630  }
631 };
632 
633 
634 //- A list compare binary predicate for reverse sort
635 template<class ListType>
636 struct greater
637 {
638  const ListType& values;
639 
640  greater(const ListType& list)
641  :
642  values(list)
643  {}
644 
645  bool operator()(const label a, const label b) const
646  {
647  return (values[b] < values[a]);
648  }
649 };
650 
651 
652 //- Fill an identity map with (map[i] == i)
653 // Optionally with an alternative start index, so that (map[i] == i+start)
654 FOAM_DEPRECATED_STRICT(2023-10, "Foam::identity(...)")
655 inline void identity(labelUList& map, label start = 0)
656 {
657  Foam::identity(map, start);
658 }
659 
660 
661 //- Count the number of matching entries.
662 // When start is specified, any occurrences before start are ignored.
663 // Linear search.
664 // Like std::count_if but works with list indexing
665 template<class ListType, class UnaryPredicate>
666 label count_if
667 (
668  const ListType& input,
669  const UnaryPredicate& pred,
670  const label start=0
671 );
672 
673 
674 //- Find index of the first occurrence that satisfies the predicate.
675 // When start is specified, any occurrences before start are ignored.
676 // Linear search.
677 // Like std::find_if but works with list indexing.
678 // \return position in list or -1 if not found.
679 template<class ListType, class UnaryPredicate>
680 label find_if
681 (
682  const ListType& input,
683  const UnaryPredicate& pred,
684  const label start=0
685 );
686 
687 
688 //- Same as ListOps::find_if
689 template<class ListType, class UnaryPredicate>
690 label find
691 (
692  const ListType& input,
693  const UnaryPredicate& pred,
694  const label start=0
695 )
696 {
697  return ListOps::find_if(input, pred, start);
698 }
699 
700 
701 //- True if there is a value in the list that satisfies the predicate.
702 // When start is specified, any occurrences before start are ignored.
703 // Linear search.
704 // \return true if found.
705 template<class ListType, class UnaryPredicate>
706 bool found_if
707 (
708  const ListType& input,
709  const UnaryPredicate& pred,
710  const label start=0
711 );
712 
713 
714 //- Same as found_if
715 template<class ListType, class UnaryPredicate>
716 bool found
717 (
718  const ListType& input,
719  const UnaryPredicate& pred,
720  const label start=0
721 )
722 {
723  return ListOps::found_if(input, pred, start);
724 }
725 
726 
727 //- Linear search to find all occurences of given element.
728 template<class ListType, class UnaryPredicate>
730 (
731  const ListType& input,
732  const UnaryPredicate& pred,
733  label start=0
734 );
735 
736 
737 //- Set various locations of the list with a specified value.
738 //
739 // \param list the list to modify
740 // \param locations where to apply the specified value
741 // An out-of-range index is silently ignored.
742 // \param val the value to set at the specified locations
743 template<class T>
744 void setValue
745 (
746  UList<T>& list,
747  const labelUList& locations,
748  const T& val
749 );
750 
751 
752 //- Set various locations of the list with a specified value.
753 //
754 // \param list the list to modify
755 // \param locations where to apply the specified value
756 // An out-of-range index is silently ignored.
757 // \param val the value to set at the specified locations
758 template<class T>
759 void setValue
760 (
761  UList<T>& list,
762  const labelHashSet& locations,
763  const T& val
764 );
765 
766 
767 //- Set various locations of the list with a specified value.
768 //
769 // \param list the list to modify
770 // \param locations where to apply the specified value
771 // An out-of-range index is silently ignored.
772 // \param val the value to set at the specified locations
773 template<class T>
775 (
776  UList<T>& list,
777  const UList<bool>& locations,
778  const T& val
779 );
780 
781 
782 //- Set various locations of the list with a specified value.
783 //
784 // \param list the list to modify
785 // \param locations where to apply the specified value
786 // An out-of-range index is silently ignored.
787 // \param val the value to set at the specified locations
788 template<class T>
789 void setValue
790 (
791  UList<T>& list,
792  const bitSet& locations,
793  const T& val
794 );
795 
797 //- Create a List from a List of a dissimilar type, using the entire list.
798 // For example, convert a list of ints to floats, vectors etc.
799 //
800 // \param input the list input values.
801 // \param op the unary conversion operator, which can be used to convert
802 // to other types.
803 //
804 // \code
805 // auto neg = ListOps::create<label>
806 // (
807 // ints,
808 // std::negate<label>()
809 // );
810 //
811 // auto labels = ListOps::create<label>
812 // (
813 // ints,
814 // labelOp<int>()
815 // );
816 //
817 // auto vectors = ListOps::create<vector>
818 // (
819 // ints,
820 // [](const int& val){ return vector(1.5*val, 0, 0); }
821 // );
822 //
823 // \endcode
824 template<class T, class T2, class UnaryOperation>
826 (
827  const UList<T2>& input,
828  const UnaryOperation& op
829 );
830 
831 
832 //- Create a List from an iterator range [first,last) of a dissimilar type.
833 // Uses std::distance for the size.
834 //
835 // \param first the begin of the iterator range
836 // \param last the end of the iterator range
837 // \param op the unary conversion operator, which can be used to convert
838 // to other types.
839 template<class T, class InputIterator, class UnaryOperation>
841 (
842  InputIterator first,
843  InputIterator last,
844  const UnaryOperation& op
845 );
846 
847 
848 //- Create a List filled with default values and various locations with
849 //- another specified value.
850 //
851 // \param len the length of the list
852 // \param locations where to apply the specified value
853 // An out-of-range index is silently ignored.
854 // \param val the value to set at the specified locations
855 // \param deflt the initialization default value
856 template<class T>
858 (
859  const label len,
860  const labelUList& locations,
861  const T& val,
862  const T& deflt = T()
863 );
864 
865 
866 //- Create a List filled with default values and various locations with
867 //- another specified value.
868 //
869 // \param len the length of the list
870 // \param locations where to apply the specified value
871 // An out-of-range index is silently ignored.
872 // \param val the value to set at the specified locations
873 // \param deflt the initialization default value
874 template<class T>
876 (
877  const label len,
878  const labelHashSet& locations,
879  const T& val,
880  const T& deflt = T()
881 );
882 
883 
884 //- Create a List filled with default values and various locations with
885 //- another specified value.
886 //
887 // \param len the length of the list
888 // \param locations where to apply the specified value
889 // An out-of-range index is silently ignored.
890 // \param val the value to set at the specified locations
891 // \param deflt the initialization default value
892 template<class T>
894 (
895  const label len,
896  const UList<bool>& locations,
897  const T& val,
898  const T& deflt = T()
899 );
900 
901 
902 //- Create a List filled with default values and various locations with
903 //- another specified value.
904 //
905 // \param len the length of the list
906 // \param locations where to apply the specified value
907 // An out-of-range index is silently ignored.
908 // \param val the value to set at the specified locations
909 // \param deflt the initialization default value
910 template<class T>
912 (
913  const label len,
914  const bitSet& locations,
915  const T& val,
916  const T& deflt = T()
917 );
918 
919 
920 //- Create a List filled with default values and one specified value,
921 //- which is copy assigned at the specified index
922 //
923 // \param len the length of the list
924 // \param index where to apply the specified value.
925 // An out-of-range index is silently ignored.
926 // \param val the value to copy assign at the specified index
927 // \param deflt the initialization default value
928 template<class T>
930 (
931  const label len,
932  const label index,
933  const T& val,
934  const T& deflt = T()
935 );
936 
937 
938 //- Create a List filled with default values and one specified value,
939 //- which is move assigned at the specified index
940 //
941 // \param len the length of the list
942 // \param index where to apply the specified value.
943 // An out-of-range index is silently ignored.
944 // \param val the value to move assign at the specified index
945 // \param deflt the initialization default value
946 //
947 // For example,
948 // \code
949 // // Gather all unique points on master
950 //
951 // List<pointField> gatheredPoints(Pstream::nProcs());
952 // gatheredPoints[Pstream::myProcNo()] = pointField
953 // (
954 // mesh.points(),
955 // uniqueMeshPoints
956 // );
957 // ...
958 //
959 // // Or else
960 // auto gatheredPoints = ListOps::createWithValue<pointField>
961 // (
962 // Pstream::nProcs(),
963 // Pstream::myProcNo(),
964 // pointField(mesh.points(), uniqueMeshPoints)
965 // );
966 // ...
967 //
968 // \endcode
969 template<class T>
971 (
972  const label len,
973  const label index,
974  T&& val,
975  const T& deflt = T()
976 );
977 
978 } // End namespace ListOps
979 
980 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
981 
982 } // End namespace Foam
983 
984 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
985 
986 #ifdef NoRepository
987  #include "ListOpsTemplates.C"
988 #endif
989 
990 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
991 
992 #endif
993 
994 // ************************************************************************* //
List< T > uniqueSort(const UList< T > &input)
Return sorted list with removal of duplicates.
label find(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as ListOps::find_if.
Definition: ListOps.H:840
label findMax(const ListType &input, label start=0)
Linear search for the index of the max element, similar to std::max_element but for lists and returns...
label findLower(const ListType &input, const T &val, const label start, const ComparePredicate &comp)
Binary search to find the index of the last element in a sorted list that is less than value...
label count_if(const ListType &input, const UnaryPredicate &pred, const label start=0)
Count the number of matching entries.
labelPair findMinMax(const ListType &input, label start=0)
Linear search for the index of the min/max element, similar to std::minmax_element but for lists and ...
labelList findIndices(const ListType &input, const UnaryPredicate &pred, label start=0)
Linear search to find all occurences of given element.
const ListType & values
Definition: ListOps.H:756
bool operator()(const label a, const label b) const
Definition: ListOps.H:783
bool found(const ListType &input, const UnaryPredicate &pred, const label start=0)
Same as found_if.
Definition: ListOps.H:871
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: BitOps.H:56
ListType rotateList(const ListType &list, const label n)
Rotate a list by n places.
IntListType renumber(const labelUList &oldToNew, const IntListType &input)
Renumber the values within a list.
labelList duplicateOrder(const UList< T > &input)
Return (sorted) indices corresponding to duplicate list values.
List< bool > select(const label n, const labelUList &locations)
Construct a selection list of bools (all false) with the given pre-size, subsequently add specified l...
Definition: BitOps.C:134
void inplaceReorder(const labelUList &oldToNew, ListType &input, const bool prune=false)
Inplace reorder the elements of a list.
label findSortedIndex(const ListType &input, typename ListType::const_reference val, const label start=0)
Binary search to find the index of the last element in a sorted list that is less than value...
Various functors for unary and binary operations. Can be used for parallel combine-reduce operations ...
labelList findIndices(const ListType &input, typename ListType::const_reference val, label start=0)
Linear search to find all occurrences of given element.
label inplaceMapValue(const labelUList &oldToNew, Container &input)
Map values. Ignore negative values.
greater(const ListType &list)
Definition: ListOps.H:778
void inplaceSubset(const BoolListType &select, ListType &input, const bool invert=false)
Inplace extract elements of the input list when select is true.
List< labelList > labelListList
List of labelList.
Definition: labelList.H:38
static bool less(const vector &x, const vector &y)
To compare normals.
const ListType & values
Definition: ListOps.H:776
UList< label > labelUList
A UList of labels.
Definition: UList.H:76
void inplaceSubsetList(ListType &input, const UnaryPredicate &pred, const bool invert=false)
Inplace subset of the list when predicate is true.
class FOAM_DEPRECATED_FOR(2017-05, "Foam::Enum") NamedEnum
Definition: NamedEnum.H:65
bool found_if(const ListType &input, const UnaryPredicate &pred, const label start=0)
True if there is a value in the list that satisfies the predicate.
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
label findMin(const ListType &input, label start=0)
Linear search for the index of the min element, similar to std::min_element but for lists and returns...
scalar y
CompactListList< label > invertOneToManyCompact(const label len, const labelUList &map)
Invert one-to-many compact map. Unmapped elements will be size 0.
Definition: ListOps.C:175
ListType reverseList(const ListType &input)
Reverse a list. First element becomes last element etc.
labelListList invertOneToMany(const label len, const labelUList &map)
Invert one-to-many map. Unmapped elements will be size 0.
Definition: ListOps.C:125
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
Definition: labelLists.C:44
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
void operator()(labelList &x, const labelUList &y) const
Definition: ListOps.C:273
List< T > subsetList(const UList< T > &input, const UnaryPredicate &pred, const bool invert=false)
Copy a subset of the input list when predicate is true.
void identity(labelUList &map, label start=0)
Fill an identity map with (map[i] == i)
Definition: ListOps.H:796
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:33
void operator()(List< T > &x, const UList< T > &y) const
void inplaceUniqueSort(ListType &input)
Inplace sorting and removal of duplicates.
#define FOAM_DEPRECATED_STRICT(since, replacement)
Definition: stdFoam.H:53
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
List< T > create(const UList< T2 > &input, const UnaryOperation &op)
Create a List from a List of a dissimilar type, using the entire list.
Pair< label > labelPair
A pair of labels.
Definition: Pair.H:51
void inplaceMapKey(const labelUList &oldToNew, Container &input)
Rewrite with mapped keys. Ignore elements with negative key.
void T(FieldField< Field, Type > &f1, const FieldField< Field, Type > &f2)
labelList uniqueOrder(const UList< T > &input)
Return (sorted) indices corresponding to unique list values.
labelList invert(const label len, const labelUList &map)
Create an inverse one-to-one mapping.
Definition: ListOps.C:28
List< T > subset(const BoolListType &select, const UList< T > &input, const bool invert=false)
Extract elements of the input list when select is true.
void inplaceRenumber(const labelUList &oldToNew, IntListType &input)
Inplace renumber the values within a list.
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition: bitSet.H:59
void inplaceRotateList(ListType< DataType > &list, label n)
Inplace reversal of a list using the Reversal Block Swapping algorithm.
label find_if(const ListType &input, const UnaryPredicate &pred, const label start=0)
Find index of the first occurrence that satisfies the predicate.
static Ostream & output(Ostream &os, const IntRange< T > &range)
Definition: IntRanges.C:44
label n
bool operator()(const label a, const label b) const
Definition: ListOps.H:763
List< T > createWithValue(const label len, const labelUList &locations, const T &val, const T &deflt=T())
Create a List filled with default values and various locations with another specified value...
label findIndex(const ListType &input, typename ListType::const_reference val, const label start=0)
Deprecated(2017-10) search for first occurrence of the given element.
Definition: ListOps.H:517
List< label > labelList
A List of labels.
Definition: List.H:61
void setValue(UList< T > &list, const labelUList &locations, const T &val)
Set various locations of the list with a specified value.
void operator()(List< T > &x, const UList< T > &y) const
void invertManyToMany(const label len, const UList< InputIntListType > &input, List< OutputIntListType > &output)
Invert many-to-many.
ListType reorder(const labelUList &oldToNew, const ListType &input, const bool prune=false)
Reorder the elements of a list.
void inplaceReverseList(ListType &input)
Inplace reversal of a list using Swap.
Map< label > invertToMap(const labelUList &values)
Create inverse mapping, which is a lookup table into the given list.
Definition: ListOps.C:105
bool equal(const UList< Type1 > &a, const UList< Type2 > &b)
Test for list equality with different but compatible data types. Eg, int32 and int64.
less(const ListType &list)
Definition: ListOps.H:758
Namespace for OpenFOAM.