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

0001 /*************************************************************************************
0002  *  Copyright (C) 2009 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 "list.h"
0020 #include "expression.h"
0021 #include "abstractexpressionvisitor.h"
0022 #include "analitzautils.h"
0023 
0024 using namespace Analitza;
0025 
0026 List::List(const List& v)
0027     : Object(Object::list)
0028 {
0029     foreach(const Object* o, v.m_elements)
0030     {
0031         m_elements.append(o->copy());
0032     }
0033 }
0034 
0035 List::List()
0036     : Object(Object::list)
0037 {}
0038 
0039 List::~List()
0040 {
0041     qDeleteAll(m_elements);
0042 }
0043 
0044 List* List::copy() const
0045 {
0046     List *v=new List;
0047     foreach(const Object* o, m_elements)
0048         v->m_elements.append(o->copy());
0049     
0050     return v;
0051 }
0052 
0053 void List::appendBranch(Object* o)
0054 {
0055     Q_ASSERT(o);
0056     m_elements.append(o);
0057 }
0058 
0059 QVariant List::accept(AbstractExpressionVisitor* e) const
0060 {
0061     return e->visit(this);
0062 }
0063 
0064 bool List::isZero() const
0065 {
0066     return m_elements.isEmpty();
0067 }
0068 
0069 bool List::matches(const Object* exp, QMap< QString, const Object* >* found) const
0070 {
0071     if(Object::vector!=exp->type())
0072         return false;
0073     const List* c=(const List*) exp;
0074     if(m_elements.count()!=c->m_elements.count())
0075         return false;
0076     
0077     bool matching=true;
0078     List::const_iterator it, it2, itEnd=m_elements.constEnd();
0079     for(it=m_elements.constBegin(), it2=c->m_elements.constBegin(); matching && it!=itEnd; ++it, ++it2)
0080     {
0081         matching &= (*it)->matches(*it2, found);
0082     }
0083     return matching;
0084 }
0085 
0086 bool List::operator==(const List& v) const
0087 {
0088     bool eq=v.size()==size();
0089     
0090     for(int i=0; eq && i<m_elements.count(); ++i) {
0091         eq = eq && AnalitzaUtils::equalTree(m_elements[i], v.m_elements[i]);
0092     }
0093     return eq;
0094 }