forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unique_everseen.hpp
57 lines (51 loc) · 1.68 KB
/
unique_everseen.hpp
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
#ifndef UNIQUE_EVERSEEN_HPP
#define UNIQUE_EVERSEEN_HPP
#include "iterbase.hpp"
#include "filter.hpp"
#include <type_traits>
#include <functional>
#include <utility>
#include <unordered_set>
#include <iterator>
namespace iter
{
//the container type must be usable in an unordered_map to achieve constant
//performance checking if it has ever been seen
template <typename Container>
auto unique_everseen(Container&& container)
-> Filter<std::function<bool(iterator_deref<Container>)>,Container>
{
using elem_t = iterator_deref<Container>;
std::unordered_set<std::decay_t<elem_t> > elem_seen;
std::function<bool(elem_t)> func =
//has to be captured by value because it goes out of scope when the
//function returns
[elem_seen](elem_t e) mutable
{
if (elem_seen.find(e) == std::end(elem_seen)){
elem_seen.insert(e);
return true;
} else {
return false;
}
};
return filter(func, std::forward<Container>(container));
}
template <typename T>
auto unique_everseen(std::initializer_list<T> il)
-> Filter<std::function<bool(T)>, std::initializer_list<T>>
{
std::unordered_set<T> elem_seen;
std::function<bool(T)> func = [elem_seen](const T& e) mutable
{
if (elem_seen.find(e) == std::end(elem_seen)){
elem_seen.insert(e);
return true;
} else {
return false;
}
};
return filter(func, il);
}
}
#endif //UNIQUE_EVERSEEN_HPP