Loading...
Searching...
No Matches
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-2025 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.cxx
34
35\*---------------------------------------------------------------------------*/
36
37#ifndef Foam_stringOpsSort_H
38#define Foam_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
58int natstrcmp(const char* s1, const char* s2);
59
60
61//- Encapsulation of natural order sorting for algorithms
62struct natural_sort
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 stringOps::natstrcmp(s1.data(), s2.data());
73 }
74
75 //- Natural compare two strings for a less-than relationship
76 static inline bool less
77 (
78 const std::string& s1,
79 const std::string& s2
80 )
81 {
82 return (natural_sort::compare(s1, s2) < 0);
83 }
84
85 //- Natural compare two strings for a greater-than relationship
86 static inline bool greater
87 (
88 const std::string& s1,
89 const std::string& s2
90 )
91 {
92 return (natural_sort::compare(s1, s2) > 0);
93 }
95 //- Default (forward) natural sorting
96 bool operator()(const std::string& s1, const std::string& s2) const
97 {
98 return (natural_sort::compare(s1, s2) < 0);
99 }
100
101 //- Reverse natural sorting
102 struct reverse
103 {
104 //- Reverse natural sorting
105 bool operator()(const std::string& s1, const std::string& s2) const
107 return (natural_sort::compare(s1, s2) > 0);
108 }
109 };
110
111
112 //- A UList compare binary predicate for natural sort
113 template<class T>
115 {
116 const UList<T>& values;
117
118 list_less(const UList<T>& list) noexcept
120 values(list)
121 {}
122
123 bool operator()(label a, label b) const
124 {
125 return (natural_sort::compare(values[a], values[b]) < 0);
126 }
127 };
128
129
130 //- A Ulist compare binary predicate for reverse natural sort
131 template<class T>
133 {
135
136 list_greater(const UList<T>& list) noexcept
137 :
138 values(list)
139 {}
140
141 bool operator()(label a, label b) const
142 {
143 return (natural_sort::compare(values[a], values[b]) > 0);
144 }
145 };
146};
147
148
149} // End namespace stringOps
150} // End namespace Foam
151
152// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
153
154#endif
155
156// ************************************************************************* //
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:89
Collection of static functions for various string-related operations.
int natstrcmp(const char *s1, const char *s2)
'Natural' compare for C-strings
Namespace for OpenFOAM.
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition UListI.H:539
const direction noexcept
Definition scalarImpl.H:265
volScalarField & b
A Ulist compare binary predicate for reverse natural sort.
bool operator()(label a, label b) const
list_greater(const UList< T > &list) noexcept
A UList compare binary predicate for natural sort.
bool operator()(label a, label b) const
list_less(const UList< T > &list) noexcept
bool operator()(const std::string &s1, const std::string &s2) const
Reverse natural sorting.
Encapsulation of natural order sorting for algorithms.
static bool greater(const std::string &s1, const std::string &s2)
Natural compare two strings for a greater-than relationship.
static int compare(const std::string &s1, const std::string &s2)
Natural compare for std::string.
static bool less(const std::string &s1, const std::string &s2)
Natural compare two strings for a less-than relationship.
bool operator()(const std::string &s1, const std::string &s2) const
Default (forward) natural sorting.