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