summaryrefslogtreecommitdiff
path: root/doc/html/heap/concepts.html
blob: 6b3a0ea3747377e33c2540995c64e459485fcb24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Concepts &amp; Interface</title>
<link rel="stylesheet" href="../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="../heap.html" title="Chapter&#160;11.&#160;Boost.Heap">
<link rel="prev" href="../heap.html" title="Chapter&#160;11.&#160;Boost.Heap">
<link rel="next" href="data_structures.html" title="Data Structures">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../heap.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../heap.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="data_structures.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="heap.concepts"></a><a class="link" href="concepts.html" title="Concepts &amp; Interface">Concepts &amp; Interface</a>
</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="concepts.html#heap.concepts.basic">Basic Priority Queue Interface</a></span></dt>
<dt><span class="section"><a href="concepts.html#heap.concepts.iterators">Priority Queue Iterators</a></span></dt>
<dt><span class="section"><a href="concepts.html#heap.concepts.comparing">Comparing Priority Queues &amp;
      Equivalence</a></span></dt>
<dt><span class="section"><a href="concepts.html#heap.concepts.merge">Merging Priority Queues</a></span></dt>
<dt><span class="section"><a href="concepts.html#heap.concepts.mutability">Mutability</a></span></dt>
<dt><span class="section"><a href="concepts.html#heap.concepts.stability">Stability</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="heap.concepts.basic"></a><a class="link" href="concepts.html#heap.concepts.basic" title="Basic Priority Queue Interface">Basic Priority Queue Interface</a>
</h3></div></div></div>
<p>
        Priority queues are queues of objects, that are ordered by their priority.
        They support the operations of adding nodes to the data structure, accessing
        the top element (the element with the highest priority), and removing the
        top element.
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          <code class="literal">boost.heap</code> implements priority queues as max-heaps to
          be consistent with the STL heap functions. This is in contrast to the typical
          textbook design, which uses min-heaps.
        </p></td></tr>
</table></div>
<h6>
<a name="heap.concepts.basic.h0"></a>
        <span><a name="heap.concepts.basic.synopsis"></a></span><a class="link" href="concepts.html#heap.concepts.basic.synopsis">Synopsis</a>
      </h6>
<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">T</span><span class="special">,</span> <span class="keyword">class</span> <span class="special">...</span><span class="identifier">Options</span><span class="special">&gt;</span>
<span class="keyword">class</span> <span class="identifier">priority_queue</span>
<span class="special">{</span>
    <span class="comment">// types</span>
    <span class="keyword">typedef</span> <span class="identifier">T</span>                   <span class="identifier">value_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">size_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">difference_type</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">allocator_type</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">value_compare</span><span class="special">;</span>

    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">reference</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">const_reference</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">pointer</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">const_pointer</span><span class="special">;</span>

    <span class="comment">// construct/copy/destruct</span>
    <span class="keyword">explicit</span> <span class="identifier">priority_queue</span><span class="special">(</span><span class="identifier">value_compare</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="special">=</span> <span class="identifier">value_compare</span><span class="special">());</span>
    <span class="identifier">priority_queue</span><span class="special">(</span><span class="identifier">priority_queue</span> <span class="keyword">const</span> <span class="special">&amp;);</span>
    <span class="identifier">priority_queue</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">priority_queue</span> <span class="keyword">const</span> <span class="special">&amp;);</span>
    <span class="identifier">priority_queue</span><span class="special">(</span><span class="identifier">priority_queue</span> <span class="special">&amp;&amp;);</span>                  <span class="comment">// move semantics (C++11 only)</span>
    <span class="identifier">priority_queue</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">=(</span><span class="identifier">priority_queue</span> <span class="special">&amp;&amp;);</span>       <span class="comment">// move semantics (C++11 only)</span>

    <span class="comment">// public member functions</span>
    <span class="emphasis"><em>unspecified</em></span> <span class="identifier">push</span><span class="special">(</span><span class="identifier">const_reference</span><span class="special">);</span>                  <span class="comment">// push new element to heap</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span><span class="special">...</span> <span class="identifier">Args</span><span class="special">&gt;</span> <span class="keyword">void</span> <span class="identifier">emplace</span><span class="special">(</span><span class="identifier">Args</span> <span class="special">&amp;&amp;...);</span>   <span class="comment">// push new element to heap, C++11 only</span>
    <span class="identifier">const_reference</span> <span class="identifier">top</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>                        <span class="comment">// return top element</span>
    <span class="keyword">void</span> <span class="identifier">pop</span><span class="special">();</span>                                         <span class="comment">// remove top element</span>
    <span class="keyword">void</span> <span class="identifier">clear</span><span class="special">();</span>                                       <span class="comment">// clear heap</span>
    <span class="identifier">size_type</span> <span class="identifier">size</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>                             <span class="comment">// number of elements</span>
    <span class="keyword">bool</span> <span class="identifier">empty</span><span class="special">()</span> <span class="keyword">const</span><span class="special">;</span>                                 <span class="comment">// priority queue is empty</span>
    <span class="identifier">allocator_type</span> <span class="identifier">get_allocator</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>           <span class="comment">// return allocator</span>
    <span class="identifier">size_type</span> <span class="identifier">max_size</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>                     <span class="comment">// maximal possible size</span>
    <span class="keyword">void</span> <span class="identifier">reserve</span><span class="special">(</span><span class="identifier">size_type</span><span class="special">);</span>                            <span class="comment">// reserve space, only available if (has_reserve == true)</span>

    <span class="comment">// heap equivalence</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">HeapType</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">==(</span><span class="identifier">HeapType</span> <span class="keyword">const</span> <span class="special">&amp;)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">HeapType</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">!=(</span><span class="identifier">HeapType</span> <span class="keyword">const</span> <span class="special">&amp;)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="comment">// heap comparison</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">HeapType</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span><span class="identifier">HeapType</span> <span class="keyword">const</span> <span class="special">&amp;)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">HeapType</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;(</span><span class="identifier">HeapType</span> <span class="keyword">const</span> <span class="special">&amp;)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">HeapType</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&gt;=(</span><span class="identifier">HeapType</span> <span class="keyword">const</span> <span class="special">&amp;)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">HeapType</span><span class="special">&gt;</span> <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;=(</span><span class="identifier">HeapType</span> <span class="keyword">const</span> <span class="special">&amp;)</span> <span class="keyword">const</span><span class="special">;</span>

    <span class="comment">// public data members</span>
    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">constant_time_size</span><span class="special">;</span>               <span class="comment">// size() has constant complexity</span>
    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">has_ordered_iterators</span><span class="special">;</span>            <span class="comment">// priority queue has <a class="link" href="concepts.html#heap.concepts.iterators" title="Priority Queue Iterators">ordered iterators</a></span>
    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">is_mergable</span><span class="special">;</span>                      <span class="comment">// priority queue is efficiently <a class="link" href="concepts.html#heap.concepts.merge" title="Merging Priority Queues">mergable</a></span>
    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">is_stable</span><span class="special">;</span>                        <span class="comment">// priority queue has a <a class="link" href="concepts.html#heap.concepts.stability" title="Stability">stable heap order</a></span>
    <span class="keyword">static</span> <span class="keyword">const</span> <span class="keyword">bool</span> <span class="identifier">has_reserve</span><span class="special">;</span>                      <span class="comment">// priority queue has a reserve() member</span>
<span class="special">};</span>
</pre>
<h6>
<a name="heap.concepts.basic.h1"></a>
        <span><a name="heap.concepts.basic.example"></a></span><a class="link" href="concepts.html#heap.concepts.basic.example">Example</a>
      </h6>
<p>
</p>
<pre class="programlisting"><span class="comment">// PriorityQueue is expected to be a max-heap of integer values</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">basic_interface</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">PriorityQueue</span> <span class="identifier">pq</span><span class="special">;</span>

    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Priority Queue: popped elements"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 3</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 2</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 1</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="heap.concepts.iterators"></a><a class="link" href="concepts.html#heap.concepts.iterators" title="Priority Queue Iterators">Priority Queue Iterators</a>
</h3></div></div></div>
<div class="toc"><dl><dt><span class="section"><a href="concepts.html#heap.concepts.iterators.ordered_iterators">Ordered
        Iterators</a></span></dt></dl></div>
<h6>
<a name="heap.concepts.iterators.h0"></a>
        <span><a name="heap.concepts.iterators.synopsis"></a></span><a class="link" href="concepts.html#heap.concepts.iterators.synopsis">Synopsis</a>
      </h6>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">iteratable_heap_interface</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">// types</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">const_iterator</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span>         <span class="identifier">ordered_iterator</span><span class="special">;</span>

    <span class="comment">// public member functions</span>
    <span class="identifier">iterator</span> <span class="identifier">begin</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="identifier">iterator</span> <span class="identifier">end</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="identifier">ordered_iterator</span> <span class="identifier">ordered_begin</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="identifier">ordered_iterator</span> <span class="identifier">ordered_end</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span> <span class="keyword">const</span><span class="special">;</span>
<span class="special">};</span>
</pre>
<p>
        Priority queues provide iterators, that can be used to traverse their elements.
        All heap iterators are const_iterators, that means they cannot be used to
        modify the values, because changing the value of a heap node may corrupt
        the heap order. Details about modifying heap nodes are described in the section
        about the <a class="link" href="concepts.html#heap.concepts.mutability" title="Mutability">mutability interface</a>.
      </p>
<p>
        Iterators do not visit heap elements in any specific order. Unless otherwise
        noted, all non-const heap member functions invalidate iterators, while all
        const member functions preserve the iterator validity.
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          Some implementations require iterators, that contain a set of elements,
          that are <span class="bold"><strong>discovered</strong></span>, but not <span class="bold"><strong>visited</strong></span>. Therefore copying iterators can be inefficient
          and should be avoided.
        </p></td></tr>
</table></div>
<h6>
<a name="heap.concepts.iterators.h1"></a>
        <span><a name="heap.concepts.iterators.example"></a></span><a class="link" href="concepts.html#heap.concepts.iterators.example">Example</a>
      </h6>
<p>
</p>
<pre class="programlisting"><span class="comment">// PriorityQueue is expected to be a max-heap of integer values</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">iterator_interface</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">PriorityQueue</span> <span class="identifier">pq</span><span class="special">;</span>

    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>

    <span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">begin</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">begin</span><span class="special">();</span>
    <span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">end</span><span class="special">();</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Priority Queue: iteration"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">begin</span><span class="special">;</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">end</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">)</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">it</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 1, 2, 3 in unspecified order</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="heap.concepts.iterators.ordered_iterators"></a><a class="link" href="concepts.html#heap.concepts.iterators.ordered_iterators" title="Ordered Iterators">Ordered
        Iterators</a>
</h4></div></div></div>
<p>
          Except for <code class="computeroutput"><a class="link" href="../boost/heap/priority_queue.html" title="Class template priority_queue">boost::heap::priority_queue</a></code>
          all <code class="literal">boost.heap</code> data structures support ordered iterators,
          which visit all elements of the heap in heap-order. The implementation
          of these <code class="literal">ordered_iterator</code>s requires some internal bookkeeping,
          so iterating the a heap in heap order has an amortized complexity of O(N*log(N)).
        </p>
<h6>
<a name="heap.concepts.iterators.ordered_iterators.h0"></a>
          <span><a name="heap.concepts.iterators.ordered_iterators.example"></a></span><a class="link" href="concepts.html#heap.concepts.iterators.ordered_iterators.example">Example</a>
        </h6>
<p>
</p>
<pre class="programlisting"><span class="comment">// PriorityQueue is expected to be a max-heap of integer values</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">ordered_iterator_interface</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">PriorityQueue</span> <span class="identifier">pq</span><span class="special">;</span>

    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>

    <span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">ordered_iterator</span> <span class="identifier">begin</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">ordered_begin</span><span class="special">();</span>
    <span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">ordered_iterator</span> <span class="identifier">end</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">ordered_end</span><span class="special">();</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Priority Queue: ordered iteration"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
    <span class="keyword">for</span> <span class="special">(</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">ordered_iterator</span> <span class="identifier">it</span> <span class="special">=</span> <span class="identifier">begin</span><span class="special">;</span> <span class="identifier">it</span> <span class="special">!=</span> <span class="identifier">end</span><span class="special">;</span> <span class="special">++</span><span class="identifier">it</span><span class="special">)</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="special">*</span><span class="identifier">it</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 3, 2, 1 (i.e. 1, 2, 3 in heap order)</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
        </p>
</div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="heap.concepts.comparing"></a><a class="link" href="concepts.html#heap.concepts.comparing" title="Comparing Priority Queues &amp; Equivalence">Comparing Priority Queues &amp;
      Equivalence</a>
</h3></div></div></div>
<p>
        The data structures of <code class="literal">boost.heap</code> can be compared with
        standard comparison operators. The comparison is performed by comparing two
        heaps element by element using <code class="literal">value_compare</code>.
      </p>
<div class="note"><table border="0" summary="Note">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../doc/src/images/note.png"></td>
<th align="left">Note</th>
</tr>
<tr><td align="left" valign="top"><p>
          Depending on the heap type, this operation can be rather expensive, because
          both data structures need to be traversed in heap order. On heaps without
          ordered iterators, the heap needs to be copied internally. The typical
          complexity is O(n log(n)).
        </p></td></tr>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="heap.concepts.merge"></a><a class="link" href="concepts.html#heap.concepts.merge" title="Merging Priority Queues">Merging Priority Queues</a>
</h3></div></div></div>
<h4>
<a name="heap.concepts.merge.h0"></a>
        <span><a name="heap.concepts.merge.mergable_priority_queues"></a></span><a class="link" href="concepts.html#heap.concepts.merge.mergable_priority_queues">Mergable
        Priority Queues</a>
      </h4>
<h6>
<a name="heap.concepts.merge.h1"></a>
        <span><a name="heap.concepts.merge.synopsis"></a></span><a class="link" href="concepts.html#heap.concepts.merge.synopsis">Synopsis</a>
      </h6>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">mergable_heap_interface</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="comment">// public member functions</span>
    <span class="keyword">void</span> <span class="identifier">merge</span><span class="special">(</span><span class="identifier">mergable_heap_interface</span> <span class="special">&amp;);</span>
<span class="special">};</span>
</pre>
<p>
        <code class="literal">boost.heap</code> has a concept of a Mergable Priority Queue.
        A mergable priority queue can efficiently be merged with a different instance
        of the same type.
      </p>
<h6>
<a name="heap.concepts.merge.h2"></a>
        <span><a name="heap.concepts.merge.example"></a></span><a class="link" href="concepts.html#heap.concepts.merge.example">Example</a>
      </h6>
<p>
</p>
<pre class="programlisting"><span class="comment">// PriorityQueue is expected to be a max-heap of integer values</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">merge_interface</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">PriorityQueue</span> <span class="identifier">pq</span><span class="special">;</span>

    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>

    <span class="identifier">PriorityQueue</span> <span class="identifier">pq2</span><span class="special">;</span>

    <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
    <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">4</span><span class="special">);</span>
    <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>

    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">merge</span><span class="special">(</span><span class="identifier">pq2</span><span class="special">);</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Priority Queue: merge"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"first queue: "</span><span class="special">;</span>
    <span class="keyword">while</span> <span class="special">(!</span><span class="identifier">pq</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span> <span class="special">{</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 5 4 3 2 1 0</span>
        <span class="identifier">pq</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="special">}</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"second queue: "</span><span class="special">;</span>
    <span class="keyword">while</span> <span class="special">(!</span><span class="identifier">pq2</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span> <span class="special">{</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 4 2 0</span>
        <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="special">}</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<h4>
<a name="heap.concepts.merge.h3"></a>
        <span><a name="heap.concepts.merge.heap_merge_algorithms"></a></span><a class="link" href="concepts.html#heap.concepts.merge.heap_merge_algorithms">Heap
        Merge Algorithms</a>
      </h4>
<p>
        <code class="literal">boost.heap</code> provides a <code class="literal">heap_merge()</code>
        algorithm that is can be used to merge different kinds of heaps. Using this
        algorithm, all <code class="literal">boost.heap</code> data structures can be merged,
        although some cannot be merged efficiently.
      </p>
<h6>
<a name="heap.concepts.merge.h4"></a>
        <span><a name="heap.concepts.merge.example0"></a></span><a class="link" href="concepts.html#heap.concepts.merge.example0">Example</a>
      </h6>
<p>
</p>
<pre class="programlisting"><span class="comment">// PriorityQueue is expected to be a max-heap of integer values</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">heap_merge_algorithm</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">PriorityQueue</span> <span class="identifier">pq</span><span class="special">;</span>

    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>

    <span class="identifier">PriorityQueue</span> <span class="identifier">pq2</span><span class="special">;</span>

    <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>
    <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">4</span><span class="special">);</span>
    <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">0</span><span class="special">);</span>

    <span class="identifier">boost</span><span class="special">::</span><span class="identifier">heap</span><span class="special">::</span><span class="identifier">heap_merge</span><span class="special">(</span><span class="identifier">pq</span><span class="special">,</span> <span class="identifier">pq2</span><span class="special">);</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Priority Queue: merge"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"first queue: "</span><span class="special">;</span>
    <span class="keyword">while</span> <span class="special">(!</span><span class="identifier">pq</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span> <span class="special">{</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 5 4 3 2 1 0</span>
        <span class="identifier">pq</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="special">}</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"second queue: "</span><span class="special">;</span>
    <span class="keyword">while</span> <span class="special">(!</span><span class="identifier">pq2</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span> <span class="special">{</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 4 2 0</span>
        <span class="identifier">pq2</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="special">}</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="heap.concepts.mutability"></a><a class="link" href="concepts.html#heap.concepts.mutability" title="Mutability">Mutability</a>
</h3></div></div></div>
<p>
        Some priority queues of <code class="literal">boost.heap</code> are mutable, that means
        the priority of their elements can be changed. To achieve mutability, <code class="literal">boost.heap</code>
        introduces the concept of <span class="bold"><strong>handles</strong></span>, which
        can be used to access the internal nodes of the priority queue in order to
        change its value and to restore the heap order.
      </p>
<h6>
<a name="heap.concepts.mutability.h0"></a>
        <span><a name="heap.concepts.mutability.synopsis"></a></span><a class="link" href="concepts.html#heap.concepts.mutability.synopsis">Synopsis</a>
      </h6>
<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">mutable_heap_interface</span>
<span class="special">{</span>
<span class="keyword">public</span><span class="special">:</span>
    <span class="keyword">typedef</span> <span class="emphasis"><em>unspecified</em></span> <span class="identifier">iterator</span><span class="special">;</span>
    <span class="keyword">struct</span> <span class="identifier">handle_type</span>
    <span class="special">{</span>
        <span class="identifier">value_type</span> <span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">*()</span> <span class="keyword">const</span><span class="special">;</span>
    <span class="special">};</span>

    <span class="keyword">static</span> <span class="identifier">handle_type</span> <span class="identifier">s_iterator_to_handle</span><span class="special">(</span><span class="identifier">iterator</span> <span class="keyword">const</span> <span class="special">&amp;);</span>

    <span class="comment">// priority queue interface</span>
    <span class="identifier">handle_type</span> <span class="identifier">push</span><span class="special">(</span><span class="identifier">T</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v</span><span class="special">);</span>

    <span class="comment">// update element via assignment and fix heap</span>
    <span class="keyword">void</span> <span class="identifier">update</span><span class="special">(</span><span class="identifier">handle_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">handle</span><span class="special">,</span> <span class="identifier">value_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v</span><span class="special">);</span>
    <span class="keyword">void</span> <span class="identifier">increase</span><span class="special">(</span><span class="identifier">handle_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">handle</span><span class="special">,</span> <span class="identifier">value_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v</span><span class="special">);</span>
    <span class="keyword">void</span> <span class="identifier">decrease</span><span class="special">(</span><span class="identifier">handle_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">handle</span><span class="special">,</span> <span class="identifier">value_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">v</span><span class="special">);</span>

    <span class="comment">// fix heap after element has been changed via the handle</span>
    <span class="keyword">void</span> <span class="identifier">update</span><span class="special">(</span><span class="identifier">handle_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">handle</span><span class="special">);</span>
    <span class="keyword">void</span> <span class="identifier">increase</span><span class="special">(</span><span class="identifier">handle_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">handle</span><span class="special">);</span>
    <span class="keyword">void</span> <span class="identifier">decrease</span><span class="special">(</span><span class="identifier">handle_type</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">handle</span><span class="special">);</span>
<span class="special">};</span>
</pre>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
          Incorrect use of <code class="literal">increase</code> or <code class="literal">decrease</code>
          may corrupt the priority queue data structure. If unsure use <code class="literal">update</code>
          can be used at the cost of efficiency.
        </p></td></tr>
</table></div>
<h6>
<a name="heap.concepts.mutability.h1"></a>
        <span><a name="heap.concepts.mutability.example"></a></span><a class="link" href="concepts.html#heap.concepts.mutability.example">Example</a>
      </h6>
<p>
</p>
<pre class="programlisting"><span class="comment">// PriorityQueue is expected to be a max-heap of integer values</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">mutable_interface</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">PriorityQueue</span> <span class="identifier">pq</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">handle_type</span> <span class="identifier">handle_t</span><span class="special">;</span>

    <span class="identifier">handle_t</span> <span class="identifier">t3</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">handle_t</span> <span class="identifier">t5</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
    <span class="identifier">handle_t</span> <span class="identifier">t1</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>

    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">update</span><span class="special">(</span><span class="identifier">t3</span><span class="special">,</span> <span class="number">4</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">increase</span><span class="special">(</span><span class="identifier">t5</span><span class="special">,</span> <span class="number">7</span><span class="special">);</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">decrease</span><span class="special">(</span><span class="identifier">t1</span><span class="special">,</span> <span class="number">0</span><span class="special">);</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Priority Queue: update"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
    <span class="keyword">while</span> <span class="special">(!</span><span class="identifier">pq</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span> <span class="special">{</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 7, 4, 0</span>
        <span class="identifier">pq</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="special">}</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<p>
        Note that handles can be stored inside the <code class="literal">value_type</code>:
      </p>
<p>
</p>
<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">heap_data</span>
<span class="special">{</span>
    <span class="identifier">fibonacci_heap</span><span class="special">&lt;</span><span class="identifier">heap_data</span><span class="special">&gt;::</span><span class="identifier">handle_type</span> <span class="identifier">handle</span><span class="special">;</span>
    <span class="keyword">int</span> <span class="identifier">payload</span><span class="special">;</span>

    <span class="identifier">heap_data</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">i</span><span class="special">):</span>
        <span class="identifier">payload</span><span class="special">(</span><span class="identifier">i</span><span class="special">)</span>
    <span class="special">{}</span>

    <span class="keyword">bool</span> <span class="keyword">operator</span><span class="special">&lt;(</span><span class="identifier">heap_data</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">rhs</span><span class="special">)</span> <span class="keyword">const</span>
    <span class="special">{</span>
        <span class="keyword">return</span> <span class="identifier">payload</span> <span class="special">&lt;</span> <span class="identifier">rhs</span><span class="special">.</span><span class="identifier">payload</span><span class="special">;</span>
    <span class="special">}</span>
<span class="special">};</span>

<span class="keyword">void</span> <span class="identifier">mutable_interface_handle_in_value</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">fibonacci_heap</span><span class="special">&lt;</span><span class="identifier">heap_data</span><span class="special">&gt;</span> <span class="identifier">heap</span><span class="special">;</span>
    <span class="identifier">heap_data</span> <span class="identifier">f</span><span class="special">(</span><span class="number">2</span><span class="special">);</span>

    <span class="identifier">fibonacci_heap</span><span class="special">&lt;</span><span class="identifier">heap_data</span><span class="special">&gt;::</span><span class="identifier">handle_type</span> <span class="identifier">handle</span> <span class="special">=</span> <span class="identifier">heap</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="identifier">f</span><span class="special">);</span>
    <span class="special">(*</span><span class="identifier">handle</span><span class="special">).</span><span class="identifier">handle</span> <span class="special">=</span> <span class="identifier">handle</span><span class="special">;</span> <span class="comment">// store handle in node</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<h4>
<a name="heap.concepts.mutability.h2"></a>
        <span><a name="heap.concepts.mutability.the_fixup_interface"></a></span><a class="link" href="concepts.html#heap.concepts.mutability.the_fixup_interface">The
        Fixup Interface</a>
      </h4>
<p>
        There are two different APIs to support mutability. The first family of functions
        provides update functionality by changing the current element by assigning
        a new value. The second family of functions can be used to fix the heap data
        structure after an element has been changed directly via a handle. While
        this provides the user with a means to modify the priority of queue elements
        without the need to change their non-priority part, this needs to be handled
        with care. The heap needs to be fixed up immediately after the priority of
        the element has been changed.
      </p>
<p>
        Beside an <code class="literal">update</code> function, two additional functions <code class="literal">increase</code>
        and <code class="literal">decrease</code> are provided, that are generally more efficient
        than the generic <code class="literal">update</code> function. However the user has
        do ensure, that the priority of an element is changed to the right direction.
      </p>
<h6>
<a name="heap.concepts.mutability.h3"></a>
        <span><a name="heap.concepts.mutability.example0"></a></span><a class="link" href="concepts.html#heap.concepts.mutability.example0">Example</a>
      </h6>
<p>
</p>
<pre class="programlisting"><span class="comment">// PriorityQueue is expected to be a max-heap of integer values</span>
<span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">&gt;</span>
<span class="keyword">void</span> <span class="identifier">mutable_fixup_interface</span><span class="special">(</span><span class="keyword">void</span><span class="special">)</span>
<span class="special">{</span>
    <span class="identifier">PriorityQueue</span> <span class="identifier">pq</span><span class="special">;</span>
    <span class="keyword">typedef</span> <span class="keyword">typename</span> <span class="identifier">PriorityQueue</span><span class="special">::</span><span class="identifier">handle_type</span> <span class="identifier">handle_t</span><span class="special">;</span>

    <span class="identifier">handle_t</span> <span class="identifier">t3</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">3</span><span class="special">);</span>
    <span class="identifier">handle_t</span> <span class="identifier">t5</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">5</span><span class="special">);</span>
    <span class="identifier">handle_t</span> <span class="identifier">t1</span> <span class="special">=</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">push</span><span class="special">(</span><span class="number">1</span><span class="special">);</span>

    <span class="special">*</span><span class="identifier">t3</span> <span class="special">=</span> <span class="number">4</span><span class="special">;</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">update</span><span class="special">(</span><span class="identifier">t3</span><span class="special">);</span>

    <span class="special">*</span><span class="identifier">t5</span> <span class="special">=</span> <span class="number">7</span><span class="special">;</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">increase</span><span class="special">(</span><span class="identifier">t5</span><span class="special">);</span>

    <span class="special">*</span><span class="identifier">t1</span> <span class="special">=</span> <span class="number">0</span><span class="special">;</span>
    <span class="identifier">pq</span><span class="special">.</span><span class="identifier">decrease</span><span class="special">(</span><span class="identifier">t1</span><span class="special">);</span>

    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="string">"Priority Queue: update with fixup"</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
    <span class="keyword">while</span> <span class="special">(!</span><span class="identifier">pq</span><span class="special">.</span><span class="identifier">empty</span><span class="special">())</span> <span class="special">{</span>
        <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">pq</span><span class="special">.</span><span class="identifier">top</span><span class="special">()</span> <span class="special">&lt;&lt;</span> <span class="string">" "</span><span class="special">;</span> <span class="comment">// 7, 4, 0</span>
        <span class="identifier">pq</span><span class="special">.</span><span class="identifier">pop</span><span class="special">();</span>
    <span class="special">}</span>
    <span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">endl</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
      </p>
<p>
        Iterators can be coverted to handles using the static member function <code class="literal">s_handle_from_iterator</code>.
        However most implementations of <code class="literal">update</code> invalidate all
        iterators. The most notable exception is the <code class="computeroutput"><a class="link" href="../boost/heap/fibonacci_heap.html" title="Class template fibonacci_heap">fibonacci
        heap</a></code>, providing a lazy update function, that just invalidates
        the iterators, that are related to this handle.
      </p>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
          After changing the priority via a handle, the heap needs to be fixed by
          calling one of the update functions. Otherwise the priority queue structure
          may be corrupted!
        </p></td></tr>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="heap.concepts.stability"></a><a class="link" href="concepts.html#heap.concepts.stability" title="Stability">Stability</a>
</h3></div></div></div>
<p>
        A priority queue is `stable', if elements with the same priority are popped
        from the heap, in the same order as they are inserted. The data structures
        provided by <code class="literal">boost.heap</code>, can be configured to be stable
        at compile time using the <code class="computeroutput"><a class="link" href="../boost/heap/stable.html" title="Struct template stable">boost::heap::stable</a></code>
        policy. Two notions of stability are supported. If a heap is configured with
        <span class="bold"><strong>no stability</strong></span>, the order of nodes of the
        same priority is undefined, if it is configured as <span class="bold"><strong>stable</strong></span>,
        nodes of the same priority are ordered by their insertion time.
      </p>
<p>
        Stability is achieved by associating an integer version count with each value
        in order to distinguish values with the same node. The type of this version
        count defaults to <code class="literal">boost::uintmax_t</code>, which is at least
        64bit on most systems. However it can be configured to use a different type
        using the <code class="computeroutput"><a class="link" href="../boost/heap/stability_counter_type.html" title="Struct template stability_counter_type">boost::heap::stability_counter_type</a></code>
        template argument.
      </p>
<div class="warning"><table border="0" summary="Warning">
<tr>
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Warning]" src="../../../doc/src/images/warning.png"></td>
<th align="left">Warning</th>
</tr>
<tr><td align="left" valign="top"><p>
          The stability counter is prone to integer overflows. If an overflow occurs
          during a <code class="literal">push()</code> call, the operation will fail and an
          exception is thrown. Later <code class="literal">push()</code> call will succeed,
          but the stable heap order will be compromised. However an integer overflow
          at 64bit is very unlikely: if an application would issue one <code class="literal">push()</code>
          operation per microsecond, the value will overflow in more than 500000
          years.
        </p></td></tr>
</table></div>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2010, 2011 Tim Blechmann<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../heap.html"><img src="../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../heap.html"><img src="../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="data_structures.html"><img src="../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>