6 * $Date: 2012-07-16 14:23:36 +0200 (Mo, 16. Jul 2012) $
7 ***************************************************************/
10 * \brief Declaration of class HashArray2D.
12 * This is a class implementing a 2-dimensional Hash array.
13 * It uses templates for the keys and the data of the objects
16 * \author René Weiskircher
19 * This file is part of the Open Graph Drawing Framework (OGDF).
23 * See README.txt in the root directory of the OGDF installation for details.
26 * This program is free software; you can redistribute it and/or
27 * modify it under the terms of the GNU General Public License
28 * Version 2 or 3 as published by the Free Software Foundation;
29 * see the file LICENSE.txt included in the packaging of this file
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
39 * You should have received a copy of the GNU General Public
40 * License along with this program; if not, write to the Free
41 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
42 * Boston, MA 02110-1301, USA.
44 * \see http://www.gnu.org/copyleft/gpl.html
45 ***************************************************************/
52 #include <ogdf/basic/HashArray.h>
53 #include <ogdf/basic/tuples.h>
54 #include <ogdf/basic/HashIterator2D.h>
56 #ifndef OGDF_HASH_ARRAY_2D_H
57 #define OGDF_HASH_ARRAY_2D_H
63 //! Indexed 2-dimensional arrays using hashing for element access.
65 * @tparam I1 is the first index type.
66 * @tparam I2 is the second index type.
67 * @tparam E is the element type.
68 * @tparam H1 is the hash function type for \a I1. Optional; uses the class DefHashFunc by default.
69 * @tparam H2 is the hash function type for \a I2. Optional; uses the class DefHashFunc by default.
71 * A 2D-hash array can be used like a usual 2-dimensional array but with a general
78 class H1
= DefHashFunc
<I1
>,
79 class H2
= DefHashFunc
<I2
> >
80 class HashArray2D
: private Hashing
< Tuple2
<I1
,I2
>, E
, HashFuncTuple
<I1
,I2
,H1
,H2
> >
83 //! The type of const-iterators for 2D-hash arrays.
84 typedef HashConstIterator2D
<I1
,I2
,E
,H1
,H2
> const_iterator
;
86 //! Creates a 2D-hash array.
89 //! Creates a 2D-hash array and sets the default value to \a x.
90 HashArray2D(const E
&defaultValue
, const H1
&hashFunc1
= H1(), const H2
&hashFunc2
= H2()) :
91 Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >(
93 HashFuncTuple
<I1
,I2
,H1
,H2
>(hashFunc1
,hashFunc2
)),
94 m_defaultValue(defaultValue
) { }
97 HashArray2D(const HashArray2D
<I1
,I2
,E
,H1
,H2
> &A
) :
98 Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >(A
),
99 m_defaultValue(A
.m_defaultValue
) { }
101 //! Assignment operator.
102 HashArray2D
&operator=(const HashArray2D
<I1
,I2
,E
,H1
,H2
> &A
) {
103 m_defaultValue
= A
.m_defaultValue
;
104 Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::operator=(A
);
111 //! Returns a const reference to entry (\a i,\a j).
112 const E
&operator()(const I1
&i
, const I2
&j
) const {
113 HashElement
<Tuple2
<I1
,I2
>,E
> *pElement
=
114 Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::lookup(Tuple2
<I1
,I2
>(i
,j
));
115 return (pElement
) ? pElement
->info() : m_defaultValue
;
118 //! Returns a reference to entry (\a i,\a j).
119 E
&operator()(const I1
&i
, const I2
&j
) {
120 Tuple2
<I1
,I2
> t(i
,j
);
121 HashElement
<Tuple2
<I1
,I2
>,E
> *pElement
=
122 Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::lookup(t
);
124 pElement
= Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::fastInsert(t
,m_defaultValue
);
125 return pElement
->info();
128 //! Returns true iff entry (\a i,\a j) is defined.
129 bool isDefined(const I1
&i
, const I2
&j
) const {
130 return Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::member(Tuple2
<I1
,I2
>(i
,j
));
133 //! Undefines the entry at index (\a i,\a j).
134 void undefine(const I1
&i
, const I2
&j
) {
135 return Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::del(Tuple2
<I1
,I2
>(i
,j
));
138 //! Returns an iterator pointing to the first element.
139 HashConstIterator2D
<I1
,I2
,E
,H1
,H2
> begin() const {
140 return HashConstIterator2D
<I1
,I2
,E
>(
141 Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::begin());
144 //! Returns the number of defined elements in the table.
146 return Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::size();
149 //! Returns if any indices are defined
151 return Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::empty();
155 //! Undefines all indices.
157 Hashing
<Tuple2
<I1
,I2
>,E
,HashFuncTuple
<I1
,I2
,H1
,H2
> >::clear();
161 E m_defaultValue
; //!< The default value of the array.