[libcxx-commits] [PATCH] D144538: Checked that complexity of std::sort_heap is 2N log(N) comparisons

Nilay Vaish via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sun Feb 26 12:55:27 PST 2023


nilayvaish updated this revision to Diff 500618.
nilayvaish added a comment.

Updated the test used for testing the complexity of heap sort implementation.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D144538/new/

https://reviews.llvm.org/D144538

Files:
  libcxx/docs/Status/Cxx20Issues.csv
  libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp


Index: libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
===================================================================
--- libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
+++ libcxx/test/libcxx/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp
@@ -15,6 +15,7 @@
 
 #include <algorithm>
 #include <cassert>
+#include <iostream>
 #include <random>
 
 #include "test_macros.h"
@@ -48,28 +49,31 @@
 
 int main(int, char**)
 {
-  const int N = 100'000;
+  constexpr int N =  (1 << 20);
   std::vector<MyInt> v;
   v.reserve(N);
   std::mt19937 g;
-  for (int i = 0; i < N; ++i)
-    v.emplace_back(g());
-  std::make_heap(v.begin(), v.end());
-
-  // The exact stats of our current implementation are recorded here.
-  // If something changes to make them go a bit up or down, that's probably fine,
-  // and we can just update this test.
-  // But if they suddenly leap upward, that's a bad thing.
-
-  stats = {};
-  std::sort_heap(v.begin(), v.end());
-  assert(stats.copied == 0);
-  assert(stats.moved == 1'764'997);
+  for (int i = 0; i < N; ++i) {
+    v.emplace_back(i);
+  }
+  for (int logn = 10; logn <= 20; ++logn) {
+    const int n = (1 << logn);
+    auto first = v.begin();
+    auto last = v.begin() + n;
+    std::shuffle(first, last, g);
+    std::make_heap(first, last);
+    // The exact stats of our current implementation are recorded here.
+    // If something changes to make them go a bit up or down, that's probably
+    // fine, and we can just update this test.
+    // But if they suddenly leap upward, that's a bad thing.
+    stats = {};
+    std::sort_heap(first, last);
+    assert(stats.copied == 0);
+    assert(stats.moved <= 2 * n + n * logn);
 #ifndef _LIBCPP_ENABLE_DEBUG_MODE
-  assert(stats.compared == 1'534'701);
+    assert(stats.compared <= n * logn);
 #endif
-
-  assert(std::is_sorted(v.begin(), v.end()));
-
+    assert(std::is_sorted(first, last));
+  }
   return 0;
 }
Index: libcxx/docs/Status/Cxx20Issues.csv
===================================================================
--- libcxx/docs/Status/Cxx20Issues.csv
+++ libcxx/docs/Status/Cxx20Issues.csv
@@ -1,6 +1,6 @@
 "Issue #","Issue Name","Meeting","Status","First released version","Labels"
 "`2070 <https://wg21.link/LWG2070>`__","``allocate_shared``\  should use ``allocator_traits<A>::construct``\ ","Toronto","Resolved by `P0674R1 <https://wg21.link/P0674R1>`__",""
-"`2444 <https://wg21.link/LWG2444>`__","Inconsistent complexity for ``std::sort_heap``\ ","Toronto","",""
+"`2444 <https://wg21.link/LWG2444>`__","Inconsistent complexity for ``std::sort_heap``\ ","Toronto","|Nothing To Do|",""
 "`2593 <https://wg21.link/LWG2593>`__","Moved-from state of Allocators","Toronto","",""
 "`2597 <https://wg21.link/LWG2597>`__","``std::log``\  misspecified for complex numbers","Toronto","",""
 "`2783 <https://wg21.link/LWG2783>`__","``stack::emplace()``\  and ``queue::emplace()``\  should return ``decltype(auto)``\ ","Toronto","|Complete|",""


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D144538.500618.patch
Type: text/x-patch
Size: 3067 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20230226/83146cf6/attachment.bin>


More information about the libcxx-commits mailing list