// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include #include constexpr size_t loop_cnt = 64; using namespace search::queryeval; template double ordered_cost_of(const std::vector &data, bool strict) { return flow::ordered_cost_of(flow::DirectAdapter(), data, FLOW(strict)); } template double dual_ordered_cost_of(const std::vector &data, bool strict) { double result = flow::ordered_cost_of(flow::DirectAdapter(), data, FLOW(strict)); AnyFlow any_flow = AnyFlow::create(strict); double total_cost = 0.0; for (const auto &item: data) { double child_cost = any_flow.strict() ? item.strict_cost : any_flow.flow() * item.cost; any_flow.update_cost(total_cost, child_cost); any_flow.add(item.estimate); } EXPECT_EQ(total_cost, result); return result; } std::vector gen_data(size_t size) { static std::mt19937 gen; static std::uniform_real_distribution estimate(0.1, 0.9); static std::uniform_real_distribution cost(1.0, 10.0); static std::uniform_real_distribution strict_cost(0.1, 5.0); std::vector result; result.reserve(size); for (size_t i = 0; i < size; ++i) { result.emplace_back(estimate(gen), cost(gen), strict_cost(gen)); } return result; } template void each_perm(std::vector &data, size_t k, F fun) { if (k <= 1) { fun(const_cast &>(data)); } else { each_perm(data, k-1, fun); for (size_t i = 0; i < k-1; ++i) { if (k & 1) { std::swap(data[0], data[k-1]); } else { std::swap(data[i], data[k-1]); } each_perm(data, k-1, fun); } } } template void each_perm(std::vector &data, F fun) { each_perm(data, data.size(), fun); } TEST(FlowTest, perm_test) { std::set> seen; std::vector data = {1,2,3,4,5}; auto hook = [&](const std::vector &perm) { EXPECT_EQ(perm.size(), 5); seen.insert(perm); }; each_perm(data, hook); EXPECT_EQ(seen.size(), 120); } template