libcruft-util/algo/search.hpp

43 lines
1.3 KiB
C++

/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright:
* 2018, Danny Robson <danny@nerdcruft.net>
*/
#pragma once
#include <functional>
namespace cruft::search {
/// Performs a linear search to discover the iterator corresponding to
/// the value which minimises a function.
///
/// Useful as a mechanism to avoid possibly expensive comparison
/// calculations; eg recomputing path distances when testing a series of
/// possible routes.
///
/// The provided function will be invoked with the supplied argument pack
/// as the first arguments and the dereferenced iterator as the final
/// argument.
template <typename InputT, typename FunctionT, typename ...Args>
InputT
minimises (InputT first, InputT last, FunctionT &&func, Args &&...args)
{
auto best_score = std::invoke (func, args..., *first);
auto best_item = first;
for (++first; first != last; ++first) {
auto this_score = std::invoke (func, args..., *first);
if (this_score < best_score) {
best_score = this_score;
best_item = first;
}
}
return best_item;
}
}