~ubuntu-branches/ubuntu/trusty/kalgebra/trusty-updates

« back to all changes in this revision

Viewing changes to analitza/expressiontype.h

  • Committer: Bazaar Package Importer
  • Author(s): Philip Muškovac
  • Date: 2011-07-08 20:10:34 UTC
  • Revision ID: james.westby@ubuntu.com-20110708201034-0cqpagx7uz4pu82n
Tags: upstream-4.6.90+repack1
Import upstream version 4.6.90+repack1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************************
 
2
 *  Copyright (C) 2010 by Aleix Pol <aleixpol@kde.org>                               *
 
3
 *                                                                                   *
 
4
 *  This program is free software; you can redistribute it and/or                    *
 
5
 *  modify it under the terms of the GNU General Public License                      *
 
6
 *  as published by the Free Software Foundation; either version 2                   *
 
7
 *  of the License, or (at your option) any later version.                           *
 
8
 *                                                                                   *
 
9
 *  This program is distributed in the hope that it will be useful,                  *
 
10
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 
11
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 
12
 *  GNU General Public License for more details.                                     *
 
13
 *                                                                                   *
 
14
 *  You should have received a copy of the GNU General Public License                *
 
15
 *  along with this program; if not, write to the Free Software                      *
 
16
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 
17
 *************************************************************************************/
 
18
 
 
19
#ifndef EXPRESSIONTYPE_H
 
20
#define EXPRESSIONTYPE_H
 
21
 
 
22
#include "analitzaexport.h"
 
23
#include <QString>
 
24
#include <QList>
 
25
#include <QMap>
 
26
 
 
27
namespace Analitza
 
28
{
 
29
 
 
30
class ANALITZA_EXPORT ExpressionType
 
31
{
 
32
        public:
 
33
                ///Just use undefined type when returning from a recursion
 
34
                enum Type { Error=0, Value, Vector, List, Lambda, Any, Many, Object, Char, Bool };
 
35
                QString toString() const;
 
36
                
 
37
                ExpressionType(Type t=Error, int any=-1);
 
38
                ExpressionType(Type t, const ExpressionType& contained, int s=0);
 
39
                ExpressionType(Type t, const QList<ExpressionType>& alternatives);
 
40
                
 
41
                /** Constructs a type that identifies a custom Object */
 
42
                ExpressionType(const QString& objectName);
 
43
                ExpressionType(const ExpressionType& t);
 
44
                
 
45
                ~ExpressionType() {/* delete contained; */}
 
46
                
 
47
                bool operator==(const ExpressionType& t) const;
 
48
                bool operator!=(const ExpressionType& t) const { return !operator==(t); }
 
49
                ExpressionType operator=(const ExpressionType& et);
 
50
                
 
51
                /** Depth search to check if it's defined */
 
52
                bool isError() const;
 
53
                
 
54
                Type type() const { return m_type; }
 
55
                ExpressionType contained() const { Q_ASSERT(m_type==Vector || m_type==List); return m_contained.first(); }
 
56
                QList<ExpressionType> alternatives() const { Q_ASSERT(m_type==Many); return m_contained; }
 
57
                
 
58
                /** In case it's a Many type, it adds @p t as an alternative. If @p t is a Many type too, they will be merged */
 
59
                void addAlternative(const ExpressionType& t);
 
60
                int size() const { return m_size; }
 
61
                int anyValue() const { return m_any; }
 
62
                
 
63
                ExpressionType& addParameter(const ExpressionType& t);
 
64
                QList<ExpressionType> parameters() const { Q_ASSERT(m_type==Lambda); return m_contained; }
 
65
                ExpressionType returnValue() const;
 
66
                
 
67
                void addAssumption(const QString& bvar, const ExpressionType& t);
 
68
                QMap<QString, ExpressionType> assumptions() const;
 
69
                QMap<QString, ExpressionType>& assumptions();
 
70
                ExpressionType assumptionFor(const QString& bvar) const { return m_assumptions.value(bvar); }
 
71
                void addAssumptions(const QMap< QString, ExpressionType >& a);
 
72
                void clearAssumptions();
 
73
                
 
74
                /** Returns a new type with the stars solved according t @p info */
 
75
                ExpressionType starsToType(const QMap<int, ExpressionType>& info) const;
 
76
                
 
77
                bool canReduceTo(const ExpressionType& type) const;
 
78
                int increaseStars(int stars);
 
79
                
 
80
                ExpressionType& simplifyStars();
 
81
                
 
82
                /** when it's a many type, reduce to the one(s) that can be reduced to */
 
83
                void reduce(const ExpressionType& type);
 
84
                
 
85
                static ExpressionType minimumType(const ExpressionType& t1, const ExpressionType& t2);
 
86
                static bool assumptionsMerge(QMap<QString, ExpressionType>& data, const QMap<QString, ExpressionType>& newmap);
 
87
                static void assumptionsUnion(QMap< QString, Analitza::ExpressionType >& data, const QMap< QString, Analitza::ExpressionType >& newmap);
 
88
                static QMap<int, ExpressionType> computeStars(const QMap<int, ExpressionType>& initial, const ExpressionType& candidate, const ExpressionType& type);
 
89
                static bool matchAssumptions(QMap<int, ExpressionType>* stars, const QMap<QString, ExpressionType>& assum1, const QMap<QString, ExpressionType>& assum2);
 
90
                static QList<ExpressionType> lambdaFromArgs(const QList<ExpressionType>& args);
 
91
                static QList<ExpressionType> manyFromArgs(const QList<ExpressionType>& args);
 
92
        private:
 
93
                static void starsSimplification(ExpressionType& t, QMap<int, int>& reductions, int& next);
 
94
                
 
95
                Type m_type;
 
96
                ///In case of list and vector the inside type
 
97
                QList<ExpressionType> m_contained;
 
98
                QMap<QString, ExpressionType> m_assumptions;
 
99
                union { int m_size; int m_any; };
 
100
        QString m_objectName;
 
101
};
 
102
 
 
103
}
 
104
#endif // EXPRESSIONTYPE_H