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-2022 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 //- Set identity map with (map[i] == i)
620 // Optionally with an alternative start index, so that (map[i] == i+start)
621 void identity(labelUList& map, label start=0);
622 
623 //- Count the number of matching entries.
624 // When start is specified, any occurrences before start are ignored.
625 // Linear search.
626 // Like std::count_if but works with list indexing
627 template<class ListType, class UnaryPredicate>
628 label count_if
629 (
630  const ListType& input,
631  const UnaryPredicate& pred,
632  const label start=0
633 );
634 
635 
636 //- Find index of the first occurrence that satisfies the predicate.
637 // When start is specified, any occurrences before start are ignored.
638 // Linear search.
639 // Like std::find_if but works with list indexing.
640 // \return position in list or -1 if not found.
641 template<class ListType, class UnaryPredicate>
642 label find_if
643 (
644  const ListType& input,
645  const UnaryPredicate& pred,
646  const label start=0
647 );
648 
649 
650 //- Same as ListOps::find_if
651 template<class ListType, class UnaryPredicate>
652 label find
653 (
654  const ListType& input,
655  const UnaryPredicate& pred,
656  const label start=0
657 )
658 {
659  return ListOps::find_if(input, pred, start);
660 }
661 
662 
663 //- True if there is a value in the list that satisfies the predicate.
664 // When start is specified, any occurrences before start are ignored.
665 // Linear search.
666 // \return true if found.
667 template<class ListType, class UnaryPredicate>
668 bool found_if
669 (
670  const ListType& input,
671  const UnaryPredicate& pred,
672  const label start=0
673 );
674 
675 
676 //- Same as found_if
677 template<class ListType, class UnaryPredicate>
678 bool found
679 (
680  const ListType& input,
681  const UnaryPredicate& pred,
682  const label start=0
683 )
684 {
685  return ListOps::found_if(input, pred, start);
686 }
687 
688 
689 //- Linear search to find all occurences of given element.
690 template<class ListType, class UnaryPredicate>
692 (
693  const ListType& input,
694  const UnaryPredicate& pred,
695  label start=0
696 );
698 
699 //- Set various locations of the list with a specified value.
700 //
701 // \param list the list to modify
702 // \param locations where to apply the specified value
703 // An out-of-range index is silently ignored.
704 // \param val the value to set at the specified locations
705 template<class T>
706 void setValue
707 (
708  UList<T>& list,
709  const labelUList& locations,
710  const T& val
711 );
712 
714 //- Set various locations of the list with a specified value.
715 //
716 // \param list the list to modify
717 // \param locations where to apply the specified value
718 // An out-of-range index is silently ignored.
719 // \param val the value to set at the specified locations
720 template<class T>
721 void setValue
722 (
723  UList<T>& list,
724  const labelHashSet& locations,
725  const T& val
726 );
727 
728 
729 //- Set various locations of the list with a specified value.
730 //
731 // \param list the list to modify
732 // \param locations where to apply the specified value
733 // An out-of-range index is silently ignored.
734 // \param val the value to set at the specified locations
735 template<class T>
736 void setValue
737 (
738  UList<T>& list,
739  const UList<bool>& locations,
740  const T& val
741 );
742 
743 
744 //- Set various locations of the list with a specified value.
745 //
746 // \param list the list to modify
747 // \param locations where to apply the specified value
748 // An out-of-range index is silently ignored.
749 // \param val the value to set at the specified locations
750 template<class T>
751 void setValue
752 (
753  UList<T>& list,
754  const bitSet& locations,
755  const T& val
756 );
757 
758 
759 //- Create a List from a List of a dissimilar type, using the entire list.
760 // For example, convert a list of ints to floats, vectors etc.
761 //
762 // \param input the list input values.
763 // \param op the unary conversion operator, which can be used to convert
764 // to other types.
765 //
766 // \code
767 // auto neg = ListOps::create<label>
768 // (
769 // ints,
770 // std::negate<label>()
771 // );
772 //
773 // auto labels = ListOps::create<label>
774 // (
775 // ints,
776 // labelOp<int>()
777 // );
778 //
779 // auto vectors = ListOps::create<vector>
780 // (
781 // ints,
782 // [](const int& val){ return vector(1.5*val, 0, 0); }
783 // );
784 //
785 // \endcode
786 template<class T, class T2, class UnaryOperation>
788 (
789  const UList<T2>& input,
790  const UnaryOperation& op
791 );
792 
793 
794 //- Create a List from an iterator range [first,last) of a dissimilar type.
795 // Uses std::distance for the size.
796 //
797 // \param first the begin of the iterator range
798 // \param last the end of the iterator range
799 // \param op the unary conversion operator, which can be used to convert
800 // to other types.
801 template<class T, class InputIterator, class UnaryOperation>
803 (
804  InputIterator first,
805  InputIterator last,
806  const UnaryOperation& op
807 );
808 
809 
810 //- Create a List filled with default values and various locations with
811 //- another specified value.
812 //
813 // \param len the length of the list
814 // \param locations where to apply the specified value
815 // An out-of-range index is silently ignored.
816 // \param val the value to set at the specified locations
817 // \param deflt the initialization default value
818 template<class T>
820 (
821  const label len,
822  const labelUList& locations,
823  const T& val,
824  const T& deflt = T()
825 );
826 
827 
828 //- Create a List filled with default values and various locations with
829 //- another specified value.
830 //
831 // \param len the length of the list
832 // \param locations where to apply the specified value
833 // An out-of-range index is silently ignored.
834 // \param val the value to set at the specified locations
835 // \param deflt the initialization default value
836 template<class T>
838 (
839  const label len,
840  const labelHashSet& locations,
841  const T& val,
842  const T& deflt = T()
843 );
844 
845 
846 //- Create a List filled with default values and various locations with
847 //- another specified value.
848 //
849 // \param len the length of the list
850 // \param locations where to apply the specified value
851 // An out-of-range index is silently ignored.
852 // \param val the value to set at the specified locations
853 // \param deflt the initialization default value
854 template<class T>
856 (
857  const label len,
858  const UList<bool>& locations,
859  const T& val,
860  const T& deflt = T()
861 );
862 
863 
864 //- Create a List filled with default values and various locations with
865 //- another specified value.
866 //
867 // \param len the length of the list
868 // \param locations where to apply the specified value
869 // An out-of-range index is silently ignored.
870 // \param val the value to set at the specified locations
871 // \param deflt the initialization default value
872 template<class T>
874 (
875  const label len,
876  const bitSet& locations,
877  const T& val,
878  const T& deflt = T()
879 );
880 
881 
882 //- Create a List filled with default values and one specified value,
883 //- which is copy assigned at the specified index
884 //
885 // \param len the length of the list
886 // \param index where to apply the specified value.
887 // An out-of-range index is silently ignored.
888 // \param val the value to copy assign at the specified index
889 // \param deflt the initialization default value
890 template<class T>
892 (
893  const label len,
894  const label index,
895  const T& val,
896  const T& deflt = T()
897 );
898 
899 
900 //- Create a List filled with default values and one specified value,
901 //- which is move assigned at the specified index
902 //
903 // \param len the length of the list
904 // \param index where to apply the specified value.
905 // An out-of-range index is silently ignored.
906 // \param val the value to move assign at the specified index
907 // \param deflt the initialization default value
908 //
909 // For example,
910 // \code
911 // // Gather all unique points on master
912 //
913 // List<pointField> gatheredPoints(Pstream::nProcs());
914 // gatheredPoints[Pstream::myProcNo()] = pointField
915 // (
916 // mesh.points(),
917 // uniqueMeshPoints
918 // );
919 // ...
920 //
921 // // Or else
922 // auto gatheredPoints = ListOps::createWithValue<pointField>
923 // (
924 // Pstream::nProcs(),
925 // Pstream::myProcNo(),
926 // pointField(mesh.points(), uniqueMeshPoints)
927 // );
928 // ...
929 //
930 // \endcode
931 template<class T>
933 (
934  const label len,
935  const label index,
936  T&& val,
937  const T& deflt = T()
938 );
939 
940 } // End namespace ListOps
941 
942 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
943 
944 } // End namespace Foam
945 
946 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
947 
948 #ifdef NoRepository
949  #include "ListOpsTemplates.C"
950 #endif
951 
952 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
953 
954 #endif
955 
956 // ************************************************************************* //
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:790
List< labelList > labelListList
A List of labelList.
Definition: labelList.H:51
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:203
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:821
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.
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:80
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
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.
static Istream & input(Istream &is, IntRange< T > &range)
Definition: IntRanges.C:48
void operator()(List< T > &x, const List< T > &y) const
void inplaceUniqueSort(ListType &input)
Inplace sorting and removal of duplicates.
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:99
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:50
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:59
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.
void identity(labelUList &map, label start=0)
Set identity map with (map[i] == i)
Definition: ListOps.C:196
less(const ListType &list)
Definition: ListOps.H:713
Namespace for OpenFOAM.