📄 mutating_algo.h
字号:
// Copyright (C) 2006 Didier Baertschiger// e-mail contact: dibaer@gmail.com// The MPTL library is free software; you can redistribute it and/or// modify it under the terms of the GNU Lesser General Public// License as published by the Free Software Foundation; either// version 2.1 of the License, or (at your option) any later version.// This library is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU// Lesser General Public License for more details.// You should have received a copy of the GNU Lesser General Public// License along with this library; if not, write to the Free Software// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA/// \file mutating_algo.h/// \brief Impl閙entation multithread des algorithmes standard de la STL./// \author Didier Baertschiger/// \date 07-11-2005////// #ifndef _MUTATING_ALGO#define _MUTATING_ALGO 1#include <algorithm>#include <iterator>#include <utility>namespace mptl{ template <typename Iterator1, typename Iterator2> class MPTLCopy { private: Iterator1 first1, last1; Iterator2 first2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLCopy(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::copy(first1_, last1_, first2_); } }; template <typename Iterator1, typename Iterator2> MPTLCopy<Iterator1, Iterator2> copy(Iterator1 first, Iterator1 last, Iterator2 result) { return MPTLCopy<Iterator1, Iterator2>(first, last, result); } template <typename Iterator1, typename Iterator2> class MPTLCopyBackward { private: Iterator1 first1, last1; Iterator2 first2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLCopyBackward(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) : first1(first1_), last1(last1_), first2(first2_) { // Pour utiliser l'algorithme copy() std::advance(first2, std::distance(last1, first1)); } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { return std::copy(first1_, last1_, first2_); } }; template <typename Iterator1, typename Iterator2> MPTLCopyBackward<Iterator1, Iterator2> copy_backward(Iterator1 first, Iterator1 last, Iterator2 result) { return MPTLCopyBackward<Iterator1, Iterator2>(first, last, result); } template <typename Iterator1, typename Iterator2> class MPTLSwapRanges { private: Iterator1 first1, last1; Iterator2 first2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLSwapRanges(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::swap_ranges(first1_, last1_, first2_); } }; template <typename Iterator1, typename Iterator2> MPTLSwapRanges<Iterator1, Iterator2> swap_ranges(Iterator1 first1, Iterator1 last1, Iterator2 first2) { return MPTLSwapRanges<Iterator1, Iterator2>(first1, last1, first2); } template <typename Iterator1, typename Iterator2, typename UnaryFunction> class MPTLTransform { private: Iterator1 first1, last1; Iterator2 first2; UnaryFunction op; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLTransform(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, UnaryFunction op_) : first1(first1_), last1(last1_), first2(first2_), op(op_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { return std::transform(first1_, last1_, first2_, op); } }; template <typename Iterator1, typename Iterator2, typename UnaryFunction> MPTLTransform<Iterator1, Iterator2, UnaryFunction> transform(Iterator1 first, Iterator1 last, Iterator2 result, UnaryFunction op) { return MPTLTransform<Iterator1, Iterator2, UnaryFunction>(first, last, result, op); } template <typename Iterator1, typename Iterator2, typename Iterator3, typename BinaryFunction> class MPTLTransformBinary { private: Iterator1 first1, last1; Iterator2 first2; Iterator3 first3; BinaryFunction binaryOp; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator3 iterator3; typedef Iterator3 resultType; typedef LastElementPolicy reduction; typedef FourIterators nIterators; MPTLTransformBinary(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, Iterator3 first3_, BinaryFunction binaryOp_) : first1(first1_), last1(last1_), first2(first2_), first3(first3_), binaryOp(binaryOp_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } Iterator3 getFirst3() { return first3; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, Iterator3 first3_) { return std::transform(first1_, last1_, first2_, first3_, binaryOp); } }; template <typename Iterator1, typename Iterator2, typename Iterator3, typename BinaryFunction> MPTLTransformBinary<Iterator1, Iterator2, Iterator3, BinaryFunction> transform(Iterator1 first1, Iterator1 last1, Iterator2 first2, Iterator3 result, BinaryFunction binaryOp) { return MPTLTransformBinary<Iterator1, Iterator2, Iterator3, BinaryFunction>(first1, last1, first2, result, binaryOp); } template <typename Iterator, typename T> class MPTLReplace { private: Iterator first1, last1; const T& oldValue; const T& newValue; public: typedef Iterator iterator1; typedef TwoIterators nIterators; MPTLReplace(Iterator first1_, Iterator last1_, const T& oldValue_, const T& newValue_) : first1(first1_), last1(last1_), oldValue(oldValue_), newValue(newValue_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } void applyAlgorithm(Iterator first, Iterator last) { std::replace(first, last, oldValue, newValue); } }; template <typename Iterator, typename T> MPTLReplace<Iterator, T> replace(Iterator first, Iterator last, const T& oldValue, const T& newValue) { return MPTLReplace<Iterator, T>(first, last, oldValue, newValue); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -