/******************************************************************* * * DESCRIPTION: Class VList, container de Value * * AUTOR: Mariana D'Abreu * * EMAIL: mailto://mdabreu@dc.uba.ar * *******************************************************************/ #include "vlist.h" #define INVALID_INDEX_MSG "Error indexing list!" #define INVALID_ASSIGN_TYPE_MSG "Invalid type in assign statement!" #define INVALID_OPERAND_MSG "Invalid operand!" #define LIST_TAG "" #define ENDL_TAG "" #define LIST_OSEP "<" #define LIST_CSEP ">" #define ITEM_SEP "," /******************************************************************* * Function: ********************************************************************/ VList::VList() :Value() { } /******************************************************************* * Function: ********************************************************************/ VList::VList( const Value &value ) :Value() { *this = value; } /******************************************************************* * Function: ********************************************************************/ VList::VList( const VList &vlist ) :Value() { *this = vlist; } /******************************************************************* * Function: ********************************************************************/ VList::VList( const long n, const RealValue &value ) :Value() { for ( int i = 1; i <= n; i++ ) { this->addElement( value ); } } /******************************************************************* * Function: ********************************************************************/ VList::~VList() { this->clear(); } /******************************************************************* * Function: ********************************************************************/ RealValue VList::value() const { VListException e; e.addText( INVALID_OPERATION_MSG ); MTHROW( e ); } /******************************************************************* * Function: ********************************************************************/ Value &VList::value( const RealValue &v ) { VListException e; e.addText( INVALID_OPERATION_MSG ); MTHROW( e ); } /******************************************************************* * Function: ********************************************************************/ VList &VList::addElement( const RealValue &v ) { Value *v1 = new Value( v ); values.push_back( v1 ); return *this; } /******************************************************************* * Function: ********************************************************************/ VList &VList::addElement( const Value &v ) { return ( this->addElement( &v ) ); } /******************************************************************* * Function: ********************************************************************/ VList &VList::addElement( const Value *v ) { MASSERT( v ); VList *v1 = (VList *)v->clone(); values.push_back( v1 ); return *this; } /******************************************************************* * Function: ********************************************************************/ VList &VList::delElementAtPos( const long index ) { if ( index < 1 || ( index > numberOfElements() ) ) { VListException e; e.addText( INVALID_INDEX_MSG ); MTHROW( e ); } int i; VList::LCursor cursor; for( i = 1, cursor = values.begin() ; cursor != values.end() && i < index; cursor++, i++ ); if ( i == index ) { if ( *cursor ) { delete (*cursor); } values.erase( cursor ); } return *this; } /******************************************************************* * Function: ********************************************************************/ VList &VList::updElementAtPos( const long index, const RealValue &v ) { Value *v1 = new Value( v ); return ( this->updElementAtPos( index, v1 ) ); } /******************************************************************* * Function: ********************************************************************/ VList &VList::updElementAtPos( const long index, const Value &v ) { return ( this->updElementAtPos( index, &v ) ); } /******************************************************************* * Function: ********************************************************************/ VList &VList::updElementAtPos( const long index, const Value *v ) { if ( index < 1 || ( index > numberOfElements() ) ) { VListException e; e.addText( INVALID_INDEX_MSG ); MTHROW( e ); } MASSERT( v ); int i; VList::LCursor cursor; for( i = 1, cursor = values.begin() ; cursor != values.end() && i < index; cursor++, i++ ); if ( i == index ) { if ( *cursor ) { delete (*cursor); } Value *v1 = v->clone(); values.insert( cursor, v1 ); values.erase( cursor ); } return *this; } /******************************************************************* * Function: ********************************************************************/ VList &VList::setAll( const RealValue &v ) { for ( int i=1; i <= numberOfElements(); i++ ) { updElementAtPos( i, v ); } return *this; } /******************************************************************* * Function: ********************************************************************/ Value *VList::elementAt( const long index ) const { Value *v = NULL; if ( index < 1 || ( index > numberOfElements() ) ) { VListException e; e.addText( INVALID_INDEX_MSG ); MTHROW( e ); } int i; ValueList::const_iterator cursor; for( i = 1, cursor = values.begin() ; cursor != values.end() && i < index; cursor++, i++ ); if ( i == index ) { v = (*cursor); } return v; } /******************************************************************* * Function: ********************************************************************/ Value &VList::operator = ( const Value &v ) { if ( !v.isList() ) { VListException e; e.addText( INVALID_ASSIGN_TYPE_MSG ); MTHROW( e ); } VList *list = (VList *)&v; return ( *this = *list ); } /******************************************************************* * Function: ********************************************************************/ Value &VList::operator = ( const VList &v ) { this->clear(); ValueList::const_iterator cursor; for( cursor = v.values.begin() ; cursor != v.values.end() ; cursor++ ) { this->addElement( ( *cursor ) ); } return *this; } /******************************************************************* * Function: ********************************************************************/ bool VList::operator == ( const Value &v ) const { if ( !v.isList() ) { return false; } VList *v1 = (VList *)&v; return ( *this == *v1 ); } /******************************************************************* * Function: ********************************************************************/ bool VList::operator == ( const VList &v ) const { if ( v.numberOfElements() != this->numberOfElements() ) { return false; } ValueList::const_iterator c1, c2; for( c1 = values.begin(), c2 = v.values.begin() ; c1 != values.end() && c2 != v.values.end(); c1++, c2++ ) { if ( *(*c1) != *(*c2) ) { return false; } } return true; } /******************************************************************* * Function: ********************************************************************/ Value *VList::operator + ( const Value &v ) const { if ( !v.isList() ) { VListException e; e.addText( INVALID_OPERAND_MSG ); MTHROW( e ); } VList *v1 = (VList *)&v; return ( *this + *v1 ); } /******************************************************************* * Function: ********************************************************************/ Value *VList::operator + ( const VList &v ) const { VList *vlist = new VList(); ValueList::const_iterator c1, c2; for( c1 = values.begin(), c2 = v.values.begin() ; c1 != values.end() && c2 != v.values.end(); c1++, c2++ ) { vlist->addElement( *(*c1) + *(*c2) ); } return vlist; } /******************************************************************* * Function: ********************************************************************/ Value *VList::operator + ( const VList *v ) const { MASSERT( v ); return ( *this + *v ); } /******************************************************************* * Function: ********************************************************************/ Value *VList::operator - ( const Value &v ) const { if ( !v.isList() ) { VListException e; e.addText( INVALID_OPERAND_MSG ); MTHROW( e ); } VList *v1 = (VList *)&v; return ( *this - *v1 ); } /******************************************************************* * Function: ********************************************************************/ Value *VList::operator - ( const VList &v ) const { VList *vlist = new VList(); ValueList::const_iterator c1, c2; for( c1 = values.begin(), c2 = v.values.begin() ; c1 != values.end() && c2 != v.values.end(); c1++, c2++ ) { vlist->addElement( *(*c1) - *(*c2) ); } return vlist; } /******************************************************************* * Function: ********************************************************************/ Value *VList::operator - ( const VList *v ) const { MASSERT( v ); return ( *this - *v ); } /******************************************************************* * Function: ********************************************************************/ Value *VList::operator * ( const RealValue &v ) const { VList *vlist = new VList(); ValueList::const_iterator cursor; for( cursor = values.begin(); cursor != values.end(); cursor++ ) { vlist->addElement( *(*cursor) * v ); } return vlist; } /******************************************************************* * Function: ********************************************************************/ RealValue VList::toReal() const { VListException e; e.addText( INVALID_OPERATION_MSG ); MTHROW( e ); } /******************************************************************* * Function: ********************************************************************/ string VList::asString() const { string str; string sep; ValueList::const_iterator cursor; Value *v; str = LIST_OSEP; for( cursor = values.begin() ; cursor != values.end(); cursor++ ) { v = (*cursor); str += sep + v->asString(); sep = ITEM_SEP; } str += LIST_CSEP; return str; } /******************************************************************* * Function: ********************************************************************/ Value *VList::clone() const { VList *list = new VList(); Value *v = NULL; ValueList::const_iterator cursor; for( cursor = values.begin() ; cursor != values.end(); cursor++ ) { v = *cursor; if ( v->isList() ) { list->addElement( v ); } else { list->addElement( v->toReal() ); } } return list; } /******************************************************************* * Function: ********************************************************************/ ostream &VList::print ( ostream &out ) const { return printIndent( out, TAB ); } /******************************************************************* * Function: ********************************************************************/ ostream &VList::printIndent( ostream &out, const string &indent ) const { ValueList::const_iterator cursor; Value *v; for( cursor = values.begin() ; cursor != values.end(); cursor++ ) { v = (*cursor); if ( v->isList() ) { VList *vl = (VList *)v; out << indent << LIST_TAG << endl; vl->printIndent( out, indent + TAB ); out << indent << ENDL_TAG << endl; } else { out << indent << LIST_OSEP << v->value() << LIST_CSEP << endl; } } return out; } /******************************************************************* * Function: ********************************************************************/ bool VList::isUndefined() const { VListException e; e.addText( INVALID_OPERATION_MSG ); MTHROW( e ); } /******************************************************************* * Function: ********************************************************************/ VList &VList::clear() { VList::LCursor cursor; for( cursor = values.begin() ; cursor != values.end(); cursor++ ) { if ( *cursor ) { delete (*cursor); } } values.erase( values.begin(), values.end() ) ; return *this; }