[Lemon-commits] [lemon_svn] klao: r1365 - in hugo/trunk/src: lemon test

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:44:51 CET 2006


Author: klao
Date: Wed Nov 10 21:14:32 2004
New Revision: 1365

Added:
   hugo/trunk/src/lemon/utility.h
Modified:
   hugo/trunk/src/lemon/Makefile.am
   hugo/trunk/src/lemon/bfs.h
   hugo/trunk/src/lemon/full_graph.h
   hugo/trunk/src/lemon/graph_utils.h
   hugo/trunk/src/lemon/maps.h
   hugo/trunk/src/lemon/preflow.h
   hugo/trunk/src/lemon/smart_graph.h
   hugo/trunk/src/test/graph_utils_test.cc
   hugo/trunk/src/test/graph_utils_test.h

Log:
* enable_if imported from BOOST
* count{Nodes,Edges} implemented via graph tags
* some #include bugs fixed


Modified: hugo/trunk/src/lemon/Makefile.am
==============================================================================
--- hugo/trunk/src/lemon/Makefile.am	(original)
+++ hugo/trunk/src/lemon/Makefile.am	Wed Nov 10 21:14:32 2004
@@ -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					\

Modified: hugo/trunk/src/lemon/bfs.h
==============================================================================
--- hugo/trunk/src/lemon/bfs.h	(original)
+++ hugo/trunk/src/lemon/bfs.h	Wed Nov 10 21:14:32 2004
@@ -25,6 +25,7 @@
 
 #include <lemon/bin_heap.h>
 #include <lemon/invalid.h>
+#include <lemon/graph_utils.h>
 
 namespace lemon {
 

Modified: hugo/trunk/src/lemon/full_graph.h
==============================================================================
--- hugo/trunk/src/lemon/full_graph.h	(original)
+++ hugo/trunk/src/lemon/full_graph.h	Wed Nov 10 21:14:32 2004
@@ -19,19 +19,19 @@
 
 
 #include <lemon/idmappable_graph_extender.h>
-
 #include <lemon/iterable_graph_extender.h>
-
 #include <lemon/alteration_observer_registry.h>
 #include <lemon/default_map.h>
 
+#include <lemon/invalid.h>
+#include <lemon/utility.h>
+
+
 ///\ingroup graphs
 ///\file
 ///\brief FullGraph and SymFullGraph classes.
 
 
-#include <lemon/invalid.h>
-
 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<FullGraph>(const FullGraph& graph) {
-    return graph.nodeNum();
-  }
-
-  template <>
-  int countEdges<FullGraph>(const FullGraph& graph) {
-    return graph.edgeNum();
-  }
-
   /// @}  
 
 } //namespace lemon
 
 
-
-
 #endif //LEMON_FULL_GRAPH_H

Modified: hugo/trunk/src/lemon/graph_utils.h
==============================================================================
--- hugo/trunk/src/lemon/graph_utils.h	(original)
+++ hugo/trunk/src/lemon/graph_utils.h	Wed Nov 10 21:14:32 2004
@@ -20,6 +20,7 @@
 #include <iterator>
 
 #include <lemon/invalid.h>
+#include <lemon/utility.h>
 
 ///\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 <typename Graph, typename ItemIt>
-  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 <typename Graph>
+  inline
+  typename enable_if<typename Graph::NodeNumTag, int>::type
+  _countNodes(const Graph &g) {
+    return g.nodeNum();
+  }
+
+  template <typename Graph>
+  inline int _countNodes(Wrap<Graph> w) {
+    return countItems<Graph, typename Graph::NodeIt>(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 <typename Graph>
-  inline int countNodes(const Graph& _g) {
-    return countItems<Graph, typename Graph::NodeIt>(_g);
+  inline int countNodes(const Graph& g) {
+    return _countNodes<Graph>(g);
+  }
+
+  // Edge counting:
+
+  template <typename Graph>
+  inline
+  typename enable_if<typename Graph::EdgeNumTag, int>::type
+  _countEdges(const Graph &g) {
+    return g.edgeNum();
+  }
+
+  template <typename Graph>
+  inline int _countEdges(Wrap<Graph> w) {
+    return countItems<Graph, typename Graph::EdgeIt>(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 <typename Graph>
-  inline int countEdges(const Graph& _g) {
-    return countItems<Graph, typename Graph::EdgeIt>(_g);
+  inline int countEdges(const Graph& g) {
+    return _countEdges<Graph>(g);
   }
 
   /// \brief Function to count the symmetric edges in the graph.
@@ -82,6 +113,7 @@
     return countItems<Graph, typename Graph::SymEdgeIt>(_g);
   }
 
+
   template <typename Graph, typename DegIt>
   inline int countNodeDegree(const Graph& _g, const typename Graph::Node& _n) {
     int num = 0;

Modified: hugo/trunk/src/lemon/maps.h
==============================================================================
--- hugo/trunk/src/lemon/maps.h	(original)
+++ hugo/trunk/src/lemon/maps.h	Wed Nov 10 21:14:32 2004
@@ -104,9 +104,6 @@
     V operator[](const K&) const { return v; }
     void set(const K&, const V&) { }
   };
-  //to document later
-  typedef Const<bool, true> True;
-  typedef Const<bool, false> False;
 
   /// \c std::map wrapper
 

Modified: hugo/trunk/src/lemon/preflow.h
==============================================================================
--- hugo/trunk/src/lemon/preflow.h	(original)
+++ hugo/trunk/src/lemon/preflow.h	Wed Nov 10 21:14:32 2004
@@ -22,6 +22,7 @@
 
 #include <lemon/invalid.h>
 #include <lemon/maps.h>
+#include <lemon/graph_utils.h>
 
 /// \file
 /// \ingroup flowalgs

Modified: hugo/trunk/src/lemon/smart_graph.h
==============================================================================
--- hugo/trunk/src/lemon/smart_graph.h	(original)
+++ hugo/trunk/src/lemon/smart_graph.h	Wed Nov 10 21:14:32 2004
@@ -27,17 +27,12 @@
 
 #include <lemon/clearable_graph_extender.h>
 #include <lemon/extendable_graph_extender.h>
-
 #include <lemon/idmappable_graph_extender.h>
-
 #include <lemon/iterable_graph_extender.h>
-
 #include <lemon/alteration_observer_registry.h>
 #include <lemon/default_map.h>
 
-
-#include <lemon/graph_utils.h>
-
+#include <lemon/utility.h>
 
 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<SmartGraph>(const SmartGraph& graph) {
-    return graph.nodeNum();
-  }
-
-  template <>
-  int countEdges<SmartGraph>(const SmartGraph& graph) {
-    return graph.edgeNum();
-  }
-
   /// @}  
 } //namespace lemon
 
 
-
-
 #endif //LEMON_SMART_GRAPH_H

Added: hugo/trunk/src/lemon/utility.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/lemon/utility.h	Wed Nov 10 21:14:32 2004
@@ -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 <typename T>
+  struct Wrap {
+    const T &value;
+    Wrap(const T &t) : value(t) {}
+  };
+
+
+
+  /**************** enable_if from BOOST ****************/
+ 
+  template <bool B, class T = void>
+  struct enable_if_c {
+    typedef T type;
+  };
+
+  template <class T>
+  struct enable_if_c<false, T> {};
+
+  template <class Cond, class T = void> 
+  struct enable_if : public enable_if_c<Cond::value, T> {};
+
+  template <bool B, class T>
+  struct lazy_enable_if_c {
+    typedef typename T::type type;
+  };
+
+  template <class T>
+  struct lazy_enable_if_c<false, T> {};
+
+  template <class Cond, class T> 
+  struct lazy_enable_if : public lazy_enable_if_c<Cond::value, T> {};
+
+
+  template <bool B, class T = void>
+  struct disable_if_c {
+    typedef T type;
+  };
+
+  template <class T>
+  struct disable_if_c<true, T> {};
+
+  template <class Cond, class T = void> 
+  struct disable_if : public disable_if_c<Cond::value, T> {};
+
+  template <bool B, class T>
+  struct lazy_disable_if_c {
+    typedef typename T::type type;
+  };
+
+  template <class T>
+  struct lazy_disable_if_c<true, T> {};
+
+  template <class Cond, class T> 
+  struct lazy_disable_if : public lazy_disable_if_c<Cond::value, T> {};
+
+} // namespace lemon
+
+#endif

Modified: hugo/trunk/src/test/graph_utils_test.cc
==============================================================================
--- hugo/trunk/src/test/graph_utils_test.cc	(original)
+++ hugo/trunk/src/test/graph_utils_test.cc	Wed Nov 10 21:14:32 2004
@@ -3,6 +3,8 @@
 #include <iostream>
 #include <vector>
 
+#include <lemon/graph_utils.h>
+
 #include <lemon/list_graph.h>
 #include <lemon/smart_graph.h>
 #include <lemon/full_graph.h>
@@ -22,6 +24,12 @@
   { // checking smart graph
     checkGraphCounters<SmartGraph>();
   }
+  {
+    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";
 

Modified: hugo/trunk/src/test/graph_utils_test.h
==============================================================================
--- hugo/trunk/src/test/graph_utils_test.h	(original)
+++ hugo/trunk/src/test/graph_utils_test.h	Wed Nov 10 21:14:32 2004
@@ -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.");
     }
   }
   



More information about the Lemon-commits mailing list