P4C
The P4 Compiler
Loading...
Searching...
No Matches
cmp.h
1
19#include <cstring>
20#include <functional>
21
22#ifndef BF_P4C_LIB_CMP_H_
23#define BF_P4C_LIB_CMP_H_
24
27
28#define OPERATOR(op, Op) \
29 struct Op { \
30 bool operator()(const T *left, const T *right) const { return op(left, right); } \
31 }; \
32 static bool op(const T *left, const T *right)
33
36template <class T>
37class LiftEqual {
38 public:
39 virtual bool operator==(const T &) const = 0;
40
41 bool operator!=(const T &other) const { return !(operator==(other)); }
42
43 OPERATOR(equal, Equal) {
44 if (left != right && left && right) return *left == *right;
45 return left == right;
46 }
47
48 OPERATOR(not_equal, NotEqual) { return !equal(left, right); }
49};
50
54template <class T>
55class LiftLess {
56 public:
57 virtual bool operator<(const T &) const = 0;
58
59 bool operator==(const T &other) const { return !operator!=(other); }
60
61 bool operator!=(const T &other) const { return operator<(other) || operator>(other); }
62
63 bool operator>(const T &other) const { return other.operator<(*dynamic_cast<const T *>(this)); }
64
65 bool operator<=(const T &other) const { return !operator>(other); }
66 bool operator>=(const T &other) const { return !(operator<(other)); }
67
68 OPERATOR(not_equal, NotEqual) { return less(left, right) || greater(left, right); }
69
70 OPERATOR(less, Less) {
71 if (left != right && left && right) return *left < *right;
72 return left < right;
73 }
74
75 OPERATOR(equal, Equal) { return !not_equal(left, right); }
76 OPERATOR(greater, Greater) { return less(right, left); }
77 OPERATOR(less_equal, LessEqual) { return !greater(left, right); }
78 OPERATOR(greater_equal, GreaterEqual) { return !less(left, right); }
79};
80
83template <class T>
84class LiftCompare : public LiftEqual<T>, public LiftLess<T> {
85 public:
86 bool operator!=(const T &other) const { return LiftEqual<T>::operator!=(other); }
87
88 OPERATOR(equal, Equal) { return LiftEqual<T>::equal(left, right); }
89
90 OPERATOR(not_equal, NotEqual) { return LiftEqual<T>::not_equal(left, right); }
91};
92
96template <class T>
98 public:
99 bool operator()(const T &a, const T &b) const {
100 return std::strcmp(a.getName().name, b.getName().name) < 0;
101 }
102
103 bool operator()(const T *a, const T *b) const {
104 // note: std::less is guaranteed to be defined for pointers, operator < is not
105 return a && b ? (*this)(*a, *b) : std::less<T *>()(a, b);
106 }
107};
108
114template <class A, class B, class AComp = std::less<A>, class BCmp = std::less<B>>
115class PairLess {
116 public:
117 using Pair = std::pair<A, B>;
118 bool operator()(const Pair &a, const Pair &b) const {
119 AComp acmp;
120 if (acmp(a.first, b.first)) return true;
121 // !(a < b) && !(b < a) => a == b
122 if (!acmp(b.first, a.first)) return BCmp()(a.second, b.second);
123 return false;
124 }
125};
126#endif /* BF_P4C_LIB_CMP_H_ */
Definition cmp.h:97
Definition cmp.h:84
Definition cmp.h:37
Definition cmp.h:55
Definition cmp.h:115