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 -------------------------------------------------------------------------------
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.C
34 
35 \*---------------------------------------------------------------------------*/
36 
37 #ifndef stringOpsSort_H
38 #define 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 <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
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 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  {
96  const UList<T>& values;
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  {
114  const UList<T>& values;
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 // ************************************************************************* //
Foam::stringOps::natural_sort::less
A list compare binary predicate for natural sort.
Definition: stringOpsSort.H:94
Foam::stringOps::natural_sort::less::values
const UList< T > & values
Definition: stringOpsSort.H:96
Foam::stringOps::natural_sort::operator()
bool operator()(const std::string &s1, const std::string &s2) const
Default (forward) natural sorting.
Definition: stringOpsSort.H:76
Foam::stringOps::natural_sort::greater::operator()
bool operator()(const label a, const label b) const
Definition: stringOpsSort.H:121
Foam::stringOps::natural_sort::greater::greater
greater(const UList< T > &list)
Definition: stringOpsSort.H:116
Foam::stringOps::natstrcmp
int natstrcmp(const char *s1, const char *s2)
'Natural' compare for C-strings
Definition: stringOpsSort.C:118
Foam::constant::physicoChemical::b
const dimensionedScalar b
Wien displacement law constant: default SI units: [m.K].
Definition: createFields.H:27
Foam::stringOps::natural_sort::less::less
less(const UList< T > &list)
Definition: stringOpsSort.H:98
Foam::stringOps::natural_sort::compare
static int compare(const std::string &s1, const std::string &s2)
Natural compare for std::string.
Definition: stringOpsSort.H:67
Foam
Namespace for OpenFOAM.
Definition: atmBoundaryLayer.C:33
Foam::stringOps::natural_sort::greater
A list compare binary predicate for reverse natural sort.
Definition: stringOpsSort.H:112
Foam::UList
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:103
Foam::stringOps::natural_sort::reverse::operator()
bool operator()(const std::string &s1, const std::string &s2) const
Reverse natural sorting.
Definition: stringOpsSort.H:85
Foam::stringOps::natural_sort::reverse
Reverse natural sorting.
Definition: stringOpsSort.H:82
Foam::stringOps::natural_sort
Encapsulation of natural order sorting for algorithms.
Definition: stringOpsSort.H:62
Foam::stringOps::natural_sort::greater::values
const UList< T > & values
Definition: stringOpsSort.H:114
stringOps.H
Foam::stringOps::natural_sort::less::operator()
bool operator()(const label a, const label b) const
Definition: stringOpsSort.H:103