-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator_range.h
74 lines (64 loc) · 1.41 KB
/
iterator_range.h
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
#pragma once
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
template <typename Iterator>
class IteratorRange {
public:
IteratorRange(Iterator first, Iterator last) : first(first), last(last) {
}
Iterator begin() const {
return first;
}
Iterator end() const {
return last;
}
size_t size() const {
return last - first;
}
private:
Iterator first, last;
};
template <typename Container>
auto Head(Container& c, int top) {
return IteratorRange(begin(c), begin(c) + min<size_t>(max(top, 0), c.size()));
}
template <typename Iterator>
class Paginator {
public:
Paginator(Iterator begin, Iterator end, size_t page_size)
{
size_t page_count = ceil((float)(end - begin) / page_size);
for (int i = 0; i < page_count; i++)
{
if (i == page_count - 1)
{
pages.push_back(IteratorRange{begin + page_size * i , end});
}
else
{
pages.push_back(IteratorRange{begin + page_size * i, begin + page_size * (i + 1)});
}
}
}
auto begin() const
{
return pages.begin();
}
auto end() const
{
return pages.end();
}
size_t size() const
{
return pages.size();
}
private:
vector<IteratorRange<Iterator>> pages;
};
template <typename C>
auto Paginate(C& c, size_t page_size) {
// Реализуйте этот шаблон функции
return Paginator{c.begin(), c.end(), page_size};
}