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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
|
/*
* $Revision: 3074 $
*
* last checkin:
* $Author: chimani $
* $Date: 2012-11-29 11:01:06 +0100 (Thu, 29 Nov 2012) $
***************************************************************/
/** \file
* \brief Declaration and implementation of a HyperGraph
*
* \author Martin Gronemann
*
* \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_HYPER_GRAPH_H
#define OGDF_HYPER_GRAPH_H
#include <ogdf/basic/GraphAttributes.h>
#include <ogdf/basic/EList.h>
#include <ogdf/basic/EFreeList.h>
namespace ogdf {
class OGDF_EXPORT HyperGraph
{
friend class HyperGraphTypes;
public:
// forward declaration to make AdjElement pointers available
class AdjElement;
//! Representation of node elements.
class NodeElement
{
friend class HyperGraph;
friend class HyperGraphTypes;
public:
//! Returns the index of this node.
int index() const { return m_index; }
//! Returns the number of incident edges.
int degree() const { return m_numAdj; }
private:
NodeElement* m_pPrev;
NodeElement* m_pNext;
AdjElement* m_pFirstAdj;
AdjElement* m_pLastAdj;
int m_numAdj;
int m_index;
}; // class NodeElement
//! Representation of hyper edges.
class EdgeElement
{
friend class HyperGraph;
friend class HyperGraphTypes;
public:
//! Returns the index of this edge.
int index() const { return m_index; }
//! Returns the number of incident nodes.
int cardinality() const { return m_numAdj; }
private:
EdgeElement* m_pPrev;
EdgeElement* m_pNext;
AdjElement* m_pFirstAdj;
AdjElement* m_pLastAdj;
int m_numAdj;
int m_index;
}; // class EdgeElement
//! Representation of adjacency elements.
class AdjElement
{
friend class HyperGraph;
friend class HyperGraphTypes;
public:
AdjElement() : m_pNode(0), m_pEdge(0) { }
//! Returns the node whose adjacency list contains this adjacency entry.
NodeElement* theNode() { return m_pNode; }
//! Returns the hyper edge associated with this adjacency entry.
EdgeElement* theEdge() { return m_pEdge; }
//! Returns the index of this adjacency entry.
int index() const { return m_index; }
private:
AdjElement(NodeElement* pNode, EdgeElement* pEdge) :
m_pNode(pNode),
m_pEdge(pEdge),
m_pPrev_nodeAdj(0),
m_pNext_nodeAdj(0),
m_pPrev_edgeAdj(0),
m_pNext_edgeAdj(0)
{ }
NodeElement* m_pNode;
EdgeElement* m_pEdge;
AdjElement* m_pPrev_nodeAdj;
AdjElement* m_pNext_nodeAdj;
AdjElement* m_pPrev_edgeAdj;
AdjElement* m_pNext_edgeAdj;
int m_index;
};
typedef NodeElement* node;
typedef EdgeElement* edge;
protected:
NodeElement* m_pFirstNode;
NodeElement* m_pLastNode;
int m_numNodes;
EdgeElement* m_pFirstEdge;
EdgeElement* m_pLastEdge;
int m_numEdges;
protected:
EFreeListIndexPool<
NodeElement,
&NodeElement::m_pNext,
&NodeElement::m_index
> m_nodeAllocator;
EFreeListIndexPool<
EdgeElement,
&EdgeElement::m_pNext,
&EdgeElement::m_index
> m_edgeAllocator;
EFreeListIndexPool<
AdjElement,
&AdjElement::m_pNext_nodeAdj, // note: we just need one next pointer, nobody cares which one.
&AdjElement::m_index
> m_adjAllocator;
template<typename E>
class GraphArrayBase
{
friend class HyperGraph;
public:
//! Interface method for resizing a graph array.
virtual void setSize(int numElements) = 0;
GraphArrayBase<E>* m_prev; //!> the embedded prev pointer.
GraphArrayBase<E>* m_next; //!> the embedded next pointer.
};
template<typename T, typename E>
class GraphArray : protected Array<T>, GraphArrayBase<E>
{
friend class HyperGraph;
public:
// Creates an empty graph array not attached to a graph.
GraphArray() : m_pGraph(0), Array<T>() { }
//! Creates a graph array attached to \a pGraph.
GraphArray(HyperGraph* pGraph) : m_pGraph(pGraph), m_initialValue(), Array<T>()
{
if (m_pGraph)
m_pGraph->registerArray(this);
}
//! Creates a graph array attached to \a pGraph with default value \a initialValue.
GraphArray(HyperGraph* pGraph, const T& initialValue) : m_pGraph(pGraph), m_initialValue(initialValue), Array<T>()
{
if (m_pGraph)
m_pGraph->registerArray(this);
}
//! Destructor.
/**
* If the array is attached to a graph, the array will be detached before it is deallocated.
*/
virtual ~GraphArray()
{
if (m_pGraph)
m_pGraph->unregisterArray(this);
}
//! Returns a const reference to the entry for an indexed element.
const T& operator[](const E* e) const
{
return Array<T>::operator [](e->index());
}
//! Returns a reference to the entry for an indexed element.
T& operator[](const E* e)
{
return Array<T>::operator [](e->index());
}
//! Returns the graph the array is attached to.
HyperGraph* graph() const
{
return m_pGraph;
}
protected:
//! Implementation of \a GraphArrayBase::setSize(int).
virtual void setSize(int numElements)
{
if (numElements > Array<T>::size())
Array<T>::resize(numElements,m_initialValue);
}
//! The graph the array is attached to.
HyperGraph* m_pGraph;
//! The initial value for unset entries.
T m_initialValue;
}; // end of class GraphArray
template<typename ElementType> class ArrayControllerTypes;
//! Array controllers manage the graph arrays of an element.
template<typename ElementType>
class ArrayController
{
friend class ArrayControllerTypes<ElementType>;
public:
//! Creates a new array controller where the actual table size is zero.
ArrayController() : m_tableSize(0)
{
ArrayControllerTypes<ElementType>::ArrayListType::init(this);
}
//! Frees the array controller and unregisters all remaining arrays.
~ArrayController();
//! Registers a new graph array and sets the size to \a m_tableSize.
void registerArray(GraphArrayBase<ElementType>* pArray);
//! Unregisters a graph array by removing it from the list.
void unregisterArray(GraphArrayBase<ElementType>* pArray);
//! function to calculate the new tableSize; returns min(2^k | 2^k >= minSize)
int newTableSize(int minSize)
{
int res = 1;
while (res < minSize)
res = res << 1;
return res;
}
//! called by the hyper graph when the amount of used entries has changed.
void numUsedIndicesChanged(int numUsedIndices);
protected:
//! First array in the chain of attached arrays
GraphArrayBase<ElementType>* m_first;
//! Last array in the chain of attached arrays
GraphArrayBase<ElementType>* m_last;
//! Number of attached arrays
int m_numArrays;
//! Current table size which corresponds to the length of all attached arrays.
int m_tableSize;
};
template<typename ElementType>
class ArrayControllerTypes
{
public:
//! Type definition for the embedded list of arrays.
typedef EList< ArrayController<ElementType>, GraphArrayBase<ElementType>,
&ArrayController<ElementType>::m_numArrays,
&ArrayController<ElementType>::m_first,
&ArrayController<ElementType>::m_last,
&GraphArrayBase<ElementType>::m_prev,
&GraphArrayBase<ElementType>::m_next> ArrayListType;
};
ArrayController<NodeElement> m_nodeArrayController; //!< controller for node arrays
ArrayController<EdgeElement> m_edgeArrayController; //!< controller for edge arrays
ArrayController<AdjElement> m_adjArrayController; //!< controller for adj arrays
void registerArray(GraphArrayBase<NodeElement>* pArray) { m_nodeArrayController.registerArray(pArray); }
void registerArray(GraphArrayBase<EdgeElement>* pArray) { m_edgeArrayController.registerArray(pArray); }
void registerArray(GraphArrayBase<AdjElement>* pArray) { m_adjArrayController.registerArray(pArray); }
void unregisterArray(GraphArrayBase<NodeElement>* pArray) { m_nodeArrayController.unregisterArray(pArray); }
void unregisterArray(GraphArrayBase<EdgeElement>* pArray) { m_edgeArrayController.unregisterArray(pArray); }
void unregisterArray(GraphArrayBase<AdjElement>* pArray) { m_adjArrayController.unregisterArray(pArray); }
//! Allocates a new NodeElement using the EFreeListIndexPool and lets the controller check if arrays have to be resized.
NodeElement* allocateNodeElement()
{
NodeElement* pResult = m_nodeAllocator.alloc();
m_nodeArrayController.numUsedIndicesChanged(m_nodeAllocator.numUsedIndices());
return pResult;
}
//! Allocates a new EdgeElement using the EFreeListIndexPool and lets the controller check if arrays have to be resized.
EdgeElement* allocateEdgeElement()
{
EdgeElement* pResult = m_edgeAllocator.alloc();
m_edgeArrayController.numUsedIndicesChanged(m_edgeAllocator.numUsedIndices());
return pResult;
}
//! Allocates a new AdjElement using the EFreeListIndexPool and lets the controller check if arrays have to be resized.
AdjElement* allocateAdjElement()
{
AdjElement* pResult = m_adjAllocator.alloc();
m_adjArrayController.numUsedIndicesChanged(m_adjAllocator.numUsedIndices());
return pResult;
}
//! Frees a used NodeElement
void freeNodeElement(NodeElement* pNode)
{
m_nodeAllocator.free(pNode);
}
//! Frees a used EdgeElement
void freeEdgeElement(EdgeElement* pEdge)
{
m_edgeAllocator.free(pEdge);
}
//! Frees a used AdjElement
void freeAdjElement(AdjElement* pAdj)
{
m_adjAllocator.free(pAdj);
}
public:
//! Dynamic arrays indexed with nodes.
template<typename T>
class NodeArray : public GraphArray<T, NodeElement>
{
public:
NodeArray(HyperGraph* pGraph) : GraphArray<T, NodeElement>(pGraph) {}
NodeArray(HyperGraph* pGraph, const T& initValue) : GraphArray<T, NodeElement>(pGraph, initValue) {}
};
//! Dynamic arrays indexed with hyper edges.
template<typename T>
class EdgeArray : public GraphArray<T, EdgeElement>
{
public:
EdgeArray(HyperGraph* pGraph) : GraphArray<T, EdgeElement>(pGraph) {}
EdgeArray(HyperGraph* pGraph, const T& initValue) : GraphArray<T, EdgeElement>(pGraph, initValue) {}
};
//! Dynamic arrays indexed with adjacency entries.
template<typename T>
class AdjArray : public GraphArray<T, AdjElement>
{
public:
AdjArray(HyperGraph* pGraph) : GraphArray<T, AdjElement>(pGraph) {}
AdjArray(HyperGraph* pGraph, const T& initValue) : GraphArray<T, AdjElement>(pGraph, initValue) {}
};
//! Creates an empty hyper graph.
HyperGraph();
//! Creates a new node.
NodeElement* newNode();
//! Creates a new edge which is not incident to any nodes.
EdgeElement* newEdge();
//! Creates a new edge which is incident to the two nodes \a pNode1, \a pNode2.
EdgeElement* newEdge(NodeElement* pNode1, NodeElement* pNode2)
{
EdgeElement* pEdge = newEdge();
newAdjElement( pNode1, pEdge );
newAdjElement( pNode2, pEdge );
return pEdge;
}
//! Creates a new \a AdjElement which makes \a pNode and \a pEdge incident.
/**
* \note This function does not check if \a pNode and \a pEdge are already incident.
* Hypergraphs can deal with duplicate AdjElements. However, for reasons of clarity
* it is not a good idea to make use of it.
* @param pNode is the node.
* @param pEdge is the hyper edge.
*/
AdjElement* newAdjElement(NodeElement* pNode, EdgeElement* pEdge);
//! Removes one endpoint \a pAdj from a hyper edge.
void delAdjElement(AdjElement* pAdj);
//! Removes the endpoint \a pNode from the hyper edge \a pEdge.
void delAdjElement(NodeElement* pNode, EdgeElement* pEdge)
{
AdjElement* pAdj = findAdjElement(pNode, pEdge);
if (pAdj) delAdjElement(pAdj);
}
//! Makes \a pNode and \a pEdge incident; returns the corresponding \a AdjElement.
AdjElement* addNode(NodeElement* pNode, EdgeElement* pEdge, bool checkIfAlreadyExists = false)
{
if (!checkIfAlreadyExists)
return newAdjElement( pNode, pEdge );
AdjElement* pAdj = findAdjElement(pNode, pEdge);
if (!pAdj)
pAdj = newAdjElement( pNode, pEdge );
return pAdj;
}
//! If \a pNode and \a pEdge are incident, the corresponding AdjElement is removed.
/**
* \note The function requires time O(min(degree(pNode), cardinality(pEdge))).
* @param pNode is the node.
* @param pEdge is the hyper edge.
* @param removeDuplicates is a flag, if it is set the function will remove any existing duplicates too.
*/
void removeNode(NodeElement* pNode, EdgeElement* pEdge, bool removeDuplicates = false)
{
AdjElement* pAdj = findAdjElement(pNode, pEdge);
if (!removeDuplicates && pAdj)
{
delAdjElement(pAdj);
} else
{
while (pAdj)
{
delAdjElement(pAdj);
pAdj = findAdjElement(pNode, pEdge);
}
}
}
//! Returns the \a AdjElement for pNode and pEdge.
/**
* In case \a pNode and \a pEdge are not incident the function returns 0.
* \note The function requires time O(min(degree(pNode), cardinality(pEdge))).
* @param pNode is the node.
* @param pEdge is the hyper edge.
*/
AdjElement* findAdjElement(NodeElement* pNode, EdgeElement* pEdge) const;
//! Deletes hyper edge \a pEdge.
void delEdge(EdgeElement* pEdge);
//! Deletes node \a pNode.
void delNode(NodeElement* pNode);
int numberOfNodes() const;
int numberOfEdges() const;
//! Clears the graph.
void clear();
/* void toCliqueGraph(
Graph* pG,
NodeArray<HyperGraph::NodeElement*>* pNodeMap = 0,
EdgeArray<HyperGraph::EdgeElement*>* pEdgeMap = 0)
{
HyperGraph::NodeArray<ogdf::node> hgNode_to_gNode(this);
for (NodeList::iterator it = HyperGraph::NodeList::begin(this); it.valid(); it++)
{
HyperGraph::NodeElement* hgNode = *it;
ogdf::node gNode = pG->newNode();
hgNode_to_gNode[ hgNode ] = gNode;
if (pNodeMap)
(*pNodeMap)[gNode] = hgNode;
}
for (HyperGraph::EdgeList::iterator it = HyperGraph::EdgeList::begin(this); it.valid(); it++)
{
EdgeElement* hgEdge = *it;
for (HyperGraph::EdgeAdjList::iterator adj_it = HyperGraph::EdgeAdjList::begin(hgEdge); adj_it.valid(); adj_it++)
{
for (HyperGraph::EdgeAdjList::iterator adj_it2 = adj_it.succ(); adj_it2.valid(); adj_it2++)
{
NodeElement* hgNode = (*adj_it)->theNode();
NodeElement* hgNode2 = (*adj_it2)->theNode();
node gNode = hgNode_to_gNode[ hgNode ];
node gNode2 = hgNode_to_gNode[ hgNode2 ];
edge gEdge = pG->newEdge(gNode, gNode2);
if (pEdgeMap)
(*pEdgeMap)[gEdge] = hgEdge;
}
}
}
}
void toStarGraph(
Graph* pG,
NodeArray<NodeElement*>* pNodeMap = 0,
EdgeArray<EdgeElement*>* pEdgeMap = 0)
{
HyperGraph::NodeArray<ogdf::node> hgNode_to_gNode(this);
for (NodeList::iterator it = HyperGraph::NodeList::begin(this); it.valid(); it++)
{
NodeElement* hgNode = *it;
node gNode = pG->newNode();
hgNode_to_gNode[ hgNode ] = gNode;
if (pNodeMap)
(*pNodeMap)[gNode] = hgNode;
}
for (HyperGraph::EdgeList::iterator it = HyperGraph::EdgeList::begin(this); it.valid(); it++)
{
EdgeElement* hgEdge = *it;
node gNodeDummy = pG->newNode();
if (pNodeMap)
(*pNodeMap)[gNodeDummy] = 0;
for (HyperGraph::EdgeAdjList::iterator adj_it = HyperGraph::EdgeAdjList::begin(hgEdge); adj_it.valid(); adj_it++)
{
NodeElement* hgNode = (*adj_it)->theNode();
node gNode = hgNode_to_gNode[ hgNode ];
edge gEdge = pG->newEdge(gNodeDummy, gNode);
if (pEdgeMap)
(*pEdgeMap)[gEdge] = hgEdge;
}
}
} */
}; // end of class HyperGraph
//! Type declarations for HyperGraph.
class HyperGraphTypes
{
public:
//! Type definition for the embedded list of nodes.
typedef EList<
HyperGraph, HyperGraph::NodeElement,
&HyperGraph::m_numNodes,
&HyperGraph::m_pFirstNode,
&HyperGraph::m_pLastNode,
&HyperGraph::NodeElement::m_pPrev,
&HyperGraph::NodeElement::m_pNext
> NodeList;
//! Type definition for the embedded list of edges.
typedef EList<
HyperGraph, HyperGraph::EdgeElement,
&HyperGraph::m_numEdges,
&HyperGraph::m_pFirstEdge,
&HyperGraph::m_pLastEdge,
&HyperGraph::EdgeElement::m_pPrev,
&HyperGraph::EdgeElement::m_pNext
> EdgeList;
//! Type definition for the embedded list of AdjElement at a node.
typedef EList<
HyperGraph::NodeElement, HyperGraph::AdjElement,
&HyperGraph::NodeElement::m_numAdj,
&HyperGraph::NodeElement::m_pFirstAdj,
&HyperGraph::NodeElement::m_pLastAdj,
&HyperGraph::AdjElement::m_pPrev_nodeAdj,
&HyperGraph::AdjElement::m_pNext_nodeAdj
> NodeAdjList;
//! Type definition for the embedded list of AdjElement at an edge.
typedef EList<
HyperGraph::EdgeElement, HyperGraph::AdjElement,
&HyperGraph::EdgeElement::m_numAdj,
&HyperGraph::EdgeElement::m_pFirstAdj,
&HyperGraph::EdgeElement::m_pLastAdj,
&HyperGraph::AdjElement::m_pPrev_edgeAdj,
&HyperGraph::AdjElement::m_pNext_edgeAdj
> EdgeAdjList;
};
template<typename ElementType>
HyperGraph::ArrayController<ElementType>::~ArrayController()
{
// Unregisters all arrays, but does not delete them.
while (!ArrayControllerTypes<ElementType>::ArrayListType::empty(this))
unregisterArray(ArrayControllerTypes<ElementType>::ArrayListType::front(this));
}
// Registers a new graph array and sets the size to \a m_tableSize.
template<typename ElementType>
inline void HyperGraph::ArrayController<ElementType>::registerArray(GraphArrayBase<ElementType>* pArray)
{
ArrayControllerTypes<ElementType>::ArrayListType::pushBack(this, pArray);
pArray->setSize(m_tableSize);
}
// Unregisters an array by removing it from the list.
template<typename ElementType>
inline void HyperGraph::ArrayController<ElementType>::unregisterArray(GraphArrayBase<ElementType>* pArray)
{
ArrayControllerTypes<ElementType>::ArrayListType::remove(this, pArray);
pArray->setSize(0);
}
template<typename ElementType>
inline void HyperGraph::ArrayController<ElementType>::numUsedIndicesChanged(int numUsedIndices)
{
// check if we have to grow
if (numUsedIndices > m_tableSize)
{
// we have to resize
// calculate new table size
m_tableSize = newTableSize(numUsedIndices);
// iterate over all arrays
for (typename ArrayControllerTypes<ElementType>::ArrayListType::iterator it = ArrayControllerTypes<ElementType>::ArrayListType::begin(this);
it.valid(); it++)
{
// and resize them
(*it)->setSize(m_tableSize);
}
}
}
inline HyperGraph::HyperGraph()
{
HyperGraphTypes::NodeList::init( this );
HyperGraphTypes::EdgeList::init( this );
}
inline HyperGraph::NodeElement* HyperGraph::newNode()
{
NodeElement* pNode = allocateNodeElement();
HyperGraphTypes::NodeList::pushBack( this, pNode );
HyperGraphTypes::NodeAdjList::init( pNode );
return pNode;
}
inline HyperGraph::EdgeElement* HyperGraph::newEdge()
{
EdgeElement* pEdge = allocateEdgeElement();
HyperGraphTypes::EdgeList::pushBack( this, pEdge );
HyperGraphTypes::EdgeAdjList::init( pEdge );
return pEdge;
}
inline HyperGraph::AdjElement* HyperGraph::newAdjElement(HyperGraph::NodeElement* pNode, HyperGraph::EdgeElement* pEdge)
{
AdjElement* pAdj = allocateAdjElement();
pAdj->m_pNode = pNode;
pAdj->m_pEdge = pEdge;
HyperGraphTypes::NodeAdjList::pushBack( pNode, pAdj );
HyperGraphTypes::EdgeAdjList::pushBack( pEdge, pAdj );
return pAdj;
}
inline void HyperGraph::delAdjElement(HyperGraph::AdjElement* pAdj)
{
HyperGraphTypes::EdgeAdjList::remove( pAdj->theEdge(), pAdj );
HyperGraphTypes::NodeAdjList::remove( pAdj->theNode(), pAdj );
freeAdjElement( pAdj );
}
inline HyperGraph::AdjElement* HyperGraph::findAdjElement(HyperGraph::NodeElement* pNode, HyperGraph::EdgeElement* pEdge) const
{
if (HyperGraphTypes::EdgeAdjList::size(pEdge) < HyperGraphTypes::NodeAdjList::size(pNode))
{
for (HyperGraphTypes::EdgeAdjList::iterator it = HyperGraphTypes::EdgeAdjList::begin(pEdge);
it.valid(); it++)
{
if ((*it)->theNode() == pNode) return (*it);
}
} else
{
for (HyperGraphTypes::NodeAdjList::iterator it = HyperGraphTypes::NodeAdjList::begin(pNode);
it.valid(); it++)
{
if ((*it)->theEdge() == pEdge) return (*it);
}
}
return 0;
}
inline void HyperGraph::delEdge(HyperGraph::EdgeElement* pEdge)
{
for (HyperGraphTypes::EdgeAdjList::iterator it = HyperGraphTypes::EdgeAdjList::begin(pEdge); it.valid();)
{
AdjElement* pAdj = *it;
HyperGraphTypes::NodeAdjList::remove( pAdj->theNode(), pAdj );
it = HyperGraphTypes::EdgeAdjList::remove( pEdge, it );
freeAdjElement( pAdj );
}
HyperGraphTypes::EdgeList::remove( this, pEdge );
freeEdgeElement( pEdge );
}
void HyperGraph::delNode(HyperGraph::NodeElement* pNode)
{
for (HyperGraphTypes::NodeAdjList::iterator it = HyperGraphTypes::NodeAdjList::begin(pNode); it.valid();)
{
AdjElement* pAdj = *it;
HyperGraphTypes::EdgeAdjList::remove( pAdj->theEdge(), pAdj );
it = HyperGraphTypes::NodeAdjList::remove( pNode, it );
freeAdjElement( pAdj );
}
HyperGraphTypes::NodeList::remove( this, pNode );
freeNodeElement( pNode );
}
inline int HyperGraph::numberOfNodes() const { return HyperGraphTypes::NodeList::size(this); }
inline int HyperGraph::numberOfEdges() const { return HyperGraphTypes::EdgeList::size(this); }
inline void HyperGraph::clear()
{
while (numberOfNodes())
{
delNode( HyperGraphTypes::NodeList::front(this) );
}
while (numberOfEdges())
{
delEdge( HyperGraphTypes::EdgeList::front(this) );
}
}
template<typename ListType, typename ArrayType>
static void writeToStream(std::ostream& outStream, ArrayType& array)
{
for (typename ListType::iterator it = ListType::begin(array.graph()); it.valid(); it++)
{
outStream << array[*it] << std::endl;
}
}
template<typename ListType, typename ArrayType>
static void readFromStream(std::istream& inStream, ArrayType& array)
{
for (typename ListType::iterator it = ListType::begin(array.graph());it.valid(); it++)
{
inStream >> array[(*it)];
}
}
template<typename T>
static std::ostream& operator<<(std::ostream& outStream, HyperGraph::NodeArray<T>& array)
{
writeToStream<HyperGraphTypes::NodeList, HyperGraph::NodeArray<T> >(outStream, array);
return outStream;
}
template<typename T>
static std::ostream& operator<<(std::ostream& outStream, HyperGraph::EdgeArray<T>& array)
{
writeToStream<HyperGraphTypes::EdgeList, HyperGraph::EdgeArray<T> >(outStream, array);
return outStream;
}
template<typename T>
static std::ostream& operator<<(std::ostream& outStream, HyperGraph::AdjArray<T>& array)
{
for (HyperGraphTypes::EdgeList::iterator it = HyperGraphTypes::EdgeList::begin(array.graph());it.valid(); it++)
{
HyperGraph::edge e = *it;
for (HyperGraphTypes::EdgeAdjList::iterator adj_it = HyperGraphTypes::EdgeAdjList::begin(e); adj_it.valid(); adj_it++)
{
outStream << array[(*adj_it)] << std::endl;
}
}
return outStream;
}
template<typename T>
static std::istream& operator>>(std::istream& inStream, HyperGraph::NodeArray<T>& array)
{
readFromStream<HyperGraphTypes::NodeList, HyperGraph::NodeArray<T> >(inStream, array);
return inStream;
}
template<typename T>
static std::istream& operator>>(std::istream& inStream, HyperGraph::EdgeArray<T>& array)
{
readFromStream<HyperGraphTypes::EdgeList, HyperGraph::EdgeArray<T> >(inStream, array);
return inStream;
}
template<typename T>
static std::istream& operator>>(std::istream& inStream, HyperGraph::AdjArray<T>& array)
{
for (HyperGraphTypes::EdgeList::iterator it = HyperGraphTypes::EdgeList::begin(array.graph());it.valid(); it++)
{
HyperGraph::edge e = *it;
for (HyperGraphTypes::EdgeAdjList::iterator adj_it = HyperGraphTypes::EdgeAdjList::begin(e); adj_it.valid(); adj_it++)
{
inStream >> array[(*adj_it)];
}
}
return inStream;
}
static std::ostream& operator<<(std::ostream& outStream, HyperGraph& graph)
{
outStream << graph.numberOfNodes() << std::endl;
outStream << graph.numberOfEdges() << std::endl;
HyperGraph::NodeArray<int> packed_index(&graph);
int i=0;
for (HyperGraphTypes::NodeList::iterator it = HyperGraphTypes::NodeList::begin(&graph);it.valid(); it++)
{
packed_index[*it] = i++;
}
for (HyperGraphTypes::EdgeList::iterator it = HyperGraphTypes::EdgeList::begin(&graph);it.valid(); it++)
{
HyperGraph::edge e = *it;
outStream << e->cardinality() << std::endl;
for (HyperGraphTypes::EdgeAdjList::iterator adj_it = HyperGraphTypes::EdgeAdjList::begin(e); adj_it.valid(); adj_it++)
{
outStream << packed_index[(*adj_it)->theNode()] << std::endl;
}
}
return outStream;
}
static std::istream& operator>>(std::istream& inStream, HyperGraph& graph)
{
int numNodes;
int numEdges;
inStream >> numNodes;
inStream >> numEdges;
HyperGraph::node* pNodes = new HyperGraph::node[numNodes];
for (int i=0; i<numNodes;i++)
pNodes[i] = graph.newNode();
for (int i=0; i<numEdges;i++)
{
HyperGraph::edge e = graph.newEdge();
int numAdj;
inStream >> numAdj;
for (int j=0; j<numAdj; j++)
{
int packedNodeIndex;
inStream >> packedNodeIndex;
graph.addNode(pNodes[packedNodeIndex], e);
}
}
return inStream;
}
} // end of namespace ogdf
#endif /* HYPERGRAPH_H_ */
|