📄 algorithm
字号:
// algorithm stl/clr header
#ifndef _CLI_ALGORITHM_
#define _CLI_ALGORITHM_
#include <cliext/memory>
namespace cliext {
// REF CLASS _ISort
ref class _ISort
{ // define crossover for insertion sort
public:
literal int _Max = 32;
};
// TEMPLATE FUNCTION for_each
template<class _InIt,
class _Fn1> inline
_Fn1 for_each_unchecked(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
for (; _First != _Last; ++_First)
_Func(*_First);
return (_Func);
}
template<class _InIt,
class _Fn1> inline
_Fn1 for_each(_InIt _First, _InIt _Last, _Fn1 _Func)
{ // perform function for each element
_STLCLRDB_RANGE(_First, _Last);
_STLCLRDB_POINTER(_Func);
return (cliext::for_each_unchecked(_Unchecked(_First), _Unchecked(_Last),
_Func));
}
// TEMPLATE FUNCTION find
template<class _InIt,
class _Ty> inline
_InIt find_unchecked(_InIt _First, _InIt _Last, const _Ty% _Val)
{ // find first matching _Val
for (; _First != _Last; ++_First)
if (*_First == _Val)
break;
return (_First);
}
template<class _InIt,
class _Ty> inline
_InIt find(_InIt _First, _InIt _Last, const _Ty% _Val)
{ // find first matching _Val
_STLCLRDB_RANGE(_First, _Last);
return (cliext::find_unchecked(_Unchecked(_First), _Unchecked(_Last),
_Val));
}
#ifdef _M_CEE_SAFE
#else /* _M_CEE_SAFE */
inline const char *find(const char *_First, const char *_Last, int _Val)
{ // find first char that matches _Val
_STLCLRDB_RANGE(_First, _Last);
_First = (const char *)std::memchr(_First, _Val, _Last - _First);
return (_First == 0 ? _Last : _First);
}
inline const signed char *find(const signed char *_First,
const signed char *_Last, int _Val)
{ // find first signed char that matches _Val
_STLCLRDB_RANGE(_First, _Last);
_First = (const signed char *)std::memchr(_First, _Val,
_Last - _First);
return (_First == 0 ? _Last : _First);
}
inline const unsigned char *find(const unsigned char *_First,
const unsigned char *_Last, int _Val)
{ // find first unsigned char that matches _Val
_STLCLRDB_RANGE(_First, _Last);
_First = (const unsigned char *)std::memchr(_First, _Val,
_Last - _First);
return (_First == 0 ? _Last : _First);
}
#endif /* _M_CEE_SAFE */
// TEMPLATE FUNCTION find_if
template<class _InIt,
class _Pr> inline
_InIt find_if_unchecked(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first satisfying _Pred
for (; _First != _Last; ++_First)
if (_Pred(*_First))
break;
return (_First);
}
template<class _InIt,
class _Pr> inline
_InIt find_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // find first satisfying _Pred
_STLCLRDB_RANGE(_First, _Last);
_STLCLRDB_POINTER(_Pred);
return (cliext::find_if_unchecked(
_Unchecked(_First), _Unchecked(_Last), _Pred));
}
// TEMPLATE FUNCTION adjacent_find
template<class _FwdIt> inline
_FwdIt adjacent_find_unchecked(_FwdIt _First, _FwdIt _Last)
{ // find first matching successor
for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; )
if (*_Firstb == *_First)
return (_Firstb);
return (_Last);
}
template<class _FwdIt> inline
_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last)
{ // find first matching successor
_STLCLRDB_RANGE(_First, _Last);
return (cliext::adjacent_find_unchecked(
_Unchecked(_First), _Unchecked(_Last)));
}
// TEMPLATE FUNCTION adjacent_find WITH PRED
template<class _FwdIt,
class _Pr> inline
_FwdIt adjacent_find_unchecked(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
{ // find first satisfying _Pred with successor
for (_FwdIt _Firstb; (_Firstb = _First) != _Last && ++_First != _Last; )
if (_Pred(*_Firstb, *_First))
return (_Firstb);
return (_Last);
}
template<class _FwdIt,
class _Pr> inline
_FwdIt adjacent_find(_FwdIt _First, _FwdIt _Last, _Pr _Pred)
{ // find first satisfying _Pred with successor
_STLCLRDB_RANGE(_First, _Last);
_STLCLRDB_POINTER(_Pred);
return (cliext::adjacent_find_unchecked(
_Unchecked(_First), _Unchecked(_Last), _Pred));
}
// TEMPLATE FUNCTION count
template<class _InIt,
class _Ty> inline
typename iterator_traits<_InIt>::difference_type
count_unchecked(_InIt _First, _InIt _Last, const _Ty% _Val)
{ // count elements that match _Val
typename iterator_traits<_InIt>::difference_type _Count = 0;
for (; _First != _Last; ++_First)
if (*_First == _Val)
++_Count;
return (_Count);
}
template<class _InIt,
class _Ty> inline
typename iterator_traits<_InIt>::difference_type
count(_InIt _First, _InIt _Last, const _Ty% _Val)
{ // count elements that match _Val
_STLCLRDB_RANGE(_First, _Last);
return (cliext::count_unchecked(_Unchecked(_First), _Unchecked(_Last),
_Val));
}
// TEMPLATE FUNCTION count_if
template<class _InIt,
class _Pr> inline
typename iterator_traits<_InIt>::difference_type
count_if_unchecked(_InIt _First, _InIt _Last, _Pr _Pred)
{ // count elements satisfying _Pred
typename iterator_traits<_InIt>::difference_type _Count = 0;
for (; _First != _Last; ++_First)
if (_Pred(*_First))
++_Count;
return (_Count);
}
template<class _InIt,
class _Pr> inline
typename iterator_traits<_InIt>::difference_type
count_if(_InIt _First, _InIt _Last, _Pr _Pred)
{ // count elements satisfying _Pred
_STLCLRDB_RANGE(_First, _Last);
_STLCLRDB_POINTER(_Pred);
return (cliext::count_if_unchecked(_Unchecked(_First), _Unchecked(_Last),
_Pred));
}
// TEMPLATE FUNCTION search
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt1 search_unchecked(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2)
{ // find first [_First2, _Last2) match
typedef iterator_traits<_FwdIt1>::difference_type _Diff1;
typedef iterator_traits<_FwdIt2>::difference_type _Diff2;
_Diff1 _Count1 = 0;
_Iter_distance(_First1, _Last1, _Count1);
_Diff2 _Count2 = 0;
_Iter_distance(_First2, _Last2, _Count2);
for (; _Count2 <= _Count1; ++_First1, --_Count1)
{ // room for match, try it
_FwdIt1 _Mid1 = _First1;
for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1, ++_Mid2)
if (_Mid2 == _Last2)
return (_First1);
else if (!(*_Mid1 == *_Mid2))
break;
}
return (_Last1);
}
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2)
{ // find first [_First2, _Last2) match
_STLCLRDB_RANGE(_First1, _Last1);
_STLCLRDB_RANGE(_First2, _Last2);
return (cliext::search_unchecked(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Unchecked(_Last2)));
}
// TEMPLATE FUNCTION search WITH PRED
template<class _FwdIt1,
class _FwdIt2,
class _Pr> inline
_FwdIt1 search_unchecked(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
{ // find first [_First2, _Last2) satisfying _Pred
typedef iterator_traits<_FwdIt1>::difference_type _Diff1;
typedef iterator_traits<_FwdIt2>::difference_type _Diff2;
_Diff1 _Count1 = 0;
_Iter_distance(_First1, _Last1, _Count1);
_Diff2 _Count2 = 0;
_Iter_distance(_First2, _Last2, _Count2);
for (; _Count2 <= _Count1; ++_First1, --_Count1)
{ // room for match, try it
_FwdIt1 _Mid1 = _First1;
for (_FwdIt2 _Mid2 = _First2; ; ++_Mid1, ++_Mid2)
if (_Mid2 == _Last2)
return (_First1);
else if (!_Pred(*_Mid1, *_Mid2))
break;
}
return (_Last1);
}
template<class _FwdIt1,
class _FwdIt2,
class _Pr> inline
_FwdIt1 search(_FwdIt1 _First1, _FwdIt1 _Last1,
_FwdIt2 _First2, _FwdIt2 _Last2, _Pr _Pred)
{ // find first [_First2, _Last2) satisfying _Pred
_STLCLRDB_RANGE(_First1, _Last1);
_STLCLRDB_RANGE(_First2, _Last2);
_STLCLRDB_POINTER(_Pred);
return (cliext::search_unchecked(_Unchecked(_First1), _Unchecked(_Last1),
_Unchecked(_First2), _Unchecked(_Last2), _Pred));
}
// TEMPLATE FUNCTION search_n
template<class _FwdIt1,
class _Diff2,
class _Ty> inline
_FwdIt1 _Search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
_Diff2 _Count, const _Ty% _Val, forward_iterator_tag)
{ // find first _Count * _Val match, forward iterators
if (_Count <= 0)
return (_First1);
for (; _First1 != _Last1; ++_First1)
if (*_First1 == _Val)
{ // found start of possible match, check it out
_FwdIt1 _Mid1 = _First1;
for (_Diff2 _Count1 = _Count; ; )
if (--_Count1 == 0)
return (_First1); // found rest of match, report it
else if (++_Mid1 == _Last1)
return (_Last1); // short match at end
else if (!(*_Mid1 == _Val))
break; // short match not at end
_First1 = _Mid1; // pick up just beyond failed match
}
return (_Last1);
}
template<class _FwdIt1,
class _Diff2,
class _Ty> inline
_FwdIt1 _Search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
_Diff2 _Count, const _Ty% _Val, random_access_iterator_tag)
{ // find first _Count * _Val match, random-access iterators
if (_Count <= 0)
return (_First1);
_FwdIt1 _Oldfirst1 = _First1;
for (; _Count <= _Last1 - _Oldfirst1; )
{ // enough room, look for a match
if (*_First1 == _Val)
{ // found part of possible match, check it out
_Diff2 _Count1 = _Count;
_FwdIt1 _Mid1 = _First1;
for (; _Oldfirst1 != _First1 && _First1[-1] == _Val; --_First1)
--_Count1; // back up over any skipped prefix
if (_Count1 <= _Last1 - _Mid1)
for (; ; ) // enough left, test suffix
if (--_Count1 == 0)
return (_First1); // found rest of match, report it
else if (!(*++_Mid1 == _Val))
break; // short match not at end
_Oldfirst1 = ++_Mid1; // failed match, take small jump
_First1 = _Oldfirst1;
}
else
{ // no match, take big jump and back up as needed
_Oldfirst1 = _First1 + 1;
_First1 += _Count;
}
}
return (_Last1);
}
template<class _FwdIt1,
class _Diff2,
class _Ty> inline
_FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
_Diff2 _Count, const _Ty% _Val)
{ // find first _Count * _Val match
_STLCLRDB_RANGE(_First1, _Last1);
return (_Search_n(_Unchecked(_First1), _Unchecked(_Last1),
_Count, _Val, _Iter_category(_First1)));
}
// TEMPLATE FUNCTION search_n WITH PRED
template<class _FwdIt1,
class _Diff2,
class _Ty,
class _Pr> inline
_FwdIt1 _Search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
_Diff2 _Count, const _Ty% _Val, _Pr _Pred, forward_iterator_tag)
{ // find first _Count * _Val satisfying _Pred, forward iterators
if (_Count <= 0)
return (_First1);
for (; _First1 != _Last1; ++_First1)
if (_Pred(*_First1, _Val))
{ // found start of possible match, check it out
_FwdIt1 _Mid1 = _First1;
for (_Diff2 _Count1 = _Count; ; )
if (--_Count1 == 0)
return (_First1); // found rest of match, report it
else if (++_Mid1 == _Last1)
return (_Last1); // short match at end
else if (!_Pred(*_Mid1, _Val))
break; // short match not at end
_First1 = _Mid1; // pick up just beyond failed match
}
return (_Last1);
}
template<class _FwdIt1,
class _Diff2,
class _Ty,
class _Pr> inline
_FwdIt1 _Search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
_Diff2 _Count, const _Ty% _Val, _Pr _Pred, random_access_iterator_tag)
{ // find first _Count * _Val satisfying _Pred, random-access iterators
if (_Count <= 0)
return (_First1);
_FwdIt1 _Oldfirst1 = _First1;
for (; _Count <= _Last1 - _Oldfirst1; )
{ // enough room, look for a match
if (_Pred(*_First1, _Val))
{ // found part of possible match, check it out
_Diff2 _Count1 = _Count;
_FwdIt1 _Mid1 = _First1;
for (; _Oldfirst1 != _First1 && _First1[-1] == _Val; --_First1)
--_Count1; // back up over any skipped prefix
if (_Count1 <= _Last1 - _Mid1)
for (; ; ) // enough left, test suffix
if (--_Count1 == 0)
return (_First1); // found rest of match, report it
else if (!_Pred(*++_Mid1, _Val))
break; // short match not at end
_Oldfirst1 = ++_Mid1; // failed match, take small jump
_First1 = _Oldfirst1;
}
else
{ // no match, take big jump and back up as needed
_Oldfirst1 = _First1 + 1;
_First1 += _Count;
}
}
return (_Last1);
}
template<class _FwdIt1,
class _Diff2,
class _Ty,
class _Pr> inline
_FwdIt1 search_n(_FwdIt1 _First1, _FwdIt1 _Last1,
_Diff2 _Count, const _Ty% _Val, _Pr _Pred)
{ // find first _Count * _Val satisfying _Pred
_STLCLRDB_RANGE(_First1, _Last1);
_STLCLRDB_POINTER(_Pred);
return (_Search_n(_Unchecked(_First1), _Unchecked(_Last1),
_Count, _Val, _Pred, _Iter_category(_First1)));
}
// TEMPLATE FUNCTION find_end
template<class _FwdIt1,
class _FwdIt2> inline
_FwdIt1 find_end_unchecked(_FwdIt1 _First1, _FwdIt1 _Last1,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -