Rips out dead code from the container.hpp and creates for_each_pai.hpp
I added two TODOs that I would like to address in the future.
This commit is contained in:
committed by
Patrick Niklaus
parent
4813488f84
commit
c93ca02fb8
@@ -1,79 +1,11 @@
|
||||
#ifndef CONTAINER_HPP
|
||||
#define CONTAINER_HPP
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <vector>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
|
||||
namespace detail
|
||||
{
|
||||
// Culled by SFINAE if reserve does not exist or is not accessible
|
||||
template <typename T>
|
||||
constexpr auto has_resize_method(T &t) noexcept -> decltype(t.resize(0), bool())
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
// Used as fallback when SFINAE culls the template method
|
||||
constexpr bool has_resize_method(...) noexcept { return false; }
|
||||
}
|
||||
|
||||
template <typename Container> void sort_unique_resize(Container &vector) noexcept
|
||||
{
|
||||
std::sort(std::begin(vector), std::end(vector));
|
||||
const auto number_of_unique_elements =
|
||||
std::unique(std::begin(vector), std::end(vector)) - std::begin(vector);
|
||||
if (detail::has_resize_method(vector))
|
||||
{
|
||||
vector.resize(number_of_unique_elements);
|
||||
}
|
||||
}
|
||||
|
||||
// template <typename T> inline void sort_unique_resize_shrink_vector(std::vector<T> &vector)
|
||||
// {
|
||||
// sort_unique_resize(vector);
|
||||
// vector.shrink_to_fit();
|
||||
// }
|
||||
|
||||
// template <typename T> inline void remove_consecutive_duplicates_from_vector(std::vector<T>
|
||||
// &vector)
|
||||
// {
|
||||
// const auto number_of_unique_elements = std::unique(vector.begin(), vector.end()) -
|
||||
// vector.begin();
|
||||
// vector.resize(number_of_unique_elements);
|
||||
// }
|
||||
|
||||
template <typename ForwardIterator, typename Function>
|
||||
Function for_each_pair(ForwardIterator begin, ForwardIterator end, Function function)
|
||||
{
|
||||
if (begin == end)
|
||||
{
|
||||
return function;
|
||||
}
|
||||
|
||||
auto next = begin;
|
||||
next = std::next(next);
|
||||
|
||||
while (next != end)
|
||||
{
|
||||
function(*begin, *next);
|
||||
begin = std::next(begin);
|
||||
next = std::next(next);
|
||||
}
|
||||
return function;
|
||||
}
|
||||
|
||||
template <class ContainerT, typename Function>
|
||||
Function for_each_pair(ContainerT &container, Function function)
|
||||
{
|
||||
return for_each_pair(std::begin(container), std::end(container), function);
|
||||
}
|
||||
|
||||
template <class Container> void append_to_container(Container &&) {}
|
||||
|
||||
template <class Container, typename T, typename... Args>
|
||||
@@ -82,8 +14,7 @@ void append_to_container(Container &&container, T value, Args &&... args)
|
||||
container.emplace_back(value);
|
||||
append_to_container(std::forward<Container>(container), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
} // namespace osrm
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* CONTAINER_HPP */
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
#ifndef FOR_EACH_PAIR_HPP
|
||||
#define FOR_EACH_PAIR_HPP
|
||||
|
||||
#include <numeric>
|
||||
#include <iterator>
|
||||
|
||||
namespace osrm
|
||||
{
|
||||
namespace util
|
||||
{
|
||||
|
||||
// TODO: check why this is not an option here:
|
||||
// std::adjacent_find(begin, end, [=](const auto& l, const auto& r){ return function(), false; });
|
||||
template <typename ForwardIterator, typename Function>
|
||||
Function for_each_pair(ForwardIterator begin, ForwardIterator end, Function function)
|
||||
{
|
||||
if (begin == end)
|
||||
{
|
||||
return function;
|
||||
}
|
||||
|
||||
auto next = begin;
|
||||
next = std::next(next);
|
||||
|
||||
while (next != end)
|
||||
{
|
||||
function(*begin, *next);
|
||||
begin = std::next(begin);
|
||||
next = std::next(next);
|
||||
}
|
||||
return function;
|
||||
}
|
||||
|
||||
template <class ContainerT, typename Function>
|
||||
Function for_each_pair(ContainerT &container, Function function)
|
||||
{
|
||||
using std::begin;
|
||||
using std::end;
|
||||
return for_each_pair(begin(container), end(container), function);
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace osrm
|
||||
|
||||
#endif /* FOR_EACH_PAIR_HPP */
|
||||
@@ -2,6 +2,7 @@
|
||||
#define JSON_UTIL_HPP
|
||||
|
||||
#include "osrm/json_container.hpp"
|
||||
#include "util/container.hpp"
|
||||
|
||||
#include <cmath>
|
||||
#include <limits>
|
||||
@@ -31,6 +32,7 @@ template <typename T> T clamp_float(T d)
|
||||
template <typename... Args> Array make_array(Args... args)
|
||||
{
|
||||
Array a;
|
||||
// TODO: check why a.values.emplace_back(args...); is not an option here
|
||||
append_to_container(a.values, args...);
|
||||
return a;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user