num_matrix.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*=============================================================================
  2. Copyright (c) 2002-2010 Hartmut Kaiser
  3. Copyright (c) 2002-2010 Joel de Guzman
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. =============================================================================*/
  7. ///////////////////////////////////////////////////////////////////////////////
  8. //
  9. // This sample demonstrates a generator formatting and printing a matrix
  10. // of integers taken from a simple vector of vectors. The size and the
  11. // contents of the printed matrix is generated randomly.
  12. //
  13. ///////////////////////////////////////////////////////////////////////////////
  14. #include <boost/config/warning_disable.hpp>
  15. #include <boost/spirit/include/karma.hpp>
  16. #include <iostream>
  17. #include <string>
  18. #include <vector>
  19. #include <cstdlib>
  20. #include <ctime>
  21. namespace karma = boost::spirit::karma;
  22. namespace client
  23. {
  24. ///////////////////////////////////////////////////////////////////////////
  25. // Our matrix generator
  26. ///////////////////////////////////////////////////////////////////////////
  27. //[tutorial_karma_nummatrix_grammar
  28. template <typename OutputIterator>
  29. struct matrix_grammar
  30. : karma::grammar<OutputIterator, std::vector<std::vector<int> >()>
  31. {
  32. matrix_grammar()
  33. : matrix_grammar::base_type(matrix)
  34. {
  35. using karma::int_;
  36. using karma::right_align;
  37. using karma::eol;
  38. element = right_align(10)[int_];
  39. row = '|' << *element << '|';
  40. matrix = row % eol;
  41. }
  42. karma::rule<OutputIterator, std::vector<std::vector<int> >()> matrix;
  43. karma::rule<OutputIterator, std::vector<int>()> row;
  44. karma::rule<OutputIterator, int()> element;
  45. };
  46. //]
  47. //[tutorial_karma_nummatrix
  48. template <typename OutputIterator>
  49. bool generate_matrix(OutputIterator& sink
  50. , std::vector<std::vector<int> > const& v)
  51. {
  52. matrix_grammar<OutputIterator> matrix;
  53. return karma::generate(
  54. sink, // destination: output iterator
  55. matrix, // the generator
  56. v // the data to output
  57. );
  58. }
  59. //]
  60. }
  61. ////////////////////////////////////////////////////////////////////////////
  62. // Main program
  63. ////////////////////////////////////////////////////////////////////////////
  64. int
  65. main()
  66. {
  67. std::cout << "/////////////////////////////////////////////////////////\n\n";
  68. std::cout << "\tPrinting integers in a matrix using Spirit...\n\n";
  69. std::cout << "/////////////////////////////////////////////////////////\n\n";
  70. // here we put the data to generate
  71. std::vector<std::vector<int> > v;
  72. // now, generate the size and the contents for the matrix
  73. std::srand((unsigned int)std::time(NULL));
  74. std::size_t rows = std::rand() / (RAND_MAX / 10);
  75. std::size_t columns = std::rand() / (RAND_MAX / 10);
  76. v.resize(rows);
  77. for (std::size_t row = 0; row < rows; ++row)
  78. {
  79. v[row].resize(columns);
  80. std::generate(v[row].begin(), v[row].end(), std::rand);
  81. }
  82. // ok, we got the matrix, now print it out
  83. std::string generated;
  84. std::back_insert_iterator<std::string> sink(generated);
  85. if (!client::generate_matrix(sink, v))
  86. {
  87. std::cout << "-------------------------\n";
  88. std::cout << "Generating failed\n";
  89. std::cout << "-------------------------\n";
  90. }
  91. else
  92. {
  93. std::cout << "-------------------------\n";
  94. std::cout << "Generated:\n" << generated << "\n";
  95. std::cout << "-------------------------\n";
  96. }
  97. return 0;
  98. }