diff -r 4cd528a30ec1 -r b6a68c15a6a3 lemon/smart_bpugraph.h --- a/lemon/smart_bpugraph.h Fri Jun 30 12:14:36 2006 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,273 +0,0 @@ -/* -*- C++ -*- - * - * This file is a part of LEMON, a generic C++ optimization library - * - * Copyright (C) 2003-2006 - * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport - * (Egervary Research Group on Combinatorial Optimization, EGRES). - * - * Permission to use, modify and distribute this software is granted - * provided that this copyright notice appears in all copies. For - * precise terms see the accompanying LICENSE file. - * - * This software is provided "AS IS" with no warranty of any kind, - * express or implied, and with no claim as to its suitability for any - * purpose. - * - */ - -#ifndef LEMON_SMART_BPUGRAPH_H -#define LEMON_SMART_BPUGRAPH_H - -///\ingroup graphs -///\file -///\brief SmartBpUGraph class. - -#include - -#include - -#include - -#include -#include - -#include - -namespace lemon { - - class SmartBpUGraphBase { - public: - - class NodeSetError : public LogicError { - virtual const char* exceptionName() const { - return "lemon::SmartBpUGraph::NodeSetError"; - } - }; - - protected: - - struct NodeT { - int first; - NodeT() {} - NodeT(int _first) : first(_first) {} - }; - - struct UEdgeT { - int aNode, next_out; - int bNode, next_in; - }; - - std::vector aNodes; - std::vector bNodes; - - std::vector edges; - - public: - - class Node { - friend class SmartBpUGraphBase; - protected: - int id; - - Node(int _id) : id(_id) {} - public: - Node() {} - Node(Invalid) { id = -1; } - bool operator==(const Node i) const {return id==i.id;} - bool operator!=(const Node i) const {return id!=i.id;} - bool operator<(const Node i) const {return id 0) { - node.id = 2 * aNodes.size() - 2; - } else { - node.id = 2 * bNodes.size() - 1; - } - } - void next(Node& node) const { - node.id -= 2; - if (node.id == -2) { - node.id = 2 * bNodes.size() - 1; - } - } - - void first(UEdge& edge) const { - edge.id = edges.size() - 1; - } - void next(UEdge& edge) const { - --edge.id; - } - - void firstFromANode(UEdge& edge, const Node& node) const { - LEMON_ASSERT((node.id & 1) == 0, NodeSetError()); - edge.id = aNodes[node.id >> 1].first; - } - void nextFromANode(UEdge& edge) const { - edge.id = edges[edge.id].next_out; - } - - void firstFromBNode(UEdge& edge, const Node& node) const { - LEMON_ASSERT((node.id & 1) == 1, NodeSetError()); - edge.id = bNodes[node.id >> 1].first; - } - void nextFromBNode(UEdge& edge) const { - edge.id = edges[edge.id].next_in; - } - - static int id(const Node& node) { - return node.id; - } - static Node nodeFromId(int id) { - return Node(id); - } - int maxNodeId() const { - return aNodes.size() > bNodes.size() ? - aNodes.size() * 2 - 2 : bNodes.size() * 2 - 1; - } - - static int id(const UEdge& edge) { - return edge.id; - } - static UEdge uEdgeFromId(int id) { - return UEdge(id); - } - int maxUEdgeId() const { - return edges.size(); - } - - static int aNodeId(const Node& node) { - return node.id >> 1; - } - static Node fromANodeId(int id) { - return Node(id << 1); - } - int maxANodeId() const { - return aNodes.size(); - } - - static int bNodeId(const Node& node) { - return node.id >> 1; - } - static Node fromBNodeId(int id) { - return Node((id << 1) + 1); - } - int maxBNodeId() const { - return bNodes.size(); - } - - Node aNode(const UEdge& edge) const { - return Node(edges[edge.id].aNode); - } - Node bNode(const UEdge& edge) const { - return Node(edges[edge.id].bNode); - } - - static bool aNode(const Node& node) { - return (node.id & 1) == 0; - } - - static bool bNode(const Node& node) { - return (node.id & 1) == 1; - } - - Node addANode() { - NodeT nodeT; - nodeT.first = -1; - aNodes.push_back(nodeT); - return Node(aNodes.size() * 2 - 2); - } - - Node addBNode() { - NodeT nodeT; - nodeT.first = -1; - bNodes.push_back(nodeT); - return Node(bNodes.size() * 2 - 1); - } - - UEdge addEdge(const Node& source, const Node& target) { - LEMON_ASSERT(((source.id ^ target.id) & 1) == 1, NodeSetError()); - UEdgeT edgeT; - if ((source.id & 1) == 0) { - edgeT.aNode = source.id; - edgeT.bNode = target.id; - } else { - edgeT.aNode = target.id; - edgeT.bNode = source.id; - } - edgeT.next_out = aNodes[edgeT.aNode >> 1].first; - aNodes[edgeT.aNode >> 1].first = edges.size(); - edgeT.next_in = bNodes[edgeT.bNode >> 1].first; - bNodes[edgeT.bNode >> 1].first = edges.size(); - edges.push_back(edgeT); - return UEdge(edges.size() - 1); - } - - void clear() { - aNodes.clear(); - bNodes.clear(); - edges.clear(); - } - - typedef True NodeNumTag; - int nodeNum() const { return aNodes.size() + bNodes.size(); } - int aNodeNum() const { return aNodes.size(); } - int bNodeNum() const { return bNodes.size(); } - - typedef True EdgeNumTag; - int uEdgeNum() const { return edges.size(); } - - }; - - - typedef BpUGraphExtender ExtendedSmartBpUGraphBase; - - /// \ingroup graphs - /// - /// \brief A smart bipartite undirected graph class. - /// - /// This is a simple and fast bipartite undirected graph implementation. - /// It is also quite memory efficient, but at the price - /// that it does not support node and edge deletions. - /// Except from this it conforms to - /// the \ref concept::BpUGraph "BpUGraph" concept. - /// \sa concept::BpUGraph. - /// - class SmartBpUGraph : public ExtendedSmartBpUGraphBase {}; - - -} //namespace lemon - - -#endif //LEMON_SMART_GRAPH_H