File: utils.h

package info (click to toggle)
libpog 0.5.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,644 kB
  • sloc: cpp: 51,038; ansic: 239; makefile: 14; python: 11; sh: 11
file content (75 lines) | stat: -rw-r--r-- 1,773 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include <ctime>
#include <functional>
#include <iomanip>
#include <sstream>
#include <variant>
#include <utility>

namespace pog {

template <typename InputIterator, typename OutputIterator, typename UnaryOperator, typename Pred>
OutputIterator transform_if(InputIterator first1, InputIterator last1, OutputIterator result, Pred pred, UnaryOperator op)
{
	while (first1 != last1)
	{
		if (pred(*first1)) {
			*result = op(*first1);
			++result;
		}
		++first1;
	}
	return result;
}

template <typename InputIterator, typename T, typename BinaryOperation, typename Pred>
T accumulate_if(InputIterator first, InputIterator last, T init, Pred pred, BinaryOperation op)
{
	for (; first != last; ++first)
	{
		if (pred(*first))
			init = op(std::move(init), *first);
	}
	return init;
}

inline void hash_combine(std::size_t&) { }

template <typename T, typename... Rest>
inline void hash_combine(std::size_t& seed, const T& v, const Rest&... rest) {
	seed ^= std::hash<T>{}(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
	hash_combine(seed, rest...);
}

template <typename... Rest>
inline std::size_t hash_combine(const Rest&... rest)
{
	std::size_t seed = 0;
	hash_combine(seed, rest...);
	return seed;
}

template<class... Ts> struct overloaded : Ts... { using Ts::operator()...; };
template<class... Ts> overloaded(Ts...) -> overloaded<Ts...>;

template <typename Variant, typename... Fs>
auto visit_with(Variant& v, Fs&&... fs)
{
	return std::visit(overloaded{
		std::forward<Fs>(fs)...
	}, v);
}

template <typename FormatT>
inline std::string current_time(FormatT&& format)
{
	auto now = std::time(nullptr);
	auto tm = std::localtime(&now);

	std::ostringstream ss;
	ss << std::put_time(tm, std::forward<FormatT>(format));
	return ss.str();
}

} // namespace pog