add new examples

This commit is contained in:
chatlanin
2023-08-14 11:02:39 +03:00
parent 0339df9279
commit 1242044571
25 changed files with 442 additions and 446 deletions

View File

@@ -4,28 +4,40 @@
#include <vector>
#include <list>
#include <map>
#include <set>
#include <unordered_map>
#include <concepts>
namespace hack::concepts
{
template<typename T>
concept is_sequence_container = std::same_as<T, std::vector<typename T::value_type>> ||
std::same_as<T, std::list<typename T::value_type>>;
concept is_map = std::same_as<T, std::map<typename T::key_type, typename T::mapped_type, typename T::key_compare, typename T::allocator_type>> ||
std::same_as<T, std::unordered_map<typename T::key_type, typename T::mapped_type, typename T::hasher, typename T::key_equal, typename T::allocator_type>>;
template<typename T>
concept is_tuple = requires (T t) { std::tuple_cat(t, std::make_tuple(1, "tuple")); };
template<typename T>
concept is_string = std::is_convertible_v<T, std::string_view>;
concept is_set = std::same_as<T, std::set<typename T::key_type, typename T::key_compare, typename T::allocator_type>>;
template<typename T>
concept is_map = std::same_as<T, std::map<typename T::key_type, typename T::mapped_type, typename T::key_compare, typename T::allocator_type>> ||
std::same_as<T, std::unordered_map<typename T::key_type, typename T::mapped_type, typename T::hasher, typename T::key_equal, typename T::allocator_type>>;
concept is_string = std::is_convertible_v<T, std::string_view>;
template<typename T, std::size_t N = 0>
concept is_sequence_container = std::same_as<T, std::vector<typename T::value_type>> || std::same_as<T, std::list<typename T::value_type>> || (std::is_array_v<T> && N > 0);
template<typename T>
concept is_associative_container = is_map<T> || is_tuple<T> || is_set<T>;
template<typename T>
concept not_defined = !std::enable_if_t<!(std::integral<T> ||
is_sequence_container<T> ||
is_map<T> ||
is_tuple<T> ||
is_set<T> ||
std::is_array<T>() ||
is_string<T>), bool>() == true;
}

View File

@@ -1,50 +0,0 @@
#pragma once
#include <algorithm>
#include <vector>
namespace hack::container
{
template<typename Range, typename... Args>
void vector_multiset(Range& r, Args... args)
{
constexpr std::size_t t = sizeof... (args);
r.reserve(t);
(r.emplace_back(std::forward<Args>(args)), ...);
}
template<typename Range, typename... Args>
void set_multiset(Range& r, Args... args)
{
(r.insert(args), ...);
}
template<typename Range, typename... Args>
int matches(Range r, Args... args)
{
return (std::count(std::cbegin(r), std::cend(r), args) + ...);
}
// -----------------------------------------------------
// remove vector element if vector sorting no important
template<typename T>
void vector_remove_at(std::vector<T>& v, std::size_t idx)
{
if (idx < v.size())
{
v[idx] = std::move(v.back());
v.pop_back();
}
}
template<typename T>
void vector_remove_at(std::vector<T>& v, typename std::vector<T>::iterator it)
{
if (it != v.end())
{
*it = std::move(v.back());
v.pop_back();
}
}
// -----------------------------------------------------
}

View File

@@ -0,0 +1,5 @@
#pragma once
#include "vector.hpp"
#include "utils.hpp"
#include "set.hpp"

11
src/hack/containers/set.hpp Executable file
View File

@@ -0,0 +1,11 @@
#pragma once
namespace hack::containers::set
{
// множественная вставка элементов
template<typename Range, typename... Args>
void multiset(Range& r, Args... args)
{
(r.insert(args), ...);
}
}

14
src/hack/containers/utils.hpp Executable file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <algorithm>
#include <vector>
namespace hack::containers::utils
{
// подсчитывает кол-во присутствующих элементов в диапозоне
template<typename Range, typename... Args>
int count(Range r, Args... args)
{
return (std::count(std::cbegin(r), std::cend(r), args) + ...);
}
}

38
src/hack/containers/vector.hpp Executable file
View File

@@ -0,0 +1,38 @@
#pragma once
#include <algorithm>
#include <vector>
namespace hack::containers::vector
{
// множественная вставка элементов
template<typename Range, typename... Args>
void multiset(Range& r, Args... args)
{
constexpr std::size_t t = sizeof... (args);
r.reserve(t);
(r.emplace_back(std::forward<Args>(args)), ...);
}
// удаление элемента вектора по его индексу, если не важна сортировка
template<typename T>
void remove_at(std::vector<T>& v, std::size_t idx)
{
if (idx < v.size())
{
v[idx] = std::move(v.back());
v.pop_back();
}
}
// удаление элемента вектора по его итератору, если не важна сортировка
template<typename T>
void remove_at(std::vector<T>& v, typename std::vector<T>::iterator it)
{
if (it != v.end())
{
*it = std::move(v.back());
v.pop_back();
}
}
}

View File

@@ -19,7 +19,7 @@ namespace hack::iterators
public:
associative_ostream_iterator(std::size_t size, ostream_type& os) : os_ { &os }, size_ { size } { }
auto& operator=(T const& item)
auto& operator=(const T& item)
{
--size_;
const auto& [key, value] = item;

View File

@@ -9,7 +9,6 @@
#include "hack/iterators/sequence_ostream_iterator.hpp"
#include "hack/iterators/associative_ostream_iterator.hpp"
#include "hack/math/matrix.hpp"
// #include "nlohmann/json.hpp"
namespace hack
{
@@ -67,7 +66,7 @@ namespace hack
template<typename T, typename... Args>
static void print(const T& data, const Args&... args)
{
count--;
--count;
print_t(data);
print(args...);
}
@@ -92,6 +91,14 @@ namespace hack
std::cout << " }" << (count != 0 ? devider : "");
}
template<concepts::is_set T>
static void print_t(const T& data)
{
std::cout << "{ ";
std::copy(data.cbegin(), data.cend(), iterators::sequence_ostream_iterator<typename T::value_type>(data.size(), std::cout));
std::cout << " }" << (count != 0 ? devider : "");
}
template<concepts::is_map T>
static void print_t(const T& data)
{
@@ -120,7 +127,7 @@ namespace hack
std::size_t index = data.size();
for (auto& r : data)
{
index--;
--index;
std::cout << "{ ";
print_t(std::get<demention>(r));
std::cout << " }" << (index != 0 ? ", " : "");

View File

@@ -1,6 +1,7 @@
#pragma once
#include <type_traits>
#include <algorithm>
namespace hack
{
@@ -10,6 +11,6 @@ namespace hack
template<typename T, typename U, typename RT = std::common_type_t<T, U>>
inline RT max(T a, U b)
{
return a > b ? a : b;
return std::max(a, b);
}
}

View File

@@ -0,0 +1,23 @@
#pragma once
#include <concepts>
#include <memory>
#include "hack/concepts/concepts.hpp"
namespace hack::memory
{
// обсуждение тут: https://stackoverflow.com/questions/10149840/c-arrays-and-make-unique
template <typename T, typename ...Args>
typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type make_unique(Args &&...args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template <typename T>
typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T>>::type make_unique(std::size_t n)
{
using RT = typename std::remove_extent<T>::type;
return std::unique_ptr<T>(new RT[n]);
}
}

View File

@@ -1,13 +0,0 @@
#pragma once
namespace hack::range
{
// являются ли числа максимум и минимум последовательности
template<typename T, typename... Args>
bool within(const T min, const T max, Args... args)
{
// 1, 5, 2, 3, 4
// ( (1 <= 2 && 5 >= 2) && (1 <= 3 && 5 >= 3) && (1 <= 4 && 5 >= 4) )
return ((min <= args && max >= args) && ...);
}
}

View File

@@ -7,6 +7,7 @@
namespace hack::utils
{
//
template<typename Result, typename... Args>
auto func_memory(Result (*f)(Args...))
{
@@ -17,7 +18,7 @@ namespace hack::utils
const auto key = std::make_tuple(args...);
const auto cached = cache.find(key);
if(cached == cache.end())
if (cached == cache.end())
{
auto result = f(args...);
cache[key] = result;
@@ -27,6 +28,8 @@ namespace hack::utils
};
}
// обединяет фукнкции в один вызов при одинаковых переменных
// все переменный захватываются по значению
template<typename T, typename... Args>
auto func_concat(T t, Args... args)
{
@@ -46,6 +49,7 @@ namespace hack::utils
}
}
// вызов unix команд из с++ кода
inline std::string unix_cmd(const std::string& cmd)
{
std::string result;

View File

@@ -1,26 +1,27 @@
inc += include_directories('.')
headers = [
'hack/concepts/concepts.hpp',
'hack/container/container.hpp',
'hack/iterators/associative_ostream_iterator.hpp',
'hack/iterators/sequence_ostream_iterator.hpp',
'hack/logger/logger.hpp',
'hack/math/matrix.hpp',
'hack/math/max.hpp',
'hack/math/vector.hpp',
'hack/range/range.hpp',
'hack/range/range.hpp',
'hack/security/is_link.hpp',
'hack/security/is_string.hpp',
'hack/security/uuid.hpp',
'hack/security/validate_email.hpp',
'hack/string/string.hpp',
'hack/string/string_concat_helper.hpp',
'hack/string/utf8_len.hpp',
'hack/utils/func_query.hpp',
'hack/utils/utils.hpp',
'hack/view/color.hpp'
'hack/containers/vector.hpp',
# 'hack/concepts/concepts.hpp',
# 'hack/iterators/associative_ostream_iterator.hpp',
# 'hack/iterators/sequence_ostream_iterator.hpp',
# 'hack/math/matrix.hpp',
# 'hack/math/max.hpp',
# 'hack/math/vector.hpp',
# 'hack/range/range.hpp',
# 'hack/range/range.hpp',
# 'hack/security/is_link.hpp',
# 'hack/security/is_string.hpp',
# 'hack/security/uuid.hpp',
# 'hack/security/validate_email.hpp',
# 'hack/string/string.hpp',
# 'hack/string/string_concat_helper.hpp',
# 'hack/string/utf8_len.hpp',
# 'hack/utils/func_query.hpp',
# 'hack/utils/utils.hpp',
# 'hack/view/color.hpp'
]
sources = []