Hugintrunk  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
alphanum.cpp
Go to the documentation of this file.
1 /*
2 The Alphanum Algorithm is an improved sorting algorithm for strings
3 containing numbers. Instead of sorting numbers in ASCII order like a
4 standard sort, this algorithm sorts numbers in numeric order.
5 
6 The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
7 
8 This implementation is Copyright (c) 2008 Dirk Jagdmann <doj@cubic.org>.
9 It is a cleanroom implementation of the algorithm and not derived by
10 other's works. In contrast to the versions written by Dave Koelle this
11 source code is distributed with the libpng/zlib license.
12 
13 This software is provided 'as-is', without any express or implied
14 warranty. In no event will the authors be held liable for any damages
15 arising from the use of this software.
16 
17 Permission is granted to anyone to use this software for any purpose,
18 including commercial applications, and to alter it and redistribute it
19 freely, subject to the following restrictions:
20 
21  1. The origin of this software must not be misrepresented; you
22  must not claim that you wrote the original software. If you use
23  this software in a product, an acknowledgment in the product
24  documentation would be appreciated but is not required.
25 
26  2. Altered source versions must be plainly marked as such, and
27  must not be misrepresented as being the original software.
28 
29  3. This notice may not be removed or altered from any source
30  distribution. */
31 
32 /* $Header: /code/doj/alphanum.hpp,v 1.3 2008/01/28 23:06:47 doj Exp $
33 
34  slightly modified version, the main function is unaltered, but the
35  interface definitions are changed to better suit hugins needs
36  */
37 
38 #include <hugin_utils/alphanum.h>
39 #include <stdlib.h>
40 
41 namespace doj
42 {
43 
44 int alphanum_impl(const char* l, const char* r)
45 {
46  enum mode_t { STRING, NUMBER } mode=STRING;
47 
48  while(*l && *r)
49  {
50  if(mode == STRING)
51  {
52  char l_char, r_char;
53  while((l_char=*l) && (r_char=*r))
54  {
55  // check if this are digit characters
56  const bool l_digit=isdigit(l_char)!=0, r_digit=isdigit(r_char)!=0;
57  // if both characters are digits, we continue in NUMBER mode
58  if(l_digit && r_digit)
59  {
60  mode=NUMBER;
61  break;
62  }
63  // if only the left character is a digit, we have a result
64  if(l_digit)
65  {
66  return -1;
67  }
68  // if only the right character is a digit, we have a result
69  if(r_digit)
70  {
71  return +1;
72  }
73  // compute the difference of both characters
74  const int diff=l_char - r_char;
75  // if they differ we have a result
76  if(diff != 0)
77  {
78  return diff;
79  }
80  // otherwise process the next characters
81  ++l;
82  ++r;
83  }
84  }
85  else // mode==NUMBER
86  {
87  // get the left number
88  char* end;
89  unsigned long l_int=strtoul(l, &end, 10);
90  l=end;
91 
92  // get the right number
93  unsigned long r_int=strtoul(r, &end, 10);
94  r=end;
95 
96  // if the difference is not equal to zero, we have a comparison result
97  const long diff=l_int-r_int;
98  if(diff != 0)
99  {
100  return diff;
101  }
102 
103  // otherwise we process the next substring in STRING mode
104  mode=STRING;
105  }
106  }
107 
108  if(*r)
109  {
110  return -1;
111  }
112  if(*l)
113  {
114  return +1;
115  }
116  return 0;
117 }
118 
119 int alphanum_comp(const std::string& l, const std::string& r)
120 {
121  return alphanum_impl(l.c_str(), r.c_str());
122 }
123 
124 int alphanum_comp(const char* l, const char* r)
125 {
126  return alphanum_impl(l, r);
127 }
128 
130 
131 bool alphanum_less::operator()(const std::string& left, const std::string& right) const
132 {
133  return alphanum_comp(left, right) < 0;
134 }
135 
136 } //namespace doj
137 
int alphanum_comp(const std::string &l, const std::string &r)
Compare l and r with the same semantics as strcmp(), but with the &quot;Alphanum Algorithm&quot; which produces...
Definition: alphanum.cpp:119
bool operator()(const std::string &left, const std::string &right) const
Definition: alphanum.cpp:131
int alphanum_impl(const char *l, const char *r)
Definition: alphanum.cpp:44