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