Boost C++ Libraries: Ticket #13140: Quadratic complexity of a flat_set constructor https://svn.boost.org/trac10/ticket/13140 <p> The documentation of <a href="http://www.boost.org/doc/libs/1_64_0/doc/html/boost/container/flat_map.html#boost.container.flat_mapconstruct-copy-destruct">boost flat_set constructor taking two iterators</a> says: </p> <pre class="wiki">template&lt;typename InputIterator&gt; flat_map(InputIterator first, InputIterator last, const Compare &amp; comp = Compare(), const allocator_type &amp; a = allocator_type()); </pre><p> Effects: Constructs an empty flat_map using the specified comparison object and allocator, and inserts elements from the range [first ,last ). </p> <p> Complexity: Linear in N if the range [first ,last ) is already sorted using comp and otherwise N logN, where N is last - first. </p> <p> However, the complexity of this constructor is O(N*N) because the implementation of the constructor inserts each element into a vector at position found by binary search and insertion is linear in distance to the end of the vector. </p> <p> A simple fix is to insert all elements at once and sort the vector backing <code>flat_set</code>. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13140 Trac 1.4.3 anonymous Fri, 28 Jul 2017 00:59:39 GMT <link>https://svn.boost.org/trac10/ticket/13140#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13140#comment:1</guid> <description> <p> I mixed <code>flat_map</code> and <code>flat_set</code> above but the same issue applies to both. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Sun, 30 Jul 2017 18:27:12 GMT</pubDate> <title>component changed; owner set https://svn.boost.org/trac10/ticket/13140#comment:2 https://svn.boost.org/trac10/ticket/13140#comment:2 <ul> <li><strong>owner</strong> set to <span class="trac-author">Ion Gaztañaga</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">container</span> </li> </ul> Ticket Constantin Makshin <cmakshin@…> Tue, 01 Aug 2017 20:45:43 GMT <link>https://svn.boost.org/trac10/ticket/13140#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13140#comment:3</guid> <description> <p> The suggested fix will create invalid map/set if the source range contains values which must be "collapsed" into one element of the target container (pairs with the same first element for map, same values for set). </p> </description> <category>Ticket</category> </item> <item> <author>victor.zverovich@…</author> <pubDate>Tue, 08 Aug 2017 01:36:20 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/13140#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13140#comment:4</guid> <description> <p> This can be fixed by additional unique pass without changing the complexity (also the sort might have to be stable in this case). </p> </description> <category>Ticket</category> </item> </channel> </rss>