FLOPC++
MP_index.cpp
Go to the documentation of this file.
00001 // ******************** FlopCpp **********************************************
00002 // File: MP_index.cpp
00003 // $Id$
00004 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
00005 // Copyright (C) 2003 Tim Helge Hultberg
00006 // All Rights Reserved.
00007 //****************************************************************************
00008 
00009 #include "MP_index.hpp"
00010 #include "MP_domain.hpp"
00011 #include "MP_set.hpp"
00012 #include "MP_model.hpp"
00013 
00014 namespace flopc {
00015   // Initialization of static member data
00016   MP_index& MP_index::Empty = *new MP_index();
00017   MP_index& MP_index::Any_index = *new MP_index();
00018   MP_index_exp MP_index_exp::Empty =  *new MP_index_exp(Constant(0.0));
00019 
00020   MP_index &MP_index::getEmpty() {
00021     return Empty;
00022   }
00023   MP_index &MP_index::Any() {
00024     return Any_index;
00025   }
00026   const MP_index_exp &MP_index_exp::getEmpty() {
00027     return Empty;
00028   }
00029 
00030   class MP_index_constant : public MP_index_base {
00031     friend class MP_index_exp;
00032   public:
00033   private:
00034     MP_index_constant(const Constant& c) : C(c) {}
00035     int evaluate() const {
00036       return int(C->evaluate()); 
00037     }
00038     MP_index* getIndex() const {
00039       return 0;
00040     }
00041     virtual MP_domain getDomain(MP_set* s) const{
00042       return MP_domain::getEmpty();
00043     }
00044     Constant C;
00045   };
00046 
00047   class MP_index_subsetRef : public MP_index_base {
00048     friend class MP_index_exp;
00049   private:
00050     MP_index_subsetRef(const SUBSETREF& s) : S(&s) {}
00051     int evaluate() const {
00052       return int(S->evaluate()); 
00053     }
00054     MP_index* getIndex() const {
00055       return S->getIndex();
00056     }
00057     virtual MP_domain getDomain(MP_set* s) const{
00058       return MP_domain(S->getDomain(s));
00059     }
00060     const SUBSETREF* S;
00061   };
00062   
00063   MP_index_exp operator+(MP_index& i,const Constant& j) {
00064     return new MP_index_sum(i,j);
00065   }
00066 
00067   MP_index_exp operator+(MP_index& i,const int& j) {
00068     return new MP_index_sum(i,Constant(j));
00069   }
00070 
00071   MP_index_exp operator-(MP_index& i,const Constant& j) {
00072     return new MP_index_dif(i,j);
00073   }
00074     
00075   MP_index_exp operator-(MP_index& i,const int& j) {
00076     return new MP_index_dif(i,Constant(j));
00077   }
00078 
00079   MP_index_exp operator*(MP_index& i,const Constant& j) {
00080     return new MP_index_mult(i,j);
00081   }
00082   
00083 
00084 } // End of namespace flopc
00085 
00086 using namespace flopc;
00087 
00088 
00089 MP_domain MP_index::getDomain(MP_set* s) const{
00090   return new MP_domain_set(s,const_cast<MP_index*>(this)) ;
00091 }
00092 
00093 MP_domain MP_index_mult::getDomain(MP_set* s) const{
00094   return left->getDomain(s); 
00095 }
00096 
00097 MP_domain MP_index_sum::getDomain(MP_set* s) const{
00098   return left->getDomain(s); 
00099 }
00100 
00101 MP_domain MP_index_dif::getDomain(MP_set* s) const{
00102   return left->getDomain(s);
00103 }
00104 
00105 MP_index_exp::MP_index_exp(int i) : 
00106   Handle<MP_index_base*>(new MP_index_constant(Constant(i))) {} 
00107 
00108 MP_index_exp::MP_index_exp(const SUBSETREF& s) : 
00109   Handle<MP_index_base*>(new MP_index_subsetRef(s)) {}
00110 
00111 MP_index_exp::MP_index_exp(const Constant& c) : 
00112   Handle<MP_index_base*>(new MP_index_constant(c)) {}
00113 
00114 MP_index_exp::MP_index_exp(MP_index& i) : 
00115   Handle<MP_index_base*>(&i) { operator->()->count++; }
00116 
00117 MP_index_exp::MP_index_exp(const MP_index_exp &other):
00118   Handle<MP_index_base*>((const Handle<MP_index_base*> &)other) {}
00119