2016-01-06 20:02:06 +00:00
|
|
|
#pragma once
|
2015-11-19 20:17:35 +00:00
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
2016-03-01 20:57:05 +00:00
|
|
|
#include <chrono>
|
2016-08-05 17:34:57 +00:00
|
|
|
#include <deque>
|
2015-11-19 20:17:35 +00:00
|
|
|
|
|
|
|
#define UNUSED(x) ((void)(x))
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
using lambda_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
|
2016-03-01 20:57:05 +00:00
|
|
|
|
2016-04-13 20:37:39 +00:00
|
|
|
std::string formatDuration(std::chrono::duration<double> seconds);
|
|
|
|
|
2016-08-05 17:34:57 +00:00
|
|
|
std::string formatTime(time_t time, const std::string& format);
|
|
|
|
|
|
|
|
template<unsigned int n, typename iterator_type>
|
|
|
|
void for_each_adjacent(
|
|
|
|
iterator_type begin,
|
|
|
|
iterator_type end,
|
|
|
|
std::function<void(const std::deque<std::reference_wrapper<const typename iterator_type::value_type>>&)> f)
|
|
|
|
{
|
|
|
|
// Get the first n values
|
|
|
|
iterator_type it = begin;
|
|
|
|
using element_type = std::reference_wrapper<const typename iterator_type::value_type>;
|
|
|
|
std::deque<element_type> values;
|
|
|
|
for (unsigned i = 0; i < n; ++i) {
|
|
|
|
if (it == end) return;
|
|
|
|
values.push_back(std::ref(*it));
|
|
|
|
if (i < n - 1) ++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (; it != end; ++it) {
|
|
|
|
f(values);
|
|
|
|
|
|
|
|
values.pop_front();
|
|
|
|
values.push_back(*it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename iterator_type>
|
|
|
|
void for_each_adjacent(
|
|
|
|
iterator_type begin,
|
|
|
|
iterator_type end,
|
|
|
|
std::function<void(const typename iterator_type::reference a, const typename iterator_type::reference b)> f)
|
|
|
|
{
|
|
|
|
for_each_adjacent<2>(begin, end, [&](const std::deque<std::reference_wrapper<const typename iterator_type::value_type>>& args) {
|
|
|
|
f(args[0], args[1]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename iterator_type>
|
|
|
|
void for_each_adjacent(
|
|
|
|
iterator_type begin,
|
|
|
|
iterator_type end,
|
|
|
|
std::function<void(const typename iterator_type::reference a, const typename iterator_type::reference b, const typename iterator_type::reference c)> f)
|
|
|
|
{
|
|
|
|
for_each_adjacent<3>(begin, end, [&](const std::deque<std::reference_wrapper<const typename iterator_type::value_type>>& args) {
|
|
|
|
f(args[0], args[1], args[2]);
|
|
|
|
});
|
|
|
|
}
|