File indexing completed on 2024-04-28 03:40:47

0001 /*************************************************************************************
0002  *  Copyright (C) 2007 by Aleix Pol <aleixpol@kde.org>                               *
0003  *                                                                                   *
0004  *  This program is free software; you can redistribute it and/or                    *
0005  *  modify it under the terms of the GNU General Public License                      *
0006  *  as published by the Free Software Foundation; either version 2                   *
0007  *  of the License, or (at your option) any later version.                           *
0008  *                                                                                   *
0009  *  This program is distributed in the hope that it will be useful,                  *
0010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of                   *
0011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                    *
0012  *  GNU General Public License for more details.                                     *
0013  *                                                                                   *
0014  *  You should have received a copy of the GNU General Public License                *
0015  *  along with this program; if not, write to the Free Software                      *
0016  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA   *
0017  *************************************************************************************/
0018 
0019 #include "variables.h"
0020 #include "expression.h"
0021 #include "value.h"
0022 #include "container.h"
0023 
0024 using namespace Analitza;
0025 
0026 Variables::Variables() : QHash<QString, Object*>()
0027 {
0028     initializeConstants();
0029 }
0030 
0031 void Variables::initializeConstants()
0032 {
0033     insert(QStringLiteral("true"), new Cn(true));
0034     insert(QStringLiteral("false"), new Cn(false));
0035     insert(QStringLiteral("pi"), new Cn(Cn::pi()));
0036     insert(QStringLiteral("e"), new Cn(Cn::e()));
0037     insert(QStringLiteral("euler"), new Cn(Cn::euler()));
0038     insert(QStringLiteral("i"), new Cn(0, 1));
0039 }
0040 
0041 Variables::Variables(const Variables& v) : QHash<QString, Object*>(v)
0042 {
0043     QHash<QString, Object*>::iterator i;
0044     for (i = this->begin(); i != this->end(); ++i)
0045         *i = (*i)->copy();
0046 }
0047 
0048 Variables::~Variables()
0049 {
0050     qDeleteAll(*this);
0051 }
0052 
0053 void Variables::modify(const QString & name, const Expression & e)
0054 {
0055     const Analitza::Object* o=e.tree();
0056     if(o->isContainer() && static_cast<const Container*>(o)->containerType()==Container::math) {
0057         o=*static_cast<const Container*>(o)->constBegin();
0058     }
0059     modify(name, o);
0060 }
0061 
0062 Cn* Variables::modify(const QString & name, const double & d)
0063 {
0064     iterator it = find(name);
0065     if(it==end() || (*it)->type()!=Object::value) {
0066         Cn* val=new Cn(d);
0067         insert(name, val);
0068         return val;
0069     } else {
0070         Cn* val = static_cast<Cn*>(*it);
0071         val->setValue(d);
0072         return val;
0073     }
0074 }
0075 
0076 void Variables::modify(const QString& name, const Object* o)
0077 {
0078     delete value(name);
0079     
0080     insert(name, o->copy());
0081 }
0082 
0083 void Variables::rename(const QString& orig, const QString& dest)
0084 {
0085     Q_ASSERT(contains(orig));
0086     insert(dest, take(orig));
0087 }
0088 
0089 Expression Variables::valueExpression(const QString& name) const
0090 {
0091     return Expression(value(name)->copy());
0092 }
0093 
0094 QString Variables::toString() const
0095 {
0096     QString dbg;
0097     dbg += QStringLiteral("Variables(");
0098     for (Variables::const_iterator it = constBegin(), itEnd = constEnd(); it != itEnd; ++it)
0099         dbg += it.key() + QLatin1Char('=') + it.value()->toString() + QLatin1String(", ");
0100     dbg += QLatin1String(")");
0101 
0102     return dbg;
0103 }
0104 
0105 #include "moc_variables.cpp"