Compare commits

..

No commits in common. "620a16574d09e8def6fa9c88202f3f64d84d107d" and "0febd6e8aa29acda9ba825dc1446e8220bc03e94" have entirely different histories.

9 changed files with 89 additions and 111 deletions

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.0)
project(BLT VERSION 0.8.0) project(BLT VERSION 0.8.0)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
@ -45,9 +45,8 @@ else()
set(PARSE_FILES "") set(PARSE_FILES "")
endif() endif()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libraries/parallel-hashmap) if(EXISTS libraries/parallel-hashmap)
message("Found Parallel Hashmaps") include_directories(libraries/parallel-hashmap)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/libraries/parallel-hashmap)
endif() endif()
#include zlib if the user has it. #include zlib if the user has it.
@ -74,9 +73,9 @@ target_include_directories(BLT PUBLIC ${CMAKE_CURRENT_BINARY_DIR}/config/)
if(${ZLIB_FOUND}) if(${ZLIB_FOUND})
target_link_libraries(BLT PUBLIC ZLIB::ZLIB) target_link_libraries(BLT PUBLIC ZLIB::ZLIB)
endif() endif()
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/libraries/parallel-hashmap) if(EXISTS ${CMAKE_SOURCE_DIR}/libraries/parallel-hashmap)
message("Including phmap") message("Including phmap")
target_include_directories(BLT PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/libraries/parallel-hashmap) target_include_directories(BLT PUBLIC libraries/parallel-hashmap)
endif() endif()
if(MSVC) if(MSVC)

View File

@ -17,7 +17,27 @@
#include "blt/std/filesystem.h" #include "blt/std/filesystem.h"
#include "blt/std/logging.h" #include "blt/std/logging.h"
#include <blt/std/hashmap.h> #ifndef HASHMAP
#if defined __has_include && __has_include(<parallel_hashmap/phmap.h>)
#define HASHMAP HASHMAP
#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>();
#else
#define HASHMAP HASHMAP
#include <unordered_map>
#include <set>
template<typename K, typename V>
using HASHMAP = std::unordered_map<K, V>();
template<typename K>
using HASHSET = std::unordered_set<K>();
#endif
#endif
namespace blt::nbt { namespace blt::nbt {

View File

@ -11,7 +11,30 @@
#include <string> #include <string>
#include <initializer_list> #include <initializer_list>
#include <optional> #include <optional>
#include <blt/std/hashmap.h>
#ifndef HASHMAP
#if defined __has_include && __has_include(<parallel_hashmap/phmap.h>)
#define HASHMAP HASHMAP
#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>();
#else
#define HASHMAP HASHMAP
#include <unordered_map>
#include <set>
template<typename K, typename V>
using HASHMAP = std::unordered_map<K, V>();
template<typename K>
using HASHSET = std::unordered_set<K>();
#endif
#endif
namespace blt::parser { namespace blt::parser {
@ -112,13 +135,13 @@ namespace blt::parser {
private: private:
public: public:
arg_vector a_flags; arg_vector a_flags;
arg_action a_action = arg_action::STORE; arg_action a_action;
arg_nargs a_nargs = 0; arg_nargs a_nargs;
std::optional<std::string> a_const{}; std::optional<std::string> a_const;
std::string a_default{}; std::string a_default;
std::string a_def{}; std::string a_def;
std::string a_help{}; std::string a_help;
std::string a_metavar{}; std::string a_metavar;
bool a_required = false; bool a_required = false;
}; };
@ -143,7 +166,7 @@ namespace blt::parser {
public: public:
arg_tokenizer() = default; arg_tokenizer() = default;
arg_tokenizer(size_t argc, const char** argv); arg_tokenizer(const char** argv, size_t argc);
inline void forward() { inline void forward() {
nextIndex++; nextIndex++;
@ -166,28 +189,23 @@ namespace blt::parser {
} }
}; };
struct arg_results {
private:
public:
};
class argparse { class argparse {
private: private:
arg_tokenizer tokenizer; 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); static bool validateArgument(const arg_properties& args);
public: public:
argparse() = default; argparse() = default;
void addArgument(const arg_properties& args); void addArgument(const arg_properties& args);
const arg_results& parse_args(int argc, const char** argv);
}; };
} }

11
include/blt/std/hash_map.h Executable file
View File

@ -0,0 +1,11 @@
/*
* Created by Brett on 31/03/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef BLT_HASH_MAP_H
#define BLT_HASH_MAP_H
#endif //BLT_HASH_MAP_H

View File

@ -1,45 +0,0 @@
/*
* Created by Brett on 31/03/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef BLT_HASH_MAP_H
#define BLT_HASH_MAP_H
#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<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,
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,
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
#endif //BLT_HASH_MAP_H

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

View File

@ -96,7 +96,7 @@ namespace blt::parser {
flags |= ALL; flags |= ALL;
} }
arg_tokenizer::arg_tokenizer(size_t argc, const char** argv) { arg_tokenizer::arg_tokenizer(const char** argv, size_t argc) {
for (size_t i = 0; i < argc; i++) for (size_t i = 0; i < argc; i++)
args.emplace_back(argv[i]); args.emplace_back(argv[i]);
} }
@ -111,27 +111,6 @@ namespace blt::parser {
BLT_WARN("(Discarding argument)"); BLT_WARN("(Discarding argument)");
return; 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

@ -7,7 +7,7 @@
#ifndef BLT_TESTS_HASHMAP_TEXTS_H #ifndef BLT_TESTS_HASHMAP_TEXTS_H
#define BLT_TESTS_HASHMAP_TEXTS_H #define BLT_TESTS_HASHMAP_TEXTS_H
#include <blt/std/hashmap.h> #include <blt/std/hash_map.h>
inline static int test_hashmaps(){ inline static int test_hashmaps(){

View File

@ -1,16 +1,15 @@
#include <unordered_map> #include <unordered_map>
//#include "binary_trees.h" #include "binary_trees.h"
//#include "logging.h" #include "logging.h"
#include "profiling_tests.h" #include "profiling_tests.h"
#include "nbt_tests.h" #include "nbt_tests.h"
#include "blt/parse/argparse.h" #include "queue_tests.h"
//#include "queue_tests.h" #include "blt/math/vectors.h"
//#include "blt/math/vectors.h" #include "blt/math/matrix.h"
//#include "blt/math/matrix.h" #include <bitset>
//#include <bitset> #include "hashmap_tests.h"
//#include "hashmap_tests.h" #include <functional>
//#include <functional>
std::function<int(int i)> test{ std::function<int(int i)> test{
[](int i) -> int { [](int i) -> int {
@ -67,10 +66,7 @@ int (*func_func)(int) = [](int i) -> int {
int (*func_func_in)(int) = &test_as_func; int (*func_func_in)(int) = &test_as_func;
int main(int argc, const char** argv) { int main(int argc, 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") { // 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++) { // for (int i = (int)blt::logging::log_level::NONE; i < (int)blt::logging::log_level::FATAL; i++) {