1 /** \file
2  * \brief Declaration and implementation of class Tuple2, Tuple3
3  *        and Tuple4.
4  *
5  * \author Carsten Gutwenger
6  *
7  * \par License:
8  * This file is part of the Open Graph Drawing Framework (OGDF).
9  *
10  * \par
11  * Copyright (C)<br>
12  * See README.md in the OGDF root directory for details.
13  *
14  * \par
15  * This program is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License
17  * Version 2 or 3 as published by the Free Software Foundation;
18  * see the file LICENSE.txt included in the packaging of this file
19  * for details.
20  *
21  * \par
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * \par
28  * You should have received a copy of the GNU General Public
29  * License along with this program; if not, see
30  * http://www.gnu.org/copyleft/gpl.html
31  */
32 
33 #pragma once
34 
35 #include <ogdf/basic/basic.h>
36 #include <ogdf/basic/Hashing.h>
37 
38 
39 namespace ogdf {
40 
41 //! Tuples of two elements (2-tuples).
42 /**
43  * @tparam E1 is the data type for the first element.
44  * @tparam E2 is the data type for the second element.
45  */
46 template<class E1, class E2> class Tuple2 {
47 public:
48 	E1 m_x1; //!< The first element.
49 	E2 m_x2; //!< The second element.
50 
51 	//! Constructs a 2-tuple using default constructors.
Tuple2()52 	Tuple2() { }
53 	//! Constructs a 2-tuple for given values.
Tuple2(const E1 & y1,const E2 & y2)54 	Tuple2(const E1 &y1, const E2 &y2) : m_x1(y1), m_x2(y2) { }
55 	//! Constructs a 2-tuple that is a copy of \p t2.
Tuple2(const Tuple2<E1,E2> & t2)56 	Tuple2(const Tuple2<E1,E2> &t2) : m_x1(t2.m_x1), m_x2(t2.m_x2) { }
57 
58 	//! Returns a reference the first element.
x1()59 	const E1 &x1() const { return m_x1; }
60 	//! Returns a reference the second element.
x2()61 	const E2 &x2() const { return m_x2; }
62 
63 	//! Returns a reference the first element.
x1()64 	E1 &x1() { return m_x1; }
65 	//! Returns a reference the second element.
x2()66 	E2 &x2() { return m_x2; }
67 
68 	// default assignment operator
69 	Tuple2& operator=(const Tuple2<E1,E2>&) = default;
70 
71 	OGDF_NEW_DELETE
72 };
73 
74 //! Equality operator for 2-tuples
75 template<class E1, class E2>
76 bool operator==(const Tuple2<E1,E2> &t1, const Tuple2<E1,E2> &t2)
77 {
78 	return t1.x1() == t2.x1() && t1.x2() == t2.x2();
79 }
80 
81 //! Inequality operator for 2-tuples
82 template<class E1, class E2>
83 bool operator!=(const Tuple2<E1,E2> &t1, const Tuple2<E1,E2> &t2)
84 {
85 	return t1.x1() != t2.x1() || t1.x2() != t2.x2();
86 }
87 
88 //! Output operator for 2-tuples.
89 template<class E1, class E2>
90 std::ostream &operator<<(std::ostream &os, const Tuple2<E1,E2> &t2)
91 {
92 	os << "(" << t2.x1() << " " << t2.x2() << ")";
93 	return os;
94 }
95 
96 
97 
98 template<typename K1_, typename K2_,
99 	typename Hash1_ = DefHashFunc<K1_>,
100 	typename Hash2_ = DefHashFunc<K2_> >
101 class HashFuncTuple
102 {
103 public:
HashFuncTuple()104 	HashFuncTuple() { }
105 
HashFuncTuple(const Hash1_ & hash1,const Hash2_ & hash2)106 	HashFuncTuple(const Hash1_ &hash1, const Hash2_ &hash2)
107 		: m_hash1(hash1), m_hash2(hash2) { }
108 
hash(const Tuple2<K1_,K2_> & key)109 	size_t hash(const Tuple2<K1_,K2_> &key) const {
110 		return 23*m_hash1.hash(key.x1()) + 443*m_hash2.hash(key.x2());
111 	}
112 
113 private:
114 	Hash1_ m_hash1;
115 	Hash2_ m_hash2;
116 };
117 
118 }
119