Skip to main content

Timeline for Reversing vector order

Current License: CC BY-SA 4.0

9 events
when toggle format what by license comment
Sep 13 at 13:29 comment added Cris Luengo Oh, by the way, I think for educational purposes, in this case, it is better to write out the loop like this rather than hide it in range algorithms, where it is not clear what the compiler will actually do. An explicit loop clearly shows what is going on, and that no intermediate copies are made, and this is the point I was trying to get across.
Sep 13 at 13:25 history edited Cris Luengo CC BY-SA 4.0
added 7 characters in body
Sep 13 at 13:24 comment added Cris Luengo @indi The “assignment hidden in the loop control” is an increment. We’re incrementing two variables in this loop, the loop counter and the index. Maybe it’s better to do that second increment in the loop body, but I find it reduces my cognitive load to do it in the control, because then the loop body is just the code that handles the one pixel. I’ll move it over.
Sep 13 at 13:18 comment added Cris Luengo @indi Obviously one assumes that the inputs are checked. I just reworked OP’s loop. Sure the range-based loops are nice, and make it harder to make mistakes, but calling any of this indexing complex is a bit of a reach, I assume a rhetorical construct to get your point across.
Sep 12 at 23:53 comment added indi Writing raw loops like this—with complex indexing calculations and *no checking at all!*—is not acceptable in 2025.
Sep 12 at 23:52 comment added indi There is a reason the modern recommendation is to use algorithms or range views. THEY ARE SAFE. They are often “correct by construction”. Take @TobySpeight’s code. So long as w is not <= 0… which is trivial to check for before starting anything, that code will produce (basically) the same low-level operations as your manual loops, and is obviously, even at a glance, correct and safe. You want to handle different strides? Trivial. Arbitrary origin? Also trivial: use a span as a sub view. Or there’s mdspan of course.
Sep 12 at 23:51 comment added indi This is really bad advice. Writing raw loops would be bad enough, but one could at least assume you’ve verified that width * height <= data.size() (I mean, you never actually check or even mention checking this but… we can at least assume). However, within the (nested!) loops, you then do a serious of rather complicated calculations on an index… including one assignment hidden in the loop control (!!!)… and at no point verify that you haven’t over/underflowed anything, or gone out of bounds. This is how nightmare bugs are made.
Sep 12 at 0:51 history edited Cris Luengo CC BY-SA 4.0
added 219 characters in body
Sep 12 at 0:41 history answered Cris Luengo CC BY-SA 4.0