stringOpsSort.H
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) 2017 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
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
26InNamespace
27 Foam::stringOps
28
29Description
30 Specialized string sorting.
31
32SourceFiles
33 stringOpsSort.C
34
35\*---------------------------------------------------------------------------*/
36
37#ifndef stringOpsSort_H
38#define stringOpsSort_H
39
40#include "stringOps.H"
41
42// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43
44namespace Foam
45{
46namespace stringOps
47{
48
49 //- 'Natural' compare for C-strings
50 // Uses algorithm and code from Jan-Marten Spit <jmspit@euronet.nl>
51 //
52 // In the 'natural' comparison, strings are compared alphabetically
53 // and numerically. Thus 'file010.txt' sorts after 'file2.txt'
54 //
55 // \param s1 left string
56 // \param s2 right string
57 // \return -1 when s1 < s2, 0 when s1 == s2, 1 when s1 > s2
58 int natstrcmp(const char* s1, const char* s2);
59
60
61 //- Encapsulation of natural order sorting for algorithms
63 {
64 //- Natural compare for std::string
65 // \return -1 when s1 < s2, 0 when s1 == s2, 1 when s1 > s2
66 static inline int compare
67 (
68 const std::string& s1,
69 const std::string& s2
70 )
71 {
72 return natstrcmp(s1.data(), s2.data());
73 }
74
75 //- Default (forward) natural sorting
76 bool operator()(const std::string& s1, const std::string& s2) const
77 {
78 return natural_sort::compare(s1, s2) < 0;
79 }
80
81 //- Reverse natural sorting
82 struct reverse
83 {
84 //- Reverse natural sorting
85 bool operator()(const std::string& s1, const std::string& s2) const
86 {
87 return natural_sort::compare(s1, s2) > 0;
88 }
89 };
90
91
92 //- A list compare binary predicate for natural sort
93 template<class T>
94 struct less
95 {
97
98 less(const UList<T>& list)
99 :
100 values(list)
101 {}
102
103 bool operator()(const label a, const label b) const
104 {
105 return natural_sort::compare(values[a], values[b]) < 0;
106 }
107 };
108
109
110 //- A list compare binary predicate for reverse natural sort
111 template<class T>
112 struct greater
113 {
115
116 greater(const UList<T>& list)
117 :
118 values(list)
119 {}
120
121 bool operator()(const label a, const label b) const
122 {
123 return natural_sort::compare(values[a], values[b]) > 0;
124 }
125 };
126 };
127
128} // End namespace stringOps
129} // End namespace Foam
130
131// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
132
133#endif
134
135// ************************************************************************* //
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: UList.H:94
int natstrcmp(const char *s1, const char *s2)
'Natural' compare for C-strings
Namespace for OpenFOAM.
volScalarField & b
Definition: createFields.H:27
A list compare binary predicate for reverse natural sort.
bool operator()(const label a, const label b) const
A list compare binary predicate for natural sort.
Definition: stringOpsSort.H:95
bool operator()(const label a, const label b) const
less(const UList< T > &list)
Definition: stringOpsSort.H:98
bool operator()(const std::string &s1, const std::string &s2) const
Reverse natural sorting.
Definition: stringOpsSort.H:85
Encapsulation of natural order sorting for algorithms.
Definition: stringOpsSort.H:63
static int compare(const std::string &s1, const std::string &s2)
Natural compare for std::string.
Definition: stringOpsSort.H:67
bool operator()(const std::string &s1, const std::string &s2) const
Default (forward) natural sorting.
Definition: stringOpsSort.H:76