6 * $Date: 2012-07-16 14:23:36 +0200 (Mo, 16. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration and implementation of class Tuple2, Tuple3
13 * \author Carsten Gutwenger
16 * This file is part of the Open Graph Drawing Framework (OGDF).
20 * See README.txt in the root directory of the OGDF installation for details.
23 * This program is free software; you can redistribute it and/or
24 * modify it under the terms of the GNU General Public License
25 * Version 2 or 3 as published by the Free Software Foundation;
26 * see the file LICENSE.txt included in the packaging of this file
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public
37 * License along with this program; if not, write to the Free
38 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
39 * Boston, MA 02110-1301, USA.
41 * \see http://www.gnu.org/copyleft/gpl.html
42 ***************************************************************/
53 #include <ogdf/basic/basic.h>
54 #include <ogdf/basic/Hashing.h>
59 //! Tuples of two elements (2-tuples).
61 * @tparam E1 is the data type for the first element.
62 * @tparam E2 is the data type for the second element.
64 template<class E1
, class E2
> class Tuple2
{
66 E1 m_x1
; //!< The first element.
67 E2 m_x2
; //!< The second element.
69 //! Constructs a 2-tuple using default constructors.
71 //! Constructs a 2-tuple for given values.
72 Tuple2(const E1
&y1
, const E2
&y2
) : m_x1(y1
), m_x2(y2
) { }
73 //! Constructs a 2-tuple that is a copy of \a t2.
74 Tuple2(const Tuple2
<E1
,E2
> &t2
) : m_x1(t2
.m_x1
), m_x2(t2
.m_x2
) { }
76 //! Returns a reference the first element.
77 const E1
&x1() const { return m_x1
; }
78 //! Returns a reference the second element.
79 const E2
&x2() const { return m_x2
; }
81 //! Returns a reference the first element.
82 E1
&x1() { return m_x1
; }
83 //! Returns a reference the second element.
84 E2
&x2() { return m_x2
; }
86 // default assignment operator
91 //! Equality operator for 2-tuples
92 template<class E1
, class E2
>
93 bool operator==(const Tuple2
<E1
,E2
> &t1
, const Tuple2
<E1
,E2
> &t2
)
95 return t1
.x1() == t2
.x1() && t1
.x2() == t2
.x2();
98 //! Inequality operator for 2-tuples
99 template<class E1
, class E2
>
100 bool operator!=(const Tuple2
<E1
,E2
> &t1
, const Tuple2
<E1
,E2
> &t2
)
102 return t1
.x1() != t2
.x1() || t1
.x2() != t2
.x2();
105 //! Output operator for 2-tuples.
106 template<class E1
, class E2
>
107 ostream
&operator<<(ostream
&os
, const Tuple2
<E1
,E2
> &t2
)
109 os
<< "(" << t2
.x1() << " " << t2
.x2() << ")";
114 //! Tuples of three elements (3-tuples).
116 * @tparam E1 is the data type for the first element.
117 * @tparam E2 is the data type for the second element.
118 * @tparam E3 is the data type for the third element.
120 template<class E1
, class E2
, class E3
> class Tuple3
{
122 E1 m_x1
; //!< The first element.
123 E2 m_x2
; //!< The second element.
124 E3 m_x3
; //!< The third element.
126 //! Constructs a 3-tuple using default constructors.
128 //! Constructs a 3-tuple for given values.
129 Tuple3(const E1
&y1
, const E2
&y2
, const E3
&y3
) :
130 m_x1(y1
), m_x2(y2
), m_x3(y3
) { }
131 //! Constructs a 3-tuple that is a copy of \a t3.
132 Tuple3(const Tuple3
<E1
,E2
,E3
> &t3
) :
133 m_x1(t3
.m_x1
), m_x2(t3
.m_x2
), m_x3(t3
.m_x3
) { }
135 //! Returns a reference the first element.
136 const E1
&x1() const { return m_x1
; }
137 //! Returns a reference the second element.
138 const E2
&x2() const { return m_x2
; }
139 //! Returns a reference the third element.
140 const E3
&x3() const { return m_x3
; }
142 //! Returns a reference the first element.
143 E1
&x1() { return m_x1
; }
144 //! Returns a reference the second element.
145 E2
&x2() { return m_x2
; }
146 //! Returns a reference the third element.
147 E3
&x3() { return m_x3
; }
149 // default assignment operator
154 //! Equality operator for 3-tuples
155 template<class E1
, class E2
, class E3
>
156 bool operator==(const Tuple3
<E1
,E2
,E3
> &t1
, const Tuple3
<E1
,E2
,E3
> &t2
)
158 return t1
.x1() == t2
.x1() && t1
.x2() == t2
.x2() && t1
.x3() == t2
.x3();
161 //! Inequality operator for 3-tuples
162 template<class E1
, class E2
, class E3
>
163 bool operator!=(const Tuple3
<E1
,E2
,E3
> &t1
, const Tuple3
<E1
,E2
,E3
> &t2
)
165 return t1
.x1() != t2
.x1() || t1
.x2() != t2
.x2() || t1
.x3() != t2
.x3();
168 //! Output operator for 3-tuples
169 template<class E1
, class E2
, class E3
>
170 ostream
&operator<<(ostream
&os
, const Tuple3
<E1
,E2
,E3
> &t3
)
172 os
<< "(" << t3
.x1() << " " << t3
.x2() << " " << t3
.x3() << ")";
177 //! Tuples of four elements (4-tuples).
179 * @tparam E1 is the data type for the first element.
180 * @tparam E2 is the data type for the second element.
181 * @tparam E3 is the data type for the third element.
182 * @tparam E4 is the data type for the fourth element.
184 template<class E1
, class E2
, class E3
, class E4
> class Tuple4
{
186 E1 m_x1
; //!< The first element.
187 E2 m_x2
; //!< The second element.
188 E3 m_x3
; //!< The third element.
189 E4 m_x4
; //!< The fourth element.
191 //! Constructs a 4-tuple using default constructors.
193 //! Constructs a 4-tuple for given values.
194 Tuple4(const E1
&y1
, const E2
&y2
, const E3
&y3
, const E4
&y4
) :
195 m_x1(y1
), m_x2(y2
), m_x3(y3
), m_x4(y4
) { }
196 //! Constructs a 4-tuple that is a copy of \a t4.
197 Tuple4(const Tuple4
<E1
,E2
,E3
,E4
> &t4
) :
198 m_x1(t4
.m_x1
), m_x2(t4
.m_x2
), m_x3(t4
.m_x3
), m_x4(t4
.m_x4
) { }
200 //! Returns a reference the first element.
201 const E1
&x1() const { return m_x1
; }
202 //! Returns a reference the second element.
203 const E2
&x2() const { return m_x2
; }
204 //! Returns a reference the third element.
205 const E3
&x3() const { return m_x3
; }
206 //! Returns a reference the fourth element.
207 const E4
&x4() const { return m_x4
; }
209 //! Returns a reference the first element.
210 E1
&x1() { return m_x1
; }
211 //! Returns a reference the second element.
212 E2
&x2() { return m_x2
; }
213 //! Returns a reference the third element.
214 E3
&x3() { return m_x3
; }
215 //! Returns a reference the fourth element.
216 E4
&x4() { return m_x4
; }
218 // default assignment operator
223 //! Equality operator for 4-tuples
224 template<class E1
, class E2
, class E3
, class E4
>
225 bool operator==(const Tuple4
<E1
,E2
,E3
,E4
> &t1
, const Tuple4
<E1
,E2
,E3
,E4
> &t2
)
227 return t1
.x1() == t2
.x1() && t1
.x2() == t2
.x2() &&
228 t1
.x3() == t2
.x3() && t1
.x4() == t2
.x4();
231 //! Inequality operator for 4-tuples
232 template<class E1
, class E2
, class E3
, class E4
>
233 bool operator!=(const Tuple4
<E1
,E2
,E3
,E4
> &t1
, const Tuple4
<E1
,E2
,E3
,E4
> &t2
)
235 return t1
.x1() != t2
.x1() || t1
.x2() != t2
.x2() ||
236 t1
.x3() != t2
.x3() || t1
.x4() != t2
.x4();
239 //! Output operator for 4-tuples
240 template<class E1
, class E2
, class E3
, class E4
>
241 ostream
&operator<<(ostream
&os
, const Tuple4
<E1
,E2
,E3
,E4
> &t4
)
243 os
<< "(" << t4
.x1() << " " << t4
.x2() << " " <<
244 t4
.x3() << " " << t4
.x4() << ")";
248 template<typename K1_
, typename K2_
,
249 typename Hash1_
= DefHashFunc
<K1_
>,
250 typename Hash2_
= DefHashFunc
<K2_
> >
256 HashFuncTuple(const Hash1_
&hash1
, const Hash2_
&hash2
)
257 : m_hash1(hash1
), m_hash2(hash2
) { }
259 size_t hash(const Tuple2
<K1_
,K2_
> &key
) const {
260 return 23*m_hash1
.hash(key
.x1()) + 443*m_hash2
.hash(key
.x2());