00001 /* 00002 * Copyright (C) 2003-2004 00003 * Slawomir Lisznianski <slisznianski@asyncnet.com> 00004 * 00005 * Permission to use, copy, modify, distribute and sell this software 00006 * and its documentation for any purpose is hereby granted without fee, 00007 * provided that the above copyright notice appear in all copies and 00008 * that both that copyright notice and this permission notice appear 00009 * in supporting documentation. Slawomir Lisznianski makes no 00010 * representations about the suitability of this software for any 00011 * purpose. It is provided "as is" without express or implied warranty. 00012 * 00013 */ 00014 00015 #ifndef RHA_SORTEDQUEUE_HPP 00016 #define RHA_SORTEDQUEUE_HPP 00017 00018 #include <deque> // For priority queue 00019 #include <algorithm> // For sorting. 00020 00021 namespace Rha 00022 { 00031 template<class T> 00032 class SortedQueue 00033 { 00034 public: 00035 typedef SortedQueue<T> OfType; 00036 typedef T Task; 00037 typedef T value_type; // stl compatibility 00038 00040 SortedQueue(); 00041 00047 ~SortedQueue(); 00048 00054 inline void setUp(); 00055 00061 inline bool empty() const; 00062 00067 inline bool full() const; 00068 00074 inline void flush(); 00075 00081 inline Task& front(); 00082 inline const Task& front() const; 00083 00088 inline void push(const Task& val); 00089 00094 inline void pop(); 00095 protected: 00096 typedef std::deque<Task> Container; 00097 Container M_container; 00098 }; 00099 } 00100 00101 template<class T> 00102 Rha::SortedQueue<T>::SortedQueue() 00103 { } 00104 00105 template<class T> 00106 Rha::SortedQueue<T>::~SortedQueue() 00107 { } 00108 00109 template<class T> 00110 void 00111 Rha::SortedQueue<T>::setUp() 00112 { } 00113 00114 template<class T> 00115 bool 00116 Rha::SortedQueue<T>::empty() const 00117 { 00118 return (M_container.empty()); 00119 } 00120 00121 template<class T> 00122 bool 00123 Rha::SortedQueue<T>::full() const 00124 { 00125 // Never full... 00126 return (false); 00127 } 00128 00129 template<class T> 00130 void 00131 Rha::SortedQueue<T>::flush() 00132 { 00133 M_container.clear(); 00134 } 00135 00136 template<class T> 00137 typename Rha::SortedQueue<T>::Task& 00138 Rha::SortedQueue<T>::front() 00139 { 00140 return (M_container.back()); 00141 } 00142 00143 template<class T> 00144 const typename Rha::SortedQueue<T>::Task& 00145 Rha::SortedQueue<T>::front() const 00146 { 00147 return (M_container.back()); 00148 } 00149 00150 template<class T> 00151 void 00152 Rha::SortedQueue<T>::push(const Task& val) 00153 { 00154 M_container.push_front(val); 00155 std::sort(M_container.begin(), M_container.end()); 00156 } 00157 00158 template<class T> 00159 void 00160 Rha::SortedQueue<T>::pop() 00161 { 00162 M_container.pop_back(); 00163 } 00164 00165 #endif // RHA_SORTEDQUEUE_HPP 00166