Predator  [unstable] git snapshot
util.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010 Kamil Dudka <kdudka@redhat.com>
3  *
4  * This file is part of predator.
5  *
6  * predator is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * predator is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with predator. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef H_GUARD_UTIL_H
21 #define H_GUARD_UTIL_H
22 
23 #include <algorithm> // for std::find()
24 #include <cstring>
25 
26 #ifndef STREQ
27 # define STREQ(s1, s2) (0 == strcmp(s1, s2))
28 #endif
29 
30 #define FIXW(w) std::fixed << std::setfill('0') << std::setw(w)
31 
32 #define RETURN_IF_COMPARED(a, b, member) do { \
33  if (a.member < b.member) \
34  return true; \
35  if (b.member < a.member) \
36  return false; \
37 } while (0)
38 
39 template <typename T>
40 void swapValues(T &a, T &b)
41 {
42  const T tmp = a;
43  a = b;
44  b = tmp;
45 }
46 
47 // ensure (a <= b)
48 template <typename T>
49 void sortValues(T &a, T &b)
50 {
51  if (b < a)
52  swapValues(a, b);
53 }
54 
55 template <typename TCont>
56 bool hasItem(const TCont &cont, const typename TCont::value_type &key)
57 {
58  return cont.end() != std::find(cont.begin(), cont.end(), key);
59 }
60 
61 template <typename TCont>
62 bool hasKey(const TCont &cont, const typename TCont::key_type &key)
63 {
64  return cont.end() != cont.find(key);
65 }
66 
67 template <typename TCont>
68 bool hasKey(const TCont *cont, const typename TCont::key_type &key)
69 {
70  return hasKey(*cont, key);
71 }
72 
73 template <typename TCont>
74 bool insertOnce(TCont &cont, const typename TCont::key_type &key)
75 {
76  return cont.insert(key)./* inserted */second;
77 }
78 
79 template <class TStack, class TFirst, class TSecond>
80 void push(TStack &dst, const TFirst &first, const TSecond &second)
81 {
82  dst.push(typename TStack::value_type(first, second));
83 }
84 
85 template <class TStack, class TFirst, class TSecond>
86 void push(TStack *dst, const TFirst &first, const TSecond &second)
87 {
88  push(*dst, first, second);
89 }
90 
91 #endif /* H_GUARD_UTIL_H */