forked from ryanhaining/cppitertools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted.hpp
More file actions
59 lines (47 loc) · 1.51 KB
/
sorted.hpp
File metadata and controls
59 lines (47 loc) · 1.51 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
53
54
55
56
57
58
59
#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, typename CompareFunc>
class SortedView;
using SortedFn = IterToolFnOptionalBindSecond<SortedView, std::less<>>;
}
constexpr impl::SortedFn sorted{};
}
template <typename Container, typename CompareFunc>
class iter::impl::SortedView {
private:
using IterIterWrap = IterIterWrapper<std::vector<iterator_type<Container>>>;
using ItIt = iterator_type<IterIterWrap>;
friend SortedFn;
Container container;
IterIterWrap sorted_iters;
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);
}
};
#endif