Skip to content

Commit

Permalink
Refactor short loops
Browse files Browse the repository at this point in the history
  • Loading branch information
mmcgr committed Apr 29, 2020
1 parent 89f35e2 commit d12c536
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AllowShortLoopsOnASingleLine: false
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: true
Expand Down
4 changes: 3 additions & 1 deletion src/AstArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ class AstTerm : public AstArgument {
static VecOwn<AstArgument> asVec(Operands... ops) {
Own<AstArgument> ary[] = {std::move(ops)...};
VecOwn<AstArgument> xs;
for (auto&& x : ary) xs.push_back(std::move(x));
for (auto&& x : ary) {
xs.push_back(std::move(x));
}
return xs;
}
};
Expand Down
8 changes: 6 additions & 2 deletions src/AstSemanticChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ AstSemanticCheckerImpl::AstSemanticCheckerImpl(AstTranslationUnit& tu) : tu(tu)
}

// check rules
for (auto* rel : program.getRelations()) checkRelation(*rel);
for (auto* clause : program.getClauses()) checkClause(*clause);
for (auto* rel : program.getRelations()) {
checkRelation(*rel);
}
for (auto* clause : program.getClauses()) {
checkClause(*clause);
}

checkNamespaces();
checkIO();
Expand Down
13 changes: 9 additions & 4 deletions src/EvaluatorUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ template <typename A, typename F /* Tuple<RamDomain,1> -> void */>
void runRange(A from, A to, A step, F&& go) {
#define GO(x) go(Tuple<RamDomain, 1>{ramBitCast(x)})
if (0 < step) {
for (auto x = from; x < to; x += step) GO(x);
for (auto x = from; x < to; x += step) {
GO(x);
}
} else if (step < 0) {
for (auto x = from; to < x; x += step) GO(x);
} else if (from != to) { // `step = 0` edge case, only if non-empty range
for (auto x = from; to < x; x += step) {
GO(x);
}
} else if (from != to) {
// `step = 0` edge case, only if non-empty range
GO(from);
}
#undef GO
Expand All @@ -40,4 +45,4 @@ void runRange(A from, A to, F&& go) {
return runRange(from, to, A(from <= to ? 1 : -1), std::forward<F>(go));
}

} // namespace souffle::evaluator
} // namespace souffle::evaluator
8 changes: 6 additions & 2 deletions src/RamOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -974,7 +974,9 @@ class RamNestedIntrinsicOperator : public RamTupleOperation {

std::vector<const RamNode*> getChildNodes() const override {
auto res = RamTupleOperation::getChildNodes();
for (auto&& x : args) res.push_back(x.get());
for (auto&& x : args) {
res.push_back(x.get());
}
return res;
}

Expand All @@ -985,7 +987,9 @@ class RamNestedIntrinsicOperator : public RamTupleOperation {

void apply(const RamNodeMapper& map) override {
RamTupleOperation::apply(map);
for (auto&& x : args) x = map(std::move(x));
for (auto&& x : args) {
x = map(std::move(x));
}
}

protected:
Expand Down
33 changes: 24 additions & 9 deletions src/json11.h
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,9 @@ struct JsonParser final {
* Advance until the current character is non-whitespace.
*/
void consume_whitespace() {
while (str[i] == ' ' || str[i] == '\r' || str[i] == '\n' || str[i] == '\t') i++;
while (str[i] == ' ' || str[i] == '\r' || str[i] == '\n' || str[i] == '\t') {
i++;
}
}

/* consume_comment()
Expand Down Expand Up @@ -864,10 +866,14 @@ struct JsonParser final {
// Integer part
if (str[i] == '0') {
i++;
if (in_range(str[i], '0', '9')) return fail("leading 0s not permitted in numbers");
if (in_range(str[i], '0', '9')) {
return fail("leading 0s not permitted in numbers");
}
} else if (in_range(str[i], '1', '9')) {
i++;
while (in_range(str[i], '0', '9')) i++;
while (in_range(str[i], '0', '9')) {
i++;
}
} else {
return fail("invalid " + esc(str[i]) + " in number");
}
Expand All @@ -880,20 +886,29 @@ struct JsonParser final {
// Decimal part
if (str[i] == '.') {
i++;
if (!in_range(str[i], '0', '9')) return fail("at least one digit required in fractional part");
if (!in_range(str[i], '0', '9')) {
return fail("at least one digit required in fractional part");
}

while (in_range(str[i], '0', '9')) i++;
while (in_range(str[i], '0', '9')) {
i++;
}
}

// Exponent part
if (str[i] == 'e' || str[i] == 'E') {
i++;

if (str[i] == '+' || str[i] == '-') i++;

if (!in_range(str[i], '0', '9')) return fail("at least one digit required in exponent");
if (str[i] == '+' || str[i] == '-') {
i++;
}

while (in_range(str[i], '0', '9')) i++;
if (!in_range(str[i], '0', '9')) {
return fail("at least one digit required in exponent");
}
while (in_range(str[i], '0', '9')) {
i++;
}
}

return std::strtod(str.c_str() + start_pos, nullptr);
Expand Down
14 changes: 10 additions & 4 deletions src/test/binary_relation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,14 @@ TEST(EqRelTest, Clear) {
TEST(EqRelTest, Duplicates) {
EqRel br;
// test inserting same pair
for (int i = 0; i < 10; ++i) br.insert(0, 0);
for (int i = 0; i < 10; ++i) {
br.insert(0, 0);
}
EXPECT_EQ(br.size(), 1);

for (int i = 0; i < 10; ++i) EXPECT_TRUE(br.contains(0, 0));
for (int i = 0; i < 10; ++i) {
EXPECT_TRUE(br.contains(0, 0));
}
EXPECT_EQ(br.size(), 1);
EXPECT_FALSE(br.contains(1, 1));

Expand Down Expand Up @@ -538,8 +542,10 @@ TEST(EqRelTest, ParallelScaling) {
const int N = 100000;
std::vector<int> data1;
std::vector<int> data2;
for (int i = 0; i < N; ++i) data1.push_back(i);
for (int i = 0; i < N; ++i) data2.push_back(i);
for (int i = 0; i < N; ++i)
data1.push_back(i);
for (int i = 0; i < N; ++i)
data2.push_back(i);

std::random_device rd;
std::mt19937 generator(rd());
Expand Down
4 changes: 3 additions & 1 deletion src/test/brie_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,9 @@ namespace {
template <typename Iter>
int card(const range<Iter>& r) {
int res = 0;
for (auto it = r.begin(); it != r.end(); ++it) res++;
for (auto it = r.begin(); it != r.end(); ++it) {
res++;
}
return res;
}

Expand Down
4 changes: 3 additions & 1 deletion src/test/btree_set_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,9 @@ TEST(BTreeSet, ChunkSplitStress) {
// fill tree
test_set t;

for (int x : data) t.insert(x);
for (int x : data) {
t.insert(x);
}

for (int j = 1; j < 100; j++) {
auto chunks = t.getChunks(j);
Expand Down
12 changes: 9 additions & 3 deletions src/test/eqrel_datastructure_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,14 @@ TEST(PiggyTest, Append) {
// larger than BLOCKSIZE
constexpr size_t N = 10000;

for (size_t i = 0; i < N; ++i) pl.append(2);
for (size_t i = 0; i < N; ++i) {
pl.append(2);
}
EXPECT_EQ(pl.size(), N + 3);
// make sure that all of our inserties are exacties
for (size_t i = 3; i < N + 3; ++i) EXPECT_EQ(pl.get(i), 2);
for (size_t i = 3; i < N + 3; ++i) {
EXPECT_EQ(pl.get(i), 2);
}
}

TEST(PiggyTest, ElementCreation) {
Expand Down Expand Up @@ -311,7 +315,9 @@ TEST(DjTest, Clear) {
EXPECT_EQ(ds.size(), 0);

// get ready 2 double free y'all
for (size_t i = 0; i < 10000; ++i) ds.makeNode();
for (size_t i = 0; i < 10000; ++i) {
ds.makeNode();
}
ds.unionNodes(1, 2);
ds.clear();
ds.clear();
Expand Down

0 comments on commit d12c536

Please sign in to comment.