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 #ifndef ALPHANUM__HPP
39 #define ALPHANUM__HPP
40 
41 #include <hugin_shared.h>
42 #include <functional>
43 #include <string>
44 
45 // TODO: make comparison with hexadecimal numbers. Extend the alphanum_comp() function by traits to choose between decimal and hexadecimal.
46 
47 namespace doj
48 {
49 
50 /**
51    Compare l and r with the same semantics as strcmp(), but with
52    the "Alphanum Algorithm" which produces more human-friendly
53    results.
54 
55    @return negative if l<r, 0 if l==r, positive if l>r.
56 */
57 IMPEX int alphanum_comp(const std::string& l, const std::string& r);
58 IMPEX int alphanum_comp(const char* l, const char* r);
59 
60 ////////////////////////////////////////////////////////////////////////////
61 /**
62    Functor class to compare two objects with the "Alphanum
63    Algorithm". If the objects are no std::string, they must
64    implement "std::ostream operator<< (std::ostream&, const Ty&)".
65 */
66 struct IMPEX alphanum_less : public std::binary_function<const std::string&, const std::string&, bool>
67 {
68     bool operator()(const std::string& left, const std::string& right) const;
69 };
70 
71 }
72 
73 #endif
74