[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>&nbsp;</o:p></p>
<p class="MsoNormal">I&#8217;m observing a weird phenomenon when iterating over a \
binomial heap after pushing two values with the same priority.&nbsp; Instead of \
printing the expected values twice, the ordered iterator seems to print three \
values.&nbsp; I do not observe this  behavior when using a Fibonacci heap or a binary \
heap.&nbsp; I&#8217;ve attached sample code that demonstrates this behavior.&nbsp; \
Can anyone help explain what&#8217;s going on?<o:p></o:p></p> <p \
class="MsoNormal"><o:p>&nbsp;</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>&nbsp;</o:p></p>
<p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:&quot;Courier New&quot;">#include \
&lt;iostream&gt;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">#include &quot;boost/heap/fibonacci_heap.hpp&quot;<o:p></o:p></span></p> \
<p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:&quot;Courier New&quot;">#include \
&quot;boost/heap/binomial_heap.hpp&quot;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">#include &quot;boost/heap/d_ary_heap.hpp&quot;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;"><o:p>&nbsp;</o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">template &lt;class HeapType&gt;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">void \
PrintHeap(HeapType const &amp; heap) {<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
std::cout &lt;&lt; &quot;Printing out heap : &quot;;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">&nbsp; for (auto it = heap.begin(); it != heap.end(); &#43;&#43;it) \
{<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:&quot;Courier New&quot;">&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; *it \
&lt;&lt; &quot; &quot;;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
}<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:&quot;Courier New&quot;">&nbsp; std::cout &lt;&lt; &quot;\n \
Printing out heap in order : &quot;;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
for (auto it = heap.ordered_begin(); it != heap.ordered_end(); &#43;&#43;it) \
{<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:&quot;Courier New&quot;">&nbsp;&nbsp;&nbsp; std::cout &lt;&lt; *it \
&lt;&lt; &quot; &quot;;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
}<o:p></o:p></span></p> <p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:&quot;Courier New&quot;">&nbsp; std::cout &lt;&lt; \
&quot;\n\n&quot;;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;"><o:p>&nbsp;</o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">int \
main(int /* argc */, char** /* argv */) {<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
typedef boost::heap::binomial_heap&lt;int&gt; BinomialHeap;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">&nbsp; BinomialHeap binomial_heap;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">&nbsp; binomial_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
binomial_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
// Prints {1,1} and {1,1,1}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
PrintHeap(binomial_heap);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;"><o:p>&nbsp;</o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
typedef boost::heap::fibonacci_heap&lt;int&gt; FibonacciHeap;<o:p></o:p></span></p> \
<p class="MsoNormal" style="text-autospace:none"><span \
style="font-family:&quot;Courier New&quot;">&nbsp; FibonacciHeap \
fibonacci_heap;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
fibonacci_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
fibonacci_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
// Prints {1,1} and {1,1}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
PrintHeap(fibonacci_heap);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;"><o:p>&nbsp;</o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
typedef boost::heap::d_ary_heap&lt;int, boost::heap::arity&lt;2&gt;, \
boost::heap::mutable_&lt;true&gt;&gt; BinaryHeap;<o:p></o:p></span></p> <p \
class="MsoNormal" style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">&nbsp; BinaryHeap binary_heap;<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
binary_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
binary_heap.push(1);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
// Prints {1,1} and {1,1}<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier New&quot;">&nbsp; \
PrintHeap(binary_heap);<o:p></o:p></span></p> <p class="MsoNormal" \
style="text-autospace:none"><span style="font-family:&quot;Courier \
New&quot;">}<o:p></o:p></span></p> <p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal"><o:p>&nbsp;</o:p></p>
<p class="MsoNormal">Thanks!<o:p></o:p></p>
<p class="MsoNormal"><b><span \
style="font-size:10.0pt;font-family:&quot;Arial&quot;,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