-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.hpp
More file actions
52 lines (44 loc) · 1.76 KB
/
algorithm.hpp
File metadata and controls
52 lines (44 loc) · 1.76 KB
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
#pragma once
#include <algorithm>
#include <utility>
namespace mh
{
template<typename Container, typename Predicate, typename Factory>
std::pair<bool, typename Container::iterator> find_or_add_if_factory(Container& container, const Predicate& pred, const Factory& factory)
{
if (auto found = std::find_if(container.begin(), container.end(), pred); found != container.end())
return { false, found };
return { true, container.insert(container.end(), factory()) };
}
template<typename Container, typename Predicate, typename T>
inline std::pair<bool, typename Container::iterator> find_or_add_if(Container& container, const Predicate& pred, const T& value)
{
return find_or_add_if_factory(container, pred, [&value]() { return value; });
}
template<typename Container, typename T>
inline std::pair<bool, typename Container::iterator> find_or_add(Container& container, const T& value)
{
return find_or_add_if(container, [&value](const auto& v) { return v == value; }, value);
}
template<typename Container>
inline bool contains(const Container& container, const typename Container::value_type& value)
{
const auto end = std::end(container);
return std::find(std::begin(container), end, value) != end;
}
template<typename Container>
inline void erase(Container& container, const typename Container::value_type& value)
{
container.erase(std::remove(std::begin(container), std::end(container), value), std::end(container));
}
template<typename Container>
inline void sort(Container& container)
{
return std::sort(std::begin(container), std::end(container));
}
template<typename Container, typename Compare>
inline void sort(Container& container, Compare&& compare)
{
return std::sort(std::begin(container), std::end(container), std::move(compare));
}
}