Difference between revisions of "Boost/BGL/BundledProperties"

From ProgrammingExamples
< Boost‎ | BGL
Jump to: navigation, search
(with bundled properties, using operator[] is more elegant (?))
(Majorly simplified)
Line 4: Line 4:
 
#include <boost/graph/adjacency_list.hpp>
 
#include <boost/graph/adjacency_list.hpp>
  
// Create a struct to hold several properties
+
// Create a struct to hold properties for each vertex
struct MyProperty
+
struct VertexProperties
 
{
 
{
   int MyIntProperty;
+
   int VertexIntProperty;
   std::string MyStringProperty;
+
   std::string VertexStringProperty;
 
};
 
};
  
// Define the type of the graph - this specifies a bundled property for vertices
+
// Create a struct to hold properties for each edge
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, MyProperty> Graph;
+
struct EdgeProperties
 +
{
 +
  int EdgeIntProperty;
 +
  std::string EdgeStringProperty;
 +
};
  
// Define the type of the graph - this specifies a bundled property for edges
+
// Define the type of the graph
//typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, boost::no_property, MyProperty> Graph;
+
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties> Graph;
  
 
int main(int,char*[])
 
int main(int,char*[])
 
{
 
{
 
   // Create a graph object
 
   // Create a graph object
   Graph g(2);
+
   Graph g;
  
   boost::property_map<Graph, int MyProperty::*>::type MyIntPropertyMap = boost::get(&MyProperty::MyIntProperty, g);
+
   // Add two vertices
   boost::put(MyIntPropertyMap, 0, 5);
+
  Graph::vertex_descriptor v0 = boost::add_vertex(g);
  boost::put(MyIntPropertyMap, 1, 10);
+
   Graph::vertex_descriptor v1 = boost::add_vertex(g);
  
   boost::property_map<Graph, std::string MyProperty::*>::type MyStringPropertyMap = boost::get(&MyProperty::MyStringProperty, g);
+
   // Add an edge
  boost::put(MyStringPropertyMap, 0, "TestName0");
+
  std::pair<Graph::edge_descriptor, bool> e01 = boost::add_edge(v0, v1, g);
  boost::put(MyStringPropertyMap, 1, "TestName1");
+
 
+
  // NOTE: the above code is equivalent to the slightly more elegant:
+
  // g[0].MyIntProperty = 5
+
  // g[1].MyIntProperty = 10
+
  // g[0].MyStringProperty = "TestName0"
+
  // g[1].MyStringProperty = "TestName1"
+
  
   typedef boost::property_map<Graph, boost::vertex_index_t>::type IndexMap;
+
   // Set and output the properties of a single vertex
   IndexMap index = get(boost::vertex_index, g);
+
  g[v0].VertexIntProperty = 5;
 +
  g[v0].VertexStringProperty = "mystring";
 +
  std::cout << g[v0].VertexIntProperty <<  " : " << g[v0].VertexStringProperty <<  std::endl;
 +
 
 +
  // Set and output the properties of a single edge
 +
  g[e01.first].EdgeIntProperty = 5;
 +
  g[e01.first].EdgeStringProperty = "mystring";
 +
  std::cout << g[e01.first].EdgeIntProperty <<  " : " << g[e01.first].EdgeStringProperty <<  std::endl;
 +
 
 +
  // Set and output the properties of each vertex
 +
  std::pair<Graph::vertex_iterator, Graph::vertex_iterator> vertexIteratorRange = boost::vertices(g);
 +
   for(Graph::vertex_iterator vertexIterator = vertexIteratorRange.first; vertexIterator != vertexIteratorRange.second; ++vertexIterator)
 +
    {
 +
    g[*vertexIterator].VertexIntProperty = 5;
 +
    g[*vertexIterator].VertexStringProperty = "test";
 +
    std::cout << g[*vertexIterator].VertexIntProperty <<  " : " << g[*vertexIterator].VertexStringProperty <<  std::endl;
 +
    }
  
   typedef boost::graph_traits<Graph>::vertex_iterator vertex_iter;
+
   // Set and output the properties of each edge
   std::pair<vertex_iter, vertex_iter> vertexPair;
+
   std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g);
   for (vertexPair = vertices(g); vertexPair.first != vertexPair.second; ++vertexPair.first)
+
   for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator)
 
     {
 
     {
     std::cout << index[*vertexPair.first] <<  " : " << MyIntPropertyMap[*vertexPair.first] << " : " << MyStringPropertyMap[*vertexPair.first] <<  std::endl;
+
    g[*edgeIterator].EdgeIntProperty = 5;
 +
    g[*edgeIterator].EdgeStringProperty = "test";
 +
     std::cout << g[*edgeIterator].EdgeIntProperty <<  " : " << g[*edgeIterator].EdgeStringProperty <<  std::endl;
 
     }
 
     }
  

Revision as of 19:22, 22 June 2011

BundledProperties.cpp

#include <iostream>
#include <boost/graph/adjacency_list.hpp>
 
// Create a struct to hold properties for each vertex
struct VertexProperties
{
  int VertexIntProperty;
  std::string VertexStringProperty;
};
 
// Create a struct to hold properties for each edge
struct EdgeProperties
{
  int EdgeIntProperty;
  std::string EdgeStringProperty;
};
 
// Define the type of the graph
typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS, VertexProperties, EdgeProperties> Graph;
 
int main(int,char*[])
{
  // Create a graph object
  Graph g;
 
  // Add two vertices
  Graph::vertex_descriptor v0 = boost::add_vertex(g);
  Graph::vertex_descriptor v1 = boost::add_vertex(g);
 
  // Add an edge
  std::pair<Graph::edge_descriptor, bool> e01 = boost::add_edge(v0, v1, g);
 
  // Set and output the properties of a single vertex
  g[v0].VertexIntProperty = 5;
  g[v0].VertexStringProperty = "mystring";
  std::cout << g[v0].VertexIntProperty <<  " : " << g[v0].VertexStringProperty <<  std::endl;
 
  // Set and output the properties of a single edge
  g[e01.first].EdgeIntProperty = 5;
  g[e01.first].EdgeStringProperty = "mystring";
  std::cout << g[e01.first].EdgeIntProperty <<  " : " << g[e01.first].EdgeStringProperty <<  std::endl;
 
  // Set and output the properties of each vertex
  std::pair<Graph::vertex_iterator, Graph::vertex_iterator> vertexIteratorRange = boost::vertices(g);
  for(Graph::vertex_iterator vertexIterator = vertexIteratorRange.first; vertexIterator != vertexIteratorRange.second; ++vertexIterator)
    {
    g[*vertexIterator].VertexIntProperty = 5;
    g[*vertexIterator].VertexStringProperty = "test";
    std::cout << g[*vertexIterator].VertexIntProperty <<  " : " << g[*vertexIterator].VertexStringProperty <<  std::endl;
    }
 
  // Set and output the properties of each edge
  std::pair<Graph::edge_iterator, Graph::edge_iterator> edgeIteratorRange = boost::edges(g);
  for(Graph::edge_iterator edgeIterator = edgeIteratorRange.first; edgeIterator != edgeIteratorRange.second; ++edgeIterator)
    {
    g[*edgeIterator].EdgeIntProperty = 5;
    g[*edgeIterator].EdgeStringProperty = "test";
    std::cout << g[*edgeIterator].EdgeIntProperty <<  " : " << g[*edgeIterator].EdgeStringProperty <<  std::endl;
    }
 
  return 0;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.6)
 
Project(BundledProperties)
 
set(Boost_USE_MULTITHREADED ON)
FIND_PACKAGE(Boost 1.38 COMPONENTS required)
 
INCLUDE_DIRECTORIES(${INCLUDE_DIRECTORIES} ${Boost_INCLUDE_DIRS})
LINK_DIRECTORIES(${LINK_DIRECTORIES} ${Boost_LIBRARY_DIRS})
 
ADD_EXECUTABLE(BundledProperties BundledProperties.cpp)
target_link_libraries(BundledProperties)