cpp-toolbox  0.0.1
A toolbox library for C++
Loading...
Searching...
No Matches
functional.hpp File Reference
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <mutex>
#include <numeric>
#include <optional>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <typeinfo>
#include <utility>
#include <variant>
#include <cpp-toolbox/cpp-toolbox_export.hpp>
#include <cpp-toolbox/functional/impl/functional_impl.hpp>
Include dependency graph for functional.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  toolbox::functional::detail::overloaded< Fs >
 用于创建重载集的辅助类 / Helper class for creating overload sets More...
 
struct  toolbox::functional::detail::is_optional< T >
 检查T是否为std::optional的类型特征 / Type trait to check if T is a std::optional More...
 
struct  toolbox::functional::detail::is_optional< std::optional< T > >
 
struct  toolbox::functional::detail::has_size< T, typename >
 检查类型是否有size()成员函数的类型特征 / Type trait to check if type has size() member function More...
 
struct  toolbox::functional::detail::has_size< T, std::void_t< decltype(std::declval< T >().size())> >
 
struct  toolbox::functional::detail::MemoizeState< Func, R, Args >
 记忆化的内部状态 / Internal state for memoization More...
 
class  toolbox::functional::MemoizedFunction< R(Args...)>
 MemoizedFunction对特定函数签名的特化 / Specialization of MemoizedFunction for specific function signature 仅声明,实现在impl中 / Declaration only, definition in impl. More...
 

Namespaces

namespace  toolbox
 
namespace  toolbox::functional
 
namespace  toolbox::functional::detail
 

Macros

#define CPP_TOOLBOX_DEFINE_SIMPLE_FUNCTOR(FunctorName, ReturnType, Params, Body)
 定义一个简单的函子(函数对象),只有一个operator() / Define a simple functor (function object) with a single operator()
 

Functions

template<class... Fs>
 toolbox::functional::detail::overloaded (Fs...) -> overloaded< Fs... >
 
template<typename... Containers>
auto toolbox::functional::detail::get_min_size (const Containers &... containers) -> size_t
 获取多个容器中的最小大小的辅助函数 / Helper function to get minimum size from multiple containers
 
template<typename Tuple , std::size_t... Is>
void toolbox::functional::detail::increment_iterators (Tuple &iter_tuple, std::index_sequence< Is... >)
 增加元组中所有迭代器的辅助函数 / Helper function to increment all iterators in a tuple
 
template<typename Tuple , std::size_t... Is>
auto toolbox::functional::detail::dereference_iterators_as_tuple (Tuple &iter_tuple, std::index_sequence< Is... >)
 解引用迭代器并创建引用元组的辅助函数 / Helper function to dereference iterators and create tuple of references
 
template<typename G , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::compose (G &&g, F &&f)
 将两个函数组合成一个函数 / Composes two functions into a single function
 
template<typename F1 , typename... FRest>
CPP_TOOLBOX_EXPORT auto toolbox::functional::compose (F1 &&f1, FRest &&... rest)
 将多个函数组合成一个函数 / Composes multiple functions into a single function
 
CPP_TOOLBOX_EXPORT auto toolbox::functional::compose ()
 空的compose函数,会抛出错误 / Empty compose function that throws an error
 
template<typename F , typename Arg1 >
CPP_TOOLBOX_EXPORT auto toolbox::functional::bind_first (F &&f, Arg1 &&arg1)
 绑定函数的第一个参数 / Binds the first argument of a function
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map (const std::optional< T > &opt, F &&f) -> std::optional< std::invoke_result_t< F, const T & > >
 在optional值上映射函数 / Maps a function over an optional value
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map (std::optional< T > &&opt, F &&f) -> std::optional< std::invoke_result_t< F, T && > >
 在optional右值上映射函数 / Maps a function over an optional rvalue
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::flatMap (const std::optional< T > &opt, F &&f) -> std::invoke_result_t< F, const T & >
 在optional值上平面映射函数 / Flat maps a function over an optional value
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::flatMap (std::optional< T > &&opt, F &&f) -> std::invoke_result_t< F, T && >
 在optional右值上平面映射函数 / Flat maps a function over an optional rvalue
 
template<typename T , typename U >
CPP_TOOLBOX_EXPORT auto toolbox::functional::orElse (const std::optional< T > &opt, U &&default_value) -> T
 返回包含的值或默认值 / Returns the contained value or a default
 
template<typename T , typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::orElseGet (const std::optional< T > &opt, F &&default_func) -> T
 返回包含的值或调用函数获取默认值 / Returns the contained value or calls function for default
 
template<typename T , typename P >
CPP_TOOLBOX_EXPORT auto toolbox::functional::filter (const std::optional< T > &opt, P &&p) -> std::optional< T >
 基于谓词过滤optional / Filters an optional based on a predicate
 
template<typename T , typename P >
CPP_TOOLBOX_EXPORT auto toolbox::functional::filter (std::optional< T > &&opt, P &&p) -> std::optional< T >
 基于谓词过滤optional右值 / Filters an optional rvalue based on a predicate
 
template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto toolbox::functional::match (const std::variant< Ts... > &var, Fs &&... visitors) -> decltype(auto)
 使用访问器函数对variant进行模式匹配 / Pattern matches on a variant using visitor functions
 
template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto toolbox::functional::match (std::variant< Ts... > &var, Fs &&... visitors) -> decltype(auto)
 对非const左值variant使用访问器函数进行匹配 / Match variant with visitor functions for non-const lvalue variant
 
template<typename... Ts, typename... Fs>
CPP_TOOLBOX_EXPORT auto toolbox::functional::match (std::variant< Ts... > &&var, Fs &&... visitors) -> decltype(auto)
 对右值variant使用访问器函数进行匹配 / Match variant with visitor functions for rvalue variant
 
template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map (const std::variant< Ts... > &var, F &&f) -> ResultVariant
 对variant中保存的值应用函数并在新variant中返回结果 / Apply a function to the value held by a variant and return result in a new variant
 
template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map (std::variant< Ts... > &var, F &&f) -> ResultVariant
 对非const左值variant中保存的值应用函数 / Apply a function to the value held by a non-const lvalue variant
 
template<typename ResultVariant , typename... Ts, typename F >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map (std::variant< Ts... > &&var, F &&f) -> ResultVariant
 对右值variant中保存的值应用函数 / Apply a function to the value held by an rvalue variant
 
template<typename Container , typename Func >
CPP_TOOLBOX_EXPORT auto toolbox::functional::map (const Container &input, Func &&f) -> std::vector< std::invoke_result_t< Func, typename Container::const_reference > >
 对容器中的每个元素应用函数并返回结果向量 / Apply a function to each element in a container and return results in a vector
 
template<typename Container , typename Predicate >
CPP_TOOLBOX_EXPORT auto toolbox::functional::filter (const Container &input, Predicate &&p) -> std::vector< typename Container::value_type >
 过滤满足谓词的容器元素 / Filter elements from a container that satisfy a predicate
 
template<typename Container , typename T , typename BinaryOp >
CPP_TOOLBOX_EXPORT auto toolbox::functional::reduce (const Container &input, T identity, BinaryOp &&op) -> T
 使用带初始值的二元操作归约容器元素 / Reduce container elements using a binary operation with initial value
 
template<typename Container , typename BinaryOp >
CPP_TOOLBOX_EXPORT auto toolbox::functional::reduce (const Container &input, BinaryOp &&op) -> typename Container::value_type
 使用二元操作归约非空容器元素 / Reduce non-empty container elements using a binary operation
 
template<typename... Containers>
CPP_TOOLBOX_EXPORT auto toolbox::functional::zip (const Containers &... containers) -> std::vector< std::tuple< decltype(*std::cbegin(std::declval< const Containers & >()))... > >
 将多个容器压缩成元组向量 / Zip multiple containers into a vector of tuples
 
template<typename ContainerKeys , typename ContainerValues , typename Key = typename std::decay_t<ContainerKeys>::value_type, typename Value = typename std::decay_t<ContainerValues>::value_type, typename Hash = std::hash<Key>, typename KeyEqual = std::equal_to<Key>, typename Alloc = std::allocator<std::pair<const Key, Value>>>
CPP_TOOLBOX_EXPORT auto toolbox::functional::zip_to_unordered_map (const ContainerKeys &keys, const ContainerValues &values) -> std::unordered_map< Key, Value, Hash, KeyEqual, Alloc >
 将两个序列压缩成无序映射 / Zip two sequences into an unordered_map
 
template<typename Signature , typename Func >
CPP_TOOLBOX_EXPORT auto toolbox::functional::memoize (Func &&f)
 创建带显式签名的记忆化函数 / Create a memoized function with explicit signature
 
template<typename R , typename... Args, typename Func >
CPP_TOOLBOX_EXPORT auto toolbox::functional::memoize_explicit (Func &&f) -> std::function< R(Args...)>
 创建带显式返回和参数类型的记忆化函数 / Create a memoized function with explicit return and argument types
 

Variables

template<typename T >
constexpr bool toolbox::functional::detail::is_optional_v = is_optional<std::decay_t<T>>::value
 
template<typename Signature >
class CPP_TOOLBOX_EXPORT toolbox::functional::MemoizedFunction
 缓存函数结果的记忆化函数类 / Memoized function class that caches function results
 

Macro Definition Documentation

◆ CPP_TOOLBOX_DEFINE_SIMPLE_FUNCTOR

#define CPP_TOOLBOX_DEFINE_SIMPLE_FUNCTOR (   FunctorName,
  ReturnType,
  Params,
  Body 
)
Value:
struct FunctorName \
{ \
ReturnType operator() Params const \
{ \
Body \
} \
};

定义一个简单的函子(函数对象),只有一个operator() / Define a simple functor (function object) with a single operator()

Parameters
FunctorName要创建的函子结构体名称 / Name of the functor struct to create
ReturnTypeoperator()的返回类型 / Return type of operator()
Paramsoperator()的参数列表(必须包含括号) / Parameter list for operator() (must include parentheses)
Body函数体代码(如果ReturnType不是void则必须包含return语句) / Function body code (must include return statement if ReturnType is not void)
// 定义一个加法函子 / Define a functor that adds two numbers
Adder, // 函子名称 / FunctorName
int, // 返回类型 / ReturnType
(int a, int b), // 参数 / Params
return a + b; // 函数体 / Body
);
// 使用函子 / Use the functor
Adder add;
int sum = add(1, 2); // 返回3 / Returns 3
// 定义一个打印函子 / Define a functor that prints a message
Printer, // 函子名称 / FunctorName
void, // 返回类型 / ReturnType
(const std::string& msg), // 参数 / Params
std::cout << msg << std::endl; // 函数体 / Body
);
#define CPP_TOOLBOX_DEFINE_SIMPLE_FUNCTOR(FunctorName, ReturnType, Params, Body)
定义一个简单的函子(函数对象),只有一个operator() / Define a simple functor (function object) with a single operator()
Definition functional.hpp:55