Simple Application Framework
1
|
00001 /* 00002 This file is part of Simple Application Framework (Saf) library. 00003 Copyright (C) 2010 - 2012 Ondrej Danek <ondrej.danek@gmail.com> 00004 00005 This library is free software: you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published 00007 by the Free Software Foundation, either version 3 of the License, or 00008 (at your option) any later version. 00009 00010 Saf is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Simple Application Framework library. If not, 00017 see <http://www.gnu.org/licenses/>. 00018 */ 00019 00028 #ifndef SAF_COLLECTION_PRIORITYQUEUE_H 00029 #define SAF_COLLECTION_PRIORITYQUEUE_H 00030 00031 #include "../Type.h" 00032 #include "../Algo/Swap.h" 00033 #include "../Algo/Predicate.h" 00034 #include "../Algo/Struct/Heap.h" 00035 #include "DynArray.h" 00036 00037 namespace Saf 00038 { 00039 namespace Collection 00040 { 00060 template <class ValType, class CompType = Algo::Predicate::Less<ValType>, 00061 class ContainerType = DynArray<ValType> > 00062 class PriorityQueue 00063 { 00064 protected: 00065 typedef PriorityQueue<ValType,CompType,ContainerType> MyType; 00066 00067 private: 00069 ContainerType m_storage; 00071 CompType m_pred; 00072 00073 public: 00075 PriorityQueue() 00076 {} 00077 00079 PriorityQueue(const MyType& queue) 00080 : m_storage(queue.m_storage), m_pred(queue.m_pred) 00081 {} 00082 00088 explicit PriorityQueue(const ContainerType& cont) 00089 : m_storage(cont) 00090 { 00091 Algo::Struct::Heap::MakeHeap(m_storage.Begin(), m_storage.End(), m_pred); 00092 } 00093 00095 MyType& operator=(MyType& queue) 00096 { 00097 m_storage = queue.m_storage; 00098 m_pred = queue.m_pred; 00099 return *this; 00100 } 00101 00107 MyType& Swap(MyType& queue) 00108 { 00109 Algo::Swap(m_storage, queue.m_storage); 00110 Algo::Swap(m_pred, queue.m_pred); 00111 return *this; 00112 } 00113 00119 bool IsEmpty() const 00120 { 00121 return m_storage.IsEmpty(); 00122 } 00123 00129 Size Elements() const 00130 { 00131 return m_storage.Elements(); 00132 } 00133 00135 ValType& Top() 00136 { 00137 return *m_storage.Front(); 00138 } 00139 00141 const ValType& Top() const 00142 { 00143 return *m_storage.Front(); 00144 } 00145 00151 MyType& Push(const ValType& val) 00152 { 00153 m_storage.PushBack(val); 00154 Algo::Struct::Heap::PushHeap(m_storage.Begin(), m_storage.End(), m_pred); 00155 return *this; 00156 } 00157 00163 MyType& Pop() 00164 { 00165 Algo::Struct::Heap::PopHeap(m_storage.Begin(), m_storage.End(), m_pred); 00166 m_storage.PopBack(); 00167 return *this; 00168 } 00169 00171 const ContainerType& Storage() 00172 { 00173 return m_storage; 00174 } 00175 }; 00176 } 00177 00179 namespace Algo 00180 { 00181 // Swap specialization for a priority queue container. 00182 template <class ValType, class CompType, class ContainerType> 00183 struct SwapFunc< Collection::PriorityQueue<ValType,CompType,ContainerType> > 00184 { 00185 void operator()(Collection::PriorityQueue<ValType,CompType,ContainerType>& x, 00186 Collection::PriorityQueue<ValType,CompType,ContainerType>& y) 00187 { 00188 x.Swap(y); 00189 } 00190 }; 00191 } 00193 } 00194 00195 #endif