SundanceListExpr.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                              Sundance
00005 //                 Copyright (2005) Sandia Corporation
00006 // 
00007 // Copyright (year first published) Sandia Corporation.  Under the terms 
00008 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government 
00009 // retains certain rights in this software.
00010 // 
00011 // This library is free software; you can redistribute it and/or modify
00012 // it under the terms of the GNU Lesser General Public License as
00013 // published by the Free Software Foundation; either version 2.1 of the
00014 // License, or (at your option) any later version.
00015 //  
00016 // This library is distributed in the hope that it will be useful, but
00017 // WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019 // Lesser General Public License for more details.
00020 //                                                                                 
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License along with this library; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00024 // USA                                                                                
00025 // Questions? Contact Kevin Long (krlong@sandia.gov), 
00026 // Sandia National Laboratories, Livermore, California, USA
00027 // 
00028 // ************************************************************************
00029 /* @HEADER@ */
00030 
00031 #include "SundanceListExpr.hpp"
00032 #include "Teuchos_TimeMonitor.hpp"
00033 
00034 using namespace Sundance;
00035 using namespace Sundance;
00036 
00037 using namespace Sundance;
00038 using namespace Teuchos;
00039 
00040 static Time& appendToListTimer() 
00041 {
00042   static RCP<Time> rtn 
00043     = TimeMonitor::getNewTimer("append to list"); 
00044   return *rtn;
00045 }
00046 
00047 ListExpr::ListExpr()
00048   : ExprBase(), elements_()
00049 {;}
00050 
00051 ListExpr::ListExpr(const Array<Expr>& elements)
00052   : ExprBase(), elements_(elements)
00053 {;}
00054 
00055 void ListExpr::append(const Expr& expr)
00056 {
00057   TimeMonitor timer(appendToListTimer());
00058   elements_.append(expr);
00059 }
00060 
00061 Expr ListExpr::flatten() const 
00062 {
00063   Expr rtn = new ListExpr();
00064 
00065   for (int i=0; i<this->size(); i++)
00066     {
00067       Expr e = element(i).flatten();
00068       for (int j=0; j<e.size(); j++)
00069         {
00070           rtn.append(e[j]);
00071         }
00072     }
00073 
00074   return rtn;
00075 }
00076 
00077 Expr ListExpr::join(const Expr& other) const 
00078 {
00079   Expr rtn = new ListExpr(elements_);
00080   
00081   for (int i=0; i<other.size(); i++)
00082     {
00083       rtn.append(other[i]);
00084     }
00085 
00086   return rtn;
00087 }
00088 
00089 int ListExpr::size() const
00090 {
00091   return elements_.size();
00092 }
00093 
00094 int ListExpr::totalSize() const 
00095 {
00096   int rtn = 0;
00097 
00098   for (int i=0; i<this->size(); i++)
00099     {
00100       rtn += elements_[i].totalSize();
00101     }
00102 
00103   return rtn;
00104 }
00105 
00106 std::ostream& ListExpr::toText(std::ostream& os, bool paren) const
00107 {
00108   os << "{";
00109   for (int i=0; i<elements_.size(); i++)
00110     {
00111       elements_[i].ptr()->toText(os, paren);
00112       if (i < elements_.size()-1) os << ", ";
00113     }
00114   os << "}";
00115   return os;
00116 }
00117 
00118 
00119 XMLObject ListExpr::toXML() const 
00120 {
00121   XMLObject rtn("ListExpr");
00122   for (int i=0; i<elements_.length(); i++)
00123     {
00124       rtn.addChild(elements_[i].toXML());
00125     }
00126   return rtn;
00127 }
00128 
00129 

Site Contact