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
39namespace 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 */
46template<class E1, class E2> class Tuple2 {
47public:
48 E1 m_x1; //!< The first element.
49 E2 m_x2; //!< The second element.
50
51 //! Constructs a 2-tuple using default constructors.
52 Tuple2() { }
53 //! Constructs a 2-tuple for given values.
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.
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.
59 const E1 &x1() const { return m_x1; }
60 //! Returns a reference the second element.
61 const E2 &x2() const { return m_x2; }
62
63 //! Returns a reference the first element.
64 E1 &x1() { return m_x1; }
65 //! Returns a reference the second element.
66 E2 &x2() { return m_x2; }
67
68 // default assignment operator
69
70 OGDF_NEW_DELETE
71};
72
73//! Equality operator for 2-tuples
74template<class E1, class E2>
75bool operator==(const Tuple2<E1,E2> &t1, const Tuple2<E1,E2> &t2)
76{
77 return t1.x1() == t2.x1() && t1.x2() == t2.x2();
78}
79
80//! Inequality operator for 2-tuples
81template<class E1, class E2>
82bool operator!=(const Tuple2<E1,E2> &t1, const Tuple2<E1,E2> &t2)
83{
84 return t1.x1() != t2.x1() || t1.x2() != t2.x2();
85}
86
87//! Output operator for 2-tuples.
88template<class E1, class E2>
89std::ostream &operator<<(std::ostream &os, const Tuple2<E1,E2> &t2)
90{
91 os << "(" << t2.x1() << " " << t2.x2() << ")";
92 return os;
93}
94
95
96
97template<typename K1_, typename K2_,
98 typename Hash1_ = DefHashFunc<K1_>,
99 typename Hash2_ = DefHashFunc<K2_> >
100class HashFuncTuple
101{
102public:
103 HashFuncTuple() { }
104
105 HashFuncTuple(const Hash1_ &hash1, const Hash2_ &hash2)
106 : m_hash1(hash1), m_hash2(hash2) { }
107
108 size_t hash(const Tuple2<K1_,K2_> &key) const {
109 return 23*m_hash1.hash(key.x1()) + 443*m_hash2.hash(key.x2());
110 }
111
112private:
113 Hash1_ m_hash1;
114 Hash2_ m_hash2;
115};
116
117}
118