update functional.h and util.h

main
黄熙来 7 months ago
parent 5b839df1f8
commit 688a027b81

@ -1,155 +1,191 @@
#ifndef MYTINYSTL_FUNCTIONAL_H_ #ifndef MYTINYSTL_FUNCTIONAL_H_
// 定义一个宏,防止头文件被重复包含
#define MYTINYSTL_FUNCTIONAL_H_ #define MYTINYSTL_FUNCTIONAL_H_
// 定义宏结束
// 这个头文件包含了 mystl 的函数对象与哈希函数
#include <cstddef> #include <cstddef>
// 包含标准库中的 cstddef 头文件,用于定义 size_t 等类型
namespace mystl namespace mystl
{ {
// 定义一个命名空间 mystl用于封装自定义的函数对象等
// 定义一元函数的参数型别和返回值型别
// 注意模板类的使用
template <class Arg, class Result> template <class Arg, class Result>
struct unarg_function struct unarg_function
{ {
typedef Arg argument_type; typedef Arg argument_type;
// 定义一个类型别名 argument_type表示单参数函数对象的参数类型
typedef Result result_type; typedef Result result_type;
// 定义一个类型别名 result_type表示单参数函数对象的结果类型
}; };
// 定义二元函数的参数型别的返回值型别 // 定义一个模板结构体 unarg_function用于表示单参数函数对象的类型特征
template <class Arg1, class Arg2, class Result> template <class Arg1, class Arg2, class Result>
struct binary_function struct binary_function
{ {
typedef Arg1 first_argument_type; typedef Arg1 first_argument_type;
// 定义一个类型别名 first_argument_type表示二元函数对象的第一个参数类型
typedef Arg2 second_argument_type; typedef Arg2 second_argument_type;
// 定义一个类型别名 second_argument_type表示二元函数对象的第二个参数类型
typedef Result result_type; typedef Result result_type;
// 定义一个类型别名 result_type表示二元函数对象的结果类型
}; };
// 关于函数对象的四则运算 // 定义一个模板结构体 binary_function用于表示二元函数对象的类型特征
// 函数对象:加法
template <class T> template <class T>
struct plus :public binary_function<T, T, T> struct plus :public binary_function<T, T, T>
{ {
T operator()(const T& x, const T& y) const { return x + y; } T operator()(const T& x, const T& y) const { return x + y; }
// 定义一个模板结构体 plus继承自 binary_function重载 operator() 实现加法操作
}; };
// 函数对象:减法 // 定义一个模板结构体 plus表示加法函数对象
template <class T> template <class T>
struct minus :public binary_function<T, T, T> struct minus :public binary_function<T, T, T>
{ {
T operator()(const T& x, const T& y) const { return x - y; } T operator()(const T& x, const T& y) const { return x - y; }
// 定义一个模板结构体 minus继承自 binary_function重载 operator() 实现减法操作
}; };
// 函数对象:乘法 // 定义一个模板结构体 minus表示减法函数对象
template <class T> template <class T>
struct multiplies :public binary_function<T, T, T> struct multiplies :public binary_function<T, T, T>
{ {
T operator()(const T& x, const T& y) const { return x * y; } T operator()(const T& x, const T& y) const { return x * y; }
// 定义一个模板结构体 multiplies继承自 binary_function重载 operator() 实现乘法操作
}; };
// 函数对象:除法 // 定义一个模板结构体 multiplies表示乘法函数对象
template <class T> template <class T>
struct divides :public binary_function<T, T, T> struct divides :public binary_function<T, T, T>
{ {
T operator()(const T& x, const T& y) const { return x / y; } T operator()(const T& x, const T& y) const { return x / y; }
// 定义一个模板结构体 divides继承自 binary_function重载 operator() 实现除法操作
}; };
// 函数对象:模取 // 定义一个模板结构体 divides表示除法函数对象
template <class T> template <class T>
struct modulus :public binary_function<T, T, T> struct modulus :public binary_function<T, T, T>
{ {
T operator()(const T& x, const T& y) const { return x % y; } T operator()(const T& x, const T& y) const { return x % y; }
// 定义一个模板结构体 modulus继承自 binary_function重载 operator() 实现取模操作
}; };
// 函数对象:否定 // 定义一个模板结构体 modulus表示取模函数对象
template <class T> template <class T>
struct negate :public unarg_function<T, T> struct negate :public unarg_function<T, T>
{ {
T operator()(const T& x) const { return -x; } T operator()(const T& x) const { return -x; }
// 定义一个模板结构体 negate继承自 unarg_function重载 operator() 实现取反操作
}; };
// 证同元素的意思是,对象与该元素做运算后,该对象仍等于自身,即零元
// 加法的证同元素 // 定义一个模板结构体 negate表示取反函数对象
template <class T> template <class T>
T identity_element(plus<T>) { return T(0); } T identity_element(plus<T>) { return T(0); }
// 定义一个模板函数 identity_element用于返回加法函数对象的单位元0
// 乘法的证同元素
template <class T> template <class T>
T identity_element(multiplies<T>) { return T(1); } T identity_element(multiplies<T>) { return T(1); }
// 定义一个模板函数 identity_element用于返回乘法函数对象的单位元1
// 关于函数对象的逻辑运算
// 函数对象:等于
template <class T> template <class T>
struct equal_to :public binary_function<T, T, bool> struct equal_to :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x == y; } bool operator()(const T& x, const T& y) const { return x == y; }
// 定义一个模板结构体 equal_to继承自 binary_function重载 operator() 实现等于比较
}; };
// 函数对象:不等于 // 定义一个模板结构体 equal_to表示等于比较函数对象
template <class T> template <class T>
struct not_equal_to :public binary_function<T, T, bool> struct not_equal_to :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x != y; } bool operator()(const T& x, const T& y) const { return x != y; }
// 定义一个模板结构体 not_equal_to继承自 binary_function重载 operator() 实现不等于比较
}; };
// 函数对象:大于 // 定义一个模板结构体 not_equal_to表示不等于比较函数对象
template <class T> template <class T>
struct greater :public binary_function<T, T, bool> struct greater :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x > y; } bool operator()(const T& x, const T& y) const { return x > y; }
// 定义一个模板结构体 greater继承自 binary_function重载 operator() 实现大于比较
}; };
// 函数对象:小于 // 定义一个模板结构体 greater表示大于比较函数对象
template <class T> template <class T>
struct less :public binary_function<T, T, bool> struct less :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x < y; } bool operator()(const T& x, const T& y) const { return x < y; }
// 定义一个模板结构体 less继承自 binary_function重载 operator() 实现小于比较
}; };
// 函数对象:大于等于 // 定义一个模板结构体 less表示小于比较函数对象
template <class T> template <class T>
struct greater_equal :public binary_function<T, T, bool> struct greater_equal :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x >= y; } bool operator()(const T& x, const T& y) const { return x >= y; }
// 定义一个模板结构体 greater_equal继承自 binary_function重载 operator() 实现大于等于比较
}; };
// 函数对象:小于等于 // 定义一个模板结构体 greater_equal表示大于等于比较函数对象
template <class T> template <class T>
struct less_equal :public binary_function<T, T, bool> struct less_equal :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x <= y; } bool operator()(const T& x, const T& y) const { return x <= y; }
// 定义一个模板结构体 less_equal继承自 binary_function重载 operator() 实现小于等于比较
}; };
// 函数对象:逻辑与 // 定义一个模板结构体 less_equal表示小于等于比较函数对象
template <class T> template <class T>
struct logical_and :public binary_function<T, T, bool> struct logical_and :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x && y; } bool operator()(const T& x, const T& y) const { return x && y; }
// 定义一个模板结构体 logical_and继承自 binary_function重载 operator() 实现逻辑与操作
}; };
// 函数对象:逻辑或 // 定义一个模板结构体 logical_and表示逻辑与函数对象
template <class T> template <class T>
struct logical_or :public binary_function<T, T, bool> struct logical_or :public binary_function<T, T, bool>
{ {
bool operator()(const T& x, const T& y) const { return x || y; } bool operator()(const T& x, const T& y) const { return x || y; }
// 定义一个模板结构体 logical_or继承自 binary_function重载 operator() 实现逻辑或操作
}; };
// 函数对象:逻辑非 // 定义一个模板结构体 logical_or表示逻辑或函数对象
template <class T> template <class T>
struct logical_not :public unarg_function<T, bool> struct logical_not :public unarg_function<T, bool>
{ {
bool operator()(const T& x) const { return !x; } bool operator()(const T& x) const { return !x; }
// 定义一个模板结构体 logical_not继承自 unarg_function重载 operator() 实现逻辑非操作
}; };
// 证同函数:不会改变元素,返回本身 // 定义一个模板结构体 logical_not表示逻辑非函数对象
template <class T> template <class T>
struct identity :public unarg_function<T, bool> struct identity :public unarg_function<T, bool>
{ {
const T& operator()(const T& x) const { return x; } const T& operator()(const T& x) const { return x; }
// 定义一个模板结构体 identity继承自 unarg_function重载 operator() 实现恒等操作
}; };
// select // 定义一个模板结构体 identity表示恒等函数对象
// 选择函数:接受一个 pair返回第一个元素
template <class Pair> template <class Pair>
struct selectfirst :public unarg_function<Pair, typename Pair::first_type> struct selectfirst :public unarg_function<Pair, typename Pair::first_type>
{ {
@ -157,9 +193,11 @@ struct selectfirst :public unarg_function<Pair, typename Pair::first_type>
{ {
return x.first; return x.first;
} }
// 定义一个模板结构体 selectfirst继承自 unarg_function重载 operator() 实现选择对的第一个元素
}; };
// 选择函数:接受一个 pair返回第二个元素 // 定义一个模板结构体 selectfirst表示选择对的第一个元素的函数对象
template <class Pair> template <class Pair>
struct selectsecond :public unarg_function<Pair, typename Pair::second_type> struct selectsecond :public unarg_function<Pair, typename Pair::second_type>
{ {
@ -167,38 +205,48 @@ struct selectsecond :public unarg_function<Pair, typename Pair::second_type>
{ {
return x.second; return x.second;
} }
// 定义一个模板结构体 selectsecond继承自 unarg_function重载 operator() 实现选择对的第二个元素
}; };
// 投射函数:返回第一参数 // 定义一个模板结构体 selectsecond表示选择对的第二个元素的函数对象
template <class Arg1, class Arg2>
struct projectfirst :public binary_function<Arg1, Arg2, Arg1>
{
Arg1 operator()(const Arg1& x, const Arg2&) const { return x; }
// 定义一个模板结构体
template <class Arg1, class Arg2> template <class Arg1, class Arg2>
struct projectfirst :public binary_function<Arg1, Arg2, Arg1> struct projectfirst :public binary_function<Arg1, Arg2, Arg1>
{ {
Arg1 operator()(const Arg1& x, const Arg2&) const { return x; } Arg1 operator()(const Arg1& x, const Arg2&) const { return x; }
// 定义一个模板结构体 projectfirst继承自 binary_function重载 operator() 实现返回第一个参数
}; };
// 投射函数:返回第二参数 // 定义一个模板结构体 projectfirst表示返回第一个参数的函数对象
template <class Arg1, class Arg2> template <class Arg1, class Arg2>
struct projectsecond :public binary_function<Arg1, Arg2, Arg1> struct projectsecond :public binary_function<Arg1, Arg2, Arg2>
{ {
Arg2 operator()(const Arg1&, const Arg2& y) const { return y; } Arg2 operator()(const Arg1&, const Arg2& y) const { return y; }
// 定义一个模板结构体 projectsecond继承自 binary_function重载 operator() 实现返回第二个参数
}; };
/*****************************************************************************************/ // 定义一个模板结构体 projectsecond表示返回第二个参数的函数对象
// 哈希函数对象
// 对于大部分类型hash function 什么都不做
template <class Key> template <class Key>
struct hash {}; struct hash {};
// 定义一个模板结构体 hash用于定义哈希函数的接口
// 针对指针的偏特化版本
template <class T> template <class T>
struct hash<T*> struct hash<T*>
{ {
size_t operator()(T* p) const noexcept size_t operator()(T* p) const noexcept
{ return reinterpret_cast<size_t>(p); } { return reinterpret_cast<size_t>(p); }
// 为指针类型特化 hash 结构体,重载 operator() 实现指针的哈希运算
}; };
// 对于整型类型,只是返回原值 // 为指针类型特化 hash 结构体
#define MYSTL_TRIVIAL_HASH_FCN(Type) \ #define MYSTL_TRIVIAL_HASH_FCN(Type) \
template <> struct hash<Type> \ template <> struct hash<Type> \
{ \ { \
@ -206,42 +254,28 @@ template <> struct hash<Type> \
{ return static_cast<size_t>(val); } \ { return static_cast<size_t>(val); } \
}; };
// 关于其他类型数据的HASH函数 // 定义一个宏 MYSTL_TRIVIAL_HASH_FCN用于为基本类型生成简单的哈希函数
MYSTL_TRIVIAL_HASH_FCN(bool) MYSTL_TRIVIAL_HASH_FCN(bool)
MYSTL_TRIVIAL_HASH_FCN(char) MYSTL_TRIVIAL_HASH_FCN(char)
MYSTL_TRIVIAL_HASH_FCN(signed char) MYSTL_TRIVIAL_HASH_FCN(signed char)
MYSTL_TRIVIAL_HASH_FCN(unsigned char) MYSTL_TRIVIAL_HASH_FCN(unsigned char)
MYSTL_TRIVIAL_HASH_FCN(wchar_t) MYSTL_TRIVIAL_HASH_FCN(wchar_t)
MYSTL_TRIVIAL_HASH_FCN(char16_t) MYSTL_TRIVIAL_HASH_FCN(char16_t)
MYSTL_TRIVIAL_HASH_FCN(char32_t) MYSTL_TRIVIAL_HASH_FCN(char32_t)
MYSTL_TRIVIAL_HASH_FCN(short) MYSTL_TRIVIAL_HASH_FCN(short)
MYSTL_TRIVIAL_HASH_FCN(unsigned short) MYSTL_TRIVIAL_HASH_FCN(unsigned short)
MYSTL_TRIVIAL_HASH_FCN(int) MYSTL_TRIVIAL_HASH_FCN(int)
MYSTL_TRIVIAL_HASH_FCN(unsigned int) MYSTL_TRIVIAL_HASH_FCN(unsigned int)
MYSTL_TRIVIAL_HASH_FCN(long) MYSTL_TRIVIAL_HASH_FCN(long)
MYSTL_TRIVIAL_HASH_FCN(unsigned long) MYSTL_TRIVIAL_HASH_FCN(unsigned long)
MYSTL_TRIVIAL_HASH_FCN(long long) MYSTL_TRIVIAL_HASH_FCN(long long)
MYSTL_TRIVIAL_HASH_FCN(unsigned long long) MYSTL_TRIVIAL_HASH_FCN(unsigned long long)
// 使用宏为各种基本类型生成简单的哈希函数
#undef MYSTL_TRIVIAL_HASH_FCN #undef MYSTL_TRIVIAL_HASH_FCN
// 取消宏定义
// 对于浮点数,逐位哈希
// 逐位哈希函数
inline size_t bitwise_hash(const unsigned char* first, size_t count) inline size_t bitwise_hash(const unsigned char* first, size_t count)
{ {
#if (_MSC_VER && _WIN64) || ((__GNUC__ || __clang__) &&__SIZEOF_POINTER__ == 8) #if (_MSC_VER && _WIN64) || ((__GNUC__ || __clang__) &&__SIZEOF_POINTER__ == 8)
@ -259,8 +293,8 @@ inline size_t bitwise_hash(const unsigned char* first, size_t count)
} }
return result; return result;
} }
// 定义一个函数 bitwise_hash用于计算给定字节序列的哈希值使用 FNV 哈希算法
// 单精度浮点
template <> template <>
struct hash<float> struct hash<float>
{ {
@ -268,28 +302,35 @@ struct hash<float>
{ {
return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(float)); return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(float));
} }
// 为 float 类型特化 hash 结构体,重载 operator() 实现 float 的哈希运算
}; };
// 多精度浮点 // 为 float 类型特化 hash 结构体
template <> template <>
struct hash<double> struct hash<double>
{ {
size_t operator()(const double& val) size_t operator()(const double& val)
{ {
return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(double)); return val == 0.0 ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(double));
} }
// 为 double 类型特化 hash 结构体,重载 operator() 实现 double 的哈希运算
}; };
// 高精度浮点 // 为 double 类型特化 hash 结构体
template <> template <>
struct hash<long double> struct hash<long double>
{ {
size_t operator()(const long double& val) size_t operator()(const long double& val)
{ {
return val == 0.0f ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(long double)); return val == 0.0 ? 0 : bitwise_hash((const unsigned char*)&val, sizeof(long double));
} }
// 为 long double 类型特化 hash 结构体,重载 operator() 实现 long double 的哈希运算
}; };
} // namespace mystl // 为 long double 类型特化 hash 结构体
#endif // !MYTINYSTL_FUNCTIONAL_H_
}
#endif
// 结束命名空间 mystl 和条件编译宏定义

@ -1,297 +1,345 @@
#ifndef MYTINYSTL_UTIL_H_ #ifndef MYTINYSTL_UTIL_H_//
#define MYTINYSTL_UTIL_H_ // 防止头文件被重复包含的宏定义开始//
#define MYTINYSTL_UTIL_H_//
// 这个文件包含一些通用工具,包括 move, forward, swap 等函数,以及 pair 等 // 防止头文件被重复包含的宏定义结束//
//
#include <cstddef> #include <cstddef>//
// 包含标准库中的cstddef头文件通常用于定义一些基本类型如size_t等//
#include "type_traits.h" //
#include "type_traits.h"//
namespace mystl // 包含自定义的type_traits头文件用于提供类型特征支持//
{ //
namespace mystl//
// move // 定义命名空间mystl用于封装自定义的STL实现//
//
template <class T> {//
typename std::remove_reference<T>::type&& move(T&& arg) noexcept //
{ template <class T>//
return static_cast<typename std::remove_reference<T>::type&&>(arg); typename std::remove_reference<T>::type&& move(T&& arg) noexcept//
} // 定义move函数模板用于将参数arg转换为右值引用//
{//
// forward return static_cast<typename std::remove_reference<T>::type&&>(arg);//
// 使用static_cast将arg转换为右值引用并返回//
template <class T> }//
T&& forward(typename std::remove_reference<T>::type& arg) noexcept //
{ template <class T>//
return static_cast<T&&>(arg); T&& forward(typename std::remove_reference<T>::type& arg) noexcept//
} // 定义forward函数模板的重载版本用于完美转发左值引用//
{//
template <class T> return static_cast<T&&>(arg);//
T&& forward(typename std::remove_reference<T>::type&& arg) noexcept // 使用static_cast将arg转换为T类型的右值引用并返回//
{ }//
static_assert(!std::is_lvalue_reference<T>::value, "bad forward"); //
return static_cast<T&&>(arg); template <class T>//
} T&& forward(typename std::remove_reference<T>::type&& arg) noexcept//
// 定义forward函数模板的重载版本用于完美转发右值引用//
// swap {//
static_assert(!std::is_lvalue_reference<T>::value, "bad forward");//
template <class Tp> // 静态断言确保T不是左值引用类型否则编译报错//
void swap(Tp& lhs, Tp& rhs) return static_cast<T&&>(arg);//
{ // 使用static_cast将arg转换为T类型的右值引用并返回//
auto tmp(mystl::move(lhs)); }//
lhs = mystl::move(rhs); //
rhs = mystl::move(tmp); template <class Tp>//
} void swap(Tp& lhs, Tp& rhs)//
// 定义swap函数模板用于交换两个变量的值//
template <class ForwardIter1, class ForwardIter2> {//
ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2) auto tmp(mystl::move(lhs));//
{ // 使用move将lhs转换为右值引用并存储在tmp中//
for (; first1 != last1; ++first1, (void) ++first2) lhs = mystl::move(rhs);//
mystl::swap(*first1, *first2); // 将rhs的值赋给lhs//
return first2; rhs = mystl::move(tmp);//
} // 将tmp的值赋给rhs//
}//
template <class Tp, size_t N> //
void swap(Tp(&a)[N], Tp(&b)[N]) template <class ForwardIter1, class ForwardIter2>//
{ ForwardIter2 swap_range(ForwardIter1 first1, ForwardIter1 last1, ForwardIter2 first2)//
mystl::swap_range(a, a + N, b); // 定义swap_range函数模板用于交换两个范围内的元素//
} {//
for (; first1 != last1; ++first1, (void) ++first2)//
// -------------------------------------------------------------------------------------- // 遍历两个范围内的元素//
// pair mystl::swap(*first1, *first2);//
// 交换对应位置的元素//
// 结构体模板 : pair return first2;//
// 两个模板参数分别表示两个数据的类型 // 返回第二个范围的结束迭代器//
// 用 first 和 second 来分别取出第一个数据和第二个数据 }//
template <class Ty1, class Ty2> //
struct pair template <class Tp, size_t N>//
{ void swap(Tp(&a)[N], Tp(&b)[N])//
typedef Ty1 first_type; // 定义swap函数模板的重载版本用于交换两个固定大小数组的值//
typedef Ty2 second_type; {//
mystl::swap_range(a, a + N, b);//
first_type first; // 保存第一个数据 // 使用swap_range函数交换两个数组的元素//
second_type second; // 保存第二个数据 }//
//
// default constructiable template <class Ty1, class Ty2>//
template <class Other1 = Ty1, class Other2 = Ty2, struct pair//
typename = typename std::enable_if< // 定义pair结构体模板用于存储两个值//
std::is_default_constructible<Other1>::value && {//
std::is_default_constructible<Other2>::value, void>::type> typedef Ty1 first_type;//
constexpr pair() typedef Ty2 second_type;//
: first(), second() // 定义类型别名,分别表示第一个和第二个值的类型//
{ //
} first_type first; //
second_type second; //
// implicit constructiable for this type // 定义两个成员变量,分别存储第一个和第二个值//
template <class U1 = Ty1, class U2 = Ty2, //
typename std::enable_if< //
std::is_copy_constructible<U1>::value && template <class Other1 = Ty1, class Other2 = Ty2,//
std::is_copy_constructible<U2>::value && typename = typename std::enable_if<//
std::is_convertible<const U1&, Ty1>::value && std::is_default_constructible<Other1>::value &&//
std::is_convertible<const U2&, Ty2>::value, int>::type = 0> std::is_default_constructible<Other2>::value, void>::type>//
constexpr pair(const Ty1& a, const Ty2& b) constexpr pair()//
: first(a), second(b) : first(), second()//
{ // 定义默认构造函数使用默认构造方式初始化first和second//
} {//
}//
// explicit constructible for this type //
template <class U1 = Ty1, class U2 = Ty2, template <class U1 = Ty1, class U2 = Ty2,//
typename std::enable_if< typename std::enable_if<//
std::is_copy_constructible<U1>::value && std::is_copy_constructible<U1>::value &&//
std::is_copy_constructible<U2>::value && std::is_copy_constructible<U2>::value &&//
(!std::is_convertible<const U1&, Ty1>::value || std::is_convertible<const U1&, Ty1>::value &&//
!std::is_convertible<const U2&, Ty2>::value), int>::type = 0> std::is_convertible<const U2&, Ty2>::value, int>::type = 0>//
explicit constexpr pair(const Ty1& a, const Ty2& b) constexpr pair(const Ty1& a, const Ty2& b)//
: first(a), second(b) : first(a), second(b)//
{ // 定义拷贝构造函数使用拷贝构造方式初始化first和second//
} {//
}//
pair(const pair& rhs) = default; //
pair(pair&& rhs) = default; template <class U1 = Ty1, class U2 = Ty2,//
typename std::enable_if<//
// implicit constructiable for other type std::is_copy_constructible<U1>::value &&//
template <class Other1, class Other2, std::is_copy_constructible<U2>::value &&//
typename std::enable_if< (!std::is_convertible<const U1&, Ty1>::value ||//
std::is_constructible<Ty1, Other1>::value && !std::is_convertible<const U2&, Ty2>::value), int>::type = 0>//
std::is_constructible<Ty2, Other2>::value && explicit constexpr pair(const Ty1& a, const Ty2& b)//
std::is_convertible<Other1&&, Ty1>::value && : first(a), second(b)//
std::is_convertible<Other2&&, Ty2>::value, int>::type = 0> // 定义显式拷贝构造函数使用拷贝构造方式初始化first和second//
constexpr pair(Other1&& a, Other2&& b) {//
: first(mystl::forward<Other1>(a)), }//
second(mystl::forward<Other2>(b)) //
{ pair(const pair& rhs) = default;//
} // 默认拷贝构造函数//
//
// explicit constructiable for other type pair(pair&& rhs) = default;//
template <class Other1, class Other2, // 默认移动构造函数//
typename std::enable_if< //
std::is_constructible<Ty1, Other1>::value && template <class Other1, class Other2,//
std::is_constructible<Ty2, Other2>::value && typename std::enable_if<//
(!std::is_convertible<Other1, Ty1>::value || std::is_constructible<Ty1, Other1>::value &&//
!std::is_convertible<Other2, Ty2>::value), int>::type = 0> std::is_constructible<Ty2, Other2>::value &&//
explicit constexpr pair(Other1&& a, Other2&& b) std::is_convertible<Other1&&, Ty1>::value &&//
: first(mystl::forward<Other1>(a)), std::is_convertible<Other2&&, Ty2>::value, int>::type = 0>//
second(mystl::forward<Other2>(b)) constexpr pair(Other1&& a, Other2&& b)//
{ : first(mystl::forward<Other1>(a)),//
} second(mystl::forward<Other2>(b))//
// 定义构造函数使用完美转发初始化first和second//
// implicit constructiable for other pair {//
template <class Other1, class Other2, }//
typename std::enable_if< //
std::is_constructible<Ty1, const Other1&>::value && template <class Other1, class Other2,//
std::is_constructible<Ty2, const Other2&>::value && typename std::enable_if<//
std::is_convertible<const Other1&, Ty1>::value && std::is_constructible<Ty1, Other1>::value &&//
std::is_convertible<const Other2&, Ty2>::value, int>::type = 0> std::is_constructible<Ty2, Other2>::value &&//
constexpr pair(const pair<Other1, Other2>& other) (!std::is_convertible<Other1, Ty1>::value ||//
: first(other.first), !std::is_convertible<Other2, Ty2>::value), int>::type = 0>//
second(other.second) explicit constexpr pair(Other1&& a, Other2&& b)//
{ : first(mystl::forward<Other1>(a)),//
} second(mystl::forward<Other2>(b))//
// 定义显式构造函数使用完美转发初始化first和second//
// explicit constructiable for other pair {//
template <class Other1, class Other2, }//
typename std::enable_if< //
std::is_constructible<Ty1, const Other1&>::value && template <class Other1, class Other2,//
std::is_constructible<Ty2, const Other2&>::value && typename std::enable_if<//
(!std::is_convertible<const Other1&, Ty1>::value || std::is_constructible<Ty1, const Other1&>::value &&//
!std::is_convertible<const Other2&, Ty2>::value), int>::type = 0> std::is_constructible<Ty2, const Other2&>::value &&//
explicit constexpr pair(const pair<Other1, Other2>& other) std::is_convertible<const Other1&, Ty1>::value &&//
: first(other.first), std::is_convertible<const Other2&, Ty2>::value, int>::type = 0>//
second(other.second) constexpr pair(const pair<Other1, Other2>& other)//
{ : first(other.first),//
} second(other.second)//
// 定义拷贝构造函数使用拷贝构造方式初始化first和second//
// implicit constructiable for other pair {//
template <class Other1, class Other2, }//
typename std::enable_if< //
std::is_constructible<Ty1, Other1>::value && template <class Other1, class Other2,//
std::is_constructible<Ty2, Other2>::value && typename std::enable_if<//
std::is_convertible<Other1, Ty1>::value && std::is_constructible<Ty1, const Other1&>::value &&//
std::is_convertible<Other2, Ty2>::value, int>::type = 0> std::is_constructible<Ty2, const Other2&>::value &&//
constexpr pair(pair<Other1, Other2>&& other) (!std::is_convertible<const Other1&, Ty1>::value ||//
: first(mystl::forward<Other1>(other.first)), !std::is_convertible<const Other2&, Ty2>::value), int>::type = 0>//
second(mystl::forward<Other2>(other.second)) explicit constexpr pair(const pair<Other1 &other)//
{ : first(other.first),//
} second(other.second)//
// 定义显式拷贝构造函数使用拷贝构造方式初始化first和second//
// explicit constructiable for other pair {//
template <class Other1, class Other2, }//
typename std::enable_if< //
std::is_constructible<Ty1, Other1>::value && template <class Other1, class Other2,//
std::is_constructible<Ty2, Other2>::value && typename std::enable_if<//
(!std::is_convertible<Other1, Ty1>::value || std::is_constructible<Ty1, Other1>::value &&//
!std::is_convertible<Other2, Ty2>::value), int>::type = 0> std::is_constructible<Ty2, Other2>::value &&//
explicit constexpr pair(pair<Other1, Other2>&& other) std::is_convertible<Other1, Ty1>::value &&//
: first(mystl::forward<Other1>(other.first)), std::is_convertible<Other2, Ty2>::value, int>::type = 0>//
second(mystl::forward<Other2>(other.second)) constexpr pair(pair<Other1, Other2>&& other)//
{ : first(mystl::forward<Other1>(other.first)),//
} second(mystl::forward<Other2>(other.second))//
// 定义构造函数使用完美转发初始化first和second//
// copy assign for this pair {//
pair& operator=(const pair& rhs) }//
{ //
if (this != &rhs) template <class Other1, class Other2,//
{ typename std::enable_if<//
first = rhs.first; std::is_constructible<Ty1, Other1>::value &&//
second = rhs.second; std::is_constructible<Ty2, Other2>::value &&//
} (!std::is_convertible<Other1, Ty1>::value ||//
return *this; !std::is_convertible<Other2, Ty2>::value), int>::type = 0>//
} explicit constexpr pair(pair<Other1, Other2>&& other)//
: first(mystl::forward<Other1>(other.first)),//
// move assign for this pair second(mystl::forward<Other2>(other.second))//
pair& operator=(pair&& rhs) // 继续使用完美转发初始化second成员变量//
{ {//
if (this != &rhs) }//
{ //
first = mystl::move(rhs.first); pair& operator=(const pair& rhs)//
second = mystl::move(rhs.second); // 定义拷贝赋值运算符//
} {//
return *this; if (this != &rhs)//
} // 如果不是自赋值//
{//
// copy assign for other pair first = rhs.first;//
template <class Other1, class Other2> // 将rhs的first赋值给当前对象的first//
pair& operator=(const pair<Other1, Other2>& other) second = rhs.second;//
{ // 将rhs的second赋值给当前对象的second//
first = other.first; }//
second = other.second; return *this;//
return *this; // 返回当前对象的引用//
} }//
//
// move assign for other pair pair& operator=(pair&& rhs)//
template <class Other1, class Other2> // 定义移动赋值运算符//
pair& operator=(pair<Other1, Other2>&& other) {//
{ if (this != &rhs)//
first = mystl::forward<Other1>(other.first); // 如果不是自赋值//
second = mystl::forward<Other2>(other.second); {//
return *this; first = mystl::move(rhs.first);//
} // 将rhs的first移动赋值给当前对象的first//
second = mystl::move(rhs.second);//
~pair() = default; // 将rhs的second移动赋值给当前对象的second//
}//
void swap(pair& other) return *this;//
{ // 返回当前对象的引用//
if (this != &other) }//
{ //
mystl::swap(first, other.first); template <class Other1, class Other2>//
mystl::swap(second, other.second); pair& operator=(const pair<Other1, Other2>& other)//
} // 定义模板化的拷贝赋值运算符//
} {//
first = other.first;//
}; // 将other的first赋值给当前对象的first//
second = other.second;//
// 重载比较操作符 // 将other的second赋值给当前对象的second//
template <class Ty1, class Ty2> return *this;//
bool operator==(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs) // 返回当前对象的引用//
{ }//
return lhs.first == rhs.first && lhs.second == rhs.second; //
} template <class Other1, class Other2>//
pair& operator=(pair<Other1, Other2>&& other)//
template <class Ty1, class Ty2> // 定义模板化的移动赋值运算符//
bool operator<(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs) {//
{ first = mystl::forward<Other1>(other.first);//
return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second); // 将other的first移动赋值给当前对象的first//
} second = mystl::forward<Other2>(other.second);//
// 将other的second移动赋值给当前对象的second//
template <class Ty1, class Ty2> return *this;//
bool operator!=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs) // 返回当前对象的引用//
{ }//
return !(lhs == rhs); //
} ~pair() = default;//
// 默认析构函数//
template <class Ty1, class Ty2> //
bool operator>(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs) void swap(pair& other)//
{ // 定义成员函数swap用于交换两个pair对象的成员变量//
return rhs < lhs; {//
} if (this != &other)//
// 如果不是自交换//
template <class Ty1, class Ty2> {//
bool operator<=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs) mystl::swap(first, other.first);//
{ // 交换first成员变量//
return !(rhs < lhs); mystl::swap(second, other.second);//
} // 交换second成员变量//
}//
template <class Ty1, class Ty2> }//
bool operator>=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs) //
{ };//
return !(lhs < rhs); //
} template <class Ty1, class Ty2>//
bool operator==(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)//
// 重载 mystl 的 swap // 定义全局函数operator==用于比较两个pair对象是否相等//
template <class Ty1, class Ty2> {//
void swap(pair<Ty1, Ty2>& lhs, pair<Ty1, Ty2>& rhs) return lhs.first == rhs.first && lhs.second == rhs.second;//
{ // 如果两个pair对象的first和second都相等则认为两个pair对象相等//
lhs.swap(rhs); }//
} //
template <class Ty1, class Ty2>//
// 全局函数,让两个数据成为一个 pair bool operator<(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)//
template <class Ty1, class Ty2> // 定义全局函数operator<用于比较两个pair对象的大小//
pair<Ty1, Ty2> make_pair(Ty1&& first, Ty2&& second) {//
{ return lhs.first < rhs.first || (lhs.first == rhs.first && lhs.second < rhs.second);//
return pair<Ty1, Ty2>(mystl::forward<Ty1>(first), mystl::forward<Ty2>(second)); // 如果lhs的first小于rhs的first或者lhs的first等于rhs的first且lhs的second小于rhs的second则认为lhs小于rhs//
} }//
//
} template <class Ty1, class Ty2>//
bool operator!=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)//
#endif // !MYTINYSTL_UTIL_H_ // 定义全局函数operator!=用于比较两个pair对象是否不相等//
{//
return !(lhs == rhs);//
// 如果两个pair对象不相等则返回true//
}//
//
template <class Ty1, class Ty2>//
bool operator>(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)//
// 定义全局函数operator>用于比较两个pair对象的大小//
{//
return rhs < lhs;//
// 如果rhs小于lhs则认为lhs大于rhs//
}//
//
template <class Ty1, class Ty2>//
bool operator<=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)//
// 定义全局函数operator<=用于比较两个pair对象的大小//
{//
return !(rhs < lhs);//
// 如果rhs不小于lhs则认为lhs小于等于rhs//
}//
//
template <class Ty1, class Ty2>//
bool operator>=(const pair<Ty1, Ty2>& lhs, const pair<Ty1, Ty2>& rhs)//
// 定义全局函数operator>=用于比较两个pair对象的大小//
{//
return !(lhs < rhs);//
// 如果lhs不小于rhs则认为lhs大于等于rhs//
}//
//
template <class Ty1, class Ty2>//
void swap(pair<Ty1, Ty2>& lhs, pair<Ty1, Ty2>& rhs)//
// 定义全局函数swap用于交换两个pair对象的成员变量//
{//
lhs.swap(rhs);//
// 调用pair对象的成员函数swap进行交换//
}//
//
template <class Ty1, class Ty2>//
pair<Ty1, Ty2> make_pair(Ty1&& first, Ty2&& second)//
// 定义全局函数make_pair用于创建pair对象//
{//
return pair<Ty1, Ty2>(mystl::forward<Ty1>(first), mystl::forward<Ty2>(second));//
// 使用完美转发创建pair对象并返回//
}//
//
} // namespace mystl//
//
#endif // MYTINYSTL_UTIL_H_//
// 防止头文件被重复包含的宏定义结束//

Loading…
Cancel
Save