forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrect_range.h
63 lines (54 loc) · 1.84 KB
/
rect_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
#pragma once
#ifndef CATA_SRC_RECT_RANGE_H
#define CATA_SRC_RECT_RANGE_H
#include "point.h"
// This is a template parameter, it's usually SDL_Rect, but that way the class
// can be used without include any SDL header.
template<typename RectType>
class rect_range
{
private:
int width;
int height;
point count;
public:
rect_range( const int w, const int h, const point &c ) : width( w ), height( h ),
count( c ) {
}
class iterator
{
private:
friend class rect_range;
const rect_range *range;
int index;
iterator( const rect_range *const r, const int i ) : range( r ), index( i ) {
}
public:
bool operator==( const iterator &rhs ) const {
return range == rhs.range && index == rhs.index;
}
bool operator!=( const iterator &rhs ) const {
return !operator==( rhs );
}
RectType operator*() const {
return { ( index % range->count.x ) *range->width, ( index / range->count.x ) *range->height, range->width, range->height };
}
iterator operator+( const int offset ) const {
return iterator( range, index + offset );
}
int operator-( const iterator &rhs ) const {
return index - rhs.index;
}
iterator &operator++() {
++index;
return *this;
}
};
iterator begin() const {
return iterator( this, 0 );
}
iterator end() const {
return iterator( this, count.x * count.y );
}
};
#endif // CATA_SRC_RECT_RANGE_H