property_iterator.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // (C) Copyright Francois Faure, iMAGIS-GRAVIR / UJF, 2001.
  2. // Distributed under the Boost Software License, Version 1.0.
  3. // (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Revision History:
  6. // 03 May 2001 Jeremy Siek
  7. // Moved property iterator code to headers.
  8. // 02 May 2001 Francois Faure
  9. // Initial version.
  10. #include <boost/graph/adjacency_list_io.hpp>
  11. #include <boost/graph/property_iter_range.hpp>
  12. #include <fstream>
  13. #include <algorithm>
  14. using namespace boost;
  15. //======== vertex properties
  16. struct toto_t {
  17. enum { num = 23063};
  18. typedef vertex_property_tag kind;
  19. };
  20. typedef property< toto_t, double > Toto;
  21. struct radius_t {
  22. enum { num = 23062};
  23. typedef vertex_property_tag kind;
  24. };
  25. typedef property< radius_t, double, Toto > Radius;
  26. struct mass_t {
  27. enum { num = 23061};
  28. typedef vertex_property_tag kind;
  29. };
  30. typedef property< mass_t, int, Radius > Mass;
  31. //====== edge properties
  32. struct stiff_t {
  33. enum { num = 23064};
  34. typedef edge_property_tag kind;
  35. };
  36. typedef property<stiff_t, double> Stiff;
  37. //===== graph type
  38. typedef Mass VertexProperty;
  39. typedef Stiff EdgeProperty;
  40. typedef adjacency_list<vecS, setS, bidirectionalS,
  41. VertexProperty, EdgeProperty> Graph;
  42. //===== utilities
  43. struct Print
  44. {
  45. template<class T>
  46. Print& operator() (const T& t) {
  47. std::cout << t << " ";
  48. return (*this);
  49. }
  50. };
  51. template<class T>
  52. struct Set
  53. {
  54. T value;
  55. Set( const T& t ):value(t){}
  56. Set& operator() (T& t) {
  57. t=value;
  58. return (*this);
  59. }
  60. };
  61. //===== program
  62. int main(int argc, char* argv[])
  63. {
  64. if (argc < 2) {
  65. std::cerr<<"args: file"<<std::endl;
  66. return EXIT_FAILURE;
  67. }
  68. std::ifstream readFile(argv[1]);
  69. Graph graph;
  70. readFile >> read( graph );
  71. std::cout << write( graph );
  72. std::cout << "radii:" << std::endl;
  73. graph_property_iter_range<Graph,radius_t>::type
  74. seqRadius = get_property_iter_range(graph,radius_t());
  75. std::for_each( seqRadius.first, seqRadius.second, Print() );
  76. std::cout << std::endl;
  77. std::cout << "stiff:" << std::endl;
  78. graph_property_iter_range<Graph,stiff_t>::type
  79. seqStiff = get_property_iter_range(graph, stiff_t());
  80. std::for_each( seqStiff.first, seqStiff.second, Print() );
  81. std::cout << std::endl;
  82. seqStiff = get_property_iter_range(graph, stiff_t());
  83. std::for_each( seqStiff.first, seqStiff.second, Set<double>(2.4) );
  84. std::cout << "new stiff:" << std::endl;
  85. seqStiff = get_property_iter_range(graph,stiff_t());
  86. std::for_each( seqStiff.first, seqStiff.second, Print() );
  87. std::cout << std::endl;
  88. return 0;
  89. }