00001 #ifndef GSGL_DATA_PQUEUE_H
00002 #define GSGL_DATA_PQUEUE_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "data/data.hpp"
00038 #include "data/indexable.hpp"
00039 #include "data/array.hpp"
00040
00041 namespace gsgl
00042 {
00043
00044 namespace data
00045 {
00046
00047 template <typename T, typename I>
00048 class pqueue_iterator;
00049
00050
00051
00052 template <typename T, typename I>
00053 class pqueue
00054 : public data_object, public indexable<T, gsgl::index_t>, public iterable<T, pqueue_iterator<T,I> >
00055 {
00056 friend class pqueue_iterator<T,I>;
00057
00058 data::simple_array<T> values;
00059 data::simple_array<I> priorities;
00060
00061 public:
00062 pqueue();
00063 pqueue(const pqueue &);
00064 pqueue & operator= (const pqueue &);
00065 virtual ~pqueue();
00066
00067
00068
00069 virtual gsgl::index_t size() const;
00070 virtual void clear();
00071
00072
00073
00074
00075 virtual const T & item(const gsgl::index_t &) const;
00076 virtual T & item(const gsgl::index_t &);
00077 virtual bool contains_index(const gsgl::index_t &) const;
00078
00079
00080
00081
00082 virtual void append(const T &);
00083 virtual void insert(const iterator &, const T &);
00084 virtual void remove(const iterator &);
00085
00086
00087
00088
00089 const T & get_head() const;
00090 T & get_head();
00091
00092 void push(const T &, const I &);
00093 void pop();
00094
00095 };
00096
00097
00098
00099
00100 template <typename T, typename I>
00101 pqueue<T,I>::pqueue()
00102 : data_object(), iterable<T, pqueue_iterator<T,I> >()
00103 {
00104 }
00105
00106
00107 template <typename T, typename I>
00108 pqueue<T,I>::pqueue(const pqueue & pq)
00109 : data_object(), iterable<T, pqueue_iterator<T,I> >(), values(pq.values), priorities(pq.priorities)
00110 {
00111 }
00112
00113
00114 template <typename T, typename I>
00115 pqueue<T,I> & pqueue<T,I>::operator= (const pqueue & pq)
00116 {
00117 values = pq.values;
00118 priorities = pq.priorities;
00119 return *this;
00120 }
00121
00122
00123 template <typename T, typename I>
00124 pqueue<T,I>::~pqueue()
00125 {
00126 }
00127
00128
00129
00130
00131 template <typename T, typename I>
00132 gsgl::index_t pqueue<T,I>::size() const
00133 {
00134 assert(values.size() == priorities.size());
00135 return values.size();
00136 }
00137
00138
00139 template <typename T, typename I>
00140 void pqueue<T,I>::clear()
00141 {
00142 values.clear();
00143 priorities.clear();
00144 }
00145
00146
00147
00148
00149 template <typename T, typename I>
00150 const T & pqueue<T,I>::item(const gsgl::index_t & index) const
00151 {
00152 return values.item(index);
00153 }
00154
00155
00156 template <typename T, typename I>
00157 T & pqueue<T,I>::item(const gsgl::index_t & index)
00158 {
00159 return values.item(index);
00160 }
00161
00162
00163 template <typename T, typename I>
00164 bool pqueue<T,I>::contains_index(const gsgl::index_t & index) const
00165 {
00166 return values.contains_index(index);
00167 }
00168
00169
00170
00171
00172 template <typename T, typename I>
00173 void pqueue<T,I>::append(const T & item)
00174 {
00175 push(item, I());
00176 }
00177
00178
00179 template <typename T, typename I>
00180 void pqueue<T,I>::insert(const iterator & i, const T & item)
00181 {
00182 push(item, priorities[i.position]);
00183 }
00184
00185
00186 template <typename T, typename I>
00187 void pqueue<T,I>::remove(const iterator & i)
00188 {
00189 values.remove(i.position);
00190 priorities.remove(i.position);
00191 }
00192
00193
00194 template <typename T, typename I>
00195 const T & pqueue<T,I>::get_head() const
00196 {
00197 try
00198 {
00199 return values[0];
00200 }
00201 catch (runtime_exception &)
00202 {
00203 throw memory_exception(__FILE__, __LINE__, L"Priority queue underflow.");
00204 }
00205 }
00206
00207
00208 template <typename T, typename I>
00209 T & pqueue<T,I>::get_head()
00210 {
00211 try
00212 {
00213 return values[0];
00214 }
00215 catch (runtime_exception &)
00216 {
00217 throw memory_exception(__FILE__, __LINE__, L"Priority queue underflow.");
00218 }
00219 }
00220
00221
00222 template <typename T, typename I>
00223 void pqueue<T,I>::push(const T & item, const I & priority)
00224 {
00225 gsgl::index_t i, sz = values.size();
00226 for (i = 0; i < sz; ++i)
00227 {
00228 if (priority >= priorities[i])
00229 break;
00230 }
00231 values.insert(item, i);
00232 priorities.insert(priority, i);
00233 }
00234
00235
00236 template <typename T, typename I>
00237 void pqueue<T,I>::pop()
00238 {
00239 if (values.size())
00240 {
00241 values.remove(0);
00242 priorities.remove(0);
00243 }
00244 else
00245 {
00246 throw memory_exception(__FILE__, __LINE__, L"Priority queue underflow in pop.");
00247 }
00248 }
00249
00250
00251
00252
00253 template <typename T, typename I>
00254 class pqueue_iterator
00255 {
00256 friend class pqueue<T, I>;
00257 const pqueue<T, I> & parent;
00258 gsgl::index_t position;
00259
00260 protected:
00261 pqueue_iterator(const iterable<T, pqueue_iterator<T,I> > & parent_iterable)
00262 : parent(dynamic_cast<const pqueue<T,I> &>(parent_iterable)), position(0) {}
00263
00264 pqueue_iterator(const pqueue_iterator & i)
00265 : parent(i.parent), position(i.position) {}
00266
00267 pqueue_iterator & operator= (const pqueue_iterator & i)
00268 {
00269 parent = i.parent;
00270 position = i.position;
00271 return *this;
00272 }
00273
00274 inline bool is_valid() const { return position < parent.values.size(); }
00275
00276 inline const T & operator* () const
00277 {
00278 if (position < parent.values.size())
00279 return parent.values[position];
00280 else
00281 throw memory_exception(__FILE__, __LINE__, L"Priority queue iterator overflow in iterator dereference.");
00282 }
00283
00284 inline void operator++ ()
00285 {
00286 if (position < parent.values.size())
00287 ++position;
00288 else
00289 throw memory_exception(__FILE__, __LINE__, L"Priority queue iterator overflow in iterator preincrement.");
00290 }
00291 };
00292
00293
00294 }
00295
00296 }
00297
00298 #endif