initial commit for version 1.5.x patch release
[OpenFOAM-1.5.x.git] / src / OpenFOAM / containers / Lists / SortableList / SortableList.H
bloba9ab2ae8294a13f04fe5a4230fcbf4e2ec5c69ed
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 Class
26     Foam::SortableList
28 Description
29     A list that is sorted upon construction or when explicitly requested
30     with the sort() method.
32 SourceFiles
33     SortableList.C
35 \*---------------------------------------------------------------------------*/
37 #ifndef SortableList_H
38 #define SortableList_H
40 #include "labelList.H"
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
44 namespace Foam
47 /*---------------------------------------------------------------------------*\
48                            Class SortableList Declaration
49 \*---------------------------------------------------------------------------*/
51 template <class Type>
52 class SortableList
54     public List<Type>
56     // Private data
58         //- Original indices
59         labelList indices_;
62 public:
64     // Public classes
66         //- Less function class used by the sort function
67         class less
68         {
69             const UList<Type>& values_;
71         public:
73             less(const UList<Type>& values)
74             :
75                 values_(values)
76             {}
78             bool operator()(const label a, const label b)
79             {
80                 return values_[a] < values_[b];
81             }
82         };
85     // Constructors
87         //- Construct from List, sorting the elements. Starts with indices set
88         //  to index in argument
89         explicit SortableList(const List<Type>&);
91         //- Construct given size. Sort later on.
92         explicit SortableList(const label size);
94         //- Construct given size and initial value. Sort later on.
95         SortableList(const label size, const Type&);
97         //- Construct as copy.
98         SortableList(const SortableList<Type>&);
101     // Member Functions
103         //- Return the list of sorted indices. Updated every sort.
104         const labelList& indices() const
105         {
106             return indices_;
107         }
109         //- Size the list. If grow can cause undefined indices (until next sort)
110         void setSize(const label);
112         //- Sort the list (if changed after construction time)
113         void sort();
115         //- Sort the list (if changed after construction time)
116         void stableSort();
119     // Member Operators
121         void operator=(const SortableList<Type>&);
126 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
128 } // End namespace Foam
130 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
132 #ifdef NoRepository
133 #   include "SortableList.C"
134 #endif
136 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
138 #endif
140 // ************************************************************************* //