File: polynomial_1_parser.h

package info (click to toggle)
cgal 4.0-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 65,068 kB
  • sloc: cpp: 500,870; ansic: 102,544; sh: 321; python: 92; makefile: 75; xml: 2
file content (361 lines) | stat: -rw-r--r-- 9,850 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (c) 2007-2010 Inria Lorraine (France). All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/next/Algebraic_kernel_d/include/CGAL/RS/polynomial_1_parser.h $
// $Id: polynomial_1_parser.h 67093 2012-01-13 11:22:39Z lrineau $
//
// Author: Luis PeƱaranda <luis.penaranda@gmx.com>
//
// (the first version of this file was written by Elias Tsigaridas)

#ifndef CGAL_RS_PARSER_1_H
#define CGAL_RS_PARSER_1_H

#include <iostream>
#include <string>
#include <algorithm>

// if boost version is 1.38 or newer, we use the new version of spirit
#include <boost/version.hpp>
#if BOOST_VERSION >= 103800
#include <boost/spirit/include/classic_core.hpp>
#define CGAL_BOOST_SPIRIT boost::spirit::classic
#else
#include <boost/spirit/core.hpp>
#define CGAL_BOOST_SPIRIT boost::spirit
#endif

namespace CGAL{

// The semantics.
// In this class we store the current pair and the whole result;
struct Semantics
{
    typedef std::pair< int, std::string >    pair_t;

    std::string                     variable;
    pair_t                          current;
    std::vector< pair_t >           result;
    std::string                     sign;

    Semantics( const std::string&  var) : variable( var) { }

    void set_variable( const std::string& var)
        {
            variable = var;
        }

    std::string get_variable() const
        {
            return variable;
        }

    bool is_first() const
        {
            return current == pair_t(-1, "F");
        }

    void add()
        {
            result.push_back( current);
            current = pair_t(-1, "F");
        }

    void init()
        {
            result.clear();
            current = pair_t(-1, "F");
            sign = "";
        }

};

//-------------    Semantic actions -----------------------------------

// Add a new monomial at the result
struct AddMonomial
{
    void operator()( char const* first, char const* last) const
        {
            if (!sem.is_first()) {
                sem.add();
            }
        }

    AddMonomial( Semantics& sem_) : sem( sem_) { }
    Semantics& sem;
};

// Set the coefficient of the current monomial
struct SetCoeff
{
    void operator() ( char const* first, char const* last) const
        {
            sem.current.second = std::string( first, last);
            if (sem.current.first == -1) {
                sem.current.first = 0;
            }
        }

    SetCoeff( Semantics& sem_) : sem( sem_) { }
    Semantics& sem;
};

// Set the exponent of the current monomial
struct SetExp
{
    void
    operator()( const char* first, const char* last) const
        {
            sem.current.first = 1;
            if (sem.current.second == "F") sem.current.second = "1";
        }

    void operator()( unsigned num) const
        {
            sem.current.first = num;
            if (sem.current.second == "F") sem.current.second = "1";
        }

    SetExp( Semantics& sem_) : sem( sem_) { }
    Semantics& sem;
};

// Set the sign of the current monomial
struct SetSign
{
    void
    operator()( char const* first, char const* last) const
        {
            std::string str( first, last);
            sem.sign = str;
        }

    SetSign( Semantics& sem_) : sem( sem_) { }
    Semantics& sem;
};

// Adjust the sign of the coefficient of the current monomial
struct AdjustCoeff
{
    void
    operator()( char const* first, char const* last) const
        {
            if (sem.sign == "-") {
                if (sem.current.second[0] == '-') {
                    sem.current.second[0] = '+';
                } else if (sem.current.second[0] == '+') {
                    sem.current.second[0] = '-';
                } else {
                    sem.current.second = '-' + sem.current.second;
                }
            }
        }

    AdjustCoeff( Semantics& sem_) : sem( sem_) { }
    Semantics& sem;
};

//----------------  Grammar -------------------------------------

// The grammar of the polynomial parser.
// The coefficient of the polynomial can also be rationals.
struct polynomial_p : public CGAL_BOOST_SPIRIT::grammar<polynomial_p>
{

    template < typename ScannerT >
    struct definition
    {
        definition(polynomial_p const& self)
            {
                CGAL_BOOST_SPIRIT::chlit<> lpar('(');
                CGAL_BOOST_SPIRIT::chlit<> rpar(')');

                CGAL_BOOST_SPIRIT::chlit<> plus('+');
                CGAL_BOOST_SPIRIT::chlit<> minus('-');
                CGAL_BOOST_SPIRIT::chlit<> mul('*');

                CGAL_BOOST_SPIRIT::strlit<>
                        varX(self.sem.get_variable().c_str());

                poly = (
                        (monomial | smonomial) [ AddMonomial( self.sem) ] >>
                        *( smonomial [ AddMonomial( self.sem) ] )
                       );

                monomial
                    = ( unumber [ SetCoeff( self.sem) ] >> !( mul >>  X ) )
                    | ( lpar >> number[ SetCoeff( self.sem) ] >> rpar >> !( mul >> X) )
                    | ( X >> mul >> number [ SetCoeff( self.sem) ] )
                    | ( X >> mul >> lpar >> number [ SetCoeff( self.sem) ] >> rpar )
                    | X;

                smonomial = ( ( plus | minus ) [ SetSign( self.sem) ] >>
                             monomial) [ AdjustCoeff( self.sem) ];

                X = ( (varX)[ SetExp( self.sem) ]  >>
                      !(
                        CGAL_BOOST_SPIRIT::ch_p('^') >>
                        ( (CGAL_BOOST_SPIRIT::uint_p) [ SetExp( self.sem) ]
                          | (lpar >>
                             CGAL_BOOST_SPIRIT::uint_p [ SetExp( self.sem) ] >>
                             rpar))
                      )
                    );

                unumber = +(CGAL_BOOST_SPIRIT::digit_p) >>
                          !(CGAL_BOOST_SPIRIT::ch_p("/") >>
                            +(CGAL_BOOST_SPIRIT::digit_p));
                number = ( (plus | minus) >> unumber );
            }

        CGAL_BOOST_SPIRIT::rule<ScannerT>
                poly, monomial, smonomial, X, unumber, number;

        CGAL_BOOST_SPIRIT::rule<ScannerT> const& start() const
            {
                return poly;
            }
    };

    polynomial_p( Semantics& sem_) : sem(sem_) { }

    Semantics& sem;
};

// The polynomial parser.
// The constructor takes the name of the variable.
struct Polynomial_parser_1
{
protected:
    // internal::Semantics                 sem;
    Semantics                           sem;
    // internal::polynomial_p              poly_p;
    polynomial_p                        poly_p;
    CGAL_BOOST_SPIRIT::parse_info<>     info;


// The default conversion is to int
    struct Converter
    {
        int operator()( const std::string str) const
            {
                return atoi( str.c_str());
            }
    };


public:
    Polynomial_parser_1( const std::string& var = "x")
        : sem( var), poly_p( sem)
        {
        }

    void parse( const std::string& in_str)
        {
            // Eliminate spaces
            std::string str(in_str);
            while (str.find(" ") < str.size() )
                {
                    size_t pos = str.find(" ");
                    str.erase( pos, 1);
                }

            sem.init();
            info = CGAL_BOOST_SPIRIT::parse(str.c_str(),
                                            poly_p,
                                            CGAL_BOOST_SPIRIT::space_p);
            std::sort( sem.result.begin(), sem.result.end());
        }

    bool is_correct() const
        {
            return info.full;
        }

    std::string error() const
        {
            if (!is_correct()) {
                return info.stop;
            }
            return "";
        }


    std::string get_variable() const
        {
            return sem.get_variable();
        }

    void set_variable( const std::string& var)
        {
            sem.set_variable( var);
        }



    template < typename OutputIterator,
               typename CONVERTER>
    OutputIterator
    result( OutputIterator oi, CONVERTER conv) const
        {
            //for (int i = 0; i < sem.result.size(); ++i) {
            //  std::cout << "(" << sem.result[i].first << ", " <<
            //    sem.result[i].second << ") ";
            //}
            //std::cout << std::endl;

            for (unsigned i = 0, j = 0; j < sem.result.size(); ++i) {
                if (sem.result[j].first == static_cast<int>( i)) {
                    *oi++ = conv( sem.result[j].second);
                    ++j;
                } else {
                    *oi++ = conv( "0");
                }

            }
            return oi;
        }


    template < typename OutputIterator >
    OutputIterator
    result( OutputIterator oi) const
        {
            return this->result( oi, Converter());
        }

};

template < typename T >
struct Convert_to
{
    T
    operator()( const std::string str) const {
        return static_cast<T>( atoi( str.c_str()));
    }
};

struct Convert_to_Gmpz
{
    Gmpz
    operator()( const std::string str) const {
        return Gmpz(str.c_str());
    }
};

} // namespace CGAL

#undef CGAL_BOOST_SPIRIT

#endif  // CGAL_RS_PARSER_1_H