From 620a16574d09e8def6fa9c88202f3f64d84d107d Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 30 Jul 2023 14:04:59 -0400 Subject: [PATCH] update hashmap to include hash, eq and alloc --- include/blt/parse/argparse.h | 37 ++++++++++++++++++++---------------- include/blt/std/hashmap.h | 33 +++++++++++++++++++++++--------- libraries/parallel-hashmap | 2 +- src/blt/parse/argparse.cpp | 23 +++++++++++++++++++++- src/tests/main.cpp | 22 ++++++++++++--------- 5 files changed, 81 insertions(+), 36 deletions(-) diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 11171e9..50d99a5 100644 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -112,13 +112,13 @@ namespace blt::parser { private: public: arg_vector a_flags; - arg_action a_action; - arg_nargs a_nargs; - std::optional a_const; - std::string a_default; - std::string a_def; - std::string a_help; - std::string a_metavar; + arg_action a_action = arg_action::STORE; + arg_nargs a_nargs = 0; + std::optional a_const{}; + std::string a_default{}; + std::string a_def{}; + std::string a_help{}; + std::string a_metavar{}; bool a_required = false; }; @@ -143,7 +143,7 @@ namespace blt::parser { public: arg_tokenizer() = default; - arg_tokenizer(const char** argv, size_t argc); + arg_tokenizer(size_t argc, const char** argv); inline void forward() { nextIndex++; @@ -166,23 +166,28 @@ namespace blt::parser { } }; - struct arg_results { - private: - - public: - - }; - class argparse { private: arg_tokenizer tokenizer; + struct { + std::vector argStorage; + HASHMAP flagAssociations; + HASHMAP nameAssociations; + } user_args; + + struct arg_results { + std::string programName; + HASHMAP positionalArgs; + + } loaded_args; + static bool validateArgument(const arg_properties& args); public: argparse() = default; void addArgument(const arg_properties& args); - + const arg_results& parse_args(int argc, const char** argv); }; } diff --git a/include/blt/std/hashmap.h b/include/blt/std/hashmap.h index 0a1be11..8caf46b 100755 --- a/include/blt/std/hashmap.h +++ b/include/blt/std/hashmap.h @@ -9,21 +9,36 @@ #ifndef HASHMAP #if defined __has_include && __has_include() -#include + + #include #include - template - using HASHMAP = phmap::flat_hash_map; - template - using HASHSET = phmap::flat_hash_set; + +template, + class Eq = phmap::priv::hash_default_eq, + class Alloc = phmap::priv::Allocator>> +using HASHMAP = phmap::flat_hash_map; +template, + class Eq = phmap::priv::hash_default_eq, + class Alloc = phmap::priv::Allocator> +using HASHSET = phmap::flat_hash_set; #else + #include #include -template -using HASHMAP = std::unordered_map; +template, + typename Eq = std::equal_to, + typename Alloc = std::allocator>> +using HASHMAP = std::unordered_map; -template -using HASHSET = std::unordered_set; +template, + typename Eq = std::equal_to, + typename Alloc = std::allocator> +using HASHSET = std::unordered_set; #endif #endif diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 77cab81..93201da 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 77cab8192a879e5d27188f97e8f2080dd7e36ca8 +Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3 diff --git a/src/blt/parse/argparse.cpp b/src/blt/parse/argparse.cpp index df7938a..c3a068c 100644 --- a/src/blt/parse/argparse.cpp +++ b/src/blt/parse/argparse.cpp @@ -96,7 +96,7 @@ namespace blt::parser { flags |= ALL; } - arg_tokenizer::arg_tokenizer(const char** argv, size_t argc) { + arg_tokenizer::arg_tokenizer(size_t argc, const char** argv) { for (size_t i = 0; i < argc; i++) args.emplace_back(argv[i]); } @@ -111,6 +111,27 @@ namespace blt::parser { BLT_WARN("(Discarding argument)"); return; } + // store one copy of the properties (arg properties could in theory be very large!) + auto pos = user_args.argStorage.size(); + user_args.argStorage.push_back(args); + auto& arg = user_args.argStorage[pos]; + // associate the arg properties per name and flag to allow for quick access when parsing + auto& names = args.a_flags.getNames(); + for (const auto& name : names) + user_args.nameAssociations[name] = &arg; + + auto& flags = args.a_flags.getFlags(); + for (const auto& flag : flags) + user_args.flagAssociations[flag] = &arg; + } + + const argparse::arg_results& argparse::parse_args(int argc, const char** argv) { + loaded_args = {}; + arg_tokenizer asToken(argc, argv); + loaded_args.programName = asToken.next(); + BLT_TRACE("Loading args for %s", loaded_args.programName.c_str()); + + return loaded_args; } diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 942618d..1ef3520 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -1,15 +1,16 @@ #include -#include "binary_trees.h" -#include "logging.h" +//#include "binary_trees.h" +//#include "logging.h" #include "profiling_tests.h" #include "nbt_tests.h" -#include "queue_tests.h" -#include "blt/math/vectors.h" -#include "blt/math/matrix.h" -#include -#include "hashmap_tests.h" -#include +#include "blt/parse/argparse.h" +//#include "queue_tests.h" +//#include "blt/math/vectors.h" +//#include "blt/math/matrix.h" +//#include +//#include "hashmap_tests.h" +//#include std::function test{ [](int i) -> int { @@ -66,7 +67,10 @@ int (*func_func)(int) = [](int i) -> int { int (*func_func_in)(int) = &test_as_func; -int main(int argc, char** argv) { +int main(int argc, const char** argv) { + blt::parser::argparse parser; + parser.addArgument({{"--foo", "-f"}}); + auto args = parser.parse_args(argc, argv); // // if (argc > 1 && std::string(argv[1]) == "--no_color") { // for (int i = (int)blt::logging::log_level::NONE; i < (int)blt::logging::log_level::FATAL; i++) {