add str_concat and func_memory

This commit is contained in:
chatlanin 2022-03-27 12:13:50 +03:00
parent 9d86def5ac
commit cac1c694c1
16 changed files with 201 additions and 63 deletions

View File

@ -5,41 +5,49 @@
#include <vector>
#include <iomanip>
#include <type_traits>
#include <string_view>
#include "string/string.hpp"
#include "string/string_concat_helper.hpp"
#include "range/range.hpp"
#include "container/container.hpp"
#include "logger/logger.hpp"
#include "math/matrix.hpp"
#include "math/vector.hpp"
#include "utils/utils.hpp"
#include <string_view>
// for example
int f(int a)
{
hack::log()("f implementatioln");
return ++a;
}
int main(int argc, char *argv[])
{
// {// ex: split_str
// {// ex: string::split_str
// std::string str { "asdf,qwer,zxcv" };
// hack::string::v_str v = hack::string::split_str(str, ',');
// for (const auto& c : v) hack::log()(c);
// }
//
// {// ex: within
// {// ex: renge::within
// hack::log()(hack::range::within(12, 34, 12, 23, 31, 17, 22, 33));
// }
//
// {// ex: vector_multiset
// {// ex: container::vector_multiset
// std::vector<std::string> v;
// hack::container::vector_multiset(v, "asdf", "qwer", "zcv");
// for(const auto& c : v) std::cout << c << std::endl;
// }
//
// {// ex: set_multiset
// {// ex: container::set_multiset
// std::set<int> s;
// hack::container::set_multiset(s, 1, 2, 3, 3, 2, 1);
// for(const auto& c : s) std::cout << c << std::endl;
// }
//
// {// ex: log
// {// ex: logger::log
// hack::log()(1234, "run in main", 1234);
// hack::warn(" # ")(1234, "run in main", 1234);
// hack::error(" - ")(1234, "run in main", 1234);
@ -60,18 +68,18 @@ int main(int argc, char *argv[])
// hack::log()(tp);
// }
//
// {// ex: matches
// {// ex: container::matches
// std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// hack::log()(hack::container::matches(v, 2, 5, 4, 12));
// }
//
// {// ex: vector_remove_at
// {// ex: container::vector_remove_at
// std::vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// hack::container::vector_remove_at(v, 3);
// hack::log()(v);
// }
// {// ex: matrix
// {// ex: math::matrix
// hack::matrix<int, 3> m_int;
// hack::matrix<int, 3> m_int_c { { 2, 3, 4, 123 }, { 0, 2, 4, 555 } };
// hack::matrix<float, 3> m_float;
@ -85,7 +93,7 @@ int main(int argc, char *argv[])
// auto i = m_int[0][0][0];
// auto f = m_float[0][0][0];
//
// hack::log()("m_int", i);
// hack::log()("m_int", i);
// hack::log()("m_float", f);
// hack::log()("empty", m_float[123][22][33]);
//
@ -100,7 +108,7 @@ int main(int argc, char *argv[])
// m_int = std::forward<hack::matrix<int, 3>>(m_int_c);
// hack::log("")("moved data: ", m_int);
//
// hack::matrix<int, 3> m_int_d = m_int;
// hack::matrix<int, 3> m_int_d = m_int;
// hack::log("")("copy data: ", m_int_d);
//
// hack::matrix<int, 3> m_int_cd { { 2, 3, 4, 3 }, { 0, 2, 4, 5 } };
@ -108,29 +116,43 @@ int main(int argc, char *argv[])
// m_int_cd = m_int;
// hack::log("")("copy data: ", m_int_cd);
// }
//
// {// ex: math::vector
// hack::vector<int, int, int> v3_1 { 8, 4, 9, };
// hack::vector<int, int, int> v3_2 { 1, 2, 3, };
// hack::log()(v3_1.get_value());
// hack::log()(v3_2.get_value());
//
// v3_1 = v3_2;
//
// hack::log()(v3_1.get_value());
// hack::log()(v3_2.get_value());
//
// hack::log()("length 3", v3_2.length());
//
// hack::vector<int, int> v2_1 { 11, 22 };
// hack::log()("length 2", v2_1.length());
//
// hack::vector<int, int, int> lerp_1 { 1, 2, 3 };
// hack::vector<int, int, int> lerp_2 { 5, 6, 7 };
// hack::log()("lerp", lerp_1.lerp(lerp_2, 0.75f));
//
// auto [x, y, z] = lerp_1.get_value();
// hack::log()("get", x, y, z);
// hack::log()("get", lerp_1.x());
// }
//
// {// ex: utils::func_memory
// auto cach_f = hack::utils::func_memory(f);
// hack::log()("result 1", cach_f(12));
// hack::log()("result 2", cach_f(12));
// }
{// ex: vector
hack::vector<int, int, int> v3_1 { 8, 4, 9, };
hack::vector<int, int, int> v3_2 { 1, 2, 3, };
hack::log()(v3_1.get_value());
hack::log()(v3_2.get_value());
v3_1 = v3_2;
hack::log()(v3_1.get_value());
hack::log()(v3_2.get_value());
hack::log()("length 3", v3_2.length());
hack::vector<int, int> v2_1 { 11, 22 };
hack::log()("length 2", v2_1.length());
hack::vector<int, int, int> lerp_1 { 1, 2, 3 };
hack::vector<int, int, int> lerp_2 { 5, 6, 7 };
hack::log()("lerp", lerp_1.lerp(lerp_2, 0.75f));
auto [x, y, z] = lerp_1.get_value();
hack::log()("get", x, y, z);
hack::log()("get", lerp_1.x());
{// ex: string::str_concat
std::string name = "tro";
std::string surname = "lolo";
const auto full_name = hack::string::str_concat + name + ", " + surname;
hack::log()(full_name);
hack::log()(hack::string::str_concat + "super", + "string");
}
}

View File

@ -28,6 +28,18 @@ deps = []
inc = []
inc += include_directories('.')
conf = configuration_data()
check_headers = [
['ncurses.h', 'HAVE_NCURSES_H'],
['curses.h', 'HAVE_CURSES_H'],
]
foreach h : check_headers
if compiler.has_header(h.get(0))
conf.set(h.get(1), 1)
endif
endforeach
subdir('src')
subdir('bin')
subdir('tests')

View File

@ -4,7 +4,6 @@ sources = []
lib = library(
'concepts',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -4,7 +4,6 @@ sources = ['container.cpp']
lib = library(
'container',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -4,7 +4,6 @@ sources = []
lib = library(
'iterators',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -4,7 +4,6 @@ sources = ['logger.cpp']
lib = library(
'logger',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -3,13 +3,22 @@
#include <memory>
#include <vector>
#include <tuple>
#include <algorithm>
#include <iostream>
#include "utils/utils.hpp"
namespace hack::matrix_utils
{
template<typename T, std::size_t N>
struct generate_tuple
{
using type = decltype(std::tuple_cat(typename generate_tuple<T, N - 1>::type{}, std::make_tuple(T{})));
};
template<typename T>
struct generate_tuple<T, 1>
{
using type = std::tuple<T>;
};
template<typename T,typename index_data, typename index_t>
class proxy
{
@ -17,7 +26,6 @@ namespace hack::matrix_utils
public:
proxy(const std::weak_ptr<index_data>& local_storage, const index_t& index) : local_storage_ { local_storage }, index_ { index } {};
~proxy() = default;
auto operator[](std::size_t index) const
{
@ -58,7 +66,7 @@ namespace hack
template<typename T, std::size_t dimensions>
class matrix
{
using index_t = typename utils::generate_tuple<std::size_t, dimensions>::type;
using index_t = typename matrix_utils::generate_tuple<std::size_t, dimensions>::type;
using vector_t = decltype(std::tuple_cat(index_t{}, std::make_tuple(T{})));
using index_data_t = std::vector<vector_t>;
@ -70,7 +78,6 @@ namespace hack
}
matrix(matrix& mt) noexcept : local_storage_ { mt.local_storage_ } { }
matrix(matrix&& mt) noexcept : local_storage_ { mt.local_storage_ } { }
~matrix() = default;
matrix& operator=(matrix&& mt)
{

View File

@ -4,7 +4,6 @@ sources = []
lib = library(
'math',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -53,13 +53,13 @@ namespace hack
auto z() const
{
if (std::tuple_size<value_t>{} < 3) throw std::out_of_range("You try get no valid vector date!");
if (std::tuple_size<value_t>{} < 3) throw std::out_of_range("You try get no valid vector data!");
return std::get<2>(value_);
}
auto w() const
{
if (std::tuple_size<value_t>{} < 4) throw std::out_of_range("You try get no valid vector date!");
if (std::tuple_size<value_t>{} < 4) throw std::out_of_range("You try get no valid vector data!");
return std::get<3>(value_);
}

View File

@ -4,7 +4,6 @@ sources = ['range.cpp']
lib = library(
'range',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -1,10 +1,9 @@
headers = ['string.hpp']
headers = ['string.hpp', 'string_concat_helper.hpp']
sources = ['string.cpp']
lib = library(
'string',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -2,6 +2,9 @@
#include <string>
#include <vector>
#include <iostream>
#include <tuple>
namespace hack::string
{

View File

@ -0,0 +1,79 @@
#pragma once
#include <string>
#include <vector>
#include <iostream>
#include <tuple>
namespace hack::string
{
template <typename... Strings>
class string_concat_helper;
template <typename String, typename... Strings>
class string_concat_helper<String, Strings...>
{
using string_part = string_concat_helper<Strings...>;
using string_long = string_concat_helper<std::string, String, Strings...>;
private:
const String& data;
string_part tail;
public:
string_concat_helper(const String& data_, string_part tail_) : data { data_ } , tail { tail_ } {}
int size() const
{
return data.size() + tail.size();
}
template <typename It>
void save(It end) const
{
const auto begin = end - data.size();
std::copy(data.cbegin(), data.cend(), begin);
tail.save(begin);
}
operator std::string() const
{
std::string result(size(), '\0');
save(result.end());
return result;
}
string_long operator+(const std::string& other) const
{
return string_long(other, *this);
}
};
template <>
class string_concat_helper<>
{
using string_part = string_concat_helper<std::string>;
public:
int size() const
{
return 0;
}
template <typename It>
void save(It) const { }
string_part operator+(const std::string& other) const
{
return string_part(other, *this);
}
};
template <typename Stream, typename... Strings>
Stream& operator<<(Stream& stream, const string_concat_helper<Strings...> strings)
{
return stream << static_cast<std::string>(strings);
}
inline string_concat_helper<> str_concat;
}

View File

@ -4,7 +4,6 @@ sources = []
lib = library(
'utils',
include_directories : inc,
install : true,
sources: [headers, sources]
)

View File

@ -1,19 +1,43 @@
#pragma once
#include <iostream>
#include <unordered_map>
#include <map>
namespace hack::utils
{
template<typename T, std::size_t N>
struct generate_tuple
template<typename Result, typename... Args>
auto func_memory(Result (*f)(Args...))
{
using type = decltype(std::tuple_cat(typename generate_tuple<T, N - 1>::type{}, std::make_tuple(T{})));
};
std::map<std::tuple<Args...>, Result> cache;
template<typename T>
struct generate_tuple<T, 1>
{
using type = std::tuple<T>;
};
return [f, cache](Args... args) mutable -> Result
{
const auto key = std::make_tuple(args...);
const auto cached = cache.find(key);
if(cached == cache.end())
{
auto result = f(args...);
cache[key] = result;
return result;
}
return cached->second;
};
}
}
// std::map<std::tuple<Args...>, Result> cache;
//
// return [f, cache](Args... args) mutable -> Result
// {
// const auto args_tuple = std::make_tuple(args...);
// const auto cached = cache.find(args_tuple);
//
// if (cached == cache.end())
// {
// auto result = f(args...);
// cache[args_tuple] = result;
// return result;
// }
// else
// return cached->second

View File

@ -4,7 +4,6 @@ sources = []
lib = library(
'view',
include_directories : inc,
install : true,
sources: [headers, sources]
)