00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00030 #ifdef DEBUG
00031 #include <cassert>
00032 #endif
00033 #include <vector>
00034 #include <algorithm>
00035
00036 #include "casterm.h"
00037 #ifndef _CASFUNCOBJ_H
00038 #include "casfuncobj.h"
00039 #endif
00040 #include "casexception.h"
00041 #include "longint.h"
00042
00049
00050
00051
00052
00053
00054
00055
00056
00057
00068 void
00069 CASTerm::GetNonSingleValues ( std::vector<const CASObject*> &lst ) const {
00070 CASExtractNonSingleValues nsv;
00071 nsv = std::for_each ( operands.begin(), operands.end(), nsv );
00072 nsv.Result ( lst );
00073 }
00074
00085 void
00086 CASTerm::GetSingleValues ( std::vector<const CASObject*> &lst ) const {
00087 CASExtractSingleValues sv;
00088 sv = std::for_each ( operands.begin(), operands.end(), sv );
00089 sv.Result ( lst );
00090 }
00091
00106 bool
00107 CASTerm::HaveSameOperands ( const CASObject *o ) const {
00108 checkptr ( o );
00109
00110 if ( GetType() != o->GetType() ) {
00111 return false;
00112 }
00113
00114 CASTerm *this_term = dynamic_cast<CASTerm*> ( Clone() );
00115 CASTerm *that_term = dynamic_cast<CASTerm*> ( o->Clone() );
00116 #ifdef DEBUG
00117 assert ( this_term != 0 );
00118 assert ( that_term != 0 );
00119 #endif
00120
00121 if ( operands.size() != that_term->operands.size() ) {
00122 return false;
00123 }
00124
00125 this_term->Sort();
00126 that_term->Sort();
00127
00128 bool ret_val = std::equal ( this_term->operands.begin(),
00129 this_term->operands.end(),
00130 that_term->operands.begin(),
00131 CASObjectEqual() );
00132
00133 CASObject::Garbage->Put ( this_term );
00134 CASObject::Garbage->Put ( that_term );
00135
00136 return ret_val;
00137 }
00138
00156 void
00157 CASTerm::GetOperands ( const CASTerm *t, std::vector<const CASObject*> &out ) const {
00158 std::copy ( t->operands.begin(),
00159 t->operands.end(),
00160 std::back_inserter ( out ) );
00161 }
00162
00179 void
00180 CASTerm::SetOperands ( CASTerm *t, const std::vector<const CASObject*> &in ) const{
00181 if ( t->operands.size() != 0 ) {
00182 CASObject::Garbage->Put ( t->operands );
00183 t->operands.clear();
00184 }
00185
00186 std::transform ( in.begin(),
00187 in.end(),
00188 std::back_inserter ( t->operands ),
00189 CASCloneObject()
00190 );
00191 }
00192
00209 void
00210 CASTerm::SetOperands ( CASTerm *t, const std::vector<CASObject*> &in ) const{
00211 if ( t->operands.size() != 0 ) {
00212 CASObject::Garbage->Put ( t->operands );
00213 t->operands.clear();
00214 }
00215
00216 std::transform ( in.begin(),
00217 in.end(),
00218 std::back_inserter ( t->operands ),
00219 CASCloneObject()
00220 );
00221 }
00222
00223
00224
00225
00226
00227
00232 CASTerm::CASTerm() {
00233 meta.UnsetSingleValue();
00234 }
00235
00239 CASTerm::~CASTerm() {
00240 #ifdef SHOWCONSTRUCTOR
00241 std::cout << "# CASTerm::~CASTerm()" << std::endl;
00242 #endif
00243 std::for_each ( operands.begin(),
00244 operands.end(),
00245 CASDestroyObject() );
00246 }
00247
00248
00249
00250
00251
00264 CASObject*
00265 CASTerm::Power ( const CASObject* exp ) const {
00266 checkptr ( exp );
00267
00268 if ( exp->IsZero() ) {
00269 LongInt *res = new LongInt ( 1L );
00270 return res;
00271 }
00272 CASTerm* result = dynamic_cast<CASTerm*> ( Clone() );
00273 #ifdef DEBUG
00274 assert ( result != 0 );
00275 #endif
00276
00277 CASObject *new_exp = result->exponent->Multiply ( exp );
00278 CASObject::Garbage->Put ( result->exponent );
00279 result->exponent = new_exp;
00280
00281 return result;
00282
00283 }
00284
00298 void
00299 CASTerm::Append ( const CASObject* o ) {
00300 operands.push_back ( o->Clone() );
00301 meta.UnsetAll();
00302 }
00303
00317 bool
00318 CASTerm::IsEqual ( const CASObject* o ) const {
00319 checkptr ( o );
00320
00321 if ( GetType() != o->GetType() ) {
00322 return false;
00323 }
00324
00325 const CASTerm* t_ptr = dynamic_cast<const CASTerm*> ( o );
00326 #ifdef DEBUG
00327 assert ( t_ptr != 0 );
00328 #endif
00329
00330 if ( operands.size() != t_ptr->operands.size() ) {
00331 return false;
00332 }
00333
00334 if ( !exponent->IsEqual ( t_ptr->exponent ) ) {
00335 return false;
00336 }
00337
00338 return std::equal ( operands.begin(),
00339 operands.end(),
00340 t_ptr->operands.begin(),
00341 CASObjectEqual() );
00342 }
00343
00358 bool
00359 CASTerm::IsLT ( const CASObject* o ) const {
00360 checkptr ( o );
00361
00362 return GetType() < o->GetType();
00363 }
00364
00379 bool
00380 CASTerm::IsGT ( const CASObject* o ) const {
00381 checkptr ( o );
00382
00383 return GetType() > o->GetType();
00384 }
00385
00392 unsigned long
00393 CASTerm::NumOperands() const {
00394 return operands.size();
00395 }
00396
00405 bool
00406 CASTerm::IsPureNumerical() const {
00407 CASIsPureNumerical pn;
00408 pn = std::for_each ( operands.begin(), operands.end(), pn );
00409 return pn.Result();
00410 }
00411
00420 CASObject*
00421 CASTerm::SortWeight() const {
00422 CASCalculateSortWeight sortweight;
00423 sortweight = std::for_each ( operands.begin(),
00424 operands.end(),
00425 CASCalculateSortWeight() );
00426 return sortweight.Result();
00427 }
00428
00437 void
00438 CASTerm::Sort() {
00439 if ( meta.IsSorted() ) {
00440 return;
00441 }
00442 std::vector<CASObject*>::iterator i = operands.begin();
00443 while ( i != operands.end() ) {
00444 ( *i )->Sort();
00445 i++;
00446 }
00447 std::sort ( operands.begin(), operands.end(), CASSortObject() );
00448 meta.SetSorted();
00449 }
00450
00457 void
00458 CASTerm::UnsetAllEvaluated() {
00459 std::vector<CASObject*>::iterator it = operands.begin();
00460 while ( it != operands.end() ) {
00461 CASTerm *term = dynamic_cast<CASTerm*> ( *it );
00462 if ( term == 0 )
00463 ( *it )->meta.UnsetEvaluated();
00464 else
00465 term->UnsetAllEvaluated();
00466 it++;
00467 }
00468 meta.UnsetEvaluated();
00469 }
00470
00477 void
00478 CASTerm::SetAllEvaluated() {
00479 std::vector<CASObject*>::iterator it = operands.begin();
00480 while ( it != operands.end() ) {
00481 CASTerm *term = dynamic_cast<CASTerm*> ( *it );
00482 if ( term == 0 )
00483 ( *it )->meta.SetEvaluated();
00484 else
00485 term->SetAllEvaluated();
00486 it++;
00487 }
00488 meta.SetEvaluated();
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498