Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r53983 - in sandbox/SOC/2009/function_graph: boost/function_graph libs/test
From: mlopez7_at_[hidden]
Date: 2009-06-16 16:47:41


Author: lopezeant
Date: 2009-06-16 16:47:40 EDT (Tue, 16 Jun 2009)
New Revision: 53983
URL: http://svn.boost.org/trac/boost/changeset/53983

Log:
template parameter rewrite finished, also added vertices()
Text files modified:
   sandbox/SOC/2009/function_graph/boost/function_graph/function_graph.hpp | 175 ++++++++++++++++++++++++++++++---------
   sandbox/SOC/2009/function_graph/libs/test/test2.cpp | 57 +++++-------
   2 files changed, 160 insertions(+), 72 deletions(-)

Modified: sandbox/SOC/2009/function_graph/boost/function_graph/function_graph.hpp
==============================================================================
--- sandbox/SOC/2009/function_graph/boost/function_graph/function_graph.hpp (original)
+++ sandbox/SOC/2009/function_graph/boost/function_graph/function_graph.hpp 2009-06-16 16:47:40 EDT (Tue, 16 Jun 2009)
@@ -15,6 +15,7 @@
 #include <boost/function_types/function_arity.hpp>
 #include <utility>
 #include <boost/optional/optional_fwd.hpp>
+#include <boost/range.hpp>
 
 namespace boost {
 
@@ -53,10 +54,6 @@
     typedef typename detail::func_graph_edge<result_type,
                                              vertex_type> edge_type;
 
- /** Constructor - Default */
- function_graph_base()
- { }
-
     /** Constructors to allow for initialization of edge */
     function_graph_base(function_type const& f)
         : edge_(f)
@@ -69,13 +66,40 @@
     function_type edge_;
 };
 
+/** @name no_domain
+ * A trait of function_graph used to declare a function_graph with no domain.
+ * @note is there somewhere else I could put this? Perhaps a traits file?
+ */
+
+struct no_domain { };
+
+
+
+/** @name function_graph_range */
+template <typename Iterator>
+struct function_graph_range
+{
+ typedef Iterator iterator_type;
+ typedef typename iterator_range<iterator_type> iterator_range_type;
+
+ /** Constructor - takes a pair of iterators */
+ function_graph_range(iterator_range_type const& r)
+ : range_(r)
+ { }
+
+ iterator_range_type range_;
+};
+
 
 
 /**
  * Empty function graph prevents instantiations such as function_graph<int> and
  * function_graph<bool (int, int)>.
  */
-template <typename T> struct function_graph { };
+
+template <typename T, typename U = no_domain> struct function_graph { };
+
+
 
 /**
  * function_graph is a data structure that implements implicit graphs and more.
@@ -85,14 +109,18 @@
  * function during execution. However, since the code needed is trivial,
  * set_edge is part of the interface. Paired with it is the default constructor.
  */
-template <typename Result, typename Vertex>
-struct function_graph<function<Result(Vertex, Vertex)> >
- : public function_graph_base<function<Result(Vertex, Vertex)> >
+
+template <typename Result, typename Vertex, typename Iterator>
+struct function_graph<function<Result(Vertex, Vertex)>, Iterator>
+ : public function_graph_base<function<Result(Vertex, Vertex)> >,
+ public function_graph_range<Range>
 {
+private:
     typedef function_graph_base<function<Result(Vertex, Vertex)> > Base;
     typedef function_graph<function<Result(Vertex, Vertex)> > This;
-public:
+ typedef function_graph_range<Iterator> RangeBase; //? not another Base
 
+public:
     typedef typename Base::function_type function_type;
     typedef typename Base::vertex_type vertex_descriptor;
     typedef typename Base::edge_type edge_descriptor;
@@ -100,22 +128,62 @@
     typedef directed_tag directed_category;
     typedef disallow_parallel_edge_tag edge_parallel_category;
     typedef adjacency_matrix_tag traversal_category;
+ typedef typename RangeBase::iterator_range_type iterator_range_type;
+ typedef typename RangeBase::iterator_type iterator_type;
 
- /** Constructor: default */
- function_graph()
- : Base()
+ /** Constructor: takes a functor and range */
+ function_graph(function_type const& f, iterator_range_type const& r)
+ : Base(f), Range(r)
     { }
+};
+
+template <typename Result, typename Vertex>
+struct function_graph<function<Result(Vertex, Vertex)>, no_domain>
+ : public function_graph_base<function<Result(Vertex, Vertex)> >
+{
+private:
+ typedef function_graph_base<function<Result(Vertex, Vertex)> > Base;
+ typedef function_graph<function<Result(Vertex, Vertex)> > This;
+
+public:
+ typedef typename Base::function_type function_type;
+ typedef typename Base::vertex_type vertex_descriptor;
+ typedef typename Base::edge_type edge_descriptor;
+ typedef typename Base::result_type result_type;
+ typedef directed_tag directed_category;
+ typedef disallow_parallel_edge_tag edge_parallel_category;
+ typedef adjacency_matrix_tag traversal_category;
 
     /** Constructor: takes a boost::function or functor */
     function_graph(function_type const& f)
         : Base(f)
     { }
-
-private:
- /** Edge function from Base */
- //using Base::edge;
 };
 
+
+
+/** @name make_function_graph
+ * Create and return a function_graph object deduced from the function arguments
+ * passed to it.
+ */
+
+//@{
+template <typename Result, typename Vertex>
+function_graph<function<Result(Vertex, Vertex)> >
+ make_function_graph(function<Result(Vertex, Vertex)> const& f)
+{
+ return function_graph<function<Result(Vertex, Vertex)> >(f);
+}
+
+template <typename Result, typename Vertex, typename Iterator>
+function_graph<function<Result(Vertex, Vertex)>, Iterator>
+ make_function_graph(function<Result(Vertex, Vertex)> const& f,
+ Iterator const& r)
+{
+ return function_graph<function<Result(Vertex, Vertex)>, Iterator>(f,r);
+}
+//@}
+
 /**
  * @note This specialization will match any function of the form E(V,V) and
  * generates the graph over an adapted boost function. Note that functions of
@@ -129,58 +197,80 @@
 
 
 
-/**
- * source(e, g) and target(e, g) are part of the incedence graph concept.
- */
+/** source(e, g) and target(e, g) are part of the incedence graph concept. */
 
-template <typename Result, typename Vertex>
+template <typename Result, typename Vertex, typename Iterator>
 Vertex source(detail::func_graph_edge<Result, Vertex> const& e,
- function_graph<function<Result(Vertex, Vertex)> > const& g)
+ function_graph<function<Result(Vertex, Vertex)>, Iterator > const&
+ g)
 {
     return e.source;
 }
 
-template <typename Result, typename Vertex>
+template <typename Result, typename Vertex, typename Iterator>
 Vertex target(detail::func_graph_edge<Result, Vertex> const& e,
- function_graph<function<Result(Vertex, Vertex)> > const& g)
+ function_graph<function<Result(Vertex, Vertex)>, Iterator > const&
+ g)
 {
     return e.target;
 }
 
 
 
-/**
+/** vertices(g) is part of the vertex list concept. */
+
+template <typename Result, typename Vertex, typename Iterator>
+std::pair<typename function_graph<function<Result(Vertex, Vertex)>,
+ Range>::iterator_type,
+ typename function_graph<function<Result(Vertex, Vertex)>,
+ Range>::iterator_type>
+vertices(function_graph<function<Result(Vertex, Vertex)>, Range> const& g) {
+ return std::make_pair(begin(g.range_), end(g.range_));
+}
+
+
+
+/** @name bind_edge
  * edge(u, v, g) is part of the adjacency matrix concept called Direct Edge
  * Access. The function must account for edges that already return. There is
  * specialization to account for functions that use bool and optional<T>.
  */
 
-#define FG_EDGE_BOOL detail::func_graph_edge<Result, Vertex>
-
-template <typename Vertex, typename Result>
-std::pair<FG_EDGE_BOOL, bool> bind_edge(Result const& x, Vertex u, Vertex v)
-{ return std::make_pair(FG_EDGE_BOOL(x, u, v), true); }
+//@{
+namespace detail {
 
-#undef FG_EDGE_BOOL
-#define FG_EDGE_BOOL detail::func_graph_edge<bool, Vertex>
+template <typename Result, typename Vertex>
+std::pair<detail::func_graph_edge<Result, Vertex>, bool>
+bind_edge(Result const& x, Vertex u, Vertex v)
+{
+ return std::make_pair(detail::func_graph_edge<Result, Vertex>(x, u, v),
+ true);
+}
 
 template <typename Vertex>
-std::pair<FG_EDGE_BOOL, bool> bind_edge(bool x, Vertex u, Vertex v)
-{ return std::make_pair(typename FG_EDGE_BOOL(x, u, v), x); }
-
-#undef FG_EDGE_BOOL
-#define FG_EDGE_BOOL detail::func_graph_edge<optional<OptType>, Vertex>
+std::pair<detail::func_graph_edge<bool, Vertex>, bool>
+bind_edge(bool x, Vertex u, Vertex v)
+{
+ return std::make_pair(typename detail::func_graph_edge
+ <bool, Vertex>(x, u, v),
+ x);
+}
 
+// This overload is specific to optional<T>
 template <typename OptType, typename Vertex>
-std::pair<FG_EDGE_BOOL, bool>
+std::pair<detail::func_graph_edge<optional<OptType>, Vertex>, bool>
 bind_edge(optional<OptType> const& x, Vertex u, Vertex v)
-{ return std::make_pair(FG_EDGE_BOOL(x, u, v), (bool)x); }
+{
+ return std::make_pair(detail::func_graph_edge
+ <optional<OptType>, Vertex>(x, u, v),
+ (bool)x);
+}
 
-#undef FG_EDGE_BOOL
+} // detail namespace
 
-#define FUNC_GRAPH function_graph<function<Result(Vertex, Vertex)> >
+#define FUNC_GRAPH function_graph<function<Result(Vertex, Vertex)>, Iterator>
 
-template <typename Result, typename Vertex>
+template <typename Result, typename Vertex, typename Iterator>
 std::pair<typename FUNC_GRAPH::edge_descriptor, bool>
 edge(typename FUNC_GRAPH::vertex_descriptor u,
      typename FUNC_GRAPH::vertex_descriptor v,
@@ -192,6 +282,9 @@
     return bind_edge(result, u, v);
 }
 
+#undef FUNC_GRAPH
+//@}
+
 } // boost namespace
 
 #endif /*FUNCTION_GRAPH_HPP_*/

Modified: sandbox/SOC/2009/function_graph/libs/test/test2.cpp
==============================================================================
--- sandbox/SOC/2009/function_graph/libs/test/test2.cpp (original)
+++ sandbox/SOC/2009/function_graph/libs/test/test2.cpp 2009-06-16 16:47:40 EDT (Tue, 16 Jun 2009)
@@ -1,6 +1,5 @@
 /**
- * Testing a function that returns a type that is always used. This means
- * the graph is closed.
+ * Testing a function that has a range
  */
 
 #include <iostream>
@@ -8,46 +7,42 @@
 #include <functional>
 #include <algorithm>
 #include <vector>
-#include <utility>
-#include "function_graph.hpp"
+//#include <utility>
+#include "function_graph_alt.hpp"
 #include <cmath>
+#include <boost/range.hpp>
 
-struct point {
- double x, y;
-}
-
-struct distance_2 {
- double operator()(point a, point b)
- { return sqrt(pow(a.x - b.x,2) + pow(a.x - b.x,2)); }
+////////
+// Boolean function
+template <typename T>
+struct less_than {
+ bool operator() (T a, T b) { return a < b; }
 };
 
 int main()
 {
+ unsigned int tempArray[5] = {1, 4, 9, 2, 7};
+ std::vector<unsigned int> vectorOfInts(tempArray, tempArray + 5);
+
     ////////
- // Create a boost function and function graph.
- typedef boost::function<double(int,int)> function_type;
- typedef boost::function_graph<function_type> graph;
- typedef graph::edge_descriptor edge_descriptor;
- function_type f = less_than<int>();
- function_type g = less_than<int>();
- graph funcGraph(f);
-
- ////////
- // Set a new function to the graph.
- funcGraph.set_function(g);
-
- ////////
- // Check the edge output.
- std::cout << "2 < 1 check ";
- if(funcGraph.edge(2,1)) std::cout << "fails." << "\n";
- else std::cout << "passes." << "\n";
+ // Create typedefs for functions and function graphs
+ typedef boost::function<bool(int,int)> function_boolean;
+ typedef boost::function_graph<function_boolean> graph_boolean;
 
     ////////
- // Check the adjacency matrix edge
- std::pair<edge_descriptor, bool> edge_pair = boost::edge(1, 2, funcGraph);
- std::cout << edge_pair.first << "\n";
+ // Create functions, graphs and edges
+ graph_boolean funcGraph_boolean(std::less<int>());
 
+ typedef std::pair<std::vector<unsigned int>::iterator,
+ std::vector<unsigned int>::iterator> iter_pair;
+
     
+ boost::iterator_range<std::vector<unsigned int>::iterator>
+ rangeIter(vectorOfInts.begin(), vectorOfInts.end());
+ boost::iterator_range<std::vector<unsigned int>::iterator>
+ rangeIter2(vectorOfInts);
+ boost::iterator_range<std::vector<unsigned int>::iterator>
+ rangeIter3(std::make_pair(vectorOfInts.begin(), vectorOfInts.end()));
     
     return 0;
 }


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk