DefaultWorklist.hpp

Go to the documentation of this file.
00001 #ifndef wali_DEFAULT_WORKLIST_GUARD
00002 #define wali_DEFAULT_WORKLIST_GUARD 1
00003 
00004 /*!
00005  * @author Nicholas Kidd
00006  */
00007 
00008 #include "wali/Common.hpp"
00009 #include "wali/Worklist.hpp"
00010 #include <list>
00011 
00012 namespace wali
00013 {
00014   /*! @class DefaultWorklist
00015    *
00016    * The default DefaultWorklist acts as a Stack and uses
00017    * std::list to hold items.
00018    */
00019 
00020   template< typename T > class DefaultWorklist : public ::wali::Worklist<T>
00021   {
00022     public:
00023 
00024       DefaultWorklist() : Worklist<T>() {}
00025 
00026       virtual ~DefaultWorklist() {
00027         clear();
00028       }
00029 
00030       /*!
00031        * put
00032        *
00033        */
00034       virtual bool put( T *item ) {
00035         if( !item->marked() ) {
00036           item->mark();
00037           wl.push_back( item );
00038           return true;
00039         }
00040         else
00041           return false;
00042       }
00043 
00044       /*!
00045        * get
00046        *
00047        * Return an item from the worklist.
00048        * Returns NULL if the DefaultWorklist is empty.
00049        * In the future it may throw an exception
00050        *
00051        * @return T *
00052        */
00053       virtual T * get() {
00054         if( !empty() ) {
00055           T *item = wl.back();
00056           wl.pop_back();
00057           item->unmark();
00058           return item;
00059         }
00060         else
00061           return 0;
00062       }
00063 
00064       /*!
00065        * emtpy
00066        *
00067        * @return true if the DefaultWorklist is empty
00068        */
00069       virtual bool empty() const {
00070         return wl.empty();
00071       }
00072 
00073       /*!
00074        * clear
00075        *
00076        * Remove and unmark each item in this worklist.
00077        */
00078       virtual void clear() {
00079         typename std::list< T* >::iterator it = wl.begin();
00080 
00081         // iterate through unmarking items
00082         for( ; it != wl.end() ; it++ ) {
00083           T * item = *it;
00084           item->unmark();
00085         }
00086         // clear the list
00087         wl.clear();
00088       }
00089 
00090     protected:
00091       std::list< T* > wl; //!< The default worklist data structure
00092 
00093   }; // class DefaultWorklist
00094 
00095 } // namespace wali
00096 
00097 #endif // wali_DEFAULT_WORKLIST_GUARD
00098