File: container.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 (192 lines) | stat: -rw-r--r-- 5,945 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
185
186
187
188
189
190
191
192
/*************************************************************************************
 *  Copyright (C) 2007 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 "container.h"

#include "expression.h"
#include "abstractexpressionvisitor.h"
#include "vector.h"
#include "value.h"
#include "list.h"

#include <QDebug>

#include "variable.h"
#include "analitzautils.h"

using namespace Analitza;

char Container::m_typeStr[][20] = {
        "none",
        "math",
        "declare",
        "lambda",
        "bvar",
        "uplimit",
        "downlimit",
        "piece",
        "piecewise",
        "otherwise",
        "domainofapplication"
};

QMap<QString, Container::ContainerType> createNameToType()
{
    QMap<QString, Container::ContainerType> ret;
    ret[QStringLiteral("declare")]=Container::declare;
    ret[QStringLiteral("math")]=Container::math;
    ret[QStringLiteral("lambda")]=Container::lambda;
    ret[QStringLiteral("bvar")]=Container::bvar;
    ret[QStringLiteral("uplimit")]=Container::uplimit;
    ret[QStringLiteral("downlimit")]=Container::downlimit;
    ret[QStringLiteral("piecewise")]=Container::piecewise;
    ret[QStringLiteral("piece")]=Container::piece;
    ret[QStringLiteral("otherwise")]=Container::otherwise;
    ret[QStringLiteral("domainofapplication")]=Container::domainofapplication;
    
    return ret;
}

QMap<QString, Container::ContainerType> Container::m_nameToType=createNameToType();

Container::Container(const Container& c) : Object(Object::container), m_cont_type(c.m_cont_type)
{
    Q_ASSERT(c.type()==Object::container);
    
    foreach(const Object* o, c.m_params)
        appendBranch(o->copy());
}

Container* Container::copy() const
{
    return new Container(*this);
}

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

bool Container::isZero() const
{
    bool a=true;
    foreach(const Object* o, m_params) {
        a = a && o->isZero();
    }
    return a;
}

Container::ContainerType Container::toContainerType(const QString& tag)
{
    return m_nameToType[tag];
}

QList<Ci*> Container::bvarCi() const
{
    QList<Ci*> ret;
    QList<Object*>::const_iterator it, itEnd=m_params.constEnd();
    
    for(it=m_params.constBegin(); it!=itEnd; ++it) {
        if((*it)->isContainer()) {
            Container* c = (Container*) (*it);
            if(c->containerType() == Container::bvar) {
                Q_ASSERT(!c->isEmpty() && c->m_params[0]->type()==Object::variable);
                ret.append((Ci*) c->m_params[0]);
            }
        }
    }
    
    return ret;
}

int Container::bvarCount() const
{
    int r=0;
    QList<Object*>::const_iterator it, itEnd=m_params.constEnd();
    for(it=m_params.constBegin(); it!=itEnd; ++it) {
        Object* o = *it;
        if(o->isContainer() && static_cast<Container*>(o)->containerType() == Container::bvar)
            r++;
    }
    
    return r;
}

QStringList Container::bvarStrings() const
{
    QStringList bvars;
    QList< Ci* > vars=bvarCi();
    foreach(Ci* var, vars) {
        bvars.append(var->name());
    }
    
    return bvars;
}

bool Container::operator==(const Container& c) const
{
    bool eq=c.m_params.count()==m_params.count();
    
    for(int i=0; eq && i<m_params.count(); ++i) {
        Object *o=m_params[i], *o1=c.m_params[i];
        eq = eq && AnalitzaUtils::equalTree(o, o1);
    }
    return eq;
}

bool Container::isNumber() const
{
    return m_cont_type==math || m_cont_type==lambda || m_cont_type==declare ||
        m_cont_type==piecewise || m_cont_type==piece || m_cont_type==otherwise;
}

QString Container::tagName() const
{
    return QString(m_typeStr[m_cont_type]);
}

bool Container::matches(const Object* exp, QMap<QString, const Object*>* found) const
{
    if(Object::container!=exp->type())
        return false;
    const Container* c=(const Container*) exp;
    if(m_params.count()!=c->m_params.count())
        return false;
    
    bool matching=true;
    Container::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);
    }
    return matching;
}

const Container* Container::extractType(Container::ContainerType t) const
{
    for(Container::const_iterator it=m_params.constBegin(); it!=m_params.constEnd(); ++it) {
        const Container *c = (const Container*) (*it);
        if(c->isContainer() && c->containerType()==t)
            return c;
    }
    return 0;
}

void Container::appendBranch(Object* o)
{
    m_params.append(o);
}