mru.h

Go to the documentation of this file.
00001 #ifndef cxxtls_mru_h_included
00002 #define cxxtls_mru_h_included
00003 //
00004 // Copyright 2012, Lowell Boggs Jr.
00005 //
00006 // This file or directory, containing source code for a computer program,
00007 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00008 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00009 // source file without charge or obligation so long as you agree to
00010 // the following:
00011 //
00012 //  1.  You must indemnify Lowell Boggs against any and all financial
00013 //      obligations caused by its use, misuse, function, or malfunction.
00014 //      Further, you acknowledge that there is no warranty of any kind,
00015 //      whatsoever.
00016 //
00017 //  2.  You agree not to attempt to patent any portion of this original
00018 //      work -- though you may attempt to patent your own extensions to
00019 //      it if you so choose.
00020 //
00021 //  3.  You keep this copyright notice with the file and all copies
00022 //      of the file and do not change it anyway except language translation.
00023 //
00024 // You are responsible for enforcing your own compliance with these
00025 // conditions and may not use this source file if you cannot agree to the
00026 // above terms and conditions.
00027 //
00028 // Warning:  not all files in this directory structure are covered by the
00029 // same copyright.  Some of them are part of the GNU source distribution
00030 // and you must obey the GPL copyright for those files.
00031 
00032 #include <list>
00033 
00034 namespace cxxtls
00035 {
00036     template<class T>
00037     class MRU
00061     {
00062     public:
00063 
00064         typedef            T   value_type;
00065         typedef std::list<T>   rep_type;
00066 
00067         typedef typename rep_type::iterator        iterator;
00068 
00069         iterator end()          { return data_.end();   }  
00070 
00071         iterator mostRecent()   { return data_.begin(); }  
00072 
00073         iterator leastRecent()  
00074             
00075         { 
00076             if(empty()) 
00077                 return data_.end();
00078             
00079             iterator rv = data_.end();
00080             --rv;
00081 
00082             return rv;
00083         }
00084 
00085         bool empty() const { return data_.empty(); }  
00086 
00087         void activate(T const &item)
00090         {
00091             iterator where = find(item);
00092 
00093             if(where != data_.end())
00094             {
00095                data_.erase(where);
00096             }
00097 
00098             data_.push_front(item);
00099 
00100         }
00101 
00102         void discard(T const &item)
00104         {
00105             iterator where = find(item);
00106 
00107             if(where != data_.end())
00108                data_.erase(where);
00109         }
00110 
00111 
00112 
00113     private:
00114         rep_type data_;
00115 
00116         iterator find(T const &query) 
00117             //  return an iterator pointing to query or end() if is not found
00118         {
00119            iterator next = data_.begin();
00120            iterator end  = data_.end();
00121 
00122            while(next != end)
00123            {
00124                if(query == *next)
00125                    return next;
00126                    
00127                ++next;
00128            }
00129 
00130            return next;
00131 
00132         }
00133 
00134 
00135 
00136     };  // class MRU
00137 
00138 }; // namespace cxxtls
00139 
00140 
00141 #endif
Generated on Wed Feb 29 22:50:04 2012 for CXXUtilities by  doxygen 1.6.3