1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
* $Revision: 3504 $
*
* last checkin:
* $Author: beyer $
* $Date: 2013-05-16 14:49:39 +0200 (Thu, 16 May 2013) $
***************************************************************/
/** \file
* \brief Declaration and implementation of hyperraph array classes
* based on Node/EdgeArray classes written by Carsten Gutwenger,
* but slightly modified (base class is common for both arrays).
*
* \author Ondrej Moris
*
* \par License:
* This file is part of the Open Graph Drawing Framework (OGDF).
*
* \par
* Copyright (C)<br>
* See README.txt in the root directory of the OGDF installation for details.
*
* \par
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* Version 2 or 3 as published by the Free Software Foundation;
* see the file LICENSE.txt included in the packaging of this file
* for details.
*
* \par
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* \par
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* \see http://www.gnu.org/copyleft/gpl.html
***************************************************************/
#ifdef _MSC_VER
#pragma once
#endif
#ifndef OGDF_HYPERGRAPH_ARRAY_H
#define OGDF_HYPERGRAPH_ARRAY_H
#include <ogdf/hypergraph/Hypergraph.h>
namespace ogdf {
//! Abstract base class for hypergraph arrays.
class HypergraphArrayBase {
public:
/**
* Pointer to list element in the list of all registered hypergraph
* arrays which references this array.
*/
ListIterator<HypergraphArrayBase *> m_it;
//!< The associated hypergraph.
const Hypergraph *m_hypergraph;
//! Initializes an array not associated with a hypergraph.
HypergraphArrayBase()
: m_hypergraph(0)
{
}
//! Initializes an array associated with \a pH.
HypergraphArrayBase(const Hypergraph *pH)
{
m_hypergraph = pH;
}
//! Destructor, unregisters the array.
virtual ~HypergraphArrayBase()
{
}
//! Returns a pointer to the associated hypergraph.
const Hypergraph *hypergraphOf() const {
return m_hypergraph;
}
//! Table re-initialization.
virtual void reinit(int initTableSize) = 0;
//! Associates the array with a new hypergraph.
virtual void reregister(const Hypergraph *H) = 0;
//! Table size enlargement.
virtual void enlargeTable(int newTableSize) = 0;
//! Disconnetion from the hypergraph.
virtual void disconnect() = 0;
}; // class HypergraphArrayBase
//! Dynamic arrays indexed with hypernodes.
template<class T> class HypernodeArray :
private Array<T>, protected HypergraphArrayBase {
//!< The default value for array elements.
T m_x;
public:
//! Constructs an empty hypernode array associated with no hypergraph.
HypernodeArray() : Array<T>(), HypergraphArrayBase() { }
//! Constructs a hypernode array associated with \a H.
HypernodeArray(const Hypergraph &H, const T &x)
: Array<T>(0,H.hypernodeArrayTableSize() - 1, x),
HypergraphArrayBase(&H), m_x(x)
{
m_it = H.registerHypernodeArray(this);
}
virtual ~HypernodeArray()
{
if (m_hypergraph)
m_hypergraph->unregisterHypernodeArray(m_it);
}
//! Returns a reference to the element with index \a v.
T &operator[](hypernode v)
{
return Array<T>::operator [](v->index());
}
//! Returns a reference to the element with index \a index.
const T &operator[](int index) const
{
return Array<T>::operator [](index);
}
//! Returns a reference to the element with index \a index.
T &operator[](int index)
{
return Array<T>::operator [](index);
}
//! Assignment operator.
HypernodeArray<T> &operator=(const HypernodeArray<T> &a)
{
Array<T>::operator =(a);
m_x = a.m_x;
reregister(a.m_hypergraph);
return *this;
}
//! Reinitializes the array. Associates the array with \a H.
void init(const Hypergraph &H)
{
Array<T>::init(H.hypernodeArrayTableSize()); reregister(&H);
}
//! Reinitializes the array. Associates the array with \a H.
void init(const Hypergraph &H, const T &x)
{
Array<T>::init(0, H.hypernodeArrayTableSize() - 1, m_x = 0);
reregister(&H);
}
virtual void reregister(const Hypergraph *H)
{
if (m_hypergraph)
m_hypergraph->unregisterHypernodeArray(m_it);
if ((m_hypergraph = H) != 0)
m_it = H->registerHypernodeArray(this);
}
private:
virtual void enlargeTable(int newTableSize)
{
Array<T>::resize(newTableSize, m_x);
}
virtual void reinit(int initTableSize)
{
Array<T>::init(0, initTableSize - 1, m_x);
}
virtual void disconnect()
{
Array<T>::init();
m_hypergraph = NULL;
}
OGDF_NEW_DELETE;
}; // class HypernodeArray<T>
//! Dynamic arrays indexed with nodes.
template<class T> class HyperedgeArray :
private Array<T>, protected HypergraphArrayBase {
//!< The default value for array elements.
T m_x;
public:
//! Constructs an empty hypernode array associated with no graph.
HyperedgeArray()
: Array<T>(), HypergraphArrayBase()
{
}
//! Constructs a hypernode array associated with \a G.
HyperedgeArray(const Hypergraph &H, const T &x)
: Array<T>(0, H.hyperedgeArrayTableSize() - 1, x),
HypergraphArrayBase(&H), m_x(x)
{
m_it = H.registerHyperedgeArray(this);
}
//! Desctructor.
virtual ~HyperedgeArray()
{
if (m_hypergraph)
m_hypergraph->unregisterHyperedgeArray(m_it);
}
//! Returns true iff the array is associated with a hypergraph.
bool valid() const
{
return (Array<T>::low() <= Array<T>::high());
}
//! Returns a reference to the element with index \a v.
T &operator[](hyperedge e)
{
return Array<T>::operator [](e->index());
}
//! Returns a reference to the element with index \a index.
const T &operator[](int index) const
{
return Array<T>::operator [](index);
}
//! Returns a reference to the element with index \a index.
T &operator[](int index)
{
return Array<T>::operator [](index);
}
//! Assignment operator.
HyperedgeArray<T> &operator=(const HyperedgeArray<T> &a)
{
Array<T>::operator =(a);
m_x = a.m_x;
reregister(a.m_hypergraph);
return *this;
}
//! Reinitializes the array. Associates the array with \a H.
void init(const Hypergraph &H)
{
Array<T>::init(H.hyperedgeArrayTableSize()); reregister(&H);
}
//! Reinitializes the array. Associates the array with \a H.
void init(const Hypergraph &H, const T &x)
{
Array<T>::init(0, H.hyperedgeArrayTableSize() - 1, m_x = 0);
reregister(&H);
}
virtual void reregister(const Hypergraph *H)
{
if (m_hypergraph)
m_hypergraph->unregisterHyperedgeArray(m_it);
if ((m_hypergraph = H) != 0)
m_it = H->registerHyperedgeArray(this);
}
private:
virtual void enlargeTable(int newTableSize)
{
Array<T>::resize(newTableSize, m_x);
}
virtual void reinit(int initTableSize)
{
Array<T>::init(0, initTableSize-1, m_x);
}
virtual void disconnect()
{
Array<T>::init();
m_hypergraph = 0;
}
OGDF_NEW_DELETE;
}; // class HyperedgeArray<T>
} // end namespace ogdf
#endif
|