aboutsummaryrefslogtreecommitdiff
path: root/src/util/algorithm_impl.hpp
blob: d7c5e3b0cb9ff5cd35ce1c97d97a95cb3c272c3e (plain)
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
#pragma once

#include "algorithm.hpp"

#include <algorithm>

template <typename ContainerType, typename Value>
requires Container<ContainerType>
constexpr auto container_find(const ContainerType &container, const Value &value) noexcept
	-> typename ContainerType::const_iterator
{
	return std::find(container.begin(), container.end(), value);
}

template <typename ContainerType, typename Predicate>
requires Container<ContainerType> &&
	std::predicate<Predicate, typename ContainerType::value_type>
constexpr auto
container_find(const ContainerType &container, Predicate predicate) noexcept ->
	typename ContainerType::const_iterator
{
	return std::find_if(container.begin(), container.end(), predicate);
}

template <typename ContainerType, typename Value>
requires Container<ContainerType>
constexpr auto container_has(const ContainerType &container, const Value &value) noexcept
	-> bool
{
	return container_find(container, value) != container.end();
}

template <typename ContainerType, typename Predicate>
requires Container<ContainerType> && HasPushBack<ContainerType> &&
	std::predicate<Predicate, typename ContainerType::value_type>
constexpr auto
container_filter(const ContainerType &container, Predicate predicate) noexcept
	-> ContainerType
{
	ContainerType filtered_container;

	std::copy_if(
		std::begin(container),
		std::end(container),
		std::back_inserter(filtered_container),
		predicate);

	return filtered_container;
}

template <typename ContainerType, typename Predicate>
requires Container<ContainerType> &&
	std::predicate<Predicate, typename ContainerType::value_type>
constexpr auto
container_filter(const ContainerType &container, Predicate predicate) noexcept
	-> ContainerType
{
	ContainerType filtered_container;

	std::copy_if(
		std::begin(container),
		std::end(container),
		std::inserter(filtered_container, filtered_container.begin()),
		predicate);

	return filtered_container;
}