00001 #ifndef __EXTRACTOR_H_LOADED
00002 #define __EXTRACTOR_H_LOADED
00003
00004 #include <list>
00005 #include <algorithm>
00006
00007 #include "edg/workload/common/common_namespace.h"
00008
00009 COMMON_NAMESPACE_BEGIN {
00010
00011 namespace utilities {
00012
00013 template <class Container>
00014 class ForwardExtractor {
00015 public:
00016 typedef typename Container::iterator iterator;
00017 typedef typename Container::value_type value_type;
00018
00019 ForwardExtractor( void );
00020 ForwardExtractor( Container &cont );
00021
00022 ~ForwardExtractor( void );
00023
00024 iterator get_next( void );
00025 void erase( const iterator &it );
00026 void remove( const value_type &val );
00027
00028 inline void clear( void ) { this->fe_container->clear(); this->fe_list.clear(); }
00029 inline void reset( Container &cont ) { this->fe_container = &cont; this->fe_list.clear(); }
00030 inline Container *operator->( void ) { return( this->fe_container ); }
00031
00032 private:
00033 typedef typename std::list<iterator>::iterator ExtraIterator;
00034
00035 Container *fe_container;
00036 std::list<iterator> fe_list;
00037 };
00038
00039 template <class Container>
00040 ForwardExtractor<Container>::ForwardExtractor<Container>( Container &cont ) : fe_container( &cont ), fe_list()
00041 {}
00042
00043 template <class Container>
00044 ForwardExtractor<Container>::ForwardExtractor<Container>( void ) : fe_container(), fe_list()
00045 {}
00046
00047 template <class Container>
00048 ForwardExtractor<Container>::~ForwardExtractor<Container>( void )
00049 {}
00050
00051 template <class Container>
00052 typename ForwardExtractor<Container>::iterator ForwardExtractor<Container>::get_next( void )
00053 {
00054 iterator next;
00055
00056 if( this->fe_container->empty() ) next = this->fe_container->end();
00057 else if( this->fe_list.empty() ) {
00058 next = this->fe_container->begin();
00059 this->fe_list.push_back( next );
00060 }
00061 else {
00062 next = this->fe_list.back();
00063 ++next;
00064
00065 if( next != this->fe_container->end() ) this->fe_list.push_back( next );
00066 }
00067
00068 return( next );
00069 }
00070
00071 template <class Container>
00072 void ForwardExtractor<Container>::erase( const iterator &pos )
00073 {
00074 ExtraIterator where;
00075
00076 where = std::find( this->fe_list.begin(), this->fe_list.end(), pos );
00077
00078 if( where != this->fe_list.end() ) {
00079 this->fe_list.erase( where );
00080 this->fe_container->erase( pos );
00081 }
00082
00083 return;
00084 }
00085
00086 template <class Container>
00087 void ForwardExtractor<Container>::remove( const value_type &val )
00088 {
00089 ExtraIterator where;
00090
00091 where = std::find( this->fe_container->begin(), this->fe_container->end(), val );
00092 if( where != this->fe_container->end() ) this->erase( where );
00093
00094 return;
00095 }
00096
00097 };
00098
00099 } COMMON_NAMESPACE_END;
00100
00101 #endif
00102
00103
00104
00105