Skip to content

Commit e3b4814

Browse files
committed
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.
1 parent 4f4144c commit e3b4814

File tree

3 files changed

+3
-3
lines changed

3 files changed

+3
-3
lines changed

src/TheNextWeek/bvh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class bvh_node : public hittable {
4949
left = objects[start];
5050
right = objects[start+1];
5151
} else {
52-
std::sort(objects.begin() + start, objects.begin() + end, comparator);
52+
std::sort(std::begin(objects) + start, std::begin(objects) + end, comparator);
5353

5454
auto mid = start + object_span/2;
5555
left = make_shared<bvh_node>(objects, start, mid);

src/TheRestOfYourLife/bvh.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class bvh_node : public hittable {
4949
left = objects[start];
5050
right = objects[start+1];
5151
} else {
52-
std::sort(objects.begin() + start, objects.begin() + end, comparator);
52+
std::sort(std::begin(objects) + start, std::begin(objects) + end, comparator);
5353

5454
auto mid = start + object_span/2;
5555
left = make_shared<bvh_node>(objects, start, mid);

src/TheRestOfYourLife/estimate_halfway.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int main() {
4646
}
4747

4848
// Sort the samples by x.
49-
std::sort(samples, samples + N, compare_by_x);
49+
std::sort(std::begin(samples), std::end(samples), compare_by_x);
5050

5151
// Find out the sample at which we have half of our area.
5252
double half_sum = sum / 2.0;

0 commit comments

Comments
 (0)