MaxList.h

00001 
00002 #ifndef MAXLIST_H
00003 #define MAXLIST_H
00004 
00005 #include <iostream>
00006 #include <list>
00007 #include <algorithm>
00008 
00009 using std::list;
00010 
00011 template<class Info>
00012 
00013 class MaxList {
00014  public:
00015     
00016     MaxList(int size);
00017     MaxList(std::list<Info> lst);  
00018     virtual ~MaxList();
00019        
00020     int getSize();
00021     void insert(Info& in);
00022        
00023     typedef typename std::list<Info>::iterator pointer; 
00024     typedef typename std::list<Info>::reverse_iterator pointer_reverse;
00025 
00026     void erase(pointer pos);
00027 
00028     const Info* search(const Info& in);
00029     bool erase(const Info& in);
00030     void pop();
00031     void popBack();
00032     void clear();
00033     bool empty();
00034     
00035 /*    std::list<Info>::iterator begin();
00036     std::list<Info>::iterator end();
00037     std::list<Info>::reverse_iterator rbegin();
00038     std::list<Info>::reverse_iterator rend();*/
00039     
00040     pointer begin();
00041     pointer end();
00042     pointer_reverse rbegin();
00043     pointer_reverse rend();
00044 
00045   private:
00046     int size;
00047     std::list<Info> mosts;
00048 };
00049 template<class Info>
00050 MaxList<Info>::MaxList(int size) {
00051   this->size = size;
00052 };
00053 
00054 
00055 template<class Info>
00056 MaxList<Info>::MaxList(std::list<Info> lst) : mosts(lst) {
00057   size = lst.size();
00058   std::sort(mosts.rbegin(), mosts.rend());
00059 };
00060 
00061 
00062 template<class Info>
00063 MaxList<Info>::~MaxList() {
00064   mosts.clear();
00065 };
00066 
00067 
00068 template<class Info>
00069 int MaxList<Info>::getSize() {
00070   return mosts.size();
00071 };
00072 
00073 template<class Info>
00074 void MaxList<Info>::insert(Info& in) {
00075 
00076   if (mosts.size() == 0) {
00077     mosts.push_front(in);
00078 
00079   } else {
00080          
00081      typedef typename std::list<Info>::iterator pointer;
00082          
00083      pointer it;
00084     
00085     int j;
00086 
00087     bool inserted = false;
00088     
00089     for(j = 0, it = mosts.begin() ; it != mosts.end() && j < size ; it++, j++) {
00090       
00091       if (in == *it) {
00092         inserted = true;
00093         break;
00094         
00095       } else if (in > *it) {
00096         mosts.insert(it, in);
00097         inserted = true;
00098         break;
00099 
00100       } 
00101     }
00102 
00103     if(!inserted)
00104       mosts.push_back(in);
00105     
00106     if (mosts.size() > this->size)
00107       mosts.pop_back();
00108   }
00109 };
00110 
00111 template<class Info>
00112 void MaxList<Info>::erase(pointer pos) {
00113   mosts.erase(pos);
00114   return true;
00115 };
00116 
00117 template<class Info>
00118 bool MaxList<Info>::erase( const Info& in ) {
00119   typedef typename std::list<Info>::iterator pointer;   
00120   pointer it = std::find(mosts.begin(), mosts.end(), in);
00121   if (it == mosts.end())
00122     return false;
00123 
00124   else {
00125     mosts.erase(it);
00126     return true;
00127   }
00128 };
00129 
00130 template<class Info>
00131 const Info* MaxList<Info>::search( const Info& in ) {
00132 
00133   typedef typename std::list<Info>::iterator pointer;
00134   
00135   pointer it;
00136 
00137   it = std::find(mosts.begin(), mosts.end(), in);
00138 
00139   return ( it == mosts.end() ) ? 0 : &(*it);
00140 };
00141 
00142 template<class Info>
00143 void MaxList<Info>::pop() {
00144   mosts.pop_front();
00145 };
00146 
00147 template<class Info>
00148 void MaxList<Info>::popBack() {
00149   mosts.pop_back();
00150 };
00151 
00152 template<class Info>
00153 void MaxList<Info>::clear() {
00154   mosts.clear();
00155 };
00156 
00157 template<class Info>
00158 bool MaxList<Info>::empty() {
00159   return mosts.empty();
00160 };
00161 
00162 template<class Info>
00163 typename std::list<Info>::iterator MaxList<Info>::begin() {
00164   return mosts.begin();
00165 };
00166 
00167 template<class Info>
00168 typename std::list<Info>::iterator MaxList<Info>::end() {
00169   return mosts.end();
00170 };
00171 
00172 template<class Info>
00173 typename std::list<Info>::reverse_iterator MaxList<Info>::rbegin() {
00174   return mosts.rbegin();
00175 };
00176 
00177 template<class Info>
00178 typename std::list<Info>::reverse_iterator MaxList<Info>::rend() {
00179   return mosts.rend();
00180 };
00181 
00182 #endif

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3