use irange for loops 10 (#69394)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/69394

Modified loops in files under fbsource/fbcode/caffe2/ from the format
```
for(TYPE var=x0;var<x_max;x++)
```
to the format
```
for(const auto var: irange(xmax))
```

This was achieved by running r-barnes's loop upgrader script (D28874212) with some modification to exclude all files under /torch/jit and a number of reversions or unused variable suppression warnings added by hand.

Test Plan: Sandcastle

Reviewed By: malfet

Differential Revision: D32837991

fbshipit-source-id: fc7c4f76d2f32a17a0faf329294b3fe7cb81df32
This commit is contained in:
Richard Barnes
2021-12-09 09:44:04 -08:00
committed by Facebook GitHub Bot
parent 2d5b3101c1
commit afb742382a
11 changed files with 103 additions and 85 deletions
+5 -4
View File
@@ -1,5 +1,6 @@
#include <gtest/gtest.h>
#include <c10/util/irange.h>
#include <torch/torch.h>
#include <test/cpp/api/support.h>
@@ -13,7 +14,7 @@ struct ExpandingArrayTest : torch::test::SeedingFixture {};
TEST_F(ExpandingArrayTest, CanConstructFromInitializerList) {
torch::ExpandingArray<5> e({1, 2, 3, 4, 5});
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
for (const auto i : c10::irange(e.size())) {
ASSERT_EQ((*e)[i], i + 1);
}
}
@@ -21,7 +22,7 @@ TEST_F(ExpandingArrayTest, CanConstructFromInitializerList) {
TEST_F(ExpandingArrayTest, CanConstructFromVector) {
torch::ExpandingArray<5> e(std::vector<int64_t>{1, 2, 3, 4, 5});
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
for (const auto i : c10::irange(e.size())) {
ASSERT_EQ((*e)[i], i + 1);
}
}
@@ -29,7 +30,7 @@ TEST_F(ExpandingArrayTest, CanConstructFromVector) {
TEST_F(ExpandingArrayTest, CanConstructFromArray) {
torch::ExpandingArray<5> e(std::array<int64_t, 5>({1, 2, 3, 4, 5}));
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
for (const auto i : c10::irange(e.size())) {
ASSERT_EQ((*e)[i], i + 1);
}
}
@@ -37,7 +38,7 @@ TEST_F(ExpandingArrayTest, CanConstructFromArray) {
TEST_F(ExpandingArrayTest, CanConstructFromSingleValue) {
torch::ExpandingArray<5> e(5);
ASSERT_EQ(e.size(), 5);
for (size_t i = 0; i < e.size(); ++i) {
for (const auto i : c10::irange(e.size())) {
ASSERT_EQ((*e)[i], 5);
}
}