[prev in list] [next in list] [prev in thread] [next in thread]
List: boost-users
Subject: [Boost-users] [Boost.Heap] Possible bug with ordered iterator and boost::heap::binomial_heap
From: "Kudo, Jun via Boost-users" <boost-users () lists ! boost ! org>
Date: 2017-06-08 20:02:38
Message-ID: 48D98FFA3A072E4CA7F700E600952E26C0B8C6DF () ASC-EXHMCHP02 ! ASC ! LOCAL
[Download RAW message or body]
[Attachment #2 (multipart/alternative)]
Hi all,
I'm observing a weird phenomenon when iterating over a binomial heap after pushing \
two values with the same priority. Instead of printing the expected values twice, \
the ordered iterator seems to print three values. I do not observe this behavior \
when using a Fibonacci heap or a binary heap. I've attached sample code that \
demonstrates this behavior. Can anyone help explain what's going on?
Boost Version : Boost 1.64
Compiler : GCC 5.3.0
#include <iostream>
#include "boost/heap/fibonacci_heap.hpp"
#include "boost/heap/binomial_heap.hpp"
#include "boost/heap/d_ary_heap.hpp"
template <class HeapType>
void PrintHeap(HeapType const & heap) {
std::cout << "Printing out heap : ";
for (auto it = heap.begin(); it != heap.end(); ++it) {
std::cout << *it << " ";
}
std::cout << "\n Printing out heap in order : ";
for (auto it = heap.ordered_begin(); it != heap.ordered_end(); ++it) {
std::cout << *it << " ";
}
std::cout << "\n\n";
}
int main(int /* argc */, char** /* argv */) {
typedef boost::heap::binomial_heap<int> BinomialHeap;
BinomialHeap binomial_heap;
binomial_heap.push(1);
binomial_heap.push(1);
// Prints {1,1} and {1,1,1}
PrintHeap(binomial_heap);
typedef boost::heap::fibonacci_heap<int> FibonacciHeap;
FibonacciHeap fibonacci_heap;
fibonacci_heap.push(1);
fibonacci_heap.push(1);
// Prints {1,1} and {1,1}
PrintHeap(fibonacci_heap);
typedef boost::heap::d_ary_heap<int, boost::heap::arity<2>, \
boost::heap::mutable_<true>> BinaryHeap; BinaryHeap binary_heap;
binary_heap.push(1);
binary_heap.push(1);
// Prints {1,1} and {1,1}
PrintHeap(binary_heap);
}
Thanks!
Jun
[Attachment #5 (text/html)]
<html xmlns:v="urn:schemas-microsoft-com:vml" \
xmlns:o="urn:schemas-microsoft-com:office:office" \
xmlns:w="urn:schemas-microsoft-com:office:word" \
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" \
xmlns="http://www.w3.org/TR/REC-html40"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hi all,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I’m observing a weird phenomenon when iterating over a \
binomial heap after pushing two values with the same priority. Instead of \
printing the expected values twice, the ordered iterator seems to print three \
values. I do not observe this behavior when using a Fibonacci heap or a binary \
heap. I’ve attached sample code that demonstrates this behavior. \
Can anyone help explain what’s going on?<o:p></o:p></p> <p \
class="MsoNormal"><o:p> </o:p></p> <p class="MsoNormal">Boost Version : Boost \
1.64<o:p></o:p></p> <p class="MsoNormal">Compiler : GCC 5.3.0<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:"Courier New"">#include \
<iostream><o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New"">#include "boost/heap/fibonacci_heap.hpp"<o:p></o:p></span></p> \
<p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:"Courier New"">#include \
"boost/heap/binomial_heap.hpp"<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New"">#include "boost/heap/d_ary_heap.hpp"<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:"Courier \
New""><o:p> </o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New"">template <class HeapType><o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New"">void \
PrintHeap(HeapType const & heap) {<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
std::cout << "Printing out heap : ";<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:"Courier \
New""> for (auto it = heap.begin(); it != heap.end(); ++it) \
{<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:"Courier New""> std::cout << *it \
<< " ";<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
}<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:"Courier New""> std::cout << "\n \
Printing out heap in order : ";<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
for (auto it = heap.ordered_begin(); it != heap.ordered_end(); ++it) \
{<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:"Courier New""> std::cout << *it \
<< " ";<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
}<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:"Courier New""> std::cout << \
"\n\n";<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New"">}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New""><o:p> </o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New"">int \
main(int /* argc */, char** /* argv */) {<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
typedef boost::heap::binomial_heap<int> BinomialHeap;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:"Courier \
New""> BinomialHeap binomial_heap;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:"Courier \
New""> binomial_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
binomial_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
// Prints {1,1} and {1,1,1}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
PrintHeap(binomial_heap);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New""><o:p> </o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
typedef boost::heap::fibonacci_heap<int> FibonacciHeap;<o:p></o:p></span></p> \
<p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:"Courier New""> FibonacciHeap \
fibonacci_heap;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
fibonacci_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
fibonacci_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
// Prints {1,1} and {1,1}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
PrintHeap(fibonacci_heap);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New""><o:p> </o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
typedef boost::heap::d_ary_heap<int, boost::heap::arity<2>, \
boost::heap::mutable_<true>> BinaryHeap;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:"Courier \
New""> BinaryHeap binary_heap;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
binary_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
binary_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
// Prints {1,1} and {1,1}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier New""> \
PrintHeap(binary_heap);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:"Courier \
New"">}<o:p></o:p></span></p> <p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Thanks!<o:p></o:p></p>
<p class="MsoNormal"><b><span \
style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#4A442A">Jun</span></b><o:p></o:p></p>
</div>
</body>
</html>
["BoostHeapTest.cpp" (text/plain)]
#include <iostream>
#include "boost/heap/fibonacci_heap.hpp"
#include "boost/heap/binomial_heap.hpp"
#include "boost/heap/d_ary_heap.hpp"
template <class HeapType>
void PrintHeap(HeapType const & heap) {
std::cout << "Printing out heap : ";
for (auto it = heap.begin(); it != heap.end(); ++it) {
std::cout << *it << " ";
}
std::cout << "\n Printing out heap in order : ";
for (auto it = heap.ordered_begin(); it != heap.ordered_end(); ++it) {
std::cout << *it << " ";
}
std::cout << "\n\n";
}
int main(int /* argc */, char** /* argv */) {
typedef boost::heap::binomial_heap<int> BinomialHeap;
BinomialHeap binomial_heap;
binomial_heap.push(1);
binomial_heap.push(1);
// Prints {1,1} and {1,1,1}
PrintHeap(binomial_heap);
typedef boost::heap::fibonacci_heap<int> FibonacciHeap;
FibonacciHeap fibonacci_heap;
fibonacci_heap.push(1);
fibonacci_heap.push(1);
// Prints {1,1} and {1,1}
PrintHeap(fibonacci_heap);
typedef boost::heap::d_ary_heap<int, boost::heap::arity<2>, boost::heap::mutable_<true>> BinaryHeap;
BinaryHeap binary_heap;
binary_heap.push(1);
binary_heap.push(1);
// Prints {1,1} and {1,1}
PrintHeap(binary_heap);
}
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
https://lists.boost.org/mailman/listinfo.cgi/boost-users
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic