瀏覽代碼

Nico Lomuto would be proud... if he didn't hate that algo so much

master
Ludovic 'Archivist' Lagouardette 3 年之前
父節點
當前提交
64c1c4e3a9
共有 2 個檔案被更改,包括 50 行新增5 行删除
  1. +3
    -4
      include/gp/algorithms/partition.hpp
  2. +47
    -1
      tests/dynarray_test.cpp

+ 3
- 4
include/gp/algorithms/partition.hpp 查看文件

@ -4,18 +4,17 @@
namespace gp {
template<typename it_t, typename pred>
it_t lomuto_partition(it_t first, it_t last, pred predicate = pred{}) {
auto i = o">--it_t(first);
auto i = it_t(first);
auto j = first;
++j;
for(; j != last; ++j) {
if(predicate(*j)){
++i;
gp::swap(*i, *j);
++i;
}
}
return o">++i;
return i;
}
template<typename it_t, typename pred>

+ 47
- 1
tests/dynarray_test.cpp 查看文件

@ -60,4 +60,50 @@ struct dynarray_test : public test_scaffold {
}
};
append_test dummy_afdglys543(new dynarray_test{});
append_test dummy_afdglys543(new dynarray_test{});
struct dynarray2_test : public test_scaffold {
uint32_t seed;
dynarray2_test() {
name = __FILE__ ":2_seed";
seed = std::random_device{}();
name += std::to_string(seed);
}
virtual int run() {
using val = gp::dynarray<int, 100>;
cheap_rand setter(seed);
std::unique_ptr<gp::array<char, 4096*4096>> store = std::make_unique<gp::array<char, 4096*4096>>();
gp::buddy alloc{&*store->begin(), store->size()};
gp::vector<val> vals{alloc};
std::uniform_int_distribution<int> dist(0,49);
std::uniform_int_distribution<int> coin(false,true);
bool happened = false;
for(int i = 0; i < 1000; i++) {
val a;
if(coin(setter)) {
a.emplace_back(dist(setter));
}
a.emplace_back(dist(setter));
for(int j = 0; j < 1000; j++) {
val b;
if(coin(setter)) {
b.emplace_back(dist(setter));
}
b.emplace_back(dist(setter));
if(a == b) {
if(! (a != b)) {
happened = true;
}
}
}
}
return !happened;
}
};
append_test dummy_5dfqlys543(new dynarray2_test{});

Loading…
取消
儲存