FNSS ns-3 adapter
ns-3 adapter for the Fast Network Simulation Setup (FNSS) toolchain
 All Classes Files 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 
41 template <class T1, class T2> class Pair : public RelOP<Pair<T1, T2> >{
42 public:
43  T1 &first;
44  T2 &second;
45 
46  Pair(bool commutative_ = false) :
47  first(stlPair.first), second(stlPair.second), stlPair(), commutative(commutative_) {}
48 
49  Pair(const T1 &first, const T2 &second, bool commutative = false) :
50  first(stlPair.first), second(stlPair.second), stlPair(first, second) {
51 
52  this->commutative = commutative;
53  }
54 
55  Pair(const std::pair <T1, T2> &p, bool commutative = false) :
56  first(stlPair.first), second(stlPair.second), stlPair(p) {
57 
58  this->commutative = commutative;
59  }
60 
61  Pair(const Pair<T1, T2> &other) :
62  first(stlPair.first), second(stlPair.second), stlPair(other.stlPair) {
63 
64  this->commutative = other.commutative;
65  }
66 
67  std::pair<T1, T2> getStlPair() const {
68  return this->stlPair;
69  }
70 
71  bool getCommutative() const {
72  return this->commutative;
73  }
74 
75  void setCommutative(bool commutative) {
76  this->commutative = commutative;
77  }
78 
79  Pair& operator=(const Pair &rhs) {
80  this->stlPair = rhs.stlPair;
81 
82  return *this;
83  }
84 
85  bool operator==(const Pair &rhs) const {
86  if(this->commutative || rhs.commutative) {
87  if((this->first == rhs.first && this->second == rhs.second) ||
88  (this->second == rhs.first && this->first == rhs.second))
89  return true;
90  else
91  return false;
92  } else {
93  return this->stlPair == rhs.stlPair;
94  }
95  }
96 
97  bool operator<(const Pair &rhs) const {
98  if (this->commutative || rhs.commutative) {
99  if (this->first < this->second) {
100  if (rhs.first < rhs.second)
101  return this->first < rhs.first || (this->first == rhs.first && this->second < rhs.second);
102  else
103  return this->first < rhs.second || (this->first == rhs.second && this->second < rhs.first);
104  } else {
105  if (rhs.first < rhs.second)
106  return this->second < rhs.first || (this->second == rhs.first && this->first < rhs.second);
107  else
108  return this->second < rhs.second || (this->second == rhs.second && this->first < rhs.first);
109  }
110  } else {
111  return this->first < rhs.first || (this->first == rhs.first && this->second < rhs.second);
112  }
113  }
114 
115 private:
116  std::pair <T1, T2> stlPair;
117  bool commutative;
118 };
119 
120 } //namespace
121 
122 #endif //PAIR_H
Definition: pair.h:11
Definition: pair.h:41