# HG changeset patch # User klao # Date 1100117672 0 # Node ID 48962802d168885637a5ee7e630157d957418d36 # Parent 04591f9a417306c12da37be7d7a9a9b747850440 * enable_if imported from BOOST * count{Nodes,Edges} implemented via graph tags * some #include bugs fixed diff -r 04591f9a4173 -r 48962802d168 src/lemon/Makefile.am --- a/src/lemon/Makefile.am Wed Nov 10 19:59:14 2004 +0000 +++ b/src/lemon/Makefile.am Wed Nov 10 20:14:32 2004 +0000 @@ -29,6 +29,7 @@ concept_check.h \ map_defines.h \ map_bits.h \ + utility.h \ iterable_graph_extender.h \ idmappable_graph_extender.h \ extendable_graph_extender.h \ diff -r 04591f9a4173 -r 48962802d168 src/lemon/bfs.h --- a/src/lemon/bfs.h Wed Nov 10 19:59:14 2004 +0000 +++ b/src/lemon/bfs.h Wed Nov 10 20:14:32 2004 +0000 @@ -25,6 +25,7 @@ #include #include +#include namespace lemon { diff -r 04591f9a4173 -r 48962802d168 src/lemon/full_graph.h --- a/src/lemon/full_graph.h Wed Nov 10 19:59:14 2004 +0000 +++ b/src/lemon/full_graph.h Wed Nov 10 20:14:32 2004 +0000 @@ -19,19 +19,19 @@ #include - #include - #include #include +#include +#include + + ///\ingroup graphs ///\file ///\brief FullGraph and SymFullGraph classes. -#include - namespace lemon { /// \addtogroup graphs @@ -58,6 +58,9 @@ // FullGraphBase(const FullGraphBase &_g) // : NodeNum(_g.nodeNum()), EdgeNum(NodeNum*NodeNum) { } + typedef True NodeNumTag; + typedef True EdgeNumTag; + ///Number of nodes. int nodeNum() const { return NodeNum; } ///Number of edges. @@ -206,21 +209,9 @@ FullGraph(int n) { construct(n); } }; - template <> - int countNodes(const FullGraph& graph) { - return graph.nodeNum(); - } - - template <> - int countEdges(const FullGraph& graph) { - return graph.edgeNum(); - } - /// @} } //namespace lemon - - #endif //LEMON_FULL_GRAPH_H diff -r 04591f9a4173 -r 48962802d168 src/lemon/graph_utils.h --- a/src/lemon/graph_utils.h Wed Nov 10 19:59:14 2004 +0000 +++ b/src/lemon/graph_utils.h Wed Nov 10 20:14:32 2004 +0000 @@ -20,6 +20,7 @@ #include #include +#include ///\ingroup gutils ///\file @@ -35,7 +36,6 @@ /// \addtogroup gutils /// @{ - // counters in the graph /// \brief Function to count the items in the graph. /// /// This function counts the items in the graph. @@ -43,23 +43,53 @@ /// it iterates on all of the items. template - inline int countItems(const Graph& _g) { + inline int countItems(const Graph& g) { int num = 0; - for (ItemIt it(_g); it != INVALID; ++it) { + for (ItemIt it(g); it != INVALID; ++it) { ++num; } return num; } + // Node counting: + + template + inline + typename enable_if::type + _countNodes(const Graph &g) { + return g.nodeNum(); + } + + template + inline int _countNodes(Wrap w) { + return countItems(w.value); + } + /// \brief Function to count the nodes in the graph. /// /// This function counts the nodes in the graph. /// The complexity of the function is O(n) but for some /// graph structure it is specialized to run in O(1). + /// + /// \todo refer how to specialize it template - inline int countNodes(const Graph& _g) { - return countItems(_g); + inline int countNodes(const Graph& g) { + return _countNodes(g); + } + + // Edge counting: + + template + inline + typename enable_if::type + _countEdges(const Graph &g) { + return g.edgeNum(); + } + + template + inline int _countEdges(Wrap w) { + return countItems(w.value); } /// \brief Function to count the edges in the graph. @@ -67,9 +97,10 @@ /// This function counts the edges in the graph. /// The complexity of the function is O(e) but for some /// graph structure it is specialized to run in O(1). + template - inline int countEdges(const Graph& _g) { - return countItems(_g); + inline int countEdges(const Graph& g) { + return _countEdges(g); } /// \brief Function to count the symmetric edges in the graph. @@ -82,6 +113,7 @@ return countItems(_g); } + template inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) { int num = 0; diff -r 04591f9a4173 -r 48962802d168 src/lemon/maps.h --- a/src/lemon/maps.h Wed Nov 10 19:59:14 2004 +0000 +++ b/src/lemon/maps.h Wed Nov 10 20:14:32 2004 +0000 @@ -104,9 +104,6 @@ V operator[](const K&) const { return v; } void set(const K&, const V&) { } }; - //to document later - typedef Const True; - typedef Const False; /// \c std::map wrapper diff -r 04591f9a4173 -r 48962802d168 src/lemon/preflow.h --- a/src/lemon/preflow.h Wed Nov 10 19:59:14 2004 +0000 +++ b/src/lemon/preflow.h Wed Nov 10 20:14:32 2004 +0000 @@ -22,6 +22,7 @@ #include #include +#include /// \file /// \ingroup flowalgs diff -r 04591f9a4173 -r 48962802d168 src/lemon/smart_graph.h --- a/src/lemon/smart_graph.h Wed Nov 10 19:59:14 2004 +0000 +++ b/src/lemon/smart_graph.h Wed Nov 10 20:14:32 2004 +0000 @@ -27,17 +27,12 @@ #include #include - #include - #include - #include #include - -#include - +#include namespace lemon { @@ -84,6 +79,9 @@ SmartGraphBase() : nodes(), edges() { } SmartGraphBase(const SmartGraphBase &_g) : nodes(_g.nodes), edges(_g.edges) { } + typedef True NodeNumTag; + typedef True EdgeNumTag; + ///Number of nodes. int nodeNum() const { return nodes.size(); } ///Number of edges. @@ -323,20 +321,8 @@ } }; - template <> - int countNodes(const SmartGraph& graph) { - return graph.nodeNum(); - } - - template <> - int countEdges(const SmartGraph& graph) { - return graph.edgeNum(); - } - /// @} } //namespace lemon - - #endif //LEMON_SMART_GRAPH_H diff -r 04591f9a4173 -r 48962802d168 src/lemon/utility.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/lemon/utility.h Wed Nov 10 20:14:32 2004 +0000 @@ -0,0 +1,110 @@ +/* -*- C++ -*- + * + * src/lemon/utility.h - Part of LEMON, a generic C++ optimization library + * + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi + * Kutatocsoport (Egervary Combinatorial Optimization Research Group, + * 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. + * + * This file contains a modified version of the enable_if library from BOOST. + * See the appropriate copyright notice below. + */ + +// Boost enable_if library + +// Copyright 2003 © The Trustees of Indiana University. + +// Use, modification, and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +// Authors: Jaakko Järvi (jajarvi at osl.iu.edu) +// Jeremiah Willcock (jewillco at osl.iu.edu) +// Andrew Lumsdaine (lums at osl.iu.edu) + + +#ifndef LEMON_UTILITY_H +#define LEMON_UTILITY_H + +namespace lemon +{ + + /// Basic type for defining "tags". A "YES" condidion for enable_if. + + /// \todo This should go to a separate "basic_types.h" (or something) + /// file. + struct True { + static const bool value = true; + }; + + /// Basic type for defining "tags". A "NO" condidion for enable_if. + struct False { + static const bool value = false; + }; + + template + struct Wrap { + const T &value; + Wrap(const T &t) : value(t) {} + }; + + + + /**************** enable_if from BOOST ****************/ + + template + struct enable_if_c { + typedef T type; + }; + + template + struct enable_if_c {}; + + template + struct enable_if : public enable_if_c {}; + + template + struct lazy_enable_if_c { + typedef typename T::type type; + }; + + template + struct lazy_enable_if_c {}; + + template + struct lazy_enable_if : public lazy_enable_if_c {}; + + + template + struct disable_if_c { + typedef T type; + }; + + template + struct disable_if_c {}; + + template + struct disable_if : public disable_if_c {}; + + template + struct lazy_disable_if_c { + typedef typename T::type type; + }; + + template + struct lazy_disable_if_c {}; + + template + struct lazy_disable_if : public lazy_disable_if_c {}; + +} // namespace lemon + +#endif diff -r 04591f9a4173 -r 48962802d168 src/test/graph_utils_test.cc --- a/src/test/graph_utils_test.cc Wed Nov 10 19:59:14 2004 +0000 +++ b/src/test/graph_utils_test.cc Wed Nov 10 20:14:32 2004 +0000 @@ -3,6 +3,8 @@ #include #include +#include + #include #include #include @@ -22,6 +24,12 @@ { // checking smart graph checkGraphCounters(); } + { + int num = 5; + FullGraph fg(num); + check(countNodes(fg) == num, "FullGraph: wrong node number."); + check(countEdges(fg) == num*num, "FullGraph: wrong edge number."); + } std::cout << __FILE__ ": All tests passed.\n"; diff -r 04591f9a4173 -r 48962802d168 src/test/graph_utils_test.h --- a/src/test/graph_utils_test.h Wed Nov 10 19:59:14 2004 +0000 +++ b/src/test/graph_utils_test.h Wed Nov 10 20:14:32 2004 +0000 @@ -30,11 +30,11 @@ Graph graph; addPetersen(graph, num); bidirGraph(graph); - check(countNodes(graph) == 2*num, "Wrong node counter."); - check(countEdges(graph) == 6*num, "Wrong edge counter."); + check(countNodes(graph) == 2*num, "Wrong node number."); + check(countEdges(graph) == 6*num, "Wrong edge number."); for (typename Graph::NodeIt it(graph); it != INVALID; ++it) { - check(countOutEdges(graph, it) == 3, "Wrong out degree counter."); - check(countInEdges(graph, it) == 3, "Wrong in degree counter."); + check(countOutEdges(graph, it) == 3, "Wrong out degree number."); + check(countInEdges(graph, it) == 3, "Wrong in degree number."); } }