alpar@906
|
1 |
/* -*- C++ -*-
|
ladanyi@1435
|
2 |
* lemon/bits/extended_pair.h - Part of LEMON, a generic C++ optimization library
|
alpar@906
|
3 |
*
|
alpar@1164
|
4 |
* Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@1359
|
5 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@906
|
6 |
*
|
alpar@906
|
7 |
* Permission to use, modify and distribute this software is granted
|
alpar@906
|
8 |
* provided that this copyright notice appears in all copies. For
|
alpar@906
|
9 |
* precise terms see the accompanying LICENSE file.
|
alpar@906
|
10 |
*
|
alpar@906
|
11 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@906
|
12 |
* express or implied, and with no claim as to its suitability for any
|
alpar@906
|
13 |
* purpose.
|
alpar@906
|
14 |
*
|
alpar@906
|
15 |
*/
|
alpar@906
|
16 |
|
alpar@921
|
17 |
#ifndef LEMON_EXTENDED_PAIR_H
|
alpar@921
|
18 |
#define LEMON_EXTENDED_PAIR_H
|
deba@782
|
19 |
|
deba@1430
|
20 |
///\ingroup misc
|
deba@1430
|
21 |
///\file
|
deba@1430
|
22 |
///\brief A more customizable pair type than std::pair.
|
deba@782
|
23 |
|
deba@1430
|
24 |
namespace lemon {
|
deba@1430
|
25 |
|
deba@1430
|
26 |
/// \brief A more customizable pair type than std::pair.
|
deba@1430
|
27 |
///
|
deba@1430
|
28 |
/// This type is a customizable pair type. The main goal
|
deba@1430
|
29 |
/// is that the constructor's parameter type does not depend
|
deba@1430
|
30 |
/// on the stored data type. This way it is possible to store
|
deba@1430
|
31 |
/// references in the extended_pair.
|
deba@1430
|
32 |
/// \code
|
deba@1430
|
33 |
/// int a; char b;
|
deba@1430
|
34 |
/// typedef extended_pair<int&, int&, char&, char&> ICPair;
|
deba@1430
|
35 |
/// ICPair p(a, b);
|
deba@1430
|
36 |
/// // like a real reference to an std::pair<int, char>
|
deba@1430
|
37 |
/// // but the pair does not exist
|
deba@1430
|
38 |
/// p.first = 42;
|
deba@1430
|
39 |
/// p.second = '@';
|
deba@1430
|
40 |
/// \endcode
|
deba@1430
|
41 |
/// \param T1 The type of first.
|
deba@1430
|
42 |
/// \param A1 The parameter type for first.
|
deba@1430
|
43 |
/// \param T2 The type of second.
|
deba@1430
|
44 |
/// \param A2 The parameter type for second.
|
deba@1430
|
45 |
template <typename T1, typename A1, typename T2, typename A2>
|
deba@1430
|
46 |
struct extended_pair {
|
deba@1430
|
47 |
/// \brief The type of first.
|
deba@1430
|
48 |
///
|
deba@1430
|
49 |
/// The type of first.
|
deba@1430
|
50 |
typedef T1 first_type;
|
deba@1430
|
51 |
/// \brief The type of second.
|
deba@1430
|
52 |
///
|
deba@1430
|
53 |
/// The type of second.
|
deba@1430
|
54 |
typedef T2 second_type;
|
deba@782
|
55 |
|
deba@1430
|
56 |
/// \brief Default constructor.
|
deba@1430
|
57 |
///
|
deba@1430
|
58 |
/// Default constructor. It calls the default constructor of
|
deba@1430
|
59 |
/// first and second.
|
deba@1430
|
60 |
extended_pair() : first(), second() {}
|
deba@782
|
61 |
|
deba@1430
|
62 |
/// \brief Constructor.
|
deba@1430
|
63 |
///
|
deba@1430
|
64 |
/// Constructor.
|
deba@1430
|
65 |
extended_pair(A1 f, A2 s) : first(f), second(s) {}
|
deba@782
|
66 |
|
deba@1430
|
67 |
/// \brief Template constructor.
|
deba@1430
|
68 |
///
|
deba@1430
|
69 |
/// Template constructor. It copies everything which has
|
deba@1430
|
70 |
/// \c first and \c second member.
|
deba@1430
|
71 |
template <class Pair>
|
deba@1430
|
72 |
extended_pair(const Pair& pair) : first(pair.first), second(pair.second) {}
|
deba@782
|
73 |
|
deba@1430
|
74 |
/// \brief The first value
|
deba@1430
|
75 |
///
|
deba@1430
|
76 |
/// The first value
|
deba@1430
|
77 |
T1 first;
|
deba@1430
|
78 |
/// \brief The second value
|
deba@1430
|
79 |
///
|
deba@1430
|
80 |
/// The second value
|
deba@1430
|
81 |
T2 second;
|
deba@1430
|
82 |
};
|
deba@1430
|
83 |
|
deba@1430
|
84 |
/// \brief Equality operator
|
deba@1430
|
85 |
///
|
deba@1430
|
86 |
/// Equality operator
|
deba@1430
|
87 |
template <typename T1, typename T2,
|
deba@1430
|
88 |
typename LA1, typename LA2, typename RA1, typename RA2>
|
deba@1430
|
89 |
bool operator==(const extended_pair<T1, LA1, T2, LA2>& left,
|
deba@1430
|
90 |
const extended_pair<T1, RA1, T2, RA2>& right) {
|
deba@1430
|
91 |
return left.first == right.first && left.second == right.second;
|
deba@1430
|
92 |
}
|
deba@1430
|
93 |
|
deba@1430
|
94 |
/// \brief Inequality operator.
|
deba@1430
|
95 |
///
|
deba@1430
|
96 |
/// Inequality operator.
|
deba@1430
|
97 |
template <typename T1, typename T2,
|
deba@1430
|
98 |
typename LA1, typename LA2, typename RA1, typename RA2>
|
deba@1430
|
99 |
bool operator!=(const extended_pair<T1, LA1, T2, LA2>& left,
|
deba@1430
|
100 |
const extended_pair<T1, RA1, T2, RA2>& right) {
|
deba@1430
|
101 |
return !(left == right);
|
deba@1430
|
102 |
}
|
deba@1430
|
103 |
|
deba@1430
|
104 |
/// \brief Less operator.
|
deba@1430
|
105 |
///
|
deba@1430
|
106 |
/// Less operator.
|
deba@1430
|
107 |
template <typename T1, typename T2,
|
deba@1430
|
108 |
typename LA1, typename LA2, typename RA1, typename RA2>
|
deba@1430
|
109 |
bool operator<(const extended_pair<T1, LA1, T2, LA2>& left,
|
deba@1430
|
110 |
const extended_pair<T1, RA1, T2, RA2>& right) {
|
deba@1430
|
111 |
return left.first < right.first ||
|
deba@1430
|
112 |
(!(right.first<left.first) && left.second < right.second);
|
deba@1430
|
113 |
}
|
deba@1430
|
114 |
|
deba@1430
|
115 |
/// \brief Greater operator.
|
deba@1430
|
116 |
///
|
deba@1430
|
117 |
/// Greater operator.
|
deba@1430
|
118 |
template <typename T1, typename T2,
|
deba@1430
|
119 |
typename LA1, typename LA2, typename RA1, typename RA2>
|
deba@1430
|
120 |
bool operator>(const extended_pair<T1, LA1, T2, LA2>& left,
|
deba@1430
|
121 |
const extended_pair<T1, RA1, T2, RA2>& right) {
|
deba@1430
|
122 |
return right < left;
|
deba@1430
|
123 |
}
|
deba@1430
|
124 |
|
deba@1430
|
125 |
/// \brief Less or equal operator.
|
deba@1430
|
126 |
///
|
deba@1430
|
127 |
/// Less or equal operator.
|
deba@1430
|
128 |
template <typename T1, typename T2,
|
deba@1430
|
129 |
typename LA1, typename LA2, typename RA1, typename RA2>
|
deba@1430
|
130 |
bool operator<=(const extended_pair<T1, LA1, T2, LA2>& left,
|
deba@1430
|
131 |
const extended_pair<T1, RA1, T2, RA2>& right) {
|
deba@1430
|
132 |
return !(right > left);
|
deba@1430
|
133 |
}
|
deba@1430
|
134 |
|
deba@1430
|
135 |
/// \brief Greater or equal operator.
|
deba@1430
|
136 |
///
|
deba@1430
|
137 |
/// Greater or equal operator.
|
deba@1430
|
138 |
template <typename T1, typename T2,
|
deba@1430
|
139 |
typename LA1, typename LA2, typename RA1, typename RA2>
|
deba@1430
|
140 |
bool operator>=(const extended_pair<T1, LA1, T2, LA2>& left,
|
deba@1430
|
141 |
const extended_pair<T1, RA1, T2, RA2>& right) {
|
deba@1430
|
142 |
return !(right < left);
|
deba@1430
|
143 |
}
|
deba@1430
|
144 |
|
deba@782
|
145 |
}
|
deba@782
|
146 |
#endif
|