DFAGenDFSet.hpp

Go to the documentation of this file.
00001 /* Interface for DFAGen Tool 
00002    DFAGenDFSet.hpp
00003    Each flow-value needs to implement this interface
00004    Shweta Behere, Andy Stone (aistone@gmail.com)
00005 */
00006 
00007 #ifndef DFAGenDFSet_H
00008 #define DFAGenDFSet_H
00009 
00010 #include <iostream>
00011 #include <set>
00012 #include <OpenAnalysis/Utils/OA_ptr.hpp>
00013 #include <OpenAnalysis/IRInterface/IRHandles.hpp>
00014 #include <OpenAnalysis/DataFlow/DataFlowSet.hpp>
00015 
00016 namespace OA {
00017   namespace DataFlow {
00018 
00026 template<typename T>
00027 class DFAGenDFSet : public virtual DataFlowSet, public std::set<T> {
00028   public:
00029     // note, this class uses this implicit copy constructor derived
00030     // from set<T>.  If you want to create an explicit constructor make
00031     // sure to use the 'explicit' keyword
00032     DFAGenDFSet () {}
00033 
00034     DFAGenDFSet (std::set<T> copy) {
00035         typename std::set<T>::iterator iter;
00036         for(iter = copy.begin(); iter != copy.end(); iter++) {
00037             insert(*iter);
00038         }
00039     }
00040 
00041     virtual ~DFAGenDFSet() {}
00042 
00043     void unionEqu(const DFAGenDFSet<T> &rhs);
00044     void intersectEqu(const DFAGenDFSet<T> &rhs);
00045     void minusEqu(const DFAGenDFSet<T> &rhs);
00046 
00047     bool isEmpty() { return this->empty(); }
00048     bool isSubset(const DFAGenDFSet<T> &rhs);
00049     bool isProperSubset(const DFAGenDFSet<T> &rhs);
00050     bool isSuperset(const DFAGenDFSet<T> &rhs);
00051     bool isProperSuperset(const DFAGenDFSet<T> &rhs);
00052 
00053     bool operator==(DataFlowSet &other) const;
00054     bool operator!=(DataFlowSet &other) const;
00055 
00056     void dump(std::ostream &os) { assert(false); }
00057 
00058     void dump(std::ostream &os, OA_ptr<IRHandlesIRInterface>) {
00059         assert(false);
00060     }
00061 
00062     virtual OA_ptr<DataFlowSet> clone();
00063 };
00064 
00065 template<typename T>
00066 void DFAGenDFSet<T>::unionEqu(const DFAGenDFSet<T> &rhs) {
00067     // copy elements of the right hand set into this set.
00068     copy(rhs.begin(), rhs.end(), inserter(*this, this->begin()));
00069 }
00070 
00071 template<typename T>
00072 void DFAGenDFSet<T>::intersectEqu(const DFAGenDFSet<T> &rhs) {
00073     typename DFAGenDFSet<T>::iterator lhsIter, lhsEnd;
00074     typename DFAGenDFSet<T>::const_iterator rhsIter, rhsEnd;
00075 
00076     lhsIter = this->begin();
00077     rhsIter = rhs.begin();
00078     lhsEnd = this->end();
00079     rhsEnd = rhs.end();
00080 
00081     // Elements in both sets are gauranteed to be sorted.  We want to remove
00082     // all elements from the left-hand set that are not in the right hand
00083     // set.  To do this we carefully iterate through both sets.
00084     // Iterate through all elements in the left-hand set
00085     while(lhsIter != lhsEnd) {
00086         // if we've reached the end in the right-hand set, remove all
00087         // remaning elements in the left hand set and stop.
00088         if(rhsIter == rhsEnd) {
00089             this->erase(lhsIter, lhsEnd);
00090             return;
00091         }
00092         // iterate through elements in the left hand set until we get to
00093         // an element that's greater than or equal to the one in the
00094         // left hand set
00095         while(rhsIter != rhsEnd && *rhsIter < *lhsIter) { ++rhsIter; }
00096 
00097         // if the left and right hand elements are not equal remove the
00098         // element in the left hand set.
00099         if(*lhsIter != *rhsIter) {
00100             this->erase(lhsIter++);
00101             lhsEnd = this->end();
00102         } else {
00103             ++lhsIter;
00104         }
00105     }
00106 }
00107 
00108 template<typename T>
00109 void DFAGenDFSet<T>::minusEqu(const DFAGenDFSet<T> &rhs) {
00110     typename DFAGenDFSet<T>::iterator lhsIter, lhsEnd;
00111     typename DFAGenDFSet<T>::const_iterator rhsIter, rhsEnd;
00112 
00113     lhsIter = this->begin();
00114     rhsIter = rhs.begin();
00115     lhsEnd = this->end();
00116     rhsEnd = rhs.end();
00117 
00118     // we want to remove every element from the left hand side that's
00119     // also in the right hand side.  To do this we iterate through each
00120     // element on the right hand side and remove them.
00121     while(rhsIter != rhsEnd) {
00122         if(lhsIter == lhsEnd) { return; }
00123 
00124         while(lhsIter != lhsEnd && *lhsIter < *rhsIter) { ++lhsIter; }
00125 
00126         // if the left and right hand elements are equal remove the
00127         // element from the left hand set.
00128         if(*lhsIter == *rhsIter) {
00129             this->erase(lhsIter++);
00130             lhsEnd = this->end();
00131         } else {
00132             ++rhsIter;
00133         }
00134     }
00135 }
00136 
00137 
00138 template<typename T>
00139 bool DFAGenDFSet<T>::isSubset(const DFAGenDFSet<T> &rhs) {
00140     /* is this set a subset of the rhs */
00141     typename DFAGenDFSet<T>::iterator iter;
00142 
00143     for(iter = this->begin(); iter != this->end(); iter++) {
00144         if(rhs.find(*iter) == rhs.end()) {
00145             return false;
00146         }
00147     }
00148 
00149     return true;
00150 }
00151 
00152 
00153 template<typename T>
00154 bool DFAGenDFSet<T>::isProperSubset(const DFAGenDFSet<T> &rhs) {
00155     return isSubset(rhs) && this->size() != rhs.size();
00156 } 
00157 
00158 template<typename T>
00159 bool DFAGenDFSet<T>::isSuperset(const DFAGenDFSet<T> &rhs) {
00160     assert(false);
00161 /*    typename DFAGenDFSet<T>::iterator iter;
00162 
00163     for(iter = rhs.begin(); iter != rhs.end(); iter++) {
00164         if(this->find(*iter) == this->end()) {
00165             return false;
00166         }
00167     }
00168 
00169     return true;*/
00170 }
00171 
00172 
00173 template<typename T>
00174 bool DFAGenDFSet<T>::isProperSuperset(const DFAGenDFSet<T> &rhs) {
00175     return isSuperset(rhs) && this->size() != rhs.size();
00176 }
00177 
00178 
00179 template<typename T>
00180 bool DFAGenDFSet<T>::operator==(DataFlowSet &other) const {
00181     DFAGenDFSet<T> *castOther;
00182 
00183     castOther = dynamic_cast<DFAGenDFSet<T> *>(&other);
00184 
00185     return std::set<T>(*this) == std::set<T>(*castOther);
00186 }
00187 
00188 
00189 template<typename T>
00190 bool DFAGenDFSet<T>::operator!=(DataFlowSet &other) const {
00191     DFAGenDFSet<T> *castOther;
00192 
00193     castOther = dynamic_cast<DFAGenDFSet<T> *>(&other);
00194 
00195     return std::set<T>(*this) != std::set<T>(*castOther);
00196 }
00197 
00198 
00199 template<typename T>
00200 OA_ptr<DataFlowSet> DFAGenDFSet<T>::clone() {
00201     OA_ptr<DataFlowSet> newSet;
00202     newSet = new DFAGenDFSet<T>(*this);
00203     return newSet;
00204 }
00205 
00206 
00207   } // end of DataFlow namespace
00208 } // end of OA namespace
00209 
00210 #endif