BcPOC
SF-HRP ASM implementation
|
00001 #include <vector> 00002 #include <functional> 00003 00004 #include <iostream> 00005 00006 /* 00007 * Class RuleQueue 00008 * 00009 * maintains active rules, order by priorities acording to PartialOrder 00010 * rules are erased acording to their Equivalence 00011 * 00012 */ 00013 00014 template< typename T, typename PartialOrder = std::less< T>, typename Equivalence = std::equal_to< T> > 00015 class RuleQueue { 00016 00017 public: 00018 typedef typename std::vector< T>::iterator iterator; 00019 00020 void insert( const T& x) { 00021 _items.push_back(x); 00022 } 00023 00024 bool erase( const T& x) { 00025 for(iterator it = _items.begin(); it != _items.end(); ++it) { 00026 if(eqiv(*it, x)) { 00027 _items.erase(it); 00028 return true; 00029 } 00030 } 00031 00032 return false; 00033 } 00034 00035 iterator top_begin() { 00036 00037 _top.clear(); //initialize new top 00038 if(!_items.empty()) { 00039 00040 T foo = _items.front(); //inicialize smallest element 00041 00042 for(iterator it = _items.begin(); it != _items.end(); ++it) { 00043 if(comp(foo, *it)) { 00044 //new smallest found 00045 _top.clear(); 00046 _top.push_back(*it); 00047 foo = *it; 00048 00049 } else if(comp(*it, foo)) { 00050 //no op, is not smallest 00051 } else { 00052 //equals, i. e. is also smallest 00053 _top.push_back(*it); 00054 } 00055 } 00056 } 00057 00058 return _top.begin(); 00059 } 00060 00061 iterator top_end() { 00062 return _top.end(); 00063 } 00064 00065 bool empty() { 00066 return _items.empty(); 00067 } 00068 00069 void Dump() { 00070 if(QUEUE_DEBUG != true) return; 00071 00072 00073 //std::cout << "Queue contains: "; 00074 00075 for(iterator it = _items.begin(); it != _items.end(); ++it) { 00076 (*it)->out(); 00077 } 00078 00079 //std::cout<< std::endl; 00080 //std::cout<< "Top contains: "; 00081 00082 for(iterator it = top_begin(); it != top_end(); ++it) { 00083 (*it)->out(); 00084 } 00085 00086 //std::cout << std::endl << std::endl; 00087 } 00088 00089 protected: 00090 PartialOrder comp; 00091 Equivalence eqiv; 00092 00093 //items in the queue 00094 std::vector< T> _items; 00095 00096 //items on top of the queue 00097 std::vector< T> _top; 00098 };