FreeFOAM The Cross-Platform CFD Toolkit
Hosted by SourceForge:
Get FreeFOAM at SourceForge.net.
            Fast, secure and Free Open Source software downloads

SortableList.C

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------------*\
00002   =========                 |
00003   \\      /  F ield         | OpenFOAM: The Open Source CFD Toolbox
00004    \\    /   O peration     |
00005     \\  /    A nd           | Copyright (C) 1991-2010 OpenCFD Ltd.
00006      \\/     M anipulation  |
00007 -------------------------------------------------------------------------------
00008 License
00009     This file is part of OpenFOAM.
00010 
00011     OpenFOAM is free software: you can redistribute it and/or modify it
00012     under the terms of the GNU General Public License as published by
00013     the Free Software Foundation, either version 3 of the License, or
00014     (at your option) any later version.
00015 
00016     OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
00017     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00019     for more details.
00020 
00021     You should have received a copy of the GNU General Public License
00022     along with OpenFOAM.  If not, see <http://www.gnu.org/licenses/>.
00023 
00024 \*---------------------------------------------------------------------------*/
00025 
00026 // * * * * * * * * * * * * Private Member Functions  * * * * * * * * * * * * //
00027 
00028 template<class T>
00029 void Foam::SortableList<T>::sortIndices(List<label>& order) const
00030 {
00031     // list lengths must be identical
00032     if (order.size() != this->size())
00033     {
00034         // avoid copying any elements, they are overwritten anyhow
00035         order.clear();
00036         order.setSize(this->size());
00037     }
00038 
00039     forAll(order, elemI)
00040     {
00041         order[elemI] = elemI;
00042     }
00043     Foam::stableSort(order, typename UList<T>::less(*this));
00044 }
00045 
00046 
00047 // * * * * * * * * * * * * * * * * Constructors  * * * * * * * * * * * * * * //
00048 
00049 template<class T>
00050 Foam::SortableList<T>::SortableList()
00051 {}
00052 
00053 
00054 template<class T>
00055 Foam::SortableList<T>::SortableList(const UList<T>& values)
00056 :
00057     List<T>(values)
00058 {
00059     sort();
00060 }
00061 
00062 
00063 template<class T>
00064 Foam::SortableList<T>::SortableList(const Xfer<List<T> >& values)
00065 :
00066     List<T>(values)
00067 {
00068     sort();
00069 }
00070 
00071 
00072 template<class T>
00073 Foam::SortableList<T>::SortableList(const label size)
00074 :
00075     List<T>(size)
00076 {}
00077 
00078 
00079 template<class T>
00080 Foam::SortableList<T>::SortableList(const label size, const T& val)
00081 :
00082     List<T>(size, val)
00083 {}
00084 
00085 
00086 template<class T>
00087 Foam::SortableList<T>::SortableList(const SortableList<T>& lst)
00088 :
00089     List<T>(lst),
00090     indices_(lst.indices())
00091 {}
00092 
00093 
00094 // * * * * * * * * * * * * * * * Member Functions  * * * * * * * * * * * * * //
00095 
00096 
00097 template<class T>
00098 void Foam::SortableList<T>::clear()
00099 {
00100     List<T>::clear();
00101     indices_.clear();
00102 }
00103 
00104 
00105 template<class T>
00106 Foam::List<T>& Foam::SortableList<T>::shrink()
00107 {
00108     indices_.clear();
00109     return static_cast<List<T>&>(*this);
00110 }
00111 
00112 
00113 template<class T>
00114 void Foam::SortableList<T>::sort()
00115 {
00116     sortIndices(indices_);
00117 
00118     List<T> lst(this->size());
00119     forAll(indices_, i)
00120     {
00121         lst[i] = this->operator[](indices_[i]);
00122     }
00123 
00124     List<T>::transfer(lst);
00125 }
00126 
00127 
00128 template<class T>
00129 void Foam::SortableList<T>::reverseSort()
00130 {
00131     sortIndices(indices_);
00132 
00133     List<T> lst(this->size());
00134     label endI = indices_.size();
00135     forAll(indices_, i)
00136     {
00137         lst[--endI] = this->operator[](indices_[i]);
00138     }
00139 
00140     List<T>::transfer(lst);
00141 }
00142 
00143 
00144 template<class T>
00145 Foam::Xfer<Foam::List<T> > Foam::SortableList<T>::xfer()
00146 {
00147     return xferMoveTo<List<T> >(*this);
00148 }
00149 
00150 
00151 // * * * * * * * * * * * * * * * Member Operators  * * * * * * * * * * * * * //
00152 
00153 template<class T>
00154 inline void Foam::SortableList<T>::operator=(const T& t)
00155 {
00156     UList<T>::operator=(t);
00157 }
00158 
00159 
00160 template<class T>
00161 inline void Foam::SortableList<T>::operator=(const UList<T>& rhs)
00162 {
00163     List<T>::operator=(rhs);
00164     indices_.clear();
00165 }
00166 
00167 
00168 template<class T>
00169 inline void Foam::SortableList<T>::operator=(const SortableList<T>& rhs)
00170 {
00171     List<T>::operator=(rhs);
00172     indices_ = rhs.indices();
00173 }
00174 
00175 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
00176 
00177 // ************************ vim: set sw=4 sts=4 et: ************************ //
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines