update functional.h

main
黄熙来 7 months ago
parent 43772d3dd2
commit 10fae69251

@ -9,6 +9,7 @@ namespace mystl
{ {
// 定义一元函数的参数型别和返回值型别 // 定义一元函数的参数型别和返回值型别
// 注意模板类的使用
template <class Arg, class Result> template <class Arg, class Result>
struct unarg_function struct unarg_function
{ {
@ -25,6 +26,7 @@ struct binary_function
typedef Result result_type; typedef Result result_type;
}; };
// 关于函数对象的四则运算
// 函数对象:加法 // 函数对象:加法
template <class T> template <class T>
struct plus :public binary_function<T, T, T> struct plus :public binary_function<T, T, T>
@ -66,7 +68,7 @@ struct negate :public unarg_function<T, T>
{ {
T operator()(const T& x) const { return -x; } T operator()(const T& x) const { return -x; }
}; };
// 证同元素的意思是,对象与该元素做运算后,该对象仍等于自身,即零元
// 加法的证同元素 // 加法的证同元素
template <class T> template <class T>
T identity_element(plus<T>) { return T(0); } T identity_element(plus<T>) { return T(0); }
@ -75,6 +77,7 @@ T identity_element(plus<T>) { return T(0); }
template <class T> template <class T>
T identity_element(multiplies<T>) { return T(1); } T identity_element(multiplies<T>) { return T(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>
@ -145,6 +148,7 @@ 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; }
}; };
// select
// 选择函数:接受一个 pair返回第一个元素 // 选择函数:接受一个 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>
@ -202,6 +206,8 @@ template <> struct hash<Type> \
{ return static_cast<size_t>(val); } \ { return static_cast<size_t>(val); } \
}; };
// 关于其他类型数据的HASH函数
MYSTL_TRIVIAL_HASH_FCN(bool) MYSTL_TRIVIAL_HASH_FCN(bool)
MYSTL_TRIVIAL_HASH_FCN(char) MYSTL_TRIVIAL_HASH_FCN(char)
@ -235,6 +241,7 @@ 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)
@ -253,6 +260,7 @@ inline size_t bitwise_hash(const unsigned char* first, size_t count)
return result; return result;
} }
// 单精度浮点
template <> template <>
struct hash<float> struct hash<float>
{ {
@ -262,6 +270,7 @@ struct hash<float>
} }
}; };
// 多精度浮点
template <> template <>
struct hash<double> struct hash<double>
{ {
@ -271,6 +280,7 @@ struct hash<double>
} }
}; };
// 高精度浮点
template <> template <>
struct hash<long double> struct hash<long double>
{ {

Loading…
Cancel
Save