initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / Lists / SortableList / SortableList.C
blob7cde863a6321e7f59c73c54635589723ff1fab8f
1 /*---------------------------------------------------------------------------*\
2   =========                 |
3   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
4    \\    /   O peration     |
5     \\  /    A nd           | Copyright (C) 1991-2008 OpenCFD Ltd.
6      \\/     M anipulation  |
7 -------------------------------------------------------------------------------
8 License
9     This file is part of OpenFOAM.
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 the
13     Free Software Foundation; either version 2 of the License, or (at your
14     option) any later version.
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.
21     You should have received a copy of the GNU General Public License
22     along with OpenFOAM; if not, write to the Free Software Foundation,
23     Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 \*---------------------------------------------------------------------------*/
27 #include "OSspecific.H"
29 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
31 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
33 // Construct from List
34 template <class Type>
35 Foam::SortableList<Type>::SortableList(const List<Type>& values)
37     List<Type>(values),
38     indices_(values.size())
40     sort();
44 // Construct given size. Sort later on.
45 template <class Type>
46 Foam::SortableList<Type>::SortableList(const label size)
48     List<Type>(size),
49     indices_(size)
53 // Construct given size and initial value. Sort later on.
54 template <class Type>
55 Foam::SortableList<Type>::SortableList(const label size, const Type& val)
57     List<Type>(size, val),
58     indices_(size)
62 // Construct as copy.
63 template <class Type>
64 Foam::SortableList<Type>::SortableList(const SortableList<Type>& lst)
66     List<Type>(lst),
67     indices_(lst.indices())
71 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
73 template <class Type>
74 void Foam::SortableList<Type>::setSize(const label newSize)
76     List<Type>::setSize(newSize);
77     indices_.setSize(newSize);
81 template <class Type>
82 void Foam::SortableList<Type>::sort()
84     forAll(indices_, i)
85     {
86         indices_[i] = i;
87     }
89     Foam::sort(indices_, less(*this));
91     List<Type> tmpValues(this->size());
93     forAll(indices_, i)
94     {
95         tmpValues[i] = this->operator[](indices_[i]);
96     }
98     List<Type>::transfer(tmpValues);
103 template <class Type>
104 void Foam::SortableList<Type>::stableSort()
106     forAll(indices_, i)
107     {
108         indices_[i] = i;
109     }
111     Foam::stableSort(indices_, less(*this));
113     List<Type> tmpValues(this->size());
115     forAll(indices_, i)
116     {
117         tmpValues[i] = this->operator[](indices_[i]);
118     }
120     List<Type>::transfer(tmpValues);
124 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
126 template <class Type>
127 void Foam::SortableList<Type>::operator=(const SortableList<Type>& rhs)
129     List<Type>::operator=(rhs);
130     indices_ = rhs.indices();
134 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
136 // ************************************************************************* //