Hugin trunk 0.1
Loading...
Searching...
No Matches
alphanum.cpp
Go to the documentation of this file.
1/*
2The Alphanum Algorithm is an improved sorting algorithm for strings
3containing numbers. Instead of sorting numbers in ASCII order like a
4standard sort, this algorithm sorts numbers in numeric order.
5
6The Alphanum Algorithm is discussed at http://www.DaveKoelle.com
7
8This implementation is Copyright (c) 2008 Dirk Jagdmann <doj@cubic.org>.
9It is a cleanroom implementation of the algorithm and not derived by
10other's works. In contrast to the versions written by Dave Koelle this
11source code is distributed with the libpng/zlib license.
12
13This software is provided 'as-is', without any express or implied
14warranty. In no event will the authors be held liable for any damages
15arising from the use of this software.
16
17Permission is granted to anyone to use this software for any purpose,
18including commercial applications, and to alter it and redistribute it
19freely, 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
39#include <stdlib.h>
40
41namespace doj
42{
43
44int 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
119int alphanum_comp(const std::string& l, const std::string& r)
120{
121 return alphanum_impl(l.c_str(), r.c_str());
122}
123
124int alphanum_comp(const char* l, const char* r)
125{
126 return alphanum_impl(l, r);
127}
128
130
131bool 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_impl(const char *l, const char *r)
Definition alphanum.cpp:44
int alphanum_comp(const std::string &l, const std::string &r)
Compare l and r with the same semantics as strcmp(), but with the "Alphanum Algorithm" which produces...
Definition alphanum.cpp:119
bool operator()(const std::string &left, const std::string &right) const
Definition alphanum.cpp:131
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41