FNSS C++ library
C++ API for the Fast Network Simulation Setup (FNSS) toolchain
 All Classes Functions Typedefs Pages
pair.h
1 #ifndef PAIR_H
2 #define PAIR_H
3 
4 #include <utility>
5 
6 namespace fnss {
7 
8 // You get operators !=,<=,>,>= if you define operators < and == and inherit from
9 // this class using your class as the template parameter. Possible
10 template <typename D>
11 class RelOP {
12 public:
13  RelOP() : derived(*static_cast<const D*>(this)) {}
14  bool operator!=(const D &rhs) const {
15  return !(derived == rhs);
16  }
17 
18  bool operator<=(const D &rhs) const {
19  return derived < rhs || derived == rhs;
20  }
21 
22  bool operator>(const D &rhs) const {
23  return !(derived < rhs) && !(derived == rhs);
24  }
25 
26  bool operator>=(const D &rhs) const {
27  return !(derived > rhs);
28  }
29 
30 private:
31  const D& derived;
32 };
33 
42 template <class T1, class T2> class Pair : public RelOP<Pair<T1, T2> >{
43 public:
44  T1 &first;
45  T2 &second;
46 
47  Pair(bool commutative_ = false) :
48  first(stlPair.first), second(stlPair.second), stlPair(),
49  commutative(commutative_) {}
50 
51  Pair(const T1 &first_, const T2 &second_, bool commutative_ = false) :
52  first(stlPair.first), second(stlPair.second), stlPair(first_, second_),
53  commutative(commutative_) {}
54 
55  Pair(const std::pair <T1, T2> &p, bool commutative_ = false) :
56  first(stlPair.first), second(stlPair.second), stlPair(p),
57  commutative(commutative_) {}
58 
59  Pair(const Pair<T1, T2> &other) :
60  first(stlPair.first), second(stlPair.second), stlPair(other.stlPair),
61  commutative(other.commutative) {}
62 
63  std::pair<T1, T2> getStlPair() const {
64  return this->stlPair;
65  }
66 
67  bool getCommutative() const {
68  return this->commutative;
69  }
70 
71  void setCommutative(bool commutative_) {
72  this->commutative = commutative_;
73  }
74 
75  Pair& operator=(const Pair &rhs) {
76  this->stlPair = rhs.stlPair;
77 
78  return *this;
79  }
80 
81  bool operator==(const Pair &rhs) const {
82  if(this->commutative || rhs.commutative) {
83  if((this->first == rhs.first && this->second == rhs.second) ||
84  (this->second == rhs.first && this->first == rhs.second))
85  return true;
86  else
87  return false;
88  } else {
89  return this->stlPair == rhs.stlPair;
90  }
91  }
92 
93  // Non-commutative pair: lexicographic order.
94  // Commutative pair: behaves as if first and second were sorted inside the pair.
95  bool operator<(const Pair &rhs) const {
96  if (this->commutative || rhs.commutative) {
97  if (this->first < this->second) {
98  if (rhs.first < rhs.second)
99  return this->first < rhs.first || (this->first == rhs.first && this->second < rhs.second);
100  else
101  return this->first < rhs.second || (this->first == rhs.second && this->second < rhs.first);
102  } else {
103  if (rhs.first < rhs.second)
104  return this->second < rhs.first || (this->second == rhs.first && this->first < rhs.second);
105  else
106  return this->second < rhs.second || (this->second == rhs.second && this->first < rhs.first);
107  }
108  } else {
109  return this->first < rhs.first || (this->first == rhs.first && this->second < rhs.second);
110  }
111  }
112 
113 private:
114  std::pair <T1, T2> stlPair;
115  bool commutative;
116 };
117 
118 } //namespace
119 
120 #endif //PAIR_H
Definition: pair.h:11
Definition: pair.h:42