Skip to content

Commit

Permalink
Remove integer aliases (#1025)
Browse files Browse the repository at this point in the history
Custom aliases are just confusing to most contributors and this
increased the review cost.
  • Loading branch information
ken-matsui authored Oct 10, 2024
1 parent b8593f2 commit 23471b6
Show file tree
Hide file tree
Showing 26 changed files with 205 additions and 186 deletions.
36 changes: 19 additions & 17 deletions src/Algos.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "Command.hpp"
#include "Exception.hpp"
#include "Logger.hpp"
#include "Rustify.hpp"

#include <algorithm>
#include <cctype>
Expand Down Expand Up @@ -64,12 +63,12 @@ execCmd(const Command& cmd) noexcept {
}

std::string
getCmdOutput(const Command& cmd, const usize retry) {
getCmdOutput(const Command& cmd, const size_t retry) {
logger::debug("Running `", cmd, '`');

int exitCode = EXIT_SUCCESS;
int waitTime = 1;
for (usize i = 0; i < retry; ++i) {
for (size_t i = 0; i < retry; ++i) {
const auto [curExitCode, stdout, stderr] = cmd.output();
static_cast<void>(stderr);
if (curExitCode == EXIT_SUCCESS) {
Expand All @@ -95,33 +94,33 @@ commandExists(const std::string_view cmd) noexcept {
}

// ref: https://wandbox.org/permlink/zRjT41alOHdwcf00
static usize
static size_t
levDistance(const std::string_view lhs, const std::string_view rhs) {
const usize lhsSize = lhs.size();
const usize rhsSize = rhs.size();
const size_t lhsSize = lhs.size();
const size_t rhsSize = rhs.size();

// for all i and j, d[i,j] will hold the Levenshtein distance between the
// first i characters of s and the first j characters of t
std::vector<std::vector<usize>> dist(
lhsSize + 1, std::vector<usize>(rhsSize + 1)
std::vector<std::vector<size_t>> dist(
lhsSize + 1, std::vector<size_t>(rhsSize + 1)
);
dist[0][0] = 0;

// source prefixes can be transformed into empty string by dropping all
// characters
for (usize i = 1; i <= lhsSize; ++i) {
for (size_t i = 1; i <= lhsSize; ++i) {
dist[i][0] = i;
}

// target prefixes can be reached from empty source prefix by inserting every
// character
for (usize j = 1; j <= rhsSize; ++j) {
for (size_t j = 1; j <= rhsSize; ++j) {
dist[0][j] = j;
}

for (usize i = 1; i <= lhsSize; ++i) {
for (usize j = 1; j <= rhsSize; ++j) {
const usize substCost = lhs[i - 1] == rhs[j - 1] ? 0 : 1;
for (size_t i = 1; i <= lhsSize; ++i) {
for (size_t j = 1; j <= rhsSize; ++j) {
const size_t substCost = lhs[i - 1] == rhs[j - 1] ? 0 : 1;
dist[i][j] = std::min({
dist[i - 1][j] + 1, // deletion
dist[i][j - 1] + 1, // insertion
Expand Down Expand Up @@ -157,12 +156,12 @@ findSimilarStr(
// Keep going with the Levenshtein distance match.
// If the LHS size is less than 3, use the LHS size minus 1 and if not,
// use the LHS size divided by 3.
const usize length = lhs.size();
const usize maxDist = length < 3 ? length - 1 : length / 3;
const size_t length = lhs.size();
const size_t maxDist = length < 3 ? length - 1 : length / 3;

std::optional<std::pair<std::string_view, usize>> similarStr = std::nullopt;
std::optional<std::pair<std::string_view, size_t>> similarStr = std::nullopt;
for (const std::string_view str : candidates) {
const usize curDist = levDistance(lhs, str);
const size_t curDist = levDistance(lhs, str);
if (curDist <= maxDist) {
// The first similar string found || More similar string found
if (!similarStr.has_value() || curDist < similarStr->second) {
Expand All @@ -180,6 +179,9 @@ findSimilarStr(

#ifdef POAC_TEST

# include "Rustify/Aliases.hpp"
# include "Rustify/Tests.hpp"

# include <array>
# include <limits>

Expand Down
3 changes: 1 addition & 2 deletions src/Algos.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "Command.hpp"
#include "Rustify.hpp"

#include <optional>
#include <span>
Expand All @@ -14,7 +13,7 @@ std::string
replaceAll(std::string str, std::string_view from, std::string_view to);

int execCmd(const Command& cmd) noexcept;
std::string getCmdOutput(const Command& cmd, usize retry = 3);
std::string getCmdOutput(const Command& cmd, size_t retry = 3);
bool commandExists(std::string_view cmd) noexcept;

// ref: https://reviews.llvm.org/differential/changeset/?ref=3315514
Expand Down
27 changes: 14 additions & 13 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <algorithm>
#include <array>
#include <cctype>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
Expand Down Expand Up @@ -97,8 +98,8 @@ BuildConfig::BuildConfig(const std::string& packageName, const bool isDebug)
}

static void
emitDep(std::ostream& os, usize& offset, const std::string_view dep) {
constexpr usize maxLineLen = 80;
emitDep(std::ostream& os, size_t& offset, const std::string_view dep) {
constexpr size_t maxLineLen = 80;
if (offset + dep.size() + 2 > maxLineLen) { // 2 for space and \.
// \ for line continuation. \ is the 80th character.
os << std::setw(static_cast<int>(maxLineLen + 3 - offset)) << " \\\n ";
Expand All @@ -115,7 +116,7 @@ emitTarget(
const std::optional<std::string>& sourceFile = std::nullopt,
const std::vector<std::string>& commands = {}
) {
usize offset = 0;
size_t offset = 0;

os << target << ':';
offset += target.size() + 2; // : and space
Expand Down Expand Up @@ -145,8 +146,8 @@ BuildConfig::emitVariable(std::ostream& os, const std::string& varName) const {
const std::string left = oss.str();
os << left << ' ';

constexpr usize maxLineLen = 80; // TODO: share across sources?
usize offset = left.size() + 1; // space
constexpr size_t maxLineLen = 80; // TODO: share across sources?
size_t offset = left.size() + 1; // space
std::string value;
for (const char c : variables.at(varName).value) {
if (c == ' ') {
Expand Down Expand Up @@ -178,7 +179,7 @@ topoSort(
const std::unordered_map<std::string, T>& list,
const std::unordered_map<std::string, std::vector<std::string>>& adjList
) {
std::unordered_map<std::string, u32> inDegree;
std::unordered_map<std::string, uint32_t> inDegree;
for (const auto& var : list) {
inDegree[var.first] = 0;
}
Expand Down Expand Up @@ -627,9 +628,9 @@ BuildConfig::processSources(const std::vector<fs::path>& sourceFilePaths) {
if (isParallel()) {
tbb::spin_mutex mtx;
tbb::parallel_for(
tbb::blocked_range<usize>(0, sourceFilePaths.size()),
[&](const tbb::blocked_range<usize>& rng) {
for (usize i = rng.begin(); i != rng.end(); ++i) {
tbb::blocked_range<size_t>(0, sourceFilePaths.size()),
[&](const tbb::blocked_range<size_t>& rng) {
for (size_t i = rng.begin(); i != rng.end(); ++i) {
processSrc(sourceFilePaths[i], buildObjTargets, &mtx);
}
}
Expand Down Expand Up @@ -783,9 +784,9 @@ BuildConfig::configureBuild() {
if (isParallel()) {
tbb::spin_mutex mtx;
tbb::parallel_for(
tbb::blocked_range<usize>(0, sourceFilePaths.size()),
[&](const tbb::blocked_range<usize>& rng) {
for (usize i = rng.begin(); i != rng.end(); ++i) {
tbb::blocked_range<size_t>(0, sourceFilePaths.size()),
[&](const tbb::blocked_range<size_t>& rng) {
for (size_t i = rng.begin(); i != rng.end(); ++i) {
processUnittestSrc(
sourceFilePaths[i], buildObjTargets, testTargets, &mtx
);
Expand Down Expand Up @@ -874,7 +875,7 @@ getMakeCommand() {
makeCommand.addArg("QUIET=1");
}

const usize numThreads = getParallelism();
const size_t numThreads = getParallelism();
if (numThreads > 1) {
makeCommand.addArg("-j" + std::to_string(numThreads));
}
Expand Down
3 changes: 2 additions & 1 deletion src/BuildConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Exception.hpp"
#include "Rustify.hpp"

#include <cstdint>
#include <optional>
#include <string>
#include <string_view>
Expand All @@ -21,7 +22,7 @@ inline const std::unordered_set<std::string> HEADER_FILE_EXTS{
};
// clang-format on

enum class VarType : u8 {
enum class VarType : uint8_t {
Recursive, // =
Simple, // :=
Cond, // ?=
Expand Down
Loading

0 comments on commit 23471b6

Please sign in to comment.