add str_concat and func_memory
This commit is contained in:
@@ -4,7 +4,6 @@ sources = []
|
||||
lib = library(
|
||||
'concepts',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ sources = ['container.cpp']
|
||||
lib = library(
|
||||
'container',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ sources = []
|
||||
lib = library(
|
||||
'iterators',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ sources = ['logger.cpp']
|
||||
lib = library(
|
||||
'logger',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
@@ -4,7 +4,6 @@ sources = []
|
||||
lib = library(
|
||||
'math',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
@@ -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_);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,6 @@ sources = ['range.cpp']
|
||||
lib = library(
|
||||
'range',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
@@ -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]
|
||||
)
|
||||
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
#include <tuple>
|
||||
|
||||
|
||||
namespace hack::string
|
||||
{
|
||||
|
||||
79
src/string/string_concat_helper.hpp
Normal file
79
src/string/string_concat_helper.hpp
Normal 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;
|
||||
}
|
||||
@@ -4,7 +4,6 @@ sources = []
|
||||
lib = library(
|
||||
'utils',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -4,7 +4,6 @@ sources = []
|
||||
lib = library(
|
||||
'view',
|
||||
include_directories : inc,
|
||||
install : true,
|
||||
sources: [headers, sources]
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user