docs/html/alphanum_8h_source.html
| | Jetson Inference
DNN Vision Library |
alphanum.h
Go to the documentation of this file.
1 #ifndef ALPHANUM__HPP
2 #define ALPHANUM__HPP
3
4 /*
5 Released under the MIT License - https://opensource.org/licenses/MIT
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 and/or sell copies of the Software, and to permit persons to whom the
12 Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included
15 in all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
21 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
22 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
23 USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /* $Header: /code/doj/alphanum.hpp,v 1.3 2008/01/28 23:06:47 doj Exp $ */
27
28 #include <cassert>
29 #include <functional>
30 #include <string>
31 #include <sstream>
32
33 #ifdef ALPHANUM_LOCALE
34 #include <cctype>
35 #endif
36
37 #ifdef DOJDEBUG
38 #include <iostream>
39 #include <typeinfo>
40 #endif
41
42 // TODO: make comparison with hexadecimal numbers. Extend the alphanum_comp() function by traits to choose between decimal and hexadecimal.
43
45 {
46
47// anonymous namespace for functions we use internally. But if you
48// are coding in C, you can use alphanum_impl() directly, since it
49// uses not C++ features.
50namespace {
51
52// if you want to honour the locale settings for detecting digit
53// characters, you should define ALPHANUM_LOCALE
54 #ifdef ALPHANUM_LOCALE
55
56bool alphanum_isdigit(int c)
57 {
58return isdigit(c);
59 }
60 #else
61
65bool alphanum_isdigit(const char c)
66 {
67return c>='0' && c<='9';
68 }
69 #endif
70
86int alphanum_impl(const char *l, const char *r)
87 {
88enum mode_t { STRING, NUMBER } mode=STRING;
89
90while(*l && *r)
91 {
92if(mode == STRING)
93 {
94char l_char, r_char;
95while((l_char=*l) && (r_char=*r))
96 {
97// check if this are digit characters
98const bool l_digit=alphanum_isdigit(l_char), r_digit=alphanum_isdigit(r_char);
99// if both characters are digits, we continue in NUMBER mode
100if(l_digit && r_digit)
101 {
102 mode=NUMBER;
103break;
104 }
105// if only the left character is a digit, we have a result
106if(l_digit) return -1;
107// if only the right character is a digit, we have a result
108if(r_digit) return +1;
109// compute the difference of both characters
110const int diff=l_char - r_char;
111// if they differ we have a result
112if(diff != 0) return diff;
113// otherwise process the next characters
114 ++l;
115 ++r;
116 }
117 }
118else // mode==NUMBER
119 {
120 #ifdef ALPHANUM_LOCALE
121// get the left number
122char *end;
123unsigned long l_int=strtoul(l, &end, 0);
124 l=end;
125
126// get the right number
127unsigned long r_int=strtoul(r, &end, 0);
128 r=end;
129 #else
130// get the left number
131unsigned long l_int=0;
132while(*l && alphanum_isdigit(*l))
133 {
134// TODO: this can overflow
135 l_int=l_int*10 + *l-'0';
136 ++l;
137 }
138
139// get the right number
140unsigned long r_int=0;
141while(*r && alphanum_isdigit(*r))
142 {
143// TODO: this can overflow
144 r_int=r_int*10 + *r-'0';
145 ++r;
146 }
147 #endif
148
149// if the difference is not equal to zero, we have a comparison result
150const long diff=l_int-r_int;
151if(diff != 0)
152return diff;
153
154// otherwise we process the next substring in STRING mode
155 mode=STRING;
156 }
157 }
158
159if(*r) return -1;
160if(*l) return +1;
161return 0;
162 }
163
164 }
165
174template <typename lT, typename rT>
175int alphanum_comp(const lT& left, const rT& right)
176 {
177 #ifdef DOJDEBUG
178 std::clog << "alphanum_comp<" << typeid(left).name() << "," << typeid(right).name() << "> " << left << "," << right << std::endl;
179 #endif
180 std::ostringstream l; l << left;
181 std::ostringstream r; r << right;
182return alphanum_impl(l.str().c_str(), r.str().c_str());
183 }
184
192template <>
193int alphanum_comp<std::string>(const std::string& l, const std::string& r)
194 {
195 #ifdef DOJDEBUG
196 std::clog << "alphanum_comp<std::string,std::string> " << l << "," << r << std::endl;
197 #endif
198return alphanum_impl(l.c_str(), r.c_str());
199 }
200
202
203// now follow a lot of overloaded alphanum_comp() functions to get a
204// direct call to alphanum_impl() upon the various combinations of c
205// and c++ strings.
206
214int alphanum_comp(char* l, char* r)
215 {
216 assert(l);
217 assert(r);
218 #ifdef DOJDEBUG
219 std::clog << "alphanum_comp<char*,char*> " << l << "," << r << std::endl;
220 #endif
221return alphanum_impl(l, r);
222 }
223
224int alphanum_comp(const char* l, const char* r)
225 {
226 assert(l);
227 assert(r);
228 #ifdef DOJDEBUG
229 std::clog << "alphanum_comp<const char*,const char*> " << l << "," << r << std::endl;
230 #endif
231return alphanum_impl(l, r);
232 }
233
234int alphanum_comp(char* l, const char* r)
235 {
236 assert(l);
237 assert(r);
238 #ifdef DOJDEBUG
239 std::clog << "alphanum_comp<char*,const char*> " << l << "," << r << std::endl;
240 #endif
241return alphanum_impl(l, r);
242 }
243
244int alphanum_comp(const char* l, char* r)
245 {
246 assert(l);
247 assert(r);
248 #ifdef DOJDEBUG
249 std::clog << "alphanum_comp<const char*,char*> " << l << "," << r << std::endl;
250 #endif
251return alphanum_impl(l, r);
252 }
253
254int alphanum_comp(const std::string& l, char* r)
255 {
256 assert(r);
257 #ifdef DOJDEBUG
258 std::clog << "alphanum_comp<std::string,char*> " << l << "," << r << std::endl;
259 #endif
260return alphanum_impl(l.c_str(), r);
261 }
262
263int alphanum_comp(char* l, const std::string& r)
264 {
265 assert(l);
266 #ifdef DOJDEBUG
267 std::clog << "alphanum_comp<char*,std::string> " << l << "," << r << std::endl;
268 #endif
269return alphanum_impl(l, r.c_str());
270 }
271
272int alphanum_comp(const std::string& l, const char* r)
273 {
274 assert(r);
275 #ifdef DOJDEBUG
276 std::clog << "alphanum_comp<std::string,const char*> " << l << "," << r << std::endl;
277 #endif
278return alphanum_impl(l.c_str(), r);
279 }
280
281int alphanum_comp(const char* l, const std::string& r)
282 {
283 assert(l);
284 #ifdef DOJDEBUG
285 std::clog << "alphanum_comp<const char*,std::string> " << l << "," << r << std::endl;
286 #endif
287return alphanum_impl(l, r.c_str());
288 }
289
291
297template<class Ty>
298struct alphanum_less : public std::binary_function<Ty, Ty, bool>
299 {
300bool operator()(const Ty& left, const Ty& right) const
301 {
302return alphanum_comp(left, right) < 0;
303 }
304 };
305
306 }
307
308 #ifdef TESTMAIN
309 #include <algorithm>
310 #include <iostream>
311 #include <iterator>
312 #include <map>
313 #include <set>
314 #include <vector>
315 int main()
316 {
317// testcases for the algorithm
318 assert(doj::alphanum_comp("","") == 0);
319 assert(doj::alphanum_comp("","a") < 0);
320 assert(doj::alphanum_comp("a","") > 0);
321 assert(doj::alphanum_comp("a","a") == 0);
322 assert(doj::alphanum_comp("","9") < 0);
323 assert(doj::alphanum_comp("9","") > 0);
324 assert(doj::alphanum_comp("1","1") == 0);
325 assert(doj::alphanum_comp("1","2") < 0);
326 assert(doj::alphanum_comp("3","2") > 0);
327 assert(doj::alphanum_comp("a1","a1") == 0);
328 assert(doj::alphanum_comp("a1","a2") < 0);
329 assert(doj::alphanum_comp("a2","a1") > 0);
330 assert(doj::alphanum_comp("a1a2","a1a3") < 0);
331 assert(doj::alphanum_comp("a1a2","a1a0") > 0);
332 assert(doj::alphanum_comp("134","122") > 0);
333 assert(doj::alphanum_comp("12a3","12a3") == 0);
334 assert(doj::alphanum_comp("12a1","12a0") > 0);
335 assert(doj::alphanum_comp("12a1","12a2") < 0);
336 assert(doj::alphanum_comp("a","aa") < 0);
337 assert(doj::alphanum_comp("aaa","aa") > 0);
338 assert(doj::alphanum_comp("Alpha 2","Alpha 2") == 0);
339 assert(doj::alphanum_comp("Alpha 2","Alpha 2A") < 0);
340 assert(doj::alphanum_comp("Alpha 2 B","Alpha 2") > 0);
341
342 assert(doj::alphanum_comp(1,1) == 0);
343 assert(doj::alphanum_comp(1,2) < 0);
344 assert(doj::alphanum_comp(2,1) > 0);
345 assert(doj::alphanum_comp(1.2,3.14) < 0);
346 assert(doj::alphanum_comp(3.14,2.71) > 0);
347 assert(doj::alphanum_comp(true,true) == 0);
348 assert(doj::alphanum_comp(true,false) > 0);
349 assert(doj::alphanum_comp(false,true) < 0);
350
351 std::string str("Alpha 2");
352 assert(doj::alphanum_comp(str,"Alpha 2") == 0);
353 assert(doj::alphanum_comp(str,"Alpha 2A") < 0);
354 assert(doj::alphanum_comp("Alpha 2 B",str) > 0);
355
356 assert(doj::alphanum_comp(str,strdup("Alpha 2")) == 0);
357 assert(doj::alphanum_comp(str,strdup("Alpha 2A")) < 0);
358 assert(doj::alphanum_comp(strdup("Alpha 2 B"),str) > 0);
359
360 #if 1
361// show usage of the comparison functor with a set
362 std::set<std::string, doj::alphanum_less<std::string> > s;
363 s.insert("Xiph Xlater 58");
364 s.insert("Xiph Xlater 5000");
365 s.insert("Xiph Xlater 500");
366 s.insert("Xiph Xlater 50");
367 s.insert("Xiph Xlater 5");
368 s.insert("Xiph Xlater 40");
369 s.insert("Xiph Xlater 300");
370 s.insert("Xiph Xlater 2000");
371 s.insert("Xiph Xlater 10000");
372 s.insert("QRS-62F Intrinsia Machine");
373 s.insert("QRS-62 Intrinsia Machine");
374 s.insert("QRS-60F Intrinsia Machine");
375 s.insert("QRS-60 Intrinsia Machine");
376 s.insert("Callisto Morphamax 7000 SE2");
377 s.insert("Callisto Morphamax 7000 SE");
378 s.insert("Callisto Morphamax 7000");
379 s.insert("Callisto Morphamax 700");
380 s.insert("Callisto Morphamax 600");
381 s.insert("Callisto Morphamax 5000");
382 s.insert("Callisto Morphamax 500");
383 s.insert("Callisto Morphamax");
384 s.insert("Alpha 2A-900");
385 s.insert("Alpha 2A-8000");
386 s.insert("Alpha 2A");
387 s.insert("Alpha 200");
388 s.insert("Alpha 2");
389 s.insert("Alpha 100");
390 s.insert("Allegia 60 Clasteron");
391 s.insert("Allegia 52 Clasteron");
392 s.insert("Allegia 51B Clasteron");
393 s.insert("Allegia 51 Clasteron");
394 s.insert("Allegia 500 Clasteron");
395 s.insert("Allegia 50 Clasteron");
396 s.insert("40X Radonius");
397 s.insert("30X Radonius");
398 s.insert("20X Radonius Prime");
399 s.insert("20X Radonius");
400 s.insert("200X Radonius");
401 s.insert("10X Radonius");
402 s.insert("1000X Radonius Maximus");
403// print sorted set to cout
404 std::copy(s.begin(), s.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
405
406// show usage of comparision functor with a map
407typedef std::map<std::string, int, doj::alphanum_less<std::string> > m_t;
408 m_t m;
409 m["z1.doc"]=1;
410 m["z10.doc"]=2;
411 m["z100.doc"]=3;
412 m["z101.doc"]=4;
413 m["z102.doc"]=5;
414 m["z11.doc"]=6;
415 m["z12.doc"]=7;
416 m["z13.doc"]=8;
417 m["z14.doc"]=9;
418 m["z15.doc"]=10;
419 m["z16.doc"]=11;
420 m["z17.doc"]=12;
421 m["z18.doc"]=13;
422 m["z19.doc"]=14;
423 m["z2.doc"]=15;
424 m["z20.doc"]=16;
425 m["z3.doc"]=17;
426 m["z4.doc"]=18;
427 m["z5.doc"]=19;
428 m["z6.doc"]=20;
429 m["z7.doc"]=21;
430 m["z8.doc"]=22;
431 m["z9.doc"]=23;
432// print sorted map to cout
433for(m_t::iterator i=m.begin(); i!=m.end(); ++i)
434 std::cout << i->first << '\t' << i->second << std::endl;
435
436// show usage of comparison functor with an STL algorithm on a vector
437 std::vector<std::string> v;
438// vector contents are reversed sorted contents of the old set
439 std::copy(s.rbegin(), s.rend(), std::back_inserter(v));
440// now sort the vector with the algorithm
441 std::sort(v.begin(), v.end(), doj::alphanum_less<std::string>());
442// and print the vector to cout
443 std::copy(v.begin(), v.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
444 #endif
445
446return 0;
447 }
448 #endif
449
450 #endif
Definition: alphanum.h:44
Functor class to compare two objects with the "Alphanum Algorithm".
Definition: alphanum.h:298
int alphanum_comp(const lT &left, const rT &right)
Compare left and right with the same semantics as strcmp(), but with the "Alphanum Algorithm" which p...
Definition: alphanum.h:175
doj::alphanum_less::operator()
bool operator()(const Ty &left, const Ty &right) const
Definition: alphanum.h:300