Test-sortList.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2  ========= |
3  \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4  \\ / O peration |
5  \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8 License
9  This file is part of OpenFOAM.
10 
11  OpenFOAM is free software: you can redistribute it and/or modify it
12  under the terms of the GNU General Public License as published by
13  the Free Software Foundation, either version 3 of the License, or
14  (at your option) any later version.
15 
16  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19  for more details.
20 
21  You should have received a copy of the GNU General Public License
22  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
23 
24 Description
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "SortableList.H"
29 #include "ListOps.H"
30 
31 using namespace Foam;
32 
33 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
34 // Main program:
35 
36 int main(int argc, char *argv[])
37 {
38  labelList orig(8);
39  orig[0] = 7;
40  orig[1] = 9;
41  orig[2] = 1;
42  orig[3] = 2;
43  orig[4] = 4;
44  orig[5] = 7;
45  orig[6] = 4;
46  orig[7] = 0;
47 
48  labelList order;
49 
50  labelList a(orig);
51  sortedOrder(a, order);
52 
53  SortableList<label> aReverse(a.size());
54  aReverse = a;
55 
56  Info<< "unsorted: " << a << endl;
57  sort(a);
58  Info<< "sorted: " << a << endl;
59  Info<< "indices: " << order << endl;
60 
61  aReverse.reverseSort();
62  Info<< "reverse sorted: " << aReverse << endl;
63  Info<< "reverse indices: " << aReverse.indices() << endl;
64 
65  SortableList<label> b(orig);
66  Info<< "unsorted: " << orig << endl;
67  Info<< "sorted: " << b << endl;
68  Info<< "indices: " << b.indices() << endl;
69 
70  Info<< "shrunk: " << b.shrink() << endl;
71  Info<< "indices: " << b.indices() << endl;
72 
73  // repeat by assignment
74  b = orig;
75  Info<< "unsorted: " << b << endl;
76  b.sort();
77 
78  Info<< "sorted: " << b << endl;
79  Info<< "indices: " << b.indices() << endl;
80 
81  // find unique/duplicate values
82  b = orig;
83 
84  Info<< "unsorted: " << b << endl;
85  uniqueOrder(b, order);
86  Info<< "unique: " << order << endl;
87  duplicateOrder(b, order);
88  Info<< "duplicate:" << order << endl;
89 
90  // sort reverse
91  Info<< "unsorted: " << b << endl;
92  b.reverseSort();
93  Info<< "rsort: " << b << endl;
94  Info<< "indices: " << b.indices() << endl;
95 
96  // transfer assignment
97  a = orig;
98  b.transfer(a);
99  Info<< "unsorted: " << b << endl;
100  b.sort();
101 
102  Info<< "sorted: " << b << endl;
103  Info<< "indices: " << b.indices() << endl;
104 
105  a.transfer(b);
106 
107  Info<< "plain: " << a << endl;
108  Info<< "sorted: " << b << endl;
109  Info<< "indices: " << b.indices() << endl;
110 
111  // sort/duplicate/unique with identical values
112  b.setSize(8);
113  b = 5;
114 
115  Info<< "unsorted: " << b << endl;
116 
117  uniqueOrder(b, order);
118  Info<< "unique: " << order << endl;
119  duplicateOrder(b, order);
120  Info<< "duplicate:" << order << endl;
121  b.sort();
122 
123  Info<< "sorted: " << b << endl;
124  Info<< "indices: " << b.indices() << endl;
125 
126  // with a single value
127  b.setSize(1);
128 
129  Info<< "unsorted: " << b << endl;
130  uniqueOrder(b, order);
131  Info<< "unique: " << order << endl;
132  duplicateOrder(b, order);
133  Info<< "duplicate:" << order << endl;
134  b.sort();
135 
136  Info<< "sorted: " << b << endl;
137  Info<< "indices: " << b.indices() << endl;
138 
139  Info<< "\nEnd\n" << endl;
140 
141  return 0;
142 }
143 
144 
145 // ************************************************************************* //
Foam::SortableList::reverseSort
void reverseSort()
Reverse (stable) sort the list.
Definition: SortableList.C:110
Foam::List::transfer
void transfer(List< T > &)
Transfer the contents of the argument List into this list.
Foam::endl
Ostream & endl(Ostream &os)
Add newline and flush stream.
Definition: Ostream.H:251
SortableList.H
Foam::sortedOrder
void sortedOrder(const UList< T > &, labelList &order)
Generate the (stable) sort order for the list.
Definition: ListOpsTemplates.C:179
Foam::duplicateOrder
void duplicateOrder(const UList< T > &, labelList &order)
Generate (sorted) indices corresponding to duplicate list values.
Definition: ListOpsTemplates.C:214
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:28
Foam::Info
messageStream Info
Foam::SortableList
A list that is sorted upon construction or when explicitly requested with the sort() method.
Definition: List.H:65
Foam
Namespace for OpenFOAM.
Definition: combustionModel.C:30
Foam::uniqueOrder
void uniqueOrder(const UList< T > &, labelList &order)
Generate (sorted) indices corresponding to unique list values.
Definition: ListOpsTemplates.C:253
Foam::List
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
Definition: HashTable.H:59
main
int main(int argc, char *argv[])
Definition: Test-sortList.C:36
Foam::SortableList::indices
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
Definition: SortableList.H:90
ListOps.H
Various functions to operate on Lists.
Foam::sort
void sort(UList< T > &)
Definition: UList.C:107
Foam::List::size
void size(const label)
Override size to be inconsistent with allocated storage.