-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtilities.h
executable file
·109 lines (93 loc) · 2.64 KB
/
Utilities.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#ifndef UTILITIES_H
#define UTILITIES_H
#include <tr1/unordered_map>
#include <cstddef>
namespace utils {
template <class K, class T>
class Multimap : public std::tr1::unordered_multimap<K,T> {
typedef typename std::tr1::unordered_multimap<K,T> _Base;
public:
typedef typename _Base::iterator iterator;
typedef typename _Base::const_iterator const_iterator;
typedef std::pair<iterator,iterator> iterator_pair;
typedef std::pair<const_iterator,const_iterator>
const_iterator_pair;
};
template <class K, class T>
class Map : public std::tr1::unordered_map<K,T> {
typedef typename std::tr1::unordered_map<K,T> _Base;
public:
typedef typename _Base::iterator iterator;
typedef typename _Base::const_iterator const_iterator;
typedef std::pair<iterator,iterator> iterator_pair;
typedef std::pair<const_iterator,const_iterator>
const_iterator_pair;
};
template <typename T>
struct Hash {
size_t operator()(T e) const {
return static_cast<std::size_t>(e);
}
};
template <typename T>
struct Hash<const T *const> {
std::size_t operator()(const T *const x) const {
return reinterpret_cast<std::size_t>(x);
}
};
template <typename T>
struct Max {
int operator()(T a) const { (void)a; return 0; }
int operator()(T a, int b, int c) const {
(void)a; (void)b; (void)c; return 0;
}
};
template <typename T>
struct Cmp {
int operator()(T a, T b) const {
return a == b ? 0 : a < b ? -1 : 1;
}
};
template <typename T>
struct Equal {
int operator()(T a, T b) const {
return a == b;
}
};
/* Usage example:
std::transform(m.begin(), m.end(), std::back_inserter(vk),
select1st<std::map<K, I>::value_type>()) ;
// */
// Select 1st from the pair.
template <typename Pair>
struct select1st {
typedef typename Pair::first_type result_type;
const result_type &operator()(const Pair &p) const {
return p.first;
}
};
// Select 2nd from the pair.
template <typename Pair>
struct select2nd {
typedef typename Pair::second_type result_type ;
const result_type &operator()(const Pair &p) const {
return p.second;
}
};
// This is a default overlap functor used below. Use this for types that
// don't use interval trees.
template <typename T>
struct Overlap {
bool operator()(T, T) const { return false; }
int low(T) const { return 0; }
int high(T) const { return 0; }
};
unsigned gcdSafe(unsigned, unsigned);
int lcm(int, int);
int tlz(unsigned);
int min(int, int);
int max(int, int);
unsigned umin(unsigned, unsigned);
unsigned umax(unsigned, unsigned);
} // End of utils namespace
#endif // UTILITIES_H