File: apply.cpp

package info (click to toggle)
analitza 4:17.08.3-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,824 kB
  • sloc: cpp: 26,899; perl: 63; sh: 16; makefile: 9
file content (184 lines) | stat: -rw-r--r-- 6,055 bytes parent folder | download | duplicates (2)
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
/*************************************************************************************
 *  Copyright (C) 2007-2010 by Aleix Pol <aleixpol@kde.org>                          *
 *                                                                                   *
 *  This program is free software; you can redistribute it and/or                    *
 *  modify it under the terms of the GNU General Public License                      *
 *  as published by the Free Software Foundation; either version 2                   *
 *  of the License, or (at your option) any later version.                           *
 *                                                                                   *
 *  This program is distributed in the hope that it will be useful,                  *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
 *  GNU General Public License for more details.                                     *
 *                                                                                   *
 *  You should have received a copy of the GNU General Public License                *
 *  along with this program; if not, write to the Free Software                      *
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
 *************************************************************************************/

#include "apply.h"
#include "abstractexpressionvisitor.h"
#include <QStringList>
#include "variable.h"
#include "analitzautils.h"
#include "container.h"

using namespace Analitza;

Apply::Apply()
    : Object(apply), m_ulimit(0), m_dlimit(0), m_domain(0), m_op(Operator::function)
{}

Apply::~Apply()
{
    delete m_dlimit;
    delete m_ulimit;
    delete m_domain;
    qDeleteAll(m_bvars);
    qDeleteAll(m_params);
}

bool Apply::addBranch(Object* o)
{
    if(o->type()==Object::oper) {
        m_op = *static_cast<Operator*>(o);
        delete o;
    } else if(o->isContainer()) {
        Container* c=static_cast<Container*>(o);
        switch(c->containerType()) {
            case Container::uplimit:
                Q_ASSERT(!m_ulimit);
                m_ulimit = c->m_params[0];
                c->m_params[0]=0;
                delete c;
                break;
            case Container::downlimit:
                Q_ASSERT(!m_dlimit);
                m_dlimit = c->m_params[0];
                c->m_params[0]=0;
                delete c;
                break;
            case Container::domainofapplication:
                Q_ASSERT(!m_domain);
                m_domain = c->m_params[0];
                c->m_params[0]=0;
                delete c;
                break;
            case Container::bvar:
                Q_ASSERT(c->m_params[0]->type()==variable);
                m_bvars += static_cast<Ci*>(c->m_params[0]);
                c->m_params[0]=0;
                delete c;
                break;
            default:
                return false;
        }
    } else
        return false;
    
    return true;
}

void Apply::appendBranch(Object *o)
{
    if(!addBranch(o))
        m_params.append(o);
}

void Apply::prependBranch(Object* o)
{
    if(!addBranch(o))
        m_params.prepend(o);
}

QVariant Apply::accept(AbstractExpressionVisitor* exp) const
{
    return exp->visit(this);
}

QStringList Apply::bvarStrings() const
{
    QStringList ret;
    foreach(const Ci* bvar, m_bvars) {
        ret.append(bvar->name());
    }
    return ret;
}

bool Apply::operator==(const Apply& a) const
{
    bool eq=a.m_params.count()==m_params.count() && a.firstOperator()==firstOperator();
    eq &= bool(a.ulimit())==bool(m_ulimit);
    eq &= bool(a.dlimit())==bool(m_dlimit);
    eq &= bool(a.domain())==bool(m_domain);
    
    if(m_ulimit) eq &= AnalitzaUtils::equalTree(m_ulimit, a.m_ulimit);
    if(m_dlimit) eq &= AnalitzaUtils::equalTree(m_dlimit, a.m_dlimit);
    if(m_domain) eq &= AnalitzaUtils::equalTree(m_domain, a.m_domain);
    
    for(int i=0; eq && i<m_params.count(); ++i) {
        Object *o=m_params[i], *o1=a.m_params[i];
        eq = eq && AnalitzaUtils::equalTree(o, o1);
    }
    
    return eq;
}

Apply* Apply::copy() const
{
    Apply* ret=new Apply;
    ret->m_dlimit = m_dlimit? m_dlimit->copy() : 0;
    ret->m_ulimit = m_ulimit? m_ulimit->copy() : 0;
    ret->m_domain = m_domain? m_domain->copy() : 0;
    ret->m_op     = m_op;
    
    foreach(const Ci* var, m_bvars)
        ret->m_bvars += (Ci*) var->copy();
    
    foreach(const Object* o, m_params)
        ret->m_params += o->copy();
    
    return ret;
}

bool Apply::matches(const Object* exp, QMap<QString, const Object*>* found) const
{
    if(Object::apply!=exp->type())
        return false;
    const Apply* c=(const Apply*) exp;
    if(m_params.count()!=c->m_params.count())
        return false;
    
    QVector<Ci*> vars=m_bvars, cvars = c->bvarCi();
    bool matching=vars.size()==cvars.size();
    matching &= m_op.matches(&c->m_op, found);
    
    for(QVector<Ci*>::const_iterator it=vars.constBegin(), cit=cvars.constBegin(), itEnd=vars.constEnd(); matching && it!=itEnd; ++it) {
        matching &= (*it)->matches(*cit, found);
    }
    
//     bool matching=true;
    Apply::const_iterator it, it2, itEnd=m_params.constEnd();
    for(it=m_params.constBegin(), it2=c->m_params.constBegin(); matching && it!=itEnd; ++it, ++it2)
        matching &= (*it)->matches(*it2, found);
    
    matching &= bool(m_ulimit)==bool(c->m_ulimit) && (!m_ulimit || m_ulimit->matches(c->m_ulimit, found));
    matching &= bool(m_dlimit)==bool(c->m_dlimit) && (!m_dlimit || m_dlimit->matches(c->m_dlimit, found));
    matching &= bool(m_domain)==bool(c->m_domain) && (!m_domain || m_domain->matches(c->m_domain, found));
    return matching;
}

bool Analitza::Apply::hasBoundings() const
{
    return m_dlimit || m_ulimit || m_domain;
}

void Apply::addBVar(Ci* bvar)
{
    m_bvars += bvar;
}

Object* Apply::at(int p) const
{
    return m_params.at(p);
}