Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <centauri/cell.h>
- #include <algorithm>
- class BlockRegionIterator
- {
- public:
- struct iterator {
- const BlockRegionIterator& base;
- Cell cell;
- iterator(const BlockRegionIterator& base, Cell cell):
- base(base), cell(cell) {}
- const iterator& operator++();
- bool operator==(const iterator& o) { return cell == o.cell; }
- bool operator!=(const iterator& o) { return cell != o.cell; }
- Cell operator*() const { return cell; }
- };
- BlockRegionIterator(BlockRegion r);
- iterator begin() const;
- iterator end() const;
- BlockRegion region() const { return _r; }
- private:
- BlockRegion _r;
- };
- inline const BlockRegionIterator::iterator&
- BlockRegionIterator::iterator::operator++()
- {
- const BlockRegion r = base.region();
- // scanline iterations
- if(cell.x == r.x2) {
- cell.x = r.x1;
- ++cell.y;
- } else {
- ++cell.x;
- }
- return *this;
- }
- inline BlockRegionIterator::BlockRegionIterator(BlockRegion r):
- _r(r)
- {
- if(_r.x2 < _r.x1) {
- std::swap(_r.x1, _r.x2);
- }
- if(_r.y2 < _r.y1) {
- std::swap(_r.y1, _r.y2);
- }
- }
- inline BlockRegionIterator::iterator BlockRegionIterator::begin() const {
- return iterator(*this, _r.lower());
- }
- inline BlockRegionIterator::iterator BlockRegionIterator::end() const {
- return iterator(*this, Cell(_r.x1, _r.y2+1));
- }
Advertisement
Add Comment
Please, Sign In to add comment