/
uniform_raster_view.h
124 lines (101 loc) · 2.67 KB
/
uniform_raster_view.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
//=======================================================================
// Copyright 2015-2107
// Author: Alex Hagen-Zanker
// University of Surrey
//
// Distributed under the MIT Licence (http://opensource.org/licenses/MIT)
//=======================================================================
//
#pragma once
#include <pronto/raster/iterator_facade.h>
#include <pronto/raster/traits.h>
#include <ranges>
namespace pronto {
namespace raster {
template<class T> //required to be a Raster and a View
class uniform_raster_view : public std::ranges::view_interface < uniform_raster_view<T>>
{
public:
class iterator: public iterator_facade<iterator>
{
public:
static const bool is_mutable = false;
static const bool is_single_pass = false;
iterator()
{}
void increment()
{
++m_index;
}
void advance(const std::ptrdiff_t & n)
{
m_index += n;
}
void decrement()
{
--m_index;
}
bool equal_to(const iterator& that) const
{
return that.m_index == m_index;
}
std::ptrdiff_t distance_to(const iterator& that) const
{
return that.m_index - m_index;
}
const T& dereference() const
{
return m_view->m_value;
}
private:
friend class uniform_raster_view;
void find_begin(const uniform_raster_view* view)
{
m_view = view;
m_index = 0;
}
void find_end(const uniform_raster_view* view)
{
m_view = view;
m_index = view->size();
}
int m_index;
const uniform_raster_view* m_view;
};
uniform_raster_view() : m_rows(0), m_cols(0), m_value(T{})
{}
uniform_raster_view(int rows, int cols, const T& value)
: m_rows(rows), m_cols(cols), m_value(value)
{}
int rows() const { return m_rows; }
int cols() const { return m_cols; }
int size() const { return m_rows * m_cols; }
iterator begin() const
{
iterator i;
i.find_begin(this);
return i;
}
iterator end() const
{
iterator i;
i.find_end(this);
return i;
}
uniform_raster_view sub_raster(int, int, int rows, int cols) const
{
return uniform_raster_view(rows, cols, m_value);
}
private:
int m_rows;
int m_cols;
T m_value;
};
template<class T>
uniform_raster_view<T> uniform(int rows, int cols, T value)
{
return uniform_raster_view(rows, cols, value);
}
}
}