update hashmap to include hash, eq and alloc

v1
Brett 2023-07-30 14:04:59 -04:00
parent 147c46a1a4
commit 620a16574d
5 changed files with 81 additions and 36 deletions

View File

@ -112,13 +112,13 @@ namespace blt::parser {
private:
public:
arg_vector a_flags;
arg_action a_action;
arg_nargs a_nargs;
std::optional<std::string> 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<std::string> 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<arg_properties> argStorage;
HASHMAP<std::string, arg_properties*> flagAssociations;
HASHMAP<std::string, arg_properties*> nameAssociations;
} user_args;
struct arg_results {
std::string programName;
HASHMAP<std::string, std::string> 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);
};
}

View File

@ -9,21 +9,36 @@
#ifndef HASHMAP
#if defined __has_include && __has_include(<parallel_hashmap/phmap.h>)
#include <parallel_hashmap/phmap.h>
#include <parallel_hashmap/phmap_fwd_decl.h>
template<typename K, typename V>
using HASHMAP = phmap::flat_hash_map<K, V>;
template<typename K>
using HASHSET = phmap::flat_hash_set<K>;
template<class K, class V,
class Hash = phmap::priv::hash_default_hash<K>,
class Eq = phmap::priv::hash_default_eq<K>,
class Alloc = phmap::priv::Allocator<phmap::priv::Pair<const K, V>>>
using HASHMAP = phmap::flat_hash_map<K, V, Hash, Eq, Alloc>;
template<class T,
class Hash = phmap::priv::hash_default_hash<T>,
class Eq = phmap::priv::hash_default_eq<T>,
class Alloc = phmap::priv::Allocator<T>>
using HASHSET = phmap::flat_hash_set<T, Hash, Eq, Alloc>;
#else
#include <unordered_map>
#include <unordered_set>
template<typename K, typename V>
using HASHMAP = std::unordered_map<K, V>;
template<typename K, typename V,
typename Hash = std::hash<K>,
typename Eq = std::equal_to<K>,
typename Alloc = std::allocator<std::pair<const K, V>>>
using HASHMAP = std::unordered_map<K, V, Hash, Eq, Alloc>;
template<typename K>
using HASHSET = std::unordered_set<K>;
template<typename K,
typename Hash = std::hash<K>,
typename Eq = std::equal_to<K>,
typename Alloc = std::allocator<K>>
using HASHSET = std::unordered_set<K, Hash, Eq, Alloc>;
#endif
#endif

@ -1 +1 @@
Subproject commit 77cab8192a879e5d27188f97e8f2080dd7e36ca8
Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3

View File

@ -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;
}

View File

@ -1,15 +1,16 @@
#include <unordered_map>
#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 <bitset>
#include "hashmap_tests.h"
#include <functional>
#include "blt/parse/argparse.h"
//#include "queue_tests.h"
//#include "blt/math/vectors.h"
//#include "blt/math/matrix.h"
//#include <bitset>
//#include "hashmap_tests.h"
//#include <functional>
std::function<int(int i)> 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++) {