On 2015-01-27 9:24 PM, Xidorn Quan wrote:
> I asked a question in #developers that what is the best way to reversely
> iterating nsTArray, and there are some suggestions:
>
> <tbsaunde> uint32_t count = array.Length(); for (uint32_t i = length - 1; i
> < length; i--)
> <smaug> iterate from length() to 1 and index using i - 1
> <jcranmer> for (uint32_t i = array.Length(); i-- > 0; ) { }
>
> tbsaunde's method should work fine, but not intuitive. smaug's looks fine
> to me, but could cause some problem if I use i instead of i-1 by mistake.
> jcranmer's... I don't think it is a good idea, anyway. None of them looks
> prefect to me.
>
>
> As we have supported range-based for loop in our code, I purpose that we
> have something like ReverseIntegerRange, and use this with range-based loop
> to iterate the index. So we can use, for example: for (auto i :
> ReverseIntegerRange(array.Length()))
>
> Maybe we can also add IntegerRange to benefit from the integer type
> dedution.
>
> How does it sound?
No, please don't do this. We need to make our container classes more
similar to the STL containers, not less, and since this is about adding
new functionality, here is what we need to do:
* Add a begin() and end() function to nsTArray that return iterators
with STL semantics.
* Add a rbegin() and rend() function to nsTArray with similar semantics
which return a reverse iterator.
* Add something similar to boost::adaptors::reverse, probably to MFBT.
That way you can write:
for (auto& elem : array)
or:
for (auto& elem : mozilla::Reverse(array))
And that would work no matter whether array is an nsTArray or a std::vector.