📄 non_mutating_algo.h
字号:
template <typename Iterator, typename Predicate> MPTLCountIf<Iterator, Predicate> count_if(Iterator first, Iterator last, Predicate pred) { return MPTLCountIf<Iterator, Predicate>(first, last, pred); } template <typename Iterator1, typename Iterator2> class MPTLMismatch { private: Iterator1 first1, last1; Iterator2 first2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef std::pair<Iterator1, Iterator2> resultType; typedef FindPairPolicy reduction; typedef ThreeIterators nIterators; MPTLMismatch(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) : first1(first1_), last1(last1_), first2(first2_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { std::pair<Iterator1, Iterator2> result = std::mismatch(first1_, last1_, first2_); if (result.first == last1_) return std::make_pair(last1, first2 + std::distance(first1, last1)); return result; } }; template <typename Iterator1, typename Iterator2> MPTLMismatch<Iterator1, Iterator2> mismatch(Iterator1 first1, Iterator1 last1, Iterator2 first2) { return MPTLMismatch<Iterator1, Iterator2>(first1, last1, first2); } template <typename Iterator1, typename Iterator2> class MPTLEqual { private: Iterator1 first1, last1; Iterator2 first2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef bool resultType; typedef BoolPolicy reduction; typedef ThreeIterators nIterators; MPTLEqual(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) : first1(first1_), last1(last1_), first2(first2_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { return std::equal(first1_, last1_, first2_); } }; template <typename Iterator1, typename Iterator2> MPTLEqual<Iterator1, Iterator2> equal(Iterator1 first1, Iterator1 last1, Iterator2 first2) { return MPTLEqual<Iterator1, Iterator2>(first1, last1, first2); } template <typename Iterator1, typename Iterator2, typename BinaryPredicate> class MPTLEqualPred { private: Iterator1 first1, last1; Iterator2 first2; BinaryPredicate binaryPred; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef bool resultType; typedef BoolPolicy reduction; typedef ThreeIterators nIterators; MPTLEqualPred(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, BinaryPredicate binaryPred_) : first1(first1_), last1(last1_), first2(first2_), binaryPred(binaryPred_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { return std::equal(first1_, last1_, first2_, binaryPred); } }; template <typename Iterator1, typename Iterator2, typename BinaryPredicate> MPTLEqualPred<Iterator1, Iterator2, BinaryPredicate> equal(Iterator1 first1, Iterator1 last1, Iterator2 first2, BinaryPredicate binaryPred) { return MPTLEqualPred<Iterator1, Iterator2, BinaryPredicate>(first1, last1, first2, binaryPred); } template <typename Iterator1, typename Iterator2> class MPTLSearch { private: Iterator1 first1, last1; Iterator2 first2, last2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator1 resultType; typedef FindPolicy reduction; typedef TwoIterators nIterators; MPTLSearch(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, Iterator2 last2_) : first1(first1_), last1(last1_), first2(first2_), last2(last2_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } resultType applyAlgorithm(Iterator1 first, Iterator1 last) { typename std::iterator_traits<Iterator1>::difference_type size1 = std::distance(last, last1); typename std::iterator_traits<Iterator2>::difference_type size2 = std::distance(first2, last2); std::advance(last, std::min(size1, size2)); Iterator1 it = std::search(first, last, first2, last2); if (it != last) return it; return last1; } }; template <typename Iterator1, typename Iterator2> MPTLSearch<Iterator1, Iterator2> search(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) { return MPTLSearch<Iterator1, Iterator2>(first1, last1, first2, last2); } template <typename Iterator1, typename Iterator2, typename BinaryPredicate> class MPTLSearchPred { private: Iterator1 first1, last1; Iterator2 first2, last2; BinaryPredicate binaryPred; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator1 resultType; typedef FindPolicy reduction; typedef TwoIterators nIterators; MPTLSearchPred(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, Iterator2 last2_, BinaryPredicate binaryPred_) : first1(first1_), last1(last1_), first2(first2_), last2(last2_), binaryPred(binaryPred_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } resultType applyAlgorithm(Iterator1 first, Iterator1 last) { typename std::iterator_traits<Iterator1>::difference_type size1 = std::distance(last, last1); typename std::iterator_traits<Iterator2>::difference_type size2 = std::distance(first2, last2); std::advance(last, std::min(size1, size2)); Iterator1 it = std::search(first, last, first2, last2, binaryPred); if (it != last) return it; return last1; } }; template <typename Iterator1, typename Iterator2, typename BinaryPredicate> MPTLSearchPred<Iterator1, Iterator2, BinaryPredicate> search(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2, BinaryPredicate binaryPred) { return MPTLSearchPred<Iterator1, Iterator2, BinaryPredicate>(first1, last1, first2, last2, binaryPred); } template <typename Iterator, typename Integer, typename T> class MPTLSearchN { private: Iterator first1, last1; Integer count; const T& value; public: typedef Iterator iterator1; typedef Iterator resultType; typedef FindPolicy reduction; typedef TwoIterators nIterators; MPTLSearchN(Iterator first1_, Iterator last1_, Integer count_, const T& value_) : first1(first1_), last1(last1_), count(count_), value(value_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } resultType applyAlgorithm(Iterator first, Iterator last) { typename std::iterator_traits<Iterator>::difference_type size = std::distance(last, last1); int tmp = (size < count)? size : count; std::advance(last, tmp); Iterator it = std::search_n(first, last, count, value); if (it == last) return last1; return it; } }; template <typename Iterator, typename Integer, typename T> MPTLSearchN<Iterator, Integer, T> search_n(Iterator first, Iterator last, Integer count, const T& value) { return MPTLSearchN<Iterator, Integer, T>(first, last, count, value); } template <typename Iterator, typename Integer, typename T, typename BinaryPredicate> class MPTLSearchNPred { private: Iterator first1, last1; Integer count; const T& value; BinaryPredicate binaryPred; public: typedef Iterator iterator1; typedef Iterator resultType; typedef FindPolicy reduction; typedef TwoIterators nIterators; MPTLSearchNPred(Iterator first1_, Iterator last1_, Integer count_, const T& value_, BinaryPredicate binaryPred_) : first1(first1_), last1(last1_), count(count_), value(value_), binaryPred(binaryPred_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } resultType applyAlgorithm(Iterator first, Iterator last) { typename std::iterator_traits<Iterator>::difference_type size = std::distance(last, last1); int tmp = (size < count)? size : count; std::advance(last, tmp); Iterator it = std::search_n(first, last, count, value, binaryPred); if (it == last) return last1; return it; } }; template <typename Iterator, typename Integer, typename T, typename BinaryPred> MPTLSearchNPred<Iterator, Integer, T, BinaryPred> search_n(Iterator first, Iterator last, Integer count, const T& value, BinaryPred binaryPred) { return MPTLSearchNPred<Iterator, Integer, T, BinaryPred>(first, last, count, value, binaryPred); } template <typename Iterator1, typename Iterator2> class MPTLFindEnd { private: Iterator1 first1, last1; Iterator2 first2, last2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator1 resultType; typedef FindEndPolicy reduction; typedef TwoIterators nIterators; MPTLFindEnd(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, Iterator2 last2_) : first1(first1_), last1(last1_), first2(first2_), last2(last2_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } resultType applyAlgorithm(Iterator1 first, Iterator1 last) { typename std::iterator_traits<Iterator1>::difference_type size1 = std::distance(last, last1); typename std::iterator_traits<Iterator2>::difference_type size2 = std::distance(first2, last2); std::advance(last, std::min(size1, size2)); Iterator1 it = std::find_end(first, last, first2, last2); if (it != last) return it; return last1; } }; template <typename Iterator1, typename Iterator2> MPTLFindEnd<Iterator1, Iterator2> find_end(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator2 last2) { return MPTLFindEnd<Iterator1, Iterator2>(first1, last1, first2, last2); } } // namespace mptl#endif /* _NON_MUTATING_ALGO */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -