From 976d44b1d90e2e93fa756ac703977fd2feea9c77 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Fri, 8 Mar 2019 09:38:13 +1100 Subject: [PATCH] region: use half-open range for region::step --- region.hpp | 18 +++++++++++------- test/region.cpp | 8 ++++---- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/region.hpp b/region.hpp index 93633efa..025e2116 100644 --- a/region.hpp +++ b/region.hpp @@ -3,12 +3,11 @@ * 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/. * - * Copyright 2010-2017 Danny Robson + * Copyright 2010-2019 Danny Robson */ -#ifndef CRUFT_UTIL_REGION_HPP -#define CRUFT_UTIL_REGION_HPP +#pragma once #include "./extent.hpp" #include "./point.hpp" @@ -156,7 +155,7 @@ namespace cruft { cursor[0] += 1; for (size_t s = 0; s < S-1; ++s) { - if (cursor[s] <= hi[s]) + if (cursor[s] < hi[s]) return *this; cursor[s] = lo[s]; @@ -173,10 +172,17 @@ namespace cruft { 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 { point_t last = p; - last[S-1] = (p + e)[S-1] + 1; + last[S-1] = (p + e)[S-1]; return cruft::view { iterator { p, p + e }, @@ -296,5 +302,3 @@ namespace cruft { template std::ostream& operator<< (std::ostream&, const cruft::region&); } - -#endif diff --git a/test/region.cpp b/test/region.cpp index b1c87c3e..fae3ac98 100644 --- a/test/region.cpp +++ b/test/region.cpp @@ -84,13 +84,13 @@ main (int, char **) // ensure make_region covers the expected values { const cruft::region2i REGION { - cruft::point2i { -1, 1 }, - cruft::point2i { 1, 2 } + cruft::point2i { 0, 0 }, + cruft::point2i { 3, 2 } }; const cruft::point2i EXPECTED[] = { - { -1, 1 }, { 0, 1 }, { 1, 1 }, - { -1, 2 }, { 0, 2 }, { 1, 2 }, + { 0, 0 }, { 1, 0 }, { 2, 0 }, + { 0, 1 }, { 1, 1 }, { 2, 1 }, }; std::vector values;