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