• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //=======================================================================
2 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
3 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 //=======================================================================
9 
10 #include <boost/config.hpp>
11 #include <iostream>
12 #include <algorithm>
13 #include <boost/graph/adjacency_list.hpp>
14 #include <boost/property_map/property_map.hpp>
15 #include <string>
16 
17 using namespace std;
18 using namespace boost;
19 
20 /*
21   Interior Property Map Basics
22 
23   An interior property map is a way of associating properties
24   with the vertices or edges of a graph. The "interior" part means
25   that the properties are stored inside the graph object. This can be
26   convenient when the need for the properties is somewhat permanent,
27   and when the properties will be with a graph for the duration of its
28   lifetime. A "distance from source vertex" property is often of this
29   kind.
30 
31   Sample Output
32 
33   Jeremy owes Rich some money
34   Jeremy owes Andrew some money
35   Jeremy owes Jeff some money
36   Jeremy owes Kinis some money
37   Andrew owes Jeremy some money
38   Andrew owes Kinis some money
39   Jeff owes Jeremy some money
40   Jeff owes Rich some money
41   Jeff owes Kinis some money
42   Kinis owes Jeremy some money
43   Kinis owes Rich some money
44 
45  */
46 
47 // create a tag for our new property
48 
49 enum vertex_first_name_t
50 {
51     vertex_first_name
52 };
53 namespace boost
54 {
55 BOOST_INSTALL_PROPERTY(vertex, first_name);
56 }
57 
58 template < class EdgeIter, class Graph >
who_owes_who(EdgeIter first,EdgeIter last,const Graph & G)59 void who_owes_who(EdgeIter first, EdgeIter last, const Graph& G)
60 {
61     // Access the propety acessor type for this graph
62     typedef
63         typename property_map< Graph, vertex_first_name_t >::const_type NamePA;
64     NamePA name = get(vertex_first_name, G);
65 
66     typedef typename boost::property_traits< NamePA >::value_type NameType;
67 
68     NameType src_name, targ_name;
69 
70     while (first != last)
71     {
72         src_name = boost::get(name, source(*first, G));
73         targ_name = boost::get(name, target(*first, G));
74         cout << src_name << " owes " << targ_name << " some money" << endl;
75         ++first;
76     }
77 }
78 
main()79 int main()
80 {
81     {
82         // Create the graph, and specify that we will use std::string to
83         // store the first name's.
84         typedef adjacency_list< vecS, vecS, directedS,
85             property< vertex_first_name_t, std::string > >
86             MyGraphType;
87 
88         typedef pair< int, int > Pair;
89         Pair edge_array[11] = { Pair(0, 1), Pair(0, 2), Pair(0, 3), Pair(0, 4),
90             Pair(2, 0), Pair(3, 0), Pair(2, 4), Pair(3, 1), Pair(3, 4),
91             Pair(4, 0), Pair(4, 1) };
92 
93         MyGraphType G(5);
94         for (int i = 0; i < 11; ++i)
95             add_edge(edge_array[i].first, edge_array[i].second, G);
96 
97         property_map< MyGraphType, vertex_first_name_t >::type name
98             = get(vertex_first_name, G);
99 
100         boost::put(name, 0, "Jeremy");
101         boost::put(name, 1, "Rich");
102         boost::put(name, 2, "Andrew");
103         boost::put(name, 3, "Jeff");
104         name[4] = "Kinis"; // you can use operator[] too
105 
106         who_owes_who(edges(G).first, edges(G).second, G);
107     }
108 
109     cout << endl;
110 
111     return 0;
112 }
113