Evo C++ Library v0.5.1
pair.h
Go to the documentation of this file.
1 // Evo C++ Library
2 /* Copyright 2019 Justin Crowell
3 Distributed under the BSD 2-Clause License -- see included file LICENSE.txt for details.
4 */
6 
7 #pragma once
8 #ifndef INCL_evo_pair_h
9 #define INCL_evo_pair_h
10 
11 #include "impl/container.h"
12 
13 namespace evo {
16 
18 
31 template<class TA, class TB>
32 struct Pair {
33  typedef Pair<TA, TB> This;
34 
35  typedef TA A;
36  typedef TA First;
37  typedef TA Key;
38  typedef TB B;
39  typedef TB Second;
40  typedef TB Value;
41 
42  TA first;
43  TB second;
44 
46  Pair() {
49  }
50 
54  Pair(const TA& a) : first(a) {
56  }
57 
62  Pair(const TA& a, const TB& b) : first(a), second(b) {
63  }
64 
68  Pair(const This& src) : first(src.first), second(src.second) {
69  }
70 
75  This& operator=(const This& src) {
76  first = src.first;
77  second = src.second;
78  return *this;
79  }
80 
81 #if defined(EVO_CPP11)
82 
85  Pair(This&& src) : first(std::move(src.first)), second(std::move(src.second)) {
86  }
87 
92  This& operator=(This&& src) {
93  first = std::move(src.first);
94  second = std::move(src.second);
95  return *this;
96  }
97 #endif
98 
102  const A& a() const
103  { return first; }
104 
108  A& a()
109  { return first; }
110 
114  const Key& key() const
115  { return first; }
116 
120  Key& key()
121  { return first; }
122 
126  const B& b() const
127  { return second; }
128 
132  B& b()
133  { return second; }
134 
138  const Value& value() const
139  { return second; }
140 
144  Value& value()
145  { return second; }
146 
151  ulong hash(ulong seed=0) const {
152  seed = DataHash<TA>::hash(first, seed);
153  return DataHash<TB>::hash(second, seed);
154  }
155 
162  int compare(const This& src) const {
163  int result = DataCompare<A>::compare(first, src.first);
164  if (result == 0)
165  result = DataCompare<B>::compare(second, src.second);
166  return result;
167  }
168 
175  bool operator==(const This& src) const
176  { return (first == src.first && second == src.second); }
177 
184  bool operator!=(const This& src) const
185  { return (first != src.first || second != src.second); }
186 
193  bool operator<(const This& src) const
194  { return (first < src.first || (first == src.first && second < src.second)); }
195 
202  bool operator<=(const This& src) const
203  { return (first < src.first || (first == src.first && second <= src.second)); }
204 
211  bool operator>(const This& src) const
212  { return (first > src.first || (first == src.first && second > src.second)); }
213 
219  bool operator>=(const This& src) const
220  { return (first > src.first || (first == src.first && second >= src.second)); }
221 };
222 
224 
225 }
226 #endif
bool operator<=(const This &src) const
Compare whether less than or equal to given pair.
Definition: pair.h:202
const A & a() const
Get first value (const).
Definition: pair.h:102
This & operator=(const This &src)
Assignment operator.
Definition: pair.h:75
bool operator>(const This &src) const
Compare whether greater than given pair.
Definition: pair.h:211
bool operator==(const This &src) const
Compare for equality.
Definition: pair.h:175
Pair(const This &src)
Copy constructor.
Definition: pair.h:68
const B & b() const
Get second value (const).
Definition: pair.h:126
TA A
First value type (same as Key)
Definition: pair.h:35
Pair(This &&src)
Move constructor (C++11).
Definition: pair.h:85
A & a()
Get first value.
Definition: pair.h:108
TB Value
Pair value type (same as B)
Definition: pair.h:40
TA first
First value (same as a() and key())
Definition: pair.h:42
static void set_default_pod(T &val)
Set new POD value to default value (0).
Definition: container.h:569
const Value & value() const
Get value for pair (second value) (const).
Definition: pair.h:138
TA First
First value type (same as Key)
Definition: pair.h:36
TB Second
Second value type (same as Value)
Definition: pair.h:39
bool operator!=(const This &src) const
Compare for inequality.
Definition: pair.h:184
Evo container foundation types and macros.
static ulong hash(const T *data, ulong size, ulong seed=0)
Compute hash value from data.
Definition: container.h:899
int compare(const This &src) const
Compare with another instance.
Definition: pair.h:162
Pair()
Constructor.
Definition: pair.h:46
Evo C++ Library namespace.
Definition: alg.h:11
B & b()
Get second value.
Definition: pair.h:132
Pair(const TA &a)
Constructor.
Definition: pair.h:54
const Key & key() const
Get key for pair (first value) (const).
Definition: pair.h:114
Key & key()
Get key for pair (first value).
Definition: pair.h:120
static int compare(const T *data1, ulong size1, const T *data2, ulong size2)
Compare data.
Definition: container.h:769
Pair< TA, TB > This
This type.
Definition: pair.h:33
TB B
Second value type (same as Value)
Definition: pair.h:38
TA Key
Pair key type (same as A)
Definition: pair.h:37
Value & value()
Get value for pair (second value).
Definition: pair.h:144
bool operator>=(const This &src) const
Compare whether greater than or equal to given pair.
Definition: pair.h:219
Stores a key/value pair of independent objects or values.
Definition: pair.h:32
This & operator=(This &&src)
Move assignment operator (C++11).
Definition: pair.h:92
bool operator<(const This &src) const
Compare whether less than given pair.
Definition: pair.h:193
Pair(const TA &a, const TB &b)
Constructor.
Definition: pair.h:62
TB second
Second value (same as b() and value())
Definition: pair.h:43
ulong hash(ulong seed=0) const
Get hash value for both values in pair.
Definition: pair.h:151