From ae451bfb2826914dcf5752e737ed783e16e5919d Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Fri, 26 Apr 2024 12:39:04 -0700 Subject: [PATCH 1/2] Standardize our use of begin/end iterators We try to keep our use of C++ dead simple for non-C++ programmers to read easily, but there are cases where we need to yield a bit. (For example, using shared pointers to keep memory management simple.) In order to use the std::sort() function, we need to provide or use begin and end iterators. With the recent change to our `estimate_halfway.cc` program, we had different ways of using such iterators. The BVH code used the member functions `begin()` and `end()`, while the `estimate_halfway` program used argument promotion from an array and the array plus an integer offset. To standardize both and reduce variance, this change uses the currently-recommended practice of using `std::begin(thing)` and `std::end(thing)`. For now, I'm avoiding spending any text explaining to the non-C++ reader what these are, in the hopes that the meaning can be easily deduced. --- src/TheNextWeek/bvh.h | 2 +- src/TheRestOfYourLife/bvh.h | 2 +- src/TheRestOfYourLife/estimate_halfway.cc | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/TheNextWeek/bvh.h b/src/TheNextWeek/bvh.h index 7bd2d99b..cdf832a1 100644 --- a/src/TheNextWeek/bvh.h +++ b/src/TheNextWeek/bvh.h @@ -49,7 +49,7 @@ class bvh_node : public hittable { left = objects[start]; right = objects[start+1]; } else { - std::sort(objects.begin() + start, objects.begin() + end, comparator); + std::sort(std::begin(objects) + start, std::begin(objects) + end, comparator); auto mid = start + object_span/2; left = make_shared(objects, start, mid); diff --git a/src/TheRestOfYourLife/bvh.h b/src/TheRestOfYourLife/bvh.h index 7bd2d99b..cdf832a1 100644 --- a/src/TheRestOfYourLife/bvh.h +++ b/src/TheRestOfYourLife/bvh.h @@ -49,7 +49,7 @@ class bvh_node : public hittable { left = objects[start]; right = objects[start+1]; } else { - std::sort(objects.begin() + start, objects.begin() + end, comparator); + std::sort(std::begin(objects) + start, std::begin(objects) + end, comparator); auto mid = start + object_span/2; left = make_shared(objects, start, mid); diff --git a/src/TheRestOfYourLife/estimate_halfway.cc b/src/TheRestOfYourLife/estimate_halfway.cc index 3f3b11a1..316fca1d 100644 --- a/src/TheRestOfYourLife/estimate_halfway.cc +++ b/src/TheRestOfYourLife/estimate_halfway.cc @@ -46,7 +46,7 @@ int main() { } // Sort the samples by x. - std::sort(samples, samples + N, compare_by_x); + std::sort(std::begin(samples), std::end(samples), compare_by_x); // Find out the sample at which we have half of our area. double half_sum = sum / 2.0; From 31ae69f382bdd26331502fd455b67b026a801ff7 Mon Sep 17 00:00:00 2001 From: Steve Hollasch Date: Fri, 26 Apr 2024 16:12:09 -0700 Subject: [PATCH 2/2] std::begin/end calls in text --- books/RayTracingTheNextWeek.html | 2 +- books/RayTracingTheRestOfYourLife.html | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/books/RayTracingTheNextWeek.html b/books/RayTracingTheNextWeek.html index db02eb38..fbd0854c 100644 --- a/books/RayTracingTheNextWeek.html +++ b/books/RayTracingTheNextWeek.html @@ -989,7 +989,7 @@ left = objects[start]; right = objects[start+1]; } else { - std::sort(objects.begin() + start, objects.begin() + end, comparator); + std::sort(std::begin(objects) + start, std::begin(objects) + end, comparator); auto mid = start + object_span/2; left = make_shared(objects, start, mid); diff --git a/books/RayTracingTheRestOfYourLife.html b/books/RayTracingTheRestOfYourLife.html index 57dc8f0a..83a0fc89 100644 --- a/books/RayTracingTheRestOfYourLife.html +++ b/books/RayTracingTheRestOfYourLife.html @@ -957,7 +957,7 @@ } // Sort the samples by x. - std::sort(samples, samples + N, compare_by_x); + std::sort(std::begin(samples), std::end(samples), compare_by_x); // Find out the sample at which we have half of our area. double half_sum = sum / 2.0;