region: use half-open range for region::step

This commit is contained in:
Danny Robson 2019-03-08 09:38:13 +11:00
parent 0d27694a51
commit 976d44b1d9
2 changed files with 15 additions and 11 deletions

View File

@ -3,12 +3,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
* *
* Copyright 2010-2017 Danny Robson <danny@nerdcruft.net> * Copyright 2010-2019 Danny Robson <danny@nerdcruft.net>
*/ */
#ifndef CRUFT_UTIL_REGION_HPP #pragma once
#define CRUFT_UTIL_REGION_HPP
#include "./extent.hpp" #include "./extent.hpp"
#include "./point.hpp" #include "./point.hpp"
@ -156,7 +155,7 @@ namespace cruft {
cursor[0] += 1; cursor[0] += 1;
for (size_t s = 0; s < S-1; ++s) { for (size_t s = 0; s < S-1; ++s) {
if (cursor[s] <= hi[s]) if (cursor[s] < hi[s])
return *this; return *this;
cursor[s] = lo[s]; cursor[s] = lo[s];
@ -173,10 +172,17 @@ namespace cruft {
point_t cursor, lo, hi; point_t cursor, lo, hi;
}; };
/// Returns an iterator that provides successive points across the
/// region.
///
/// The points are in the half open range [p, p+e). ie, the
/// 'bottom-right' corner will never be returned. If you need this
/// behaviour then construct a larger range.
auto step (void) const auto step (void) const
{ {
point_t last = p; point_t last = p;
last[S-1] = (p + e)[S-1] + 1; last[S-1] = (p + e)[S-1];
return cruft::view { return cruft::view {
iterator { p, p + e }, iterator { p, p + e },
@ -296,5 +302,3 @@ namespace cruft {
template <size_t S, typename T> template <size_t S, typename T>
std::ostream& operator<< (std::ostream&, const cruft::region<S,T>&); std::ostream& operator<< (std::ostream&, const cruft::region<S,T>&);
} }
#endif

View File

@ -84,13 +84,13 @@ main (int, char **)
// ensure make_region covers the expected values // ensure make_region covers the expected values
{ {
const cruft::region2i REGION { const cruft::region2i REGION {
cruft::point2i { -1, 1 }, cruft::point2i { 0, 0 },
cruft::point2i { 1, 2 } cruft::point2i { 3, 2 }
}; };
const cruft::point2i EXPECTED[] = { const cruft::point2i EXPECTED[] = {
{ -1, 1 }, { 0, 1 }, { 1, 1 }, { 0, 0 }, { 1, 0 }, { 2, 0 },
{ -1, 2 }, { 0, 2 }, { 1, 2 }, { 0, 1 }, { 1, 1 }, { 2, 1 },
}; };
std::vector<cruft::point2i> values; std::vector<cruft::point2i> values;