forked from alabuzhev/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted.hpp
76 lines (61 loc) · 1.96 KB
/
sorted.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef ITER_SORTED_HPP_
#define ITER_SORTED_HPP_
#include "internal/iterbase.hpp"
#include "internal/iteratoriterator.hpp"
#include <iterator>
#include <algorithm>
#include <vector>
namespace iter {
namespace impl {
template <typename Container>
class SortedView;
}
template <typename Container, typename CompareFunc>
impl::SortedView<Container> sorted(Container&&, CompareFunc);
}
template <typename Container>
class iter::impl::SortedView {
private:
using IterIterWrap = IterIterWrapper<std::vector<iterator_type<Container>>>;
using ItIt = iterator_type<IterIterWrap>;
template <typename C, typename F>
friend SortedView<C> iter::sorted(C&&, F);
Container container;
IterIterWrap sorted_iters;
template <typename CompareFunc>
SortedView(Container&& in_container, CompareFunc compare_func)
: container(std::forward<Container>(in_container)) {
// Fill the sorted_iters vector with an iterator to each
// element in the container
for (auto iter = std::begin(this->container);
iter != std::end(this->container); ++iter) {
this->sorted_iters.get().push_back(iter);
}
// sort by comparing the elements that the iterators point to
std::sort(std::begin(sorted_iters.get()), std::end(sorted_iters.get()),
[compare_func](iterator_type<Container> it1,
iterator_type<Container> it2) {
return compare_func(*it1, *it2);
});
}
public:
SortedView(SortedView&&) = default;
ItIt begin() {
return std::begin(sorted_iters);
}
ItIt end() {
return std::end(sorted_iters);
}
};
template <typename Container, typename CompareFunc>
iter::impl::SortedView<Container> iter::sorted(
Container&& container, CompareFunc compare_func) {
return {std::forward<Container>(container), compare_func};
}
namespace iter {
template <typename Container>
auto sorted(Container&& container) {
return sorted(std::forward<Container>(container), std::less<>{});
}
}
#endif