aboutsummaryrefslogtreecommitdiff
path: root/src/util/algorithm.tpp
blob: 00269ed74f908973c1ab6e1c0ec9de3a0364823c (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
#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 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;
}