Skip to content

Commit

Permalink
Remove HashSet alias (#962)
Browse files Browse the repository at this point in the history
closes: #953
  • Loading branch information
ken-matsui authored Jul 23, 2024
1 parent f85bddf commit b322899
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 47 deletions.
64 changes: 34 additions & 30 deletions src/BuildConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <tbb/spin_mutex.h>
#include <thread>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

Expand Down Expand Up @@ -92,7 +93,7 @@ operator<<(std::ostream& os, const Variable& var) {
struct Target {
std::vector<std::string> commands;
std::optional<std::string> sourceFile;
HashSet<std::string> remDeps;
std::unordered_set<std::string> remDeps;
};

struct BuildConfig {
Expand All @@ -103,8 +104,8 @@ struct BuildConfig {
std::unordered_map<std::string, std::vector<std::string>> varDeps;
std::unordered_map<std::string, Target> targets;
std::unordered_map<std::string, std::vector<std::string>> targetDeps;
std::optional<HashSet<std::string>> phony;
std::optional<HashSet<std::string>> all;
std::optional<std::unordered_set<std::string>> phony;
std::optional<std::unordered_set<std::string>> all;

std::string OUT_DIR;
std::string CXX = "clang++";
Expand Down Expand Up @@ -133,7 +134,7 @@ struct BuildConfig {

void defineVar(
const std::string& name, const Variable& value,
const HashSet<std::string>& dependsOn = {}
const std::unordered_set<std::string>& dependsOn = {}
) {
variables[name] = value;
for (const std::string& dep : dependsOn) {
Expand All @@ -143,20 +144,20 @@ struct BuildConfig {
}
void defineSimpleVar(
const std::string& name, const std::string& value,
const HashSet<std::string>& dependsOn = {}
const std::unordered_set<std::string>& dependsOn = {}
) {
defineVar(name, { value, VarType::Simple }, dependsOn);
}
void defineCondVar(
const std::string& name, const std::string& value,
const HashSet<std::string>& dependsOn = {}
const std::unordered_set<std::string>& dependsOn = {}
) {
defineVar(name, { value, VarType::Cond }, dependsOn);
}

void defineTarget(
const std::string& name, const std::vector<std::string>& commands,
const HashSet<std::string>& remDeps = {},
const std::unordered_set<std::string>& remDeps = {},
const std::optional<std::string>& sourceFile = std::nullopt
) {
targets[name] = { commands, sourceFile, remDeps };
Expand All @@ -178,7 +179,7 @@ struct BuildConfig {
}
}

void setAll(const HashSet<std::string>& dependsOn) {
void setAll(const std::unordered_set<std::string>& dependsOn) {
all = dependsOn;
}

Expand All @@ -194,7 +195,8 @@ struct BuildConfig {

void processSrc(
BuildConfig& config, const fs::path& sourceFilePath,
HashSet<std::string>& buildObjTargets, tbb::spin_mutex* mtx = nullptr
std::unordered_set<std::string>& buildObjTargets,
tbb::spin_mutex* mtx = nullptr
) const;
};

Expand All @@ -213,7 +215,7 @@ emitDep(std::ostream& os, usize& offset, const std::string_view dep) {
static void
emitTarget(
std::ostream& os, const std::string_view target,
const HashSet<std::string>& dependsOn,
const std::unordered_set<std::string>& dependsOn,
const std::optional<std::string>& sourceFile = std::nullopt,
const std::vector<std::string>& commands = {}
) {
Expand Down Expand Up @@ -378,13 +380,13 @@ BuildConfig::runMM(const std::string& sourceFile, const bool isTest) const {
return getCmdOutput(command);
}

static HashSet<std::string>
static std::unordered_set<std::string>
parseMMOutput(const std::string& mmOutput, std::string& target) {
std::istringstream iss(mmOutput);
std::getline(iss, target, ':');

std::string dependency;
HashSet<std::string> deps;
std::unordered_set<std::string> deps;
bool isFirst = true;
while (std::getline(iss, dependency, ' ')) {
if (!dependency.empty() && dependency.front() != '\\') {
Expand Down Expand Up @@ -474,8 +476,8 @@ printfCmd(const std::string_view header, const std::string_view body) {
static void
defineCompileTarget(
BuildConfig& config, const std::string& objTarget,
const std::string& sourceFile, const HashSet<std::string>& remDeps,
const bool isTest = false
const std::string& sourceFile,
const std::unordered_set<std::string>& remDeps, const bool isTest = false
) {
std::vector<std::string> commands;
commands.push_back("@mkdir -p $(@D)");
Expand All @@ -490,7 +492,7 @@ defineCompileTarget(
static void
defineLinkTarget(
BuildConfig& config, const std::string& binTarget,
const HashSet<std::string>& deps
const std::unordered_set<std::string>& deps
) {
std::vector<std::string> commands;
commands.push_back("$(CXX) $(CXXFLAGS) $^ $(LIBS) -o $@");
Expand Down Expand Up @@ -523,9 +525,11 @@ mapHeaderToObj(const fs::path& headerPath, const fs::path& buildOutDir) {
// depending header files for the source file.
static void
collectBinDepObjs( // NOLINT(misc-no-recursion)
HashSet<std::string>& deps, const std::string_view sourceFileName,
const HashSet<std::string>& objTargetDeps,
const HashSet<std::string>& buildObjTargets, const BuildConfig& config
std::unordered_set<std::string>& deps,
const std::string_view sourceFileName,
const std::unordered_set<std::string>& objTargetDeps,
const std::unordered_set<std::string>& buildObjTargets,
const BuildConfig& config
) {
for (const fs::path headerPath : objTargetDeps) {
if (sourceFileName == headerPath.stem()) {
Expand Down Expand Up @@ -649,10 +653,10 @@ BuildConfig::setVariables(const bool isDebug) {
void
BuildConfig::processSrc(
BuildConfig& config, const fs::path& sourceFilePath,
HashSet<std::string>& buildObjTargets, tbb::spin_mutex* mtx
std::unordered_set<std::string>& buildObjTargets, tbb::spin_mutex* mtx
) const {
std::string objTarget; // source.o
const HashSet<std::string> objTargetDeps =
const std::unordered_set<std::string> objTargetDeps =
parseMMOutput(runMM(sourceFilePath), objTarget);

const fs::path targetBaseDir = fs::relative(
Expand All @@ -675,11 +679,11 @@ BuildConfig::processSrc(
}
}

static HashSet<std::string>
static std::unordered_set<std::string>
processSources(
BuildConfig& config, const std::vector<fs::path>& sourceFilePaths
) {
HashSet<std::string> buildObjTargets;
std::unordered_set<std::string> buildObjTargets;

if (isParallel()) {
tbb::spin_mutex mtx;
Expand All @@ -705,9 +709,9 @@ processSources(
static void
processTestSrc(
BuildConfig& config, const fs::path& sourceFilePath,
const HashSet<std::string>& buildObjTargets,
std::vector<std::string>& testCommands, HashSet<std::string>& testTargets,
tbb::spin_mutex* mtx = nullptr
const std::unordered_set<std::string>& buildObjTargets,
std::vector<std::string>& testCommands,
std::unordered_set<std::string>& testTargets, tbb::spin_mutex* mtx = nullptr
) {
if (!config.containsTestCode(
sourceFilePath.string().substr(PATH_FROM_OUT_DIR.size())
Expand All @@ -716,7 +720,7 @@ processTestSrc(
}

std::string objTarget; // source.o
const HashSet<std::string> objTargetDeps =
const std::unordered_set<std::string> objTargetDeps =
parseMMOutput(config.runMM(sourceFilePath, /*isTest=*/true), objTarget);

const fs::path targetBaseDir = fs::relative(
Expand All @@ -734,7 +738,7 @@ processTestSrc(
(testTargetBaseDir / "test_").string() + testTargetName;

// Test binary target.
HashSet<std::string> testTargetDeps = { testObjTarget };
std::unordered_set<std::string> testTargetDeps = { testObjTarget };
collectBinDepObjs(
testTargetDeps, sourceFilePath.stem().string(), objTargetDeps,
buildObjTargets, config
Expand Down Expand Up @@ -795,12 +799,12 @@ configureBuild(BuildConfig& config, const bool isDebug) {
config.defineSimpleVar("SRCS", srcs);

// Source Pass
const HashSet<std::string> buildObjTargets =
const std::unordered_set<std::string> buildObjTargets =
processSources(config, sourceFilePaths);

// Project binary target.
const std::string mainObjTarget = config.buildOutDir / "main.o";
HashSet<std::string> projTargetDeps = { mainObjTarget };
std::unordered_set<std::string> projTargetDeps = { mainObjTarget };
collectBinDepObjs(
projTargetDeps, "",
config.targets.at(mainObjTarget).remDeps, // we don't need sourceFile
Expand All @@ -816,7 +820,7 @@ configureBuild(BuildConfig& config, const bool isDebug) {

// Test Pass
std::vector<std::string> testCommands;
HashSet<std::string> testTargets;
std::unordered_set<std::string> testTargets;
if (isParallel()) {
tbb::spin_mutex mtx;
tbb::parallel_for(
Expand Down
7 changes: 3 additions & 4 deletions src/BuildConfig.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
#pragma once

#include "Rustify.hpp"

#include <string>
#include <unordered_set>

// clang-format off
inline const HashSet<std::string> SOURCE_FILE_EXTS{
inline const std::unordered_set<std::string> SOURCE_FILE_EXTS{
".c", ".c++", ".cc", ".cpp", ".cxx"
};
inline const HashSet<std::string> HEADER_FILE_EXTS{
inline const std::unordered_set<std::string> HEADER_FILE_EXTS{
".h", ".h++", ".hh", ".hpp", ".hxx"
};
// clang-format on
Expand Down
11 changes: 6 additions & 5 deletions src/Cmd/Add.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <string_view>
#include <toml.hpp>
#include <unordered_map>
#include <unordered_set>

static int addMain(std::span<const std::string_view> args);

Expand Down Expand Up @@ -53,7 +54,7 @@ handleNextArg(

static void
handleDependency(
HashSet<std::string_view>& newDeps, const std::string_view dep
std::unordered_set<std::string_view>& newDeps, const std::string_view dep
) {
if (newDeps.contains(dep)) {
logger::warn("The dependency `", dep, "` is already in the poac.toml");
Expand Down Expand Up @@ -97,9 +98,9 @@ getDependencyName(const std::string_view dep) {

static int
addDependencyToManifest(
const HashSet<std::string_view>& newDeps, bool isSystemDependency,
std::string& version, std::string& tag, std::string& rev,
std::string& branch
const std::unordered_set<std::string_view>& newDeps,
bool isSystemDependency, std::string& version, std::string& tag,
std::string& rev, std::string& branch
) {
toml::value depData = toml::table{};

Expand Down Expand Up @@ -157,7 +158,7 @@ addMain(const std::span<const std::string_view> args) {
return EXIT_FAILURE;
}

HashSet<std::string_view> newDeps = {};
std::unordered_set<std::string_view> newDeps = {};

bool isSystemDependency = false;
std::string version; // Only used with system-dependencies
Expand Down
1 change: 0 additions & 1 deletion src/Cmd/Build.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#include "../Cli.hpp"
#include "../Rustify.hpp"

#include <string>

Expand Down
5 changes: 3 additions & 2 deletions src/Manifest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <string>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>

Expand Down Expand Up @@ -232,7 +233,7 @@ validatePackageName(const std::string_view name) noexcept {
}

// Using C++ keywords
const HashSet<std::string_view> keywords = {
const std::unordered_set<std::string_view> keywords = {
#include "Keywords.def"
};
if (keywords.contains(name)) {
Expand Down Expand Up @@ -435,7 +436,7 @@ static const fs::path CACHE_DIR(getXdgCacheHome() / "poac");
static const fs::path GIT_DIR(CACHE_DIR / "git");
static const fs::path GIT_SRC_DIR(GIT_DIR / "src");

static const HashSet<char> ALLOWED_CHARS = {
static const std::unordered_set<char> ALLOWED_CHARS = {
'-', '_', '/', '.', '+' // allowed in the dependency name
};

Expand Down
3 changes: 2 additions & 1 deletion src/Manifest.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <unordered_set>
#include <vector>

struct DepMetadata {
Expand All @@ -15,7 +16,7 @@ struct DepMetadata {
};

struct Profile {
HashSet<std::string> cxxflags;
std::unordered_set<std::string> cxxflags;
bool lto = false;
std::optional<bool> debug = std::nullopt;
std::optional<usize> opt_level = std::nullopt;
Expand Down
3 changes: 0 additions & 3 deletions src/Rustify/Aliases.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ using f32 = float;
using f64 = double;
// NOLINTEND(readability-identifier-naming)

template <typename K>
using HashSet = std::unordered_set<K>;

// NOLINTBEGIN(google-global-names-in-headers)
using std::literals::string_literals::operator""s;
using std::literals::string_view_literals::operator""sv;
Expand Down
2 changes: 1 addition & 1 deletion srcOld/Resolver/Resolve.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ hash_value(const Cache& i) -> usize {
return seed;
}

using IntervalCache = HashSet<Cache>;
using IntervalCache = std::unordered_set<Cache>;

inline auto
cache_exists(const IntervalCache& cache, const Package& package) -> bool {
Expand Down

0 comments on commit b322899

Please sign in to comment.