SortableListEFA.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 | www.openfoam.com
6  \\/ M anipulation |
7 -------------------------------------------------------------------------------
8  Copyright (C) 2016 OpenFOAM Foundation
9 -------------------------------------------------------------------------------
10 License
11  This file is part of OpenFOAM.
12 
13  OpenFOAM is free software: you can redistribute it and/or modify it
14  under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25 
26 \*---------------------------------------------------------------------------*/
27 
28 #include "OSspecific.H"
29 
30 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
31 
32 template <class Type>
34 :
35  List<Type>(values),
36  indices_(values.size())
37 {
38  sort();
39 }
40 
41 
42 template <class Type>
44 :
45  List<Type>(size),
46  indices_(size)
47 {
48  forAll(indices_, i)
49  {
50  indices_[i] = i;
51  }
52 }
53 
54 
55 template <class Type>
56 Foam::SortableListEFA<Type>::SortableListEFA(const label size, const Type& val)
57 :
58  List<Type>(size, val),
59  indices_(size)
60 {
61  forAll(indices_, i)
62  {
63  indices_[i] = i;
64  }
65 }
66 
67 
68 template <class Type>
69 Foam::SortableListEFA<Type>::SortableListEFA(const SortableListEFA<Type>& lst)
70 :
71  List<Type>(lst),
72  indices_(lst.indices())
73 {
74  forAll(indices_, i)
75  {
76  indices_[i] = i;
77  }
78 
79 }
80 
81 
82 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
83 
84 template <class Type>
85 void Foam::SortableListEFA<Type>::setSize(const label newSize)
86 {
87  List<Type>::setSize(newSize);
88  indices_.setSize(newSize);
89 }
90 
91 
92 template <class Type>
94 {
95  forAll(indices_, i)
96  {
97  indices_[i] = i;
98  }
99 
100  Foam::sort(indices_, less(*this));
101 
102  List<Type> tmpValues(this->size());
103 
104  forAll(indices_, i)
105  {
106  tmpValues[i] = this->operator[](indices_[i]);
107  }
108 
109  List<Type>::transfer(tmpValues);
110 }
111 
112 
113 template <class Type>
114 void Foam::SortableListEFA<Type>::partialSort(int M, int start)
115 {
116  std::partial_sort
117  (
118  indices_.begin()+start,
119  indices_.begin()+start+M,
120  indices_.end(),
121  more(*this)
122  );
123 }
124 
125 
126 template <class Type>
128 {
129  forAll(indices_, i)
130  {
131  indices_[i] = i;
132  }
133 
134  Foam::stableSort(indices_, less(*this));
135 
136  List<Type> tmpValues(this->size());
137 
138  forAll(indices_, i)
139  {
140  tmpValues[i] = this->operator[](indices_[i]);
141  }
142 
144 }
145 
146 
147 // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
148 
149 template <class Type>
151 {
153  indices_ = rhs.indices();
154 }
155 
156 
157 // ************************************************************************* //
void transfer(List< T > &list)
Transfer the contents of the argument List into this list and annul the argument list.
Definition: List.C:326
SortableListEFA(const List< Type > &)
Construct from List, sorting the elements. Starts with indices set.
void stableSort(UList< T > &list)
Stable sort the list.
Definition: UList.C:312
A list that is sorted upon construction or when explicitly requested with the sort() method...
static bool less(const vector &x, const vector &y)
To compare normals.
const labelList & indices() const
Return the list of sorted indices. Updated every sort.
#define forAll(list, i)
Loop across all elements in list.
Definition: stdFoam.H:421
void operator=(const SortableListEFA< Type > &)
List< T > values(const HashTable< T, Key, Hash > &tbl, const bool doSort=false)
List of values from HashTable, optionally sorted.
Definition: HashOps.H:164
Functions used by OpenFOAM that are specific to POSIX compliant operating systems and need to be repl...
void setSize(const label n)
Alias for resize()
Definition: List.H:316
void operator=(const UList< T > &list)
Assignment to UList operator. Takes linear time.
Definition: List.C:360
void sort(UList< T > &list)
Sort the list.
Definition: UList.C:296
Less function class used by the sort function.
void setSize(const label)
Size the list. If grow can cause undefined indices (until next sort)
void partialSort(int M, int start)
Partial sort the list (if changed after construction time)
void stableSort()
Sort the list (if changed after construction time)
#define M(I)
void sort()
Sort the list (if changed after construction time)