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