gravatar
kpeter (Peter Kovacs)
kpeter@inf.elte.hu
Fixes in the heap concept to avoid warnings (#180)
0 1 0
default
1 file changed with 42 insertions and 2 deletions:
↑ Collapse diff ↑
Ignore white space 768 line context
1 1
/* -*- mode: C++; indent-tabs-mode: nil; -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library.
4 4
 *
5 5
 * Copyright (C) 2003-2009
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_CONCEPTS_HEAP_H
20 20
#define LEMON_CONCEPTS_HEAP_H
21 21

	
22 22
///\ingroup concept
23 23
///\file
24 24
///\brief The concept of heaps.
25 25

	
26 26
#include <lemon/core.h>
27 27
#include <lemon/concept_check.h>
28 28

	
29 29
namespace lemon {
30 30

	
31 31
  namespace concepts {
32 32

	
33 33
    /// \addtogroup concept
34 34
    /// @{
35 35

	
36 36
    /// \brief The heap concept.
37 37
    ///
38 38
    /// This concept class describes the main interface of heaps.
39 39
    /// The various \ref heaps "heap structures" are efficient
40 40
    /// implementations of the abstract data type \e priority \e queue.
41 41
    /// They store items with specified values called \e priorities
42 42
    /// in such a way that finding and removing the item with minimum
43 43
    /// priority are efficient. The basic operations are adding and
44 44
    /// erasing items, changing the priority of an item, etc.
45 45
    ///
46 46
    /// Heaps are crucial in several algorithms, such as Dijkstra and Prim.
47 47
    /// Any class that conforms to this concept can be used easily in such
48 48
    /// algorithms.
49 49
    ///
50 50
    /// \tparam PR Type of the priorities of the items.
51 51
    /// \tparam IM A read-writable item map with \c int values, used
52 52
    /// internally to handle the cross references.
53 53
    /// \tparam CMP A functor class for comparing the priorities.
54 54
    /// The default is \c std::less<PR>.
55 55
#ifdef DOXYGEN
56 56
    template <typename PR, typename IM, typename CMP>
57 57
#else
58 58
    template <typename PR, typename IM, typename CMP = std::less<PR> >
59 59
#endif
60 60
    class Heap {
61 61
    public:
62 62

	
63 63
      /// Type of the item-int map.
64 64
      typedef IM ItemIntMap;
65 65
      /// Type of the priorities.
66 66
      typedef PR Prio;
67 67
      /// Type of the items stored in the heap.
68 68
      typedef typename ItemIntMap::Key Item;
69 69

	
70 70
      /// \brief Type to represent the states of the items.
71 71
      ///
72 72
      /// Each item has a state associated to it. It can be "in heap",
73 73
      /// "pre-heap" or "post-heap". The latter two are indifferent from the
74 74
      /// heap's point of view, but may be useful to the user.
75 75
      ///
76 76
      /// The item-int map must be initialized in such way that it assigns
77 77
      /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
78 78
      enum State {
79 79
        IN_HEAP = 0,    ///< = 0. The "in heap" state constant.
80 80
        PRE_HEAP = -1,  ///< = -1. The "pre-heap" state constant.
81 81
        POST_HEAP = -2  ///< = -2. The "post-heap" state constant.
82 82
      };
83 83

	
84 84
      /// \brief Constructor.
85 85
      ///
86 86
      /// Constructor.
87 87
      /// \param map A map that assigns \c int values to keys of type
88 88
      /// \c Item. It is used internally by the heap implementations to
89 89
      /// handle the cross references. The assigned value must be
90 90
      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
91
#ifdef DOXYGEN
91 92
      explicit Heap(ItemIntMap &map) {}
93
#else
94
      explicit Heap(ItemIntMap&) {}
95
#endif      
92 96

	
93 97
      /// \brief Constructor.
94 98
      ///
95 99
      /// Constructor.
96 100
      /// \param map A map that assigns \c int values to keys of type
97 101
      /// \c Item. It is used internally by the heap implementations to
98 102
      /// handle the cross references. The assigned value must be
99 103
      /// \c PRE_HEAP (<tt>-1</tt>) for each item.
100 104
      /// \param comp The function object used for comparing the priorities.
105
#ifdef DOXYGEN
101 106
      explicit Heap(ItemIntMap &map, const CMP &comp) {}
107
#else
108
      explicit Heap(ItemIntMap&, const CMP&) {}
109
#endif      
102 110

	
103 111
      /// \brief The number of items stored in the heap.
104 112
      ///
105 113
      /// This function returns the number of items stored in the heap.
106 114
      int size() const { return 0; }
107 115

	
108 116
      /// \brief Check if the heap is empty.
109 117
      ///
110 118
      /// This function returns \c true if the heap is empty.
111 119
      bool empty() const { return false; }
112 120

	
113 121
      /// \brief Make the heap empty.
114 122
      ///
115 123
      /// This functon makes the heap empty.
116 124
      /// It does not change the cross reference map. If you want to reuse
117 125
      /// a heap that is not surely empty, you should first clear it and
118 126
      /// then you should set the cross reference map to \c PRE_HEAP
119 127
      /// for each item.
120 128
      void clear() {}
121 129

	
122 130
      /// \brief Insert an item into the heap with the given priority.
123 131
      ///
124 132
      /// This function inserts the given item into the heap with the
125 133
      /// given priority.
126 134
      /// \param i The item to insert.
127 135
      /// \param p The priority of the item.
128 136
      /// \pre \e i must not be stored in the heap.
137
#ifdef DOXYGEN
129 138
      void push(const Item &i, const Prio &p) {}
139
#else
140
      void push(const Item&, const Prio&) {}
141
#endif      
130 142

	
131 143
      /// \brief Return the item having minimum priority.
132 144
      ///
133 145
      /// This function returns the item having minimum priority.
134 146
      /// \pre The heap must be non-empty.
135
      Item top() const {}
147
      Item top() const { return Item(); }
136 148

	
137 149
      /// \brief The minimum priority.
138 150
      ///
139 151
      /// This function returns the minimum priority.
140 152
      /// \pre The heap must be non-empty.
141
      Prio prio() const {}
153
      Prio prio() const { return Prio(); }
142 154

	
143 155
      /// \brief Remove the item having minimum priority.
144 156
      ///
145 157
      /// This function removes the item having minimum priority.
146 158
      /// \pre The heap must be non-empty.
147 159
      void pop() {}
148 160

	
149 161
      /// \brief Remove the given item from the heap.
150 162
      ///
151 163
      /// This function removes the given item from the heap if it is
152 164
      /// already stored.
153 165
      /// \param i The item to delete.
154 166
      /// \pre \e i must be in the heap.
167
#ifdef DOXYGEN
155 168
      void erase(const Item &i) {}
169
#else
170
      void erase(const Item&) {}
171
#endif      
156 172

	
157 173
      /// \brief The priority of the given item.
158 174
      ///
159 175
      /// This function returns the priority of the given item.
160 176
      /// \param i The item.
161 177
      /// \pre \e i must be in the heap.
178
#ifdef DOXYGEN
162 179
      Prio operator[](const Item &i) const {}
180
#else
181
      Prio operator[](const Item&) const { return Prio(); }
182
#endif      
163 183

	
164 184
      /// \brief Set the priority of an item or insert it, if it is
165 185
      /// not stored in the heap.
166 186
      ///
167 187
      /// This method sets the priority of the given item if it is
168 188
      /// already stored in the heap. Otherwise it inserts the given
169 189
      /// item into the heap with the given priority.
170 190
      ///
171 191
      /// \param i The item.
172 192
      /// \param p The priority.
193
#ifdef DOXYGEN
173 194
      void set(const Item &i, const Prio &p) {}
195
#else
196
      void set(const Item&, const Prio&) {}
197
#endif      
174 198

	
175 199
      /// \brief Decrease the priority of an item to the given value.
176 200
      ///
177 201
      /// This function decreases the priority of an item to the given value.
178 202
      /// \param i The item.
179 203
      /// \param p The priority.
180 204
      /// \pre \e i must be stored in the heap with priority at least \e p.
205
#ifdef DOXYGEN
181 206
      void decrease(const Item &i, const Prio &p) {}
207
#else
208
      void decrease(const Item&, const Prio&) {}
209
#endif      
182 210

	
183 211
      /// \brief Increase the priority of an item to the given value.
184 212
      ///
185 213
      /// This function increases the priority of an item to the given value.
186 214
      /// \param i The item.
187 215
      /// \param p The priority.
188 216
      /// \pre \e i must be stored in the heap with priority at most \e p.
217
#ifdef DOXYGEN
189 218
      void increase(const Item &i, const Prio &p) {}
219
#else
220
      void increase(const Item&, const Prio&) {}
221
#endif      
190 222

	
191 223
      /// \brief Return the state of an item.
192 224
      ///
193 225
      /// This method returns \c PRE_HEAP if the given item has never
194 226
      /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
195 227
      /// and \c POST_HEAP otherwise.
196 228
      /// In the latter case it is possible that the item will get back
197 229
      /// to the heap again.
198 230
      /// \param i The item.
231
#ifdef DOXYGEN
199 232
      State state(const Item &i) const {}
233
#else
234
      State state(const Item&) const { return PRE_HEAP; }
235
#endif      
200 236

	
201 237
      /// \brief Set the state of an item in the heap.
202 238
      ///
203 239
      /// This function sets the state of the given item in the heap.
204 240
      /// It can be used to manually clear the heap when it is important
205 241
      /// to achive better time complexity.
206 242
      /// \param i The item.
207 243
      /// \param st The state. It should not be \c IN_HEAP.
244
#ifdef DOXYGEN
208 245
      void state(const Item& i, State st) {}
246
#else
247
      void state(const Item&, State) {}
248
#endif      
209 249

	
210 250

	
211 251
      template <typename _Heap>
212 252
      struct Constraints {
213 253
      public:
214 254
        void constraints() {
215 255
          typedef typename _Heap::Item OwnItem;
216 256
          typedef typename _Heap::Prio OwnPrio;
217 257
          typedef typename _Heap::State OwnState;
218 258

	
219 259
          Item item;
220 260
          Prio prio;
221 261
          item=Item();
222 262
          prio=Prio();
223 263
          ignore_unused_variable_warning(item);
224 264
          ignore_unused_variable_warning(prio);
225 265

	
226 266
          OwnItem own_item;
227 267
          OwnPrio own_prio;
228 268
          OwnState own_state;
229 269
          own_item=Item();
230 270
          own_prio=Prio();
231 271
          ignore_unused_variable_warning(own_item);
232 272
          ignore_unused_variable_warning(own_prio);
233 273
          ignore_unused_variable_warning(own_state);
234 274

	
235 275
          _Heap heap1(map);
236 276
          _Heap heap2 = heap1;
237 277
          ignore_unused_variable_warning(heap1);
238 278
          ignore_unused_variable_warning(heap2);
239 279

	
240 280
          int s = heap.size();
241 281
          ignore_unused_variable_warning(s);
242 282
          bool e = heap.empty();
243 283
          ignore_unused_variable_warning(e);
244 284

	
245 285
          prio = heap.prio();
246 286
          item = heap.top();
247 287
          prio = heap[item];
248 288
          own_prio = heap.prio();
249 289
          own_item = heap.top();
250 290
          own_prio = heap[own_item];
251 291

	
252 292
          heap.push(item, prio);
253 293
          heap.push(own_item, own_prio);
254 294
          heap.pop();
255 295

	
256 296
          heap.set(item, prio);
257 297
          heap.decrease(item, prio);
258 298
          heap.increase(item, prio);
259 299
          heap.set(own_item, own_prio);
260 300
          heap.decrease(own_item, own_prio);
261 301
          heap.increase(own_item, own_prio);
262 302

	
263 303
          heap.erase(item);
264 304
          heap.erase(own_item);
265 305
          heap.clear();
266 306

	
267 307
          own_state = heap.state(own_item);
268 308
          heap.state(own_item, own_state);
269 309

	
270 310
          own_state = _Heap::PRE_HEAP;
271 311
          own_state = _Heap::IN_HEAP;
272 312
          own_state = _Heap::POST_HEAP;
273 313
        }
274 314

	
275 315
        _Heap& heap;
276 316
        ItemIntMap& map;
277 317
      };
278 318
    };
279 319

	
280 320
    /// @}
281 321
  } // namespace lemon
282 322
}
283 323
#endif
0 comments (0 inline)