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 -------------------------------------------------------------------------------
10 License
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 
26 InNamespace
27  Foam::stringOps
28 
29 Description
30  Specialized string sorting.
31 
32 SourceFiles
33  stringOpsSort.cxx
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef Foam_stringOpsSort_H
38 #define Foam_stringOpsSort_H
39 
40 #include "stringOps.H"
41 
42 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
43 
44 namespace Foam
45 {
46 namespace stringOps
47 {
48 
49 //- 'Natural' compare for C-strings
50 // Uses algorithm and code from Jan-Marten Spit <[email protected]>
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
62 struct natural_sort
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 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  }
94 
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
106  {
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>
114  struct list_less
115  {
116  const UList<T>& values;
117 
118  list_less(const UList<T>& list) noexcept
119  :
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  {
134  const UList<T>& values;
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 // ************************************************************************* //
bool operator()(const std::string &s1, const std::string &s2) const
Reverse natural sorting.
list_less(const UList< T > &list) noexcept
bool operator()(const std::string &s1, const std::string &s2) const
Default (forward) natural sorting.
A UList compare binary predicate for natural sort.
static bool greater(const std::string &s1, const std::string &s2)
Natural compare two strings for a greater-than relationship.
Definition: stringOpsSort.H:95
A Ulist compare binary predicate for reverse natural sort.
list_greater(const UList< T > &list) noexcept
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
static int compare(const std::string &s1, const std::string &s2)
Natural compare for std::string.
Definition: stringOpsSort.H:71
void reverse(UList< T > &list, const label n)
Reverse the first n elements of the list.
Definition: UListI.H:532
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition: HashTable.H:105
const direction noexcept
Definition: scalarImpl.H:265
bool operator()(label a, label b) const
int natstrcmp(const char *s1, const char *s2)
&#39;Natural&#39; compare for C-strings
static bool less(const std::string &s1, const std::string &s2)
Natural compare two strings for a less-than relationship.
Definition: stringOpsSort.H:83
bool operator()(label a, label b) const
Namespace for OpenFOAM.