namespace std _GLIBCXX_VISIBILITY(default)
{
template<typename _ForwardIterator1, typename _ForwardIterator2>
inline void
iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b) //交换两迭代器所指内容
{
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator1>)
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator2>)
swap(*__a, *__b); //交换迭代器所指内容
}
template<typename _ForwardIterator1, typename _ForwardIterator2>
_ForwardIterator2
swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
_ForwardIterator2 __first2) //交换两序列元素
{
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator1>)
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator2>)
__glibcxx_requires_valid_range(__first1, __last1);
for (; __first1 != __last1; ++__first1, (void)++__first2) //执行序列1循环
std::iter_swap(__first1, __first2); //交换迭代器所指内容
return __first2; //返回序列2
}
template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline const _Tp&
min(const _Tp& __a, const _Tp& __b) //取两对象中的极小值
{
__glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
//return __b < __a ? __b : __a;
if (__b < __a)
return __b; //若b<a,则返回b
return __a; //否则返回a
}
template<typename _Tp>
_GLIBCXX14_CONSTEXPR
inline const _Tp&
max(const _Tp& __a, const _Tp& __b) //取两对象中的极大值
{
__glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
//return __a < __b ? __b : __a;
if (__a < __b)
return __b; //若b>a,则返回b
return __a; //否则返回a
}
template<typename _Tp, typename _Compare>
_GLIBCXX14_CONSTEXPR
inline const _Tp&
min(const _Tp& __a, const _Tp& __b, _Compare __comp) //使用仿函数comp判断对象大小
{
if (__comp(__b, __a))
return __b; //仿函数comp判断对象大小,判断为true时返回b
return __a; //否则返回a
}
template<typename _Tp, typename _Compare>
_GLIBCXX14_CONSTEXPR
inline const _Tp&
max(const _Tp& __a, const _Tp& __b, _Compare __comp) //使用仿函数comp判断对象大小
{
//return __comp(__a, __b) ? __b : __a;
if (__comp(__a, __b))
return __b; //仿函数判断为true时返回b
return __a; //否则返回a
}
template<typename _Iterator>
inline _Iterator
__niter_base(_Iterator __it) //移除迭代器的封装,如封装为反向迭代器等
{ return __it; } //返回迭代器本身
template<typename _ForwardIterator, typename _Tp> //模板函数__fill_a
inline typename
__gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type //!__is_scalar
__fill_a(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value) //迭代器范围[first, last)填充值value
{
for (; __first != __last; ++__first) //序列循环
*__first = __value; //赋值value
}
template<typename _ForwardIterator, typename _Tp>
inline typename
__gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type //__is_scalar
__fill_a(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __value) //迭代器范围[first, last)填充值value
{
const _Tp __tmp = __value;
for (; __first != __last; ++__first)
*__first = __tmp;
}
template<typename _Tp>
inline typename
__gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
__fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
{
const _Tp __tmp = __c;
if (const size_t __len = __last - __first)
__builtin_memset(__first, static_cast<unsigned char>(__tmp), __len); //内建memset函数给序列赋值
}
template<typename _ForwardIterator, typename _Tp>
inline void
fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
{
__glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
_ForwardIterator>)
__glibcxx_requires_valid_range(__first, __last);
std::__fill_a(std::__niter_base(__first), std::__niter_base(__last), //移除迭代器封装
__value); //调用内建memset或循环赋值填充序列
}
template<typename _OutputIterator, typename _Size, typename _Tp> //模板函数__fill_n_a
inline typename
__gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type //!__is_scalar
__fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
{
for (__decltype(__n + 0) __niter = __n;
__niter > 0; --__niter, (void) ++__first) //循环n次
*__first = __value; //赋值value
return __first;
}
template<typename _OutputIterator, typename _Size, typename _Tp> //模板函数__fill_n_a
inline typename
__gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type //__is_scalar
__fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
{
const _Tp __tmp = __value;
for (__decltype(__n + 0) __niter = __n;
__niter > 0; --__niter, (void) ++__first) //循环n次
*__first = __tmp; //赋值value
return __first;
}
template<typename _Size, typename _Tp> //模板函数__fill_n_a
inline typename
__gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type //__is_byte
__fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
{
std::__fill_a(__first, __first + __n, __c); //调用指针类型__fill_a
return __first + __n;
}
template<typename _OI, typename _Size, typename _Tp> //模板函数fill_n
inline _OI
fill_n(_OI __first, _Size __n, const _Tp& __value)
{
__glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
}
template<bool _BoolType> //模板类__equal<bool>
struct __equal
{
template<typename _II1, typename _II2>
static bool
equal(_II1 __first1, _II1 __last1, _II2 __first2) //序列2长度必须大于或等于序列1
{
for (; __first1 != __last1; ++__first1, (void) ++__first2) //执行序列1的循环
if (!(*__first1 == *__first2)) //判断迭代器所指内容是否相等
return false; //对应元素不相等返回false
return true;
}
};
template<> //模板类特例化版本__equal<true>
struct __equal<true>
{
template<typename _Tp>
static bool
equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
{
if (const size_t __len = (__last1 - __first1)) //比较长度为序列1长度
return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) * __len); //使用内建的memcmp函数比较两序列内容
return true;
}
};
template<typename _II1, typename _II2>
inline bool
__equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
{
typedef typename iterator_traits<_II1>::value_type _ValueType1; //迭代器1所指内容类型
typedef typename iterator_traits<_II2>::value_type _ValueType2; //迭代器2所指内容类型
const bool __simple = ((__is_integer<_ValueType1>::__value //迭代器1所指内容是否为整数
|| __is_pointer<_ValueType1>::__value) //迭代器1所指内容是否为指针
&& __is_pointer<_II1>::__value //迭代器1是否为指针
&& __is_pointer<_II2>::__value //迭代器2是否为指针
&& __are_same<_ValueType1, _ValueType2>::__value); //迭代器1和2所指内容类型是否相等
//__simple为true时使用内建memcmp函数判断序列元素是否相等,为false时执行序列1循环一一比较
return std::__equal<__simple>::equal(__first1, __last1, __first2); //根据__simple值决定调用equal<bool>版本
}
template<typename, typename> //模板类__lc_rai<typename, typename>
struct __lc_rai
{
template<typename _II1, typename _II2>
static _II1
__newlast1(_II1, _II1 __last1, _II2, _II2)
{ return __last1; } //返回迭代器参数__last1
template<typename _II>
static bool
__cnd2(_II __first, _II __last)
{ return __first != __last; } //比较两迭代器是否相等
};
template<> //模板类特例化版本
struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag> //随机访问迭代器
{
template<typename _RAI1, typename _RAI2>
static _RAI1
__newlast1(_RAI1 __first1, _RAI1 __last1,
_RAI2 __first2, _RAI2 __last2) //返回两序列比较后迭代器1的位置
{
const typename iterator_traits<_RAI1>::difference_type //表迭代器之间距离的类型
__diff1 = __last1 - __first1; //迭代器_RAI1[first, last)之间的距离
const typename iterator_traits<_RAI2>::difference_type
__diff2 = __last2 - __first2; //迭代器_RAI2[first, last)之间的距离
return __diff2 < __diff1 ? __first1 + __diff2 : __last1; //两间距比较,_RAI1间距大时返回last1,_RAI1间距小时返回first+diff2
}
template<typename _RAI> //特例化模样类中的模板函数__cnd2
static bool
__cnd2(_RAI, _RAI)
{ return true; } //直接返回true
};
template<typename _II1, typename _II2, typename _Compare> //模板类__lexicographical_compare_impl
bool
__lexicographical_compare_impl(_II1 __first1, _II1 __last1,
_II2 __first2, _II2 __last2,
_Compare __comp) //使用仿函数比较两序列元素
{
typedef typename iterator_traits<_II1>::iterator_category _Category1; //迭代器_II1类型
typedef typename iterator_traits<_II2>::iterator_category _Category2; //迭代器_II2类型
typedef std::__lc_rai<_Category1, _Category2> __rai_type;
__last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
++__first1, (void)++__first2)
{
if (__comp(__first1, __first2))
return true;
if (__comp(__first2, __first1))
return false;
}
return __first1 == __last1 && __first2 != __last2;
}
template<bool _BoolType>
struct __lexicographical_compare
{
template<typename _II1, typename _II2>
static bool __lc(_II1, _II1, _II2, _II2);
};
template<bool _BoolType>
template<typename _II1, typename _II2>
bool
__lexicographical_compare<_BoolType>::
__lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
{
return std::__lexicographical_compare_impl(__first1, __last1,
__first2, __last2,
__gnu_cxx::__ops::__iter_less_iter());
}
template<> //模板类特例化版本__lexicographical_compare<true>
struct __lexicographical_compare<true>
{
template<typename _Tp, typename _Up>
static bool
__lc(const _Tp* __first1, const _Tp* __last1,
const _Up* __first2, const _Up* __last2)
{
const size_t __len1 = __last1 - __first1;
const size_t __len2 = __last2 - __first2;
if (const size_t __len = std::min(__len1, __len2))
if (int __result = __builtin_memcmp(__first1, __first2, __len))
return __result < 0;
return __len1 < __len2;
}
};
template<typename _II1, typename _II2> //模板函数__lexicographical_compare_aux
inline bool
__lexicographical_compare_aux(_II1 __first1, _II1 __last1,
_II2 __first2, _II2 __last2)
{
typedef typename iterator_traits<_II1>::value_type _ValueType1;
typedef typename iterator_traits<_II2>::value_type _ValueType2;
const bool __simple =
(__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
&& !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
&& !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
&& __is_pointer<_II1>::__value
&& __is_pointer<_II2>::__value);
return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
__first2, __last2);
}
template<typename _ForwardIterator, typename _Tp, typename _Compare>
_ForwardIterator
__lower_bound(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __val, _Compare __comp)
{
typedef typename iterator_traits<_ForwardIterator>::difference_type
_DistanceType;
_DistanceType __len = std::distance(__first, __last);
while (__len > 0)
{
_DistanceType __half = __len >> 1;
_ForwardIterator __middle = __first;
std::advance(__middle, __half);
if (__comp(__middle, __val))
{
__first = __middle;
++__first;
__len = __len - __half - 1;
}
else
__len = __half;
}
return __first;
}
template<typename _ForwardIterator, typename _Tp>
inline _ForwardIterator
lower_bound(_ForwardIterator __first, _ForwardIterator __last,
const _Tp& __val)
{
// concept requirements
__glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
__glibcxx_function_requires(_LessThanOpConcept<
typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
__glibcxx_requires_partitioned_lower(__first, __last, __val);
return std::__lower_bound(__first, __last, __val,
__gnu_cxx::__ops::__iter_less_val());
}
inline _GLIBCXX_CONSTEXPR int
__lg(int __n)
{ return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
inline _GLIBCXX_CONSTEXPR unsigned
__lg(unsigned __n)
{ return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
inline _GLIBCXX_CONSTEXPR long
__lg(long __n)
{ return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
inline _GLIBCXX_CONSTEXPR unsigned long
__lg(unsigned long __n)
{ return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
inline _GLIBCXX_CONSTEXPR long long
__lg(long long __n)
{ return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
inline _GLIBCXX_CONSTEXPR unsigned long long
__lg(unsigned long long __n)
{ return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
template<typename _II1, typename _II2>
inline bool //输入参数为3个迭代器
equal(_II1 __first1, _II1 __last1, _II2 __first2) //序列1[first, last),序列2 first
{
__glibcxx_function_requires(_InputIteratorConcept<_II1>)
__glibcxx_function_requires(_InputIteratorConcept<_II2>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_II1>::value_type,
typename iterator_traits<_II2>::value_type>)
__glibcxx_requires_valid_range(__first1, __last1);
return std::__equal_aux(std::__niter_base(__first1), //调用__equal_aux判断序列是否相等
std::__niter_base(__last1), //__niter_base转换正向或反向迭代器为正向迭代器
std::__niter_base(__first2));
}
template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
inline bool
equal(_IIter1 __first1, _IIter1 __last1,
_IIter2 __first2, _BinaryPredicate __binary_pred) //通过仿函数执行判断序列
{
__glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
__glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
__glibcxx_requires_valid_range(__first1, __last1);
for (; __first1 != __last1; ++__first1, (void)++__first2) //执行序列1的循环
if (!bool(__binary_pred(*__first1, *__first2))) //通过仿函数__binary_pred执行序列判断
return false;
return true;
}
template<typename _II1, typename _II2>
inline bool //输入参数为四个迭代器,序列1[first, last),序列2[first, last)
__equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
{
using _RATag = random_access_iterator_tag; //声明_RATag为随机访问迭代器
using _Cat1 = typename iterator_traits<_II1>::iterator_category; //序列1迭代器类型
using _Cat2 = typename iterator_traits<_II2>::iterator_category; //序列2迭代器类型
using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>; //序列1,2是否为随机访问迭代器
if (_RAIters()) //序列1,2都为随机访问迭代器时执行以下代码
{
auto __d1 = std::distance(__first1, __last1); //distance获取两迭代器之间的距离
auto __d2 = std::distance(__first2, __last2);
if (__d1 != __d2) //判断两序列长度是否相等,不相等时直接返回false
return false;
return _GLIBCXX_STD_A::equal(__first1, __last1, __first2); //序列长度相等时执行equal输入为三个参数的版本
}
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2) //执行for循环,两迭代器同步前向+1前进
if (!(*__first1 == *__first2)) //判断两迭代器所指内容是否相等,不相等时直接返回false
return false;
return __first1 == __last1 && __first2 == __last2; //循环结束同步到达尾结点时返回true
}
template<typename _II1, typename _II2, typename _BinaryPredicate>
inline bool //输入参数为四个迭代器,使用仿函数执行序列判断
__equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
_BinaryPredicate __binary_pred) //执行步骤与上面常规判断代码一致
{
using _RATag = random_access_iterator_tag;
using _Cat1 = typename iterator_traits<_II1>::iterator_category;
using _Cat2 = typename iterator_traits<_II2>::iterator_category;
using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
if (_RAIters())
{
auto __d1 = std::distance(__first1, __last1);
auto __d2 = std::distance(__first2, __last2);
if (__d1 != __d2)
return false;
return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
__binary_pred); //调用equal带仿函数判断版本
}
for (; __first1 != __last1 && __first2 != __last2;
++__first1, (void)++__first2)
if (!bool(__binary_pred(*__first1, *__first2))) //使用仿函数执行序列元素的判断
return false;
return __first1 == __last1 && __first2 == __last2;
}
template<typename _II1, typename _II2> //模板类lexicographical_compare
inline bool
lexicographical_compare(_II1 __first1, _II1 __last1,
_II2 __first2, _II2 __last2) //以字典排序对序列进行比较
{
__glibcxx_function_requires(_InputIteratorConcept<_II1>)
__glibcxx_function_requires(_InputIteratorConcept<_II2>)
__glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
__glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
__glibcxx_requires_valid_range(__first1, __last1);
__glibcxx_requires_valid_range(__first2, __last2);
return std::__lexicographical_compare_aux(std::__niter_base(__first1),
std::__niter_base(__last1),
std::__niter_base(__first2),
std::__niter_base(__last2));
}
template<typename _II1, typename _II2, typename _Compare>
inline bool
lexicographical_compare(_II1 __first1, _II1 __last1,
_II2 __first2, _II2 __last2, _Compare __comp) //以仿函数__comp比较两序列
{
// concept requirements
__glibcxx_function_requires(_InputIteratorConcept<_II1>)
__glibcxx_function_requires(_InputIteratorConcept<_II2>)
__glibcxx_requires_valid_range(__first1, __last1);
__glibcxx_requires_valid_range(__first2, __last2);
return std::__lexicographical_compare_impl
(__first1, __last1, __first2, __last2,
__gnu_cxx::__ops::__iter_comp_iter(__comp));
}
template<typename _InputIterator1, typename _InputIterator2,
typename _BinaryPredicate>
pair<_InputIterator1, _InputIterator2>
__mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _BinaryPredicate __binary_pred)
{
while (__first1 != __last1 && __binary_pred(__first1, __first2))
{
++__first1;
++__first2;
}
return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
}
template<typename _InputIterator1, typename _InputIterator2>
inline pair<_InputIterator1, _InputIterator2>
mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2)
{ //找出两序列第一个不匹配点,返回两迭代器组成的pair
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
__glibcxx_function_requires(_EqualOpConcept<
typename iterator_traits<_InputIterator1>::value_type,
typename iterator_traits<_InputIterator2>::value_type>)
__glibcxx_requires_valid_range(__first1, __last1);
return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
__gnu_cxx::__ops::__iter_equal_to_iter());
}
template<typename _InputIterator1, typename _InputIterator2,
typename _BinaryPredicate>
inline pair<_InputIterator1, _InputIterator2>
mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
_InputIterator2 __first2, _BinaryPredicate __binary_pred)
{ //以仿函数__binary_pred找出两序列不匹配点
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
__glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
__glibcxx_requires_valid_range(__first1, __last1);
return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
__gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
}
} // namespace std
STL源码剖析(十九)基本算法<stl_algobase.h>
最新推荐文章于 2022-08-04 18:51:17 发布