📄 numeric_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 numeric_algo.h/// \brief Impl閙entation multithread des algorithmes standard de la STL./// \author Didier Baertschiger/// \date 08-11-2005////// #ifndef _NUMERIC_ALGO#define _NUMERIC_ALGO 1#include <algorithm>#include <iterator>#include <utility>namespace mptl{ template <typename Iterator, typename T> class MPTLAccumulate { private: Iterator first1, last1; T init; public: typedef Iterator iterator1; typedef T resultType; typedef AccumulationPolicy<std::plus<resultType> > reduction; typedef TwoIterators nIterators; MPTLAccumulate(Iterator first1_, Iterator last1_, T init_) : first1(first1_), last1(last1_), init(init_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } resultType applyAlgorithm(Iterator first, Iterator last) { return std::accumulate(first, last, init); } }; template <typename Iterator, typename T> MPTLAccumulate<Iterator, T> accumulate(Iterator first, Iterator last, T init) { return MPTLAccumulate<Iterator, T>(first, last, init); } template <typename Iterator, typename T, typename BinaryFunction> class MPTLAccumulateBinary { private: Iterator first1, last1; T init; BinaryFunction binaryOp; public: typedef Iterator iterator1; typedef T resultType; typedef AccumulationPolicy<BinaryFunction> reduction; typedef TwoIterators nIterators; MPTLAccumulateBinary(Iterator first1_, Iterator last1_, T init_, BinaryFunction binaryOp_) : first1(first1_), last1(last1_), init(init_), binaryOp(binaryOp_) { } Iterator getFirst1() { return first1; } Iterator getLast1() { return last1; } resultType applyAlgorithm(Iterator first, Iterator last) { return std::accumulate(first, last, init, binaryOp); } }; template <typename Iterator, typename T, typename BinaryFunction> MPTLAccumulateBinary<Iterator, T, BinaryFunction> accumulate(Iterator first, Iterator last, T init, BinaryFunction binaryOp) { return MPTLAccumulateBinary<Iterator, T, BinaryFunction>(first, last, init, binaryOp); } // UTILISER UNIQUEMENT SI LE CONTAINER SOURCE ET DIFFERENT QUE LE CONTAINER // DESTINATION. template <typename Iterator1, typename Iterator2> class MPTLAdjacentDifference { private: Iterator1 first1, last1; Iterator2 first2; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLAdjacentDifference(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_) { Iterator2 result = std::adjacent_difference(first1_, last1_, first2_); if (first1_ != first1) { Iterator1 tmp = first1_; --tmp; *first2_ = *first1_ - *tmp; } return result; } }; template <typename Iterator1, typename Iterator2> MPTLAdjacentDifference<Iterator1, Iterator2> adjacent_difference(Iterator1 first, Iterator1 last, Iterator2 result) { return MPTLAdjacentDifference<Iterator1, Iterator2>(first, last, result); } // UTILISER UNIQUEMENT SI LE CONTAINER SOURCE ET DIFFERENT QUE LE CONTAINER // DESTINATION. template <typename Iterator1, typename Iterator2, typename BinaryFunction> class MPTLAdjacentDifferenceBinary { private: Iterator1 first1, last1; Iterator2 first2; BinaryFunction binaryOp; public: typedef Iterator1 iterator1; typedef Iterator2 iterator2; typedef Iterator2 resultType; typedef LastElementPolicy reduction; typedef ThreeIterators nIterators; MPTLAdjacentDifferenceBinary(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_, BinaryFunction binaryOp_) : first1(first1_), last1(last1_), first2(first2_), binaryOp(binaryOp_) { } Iterator1 getFirst1() { return first1; } Iterator1 getLast1() { return last1; } Iterator2 getFirst2() { return first2; } resultType applyAlgorithm(Iterator1 first1_, Iterator1 last1_, Iterator2 first2_) { Iterator2 result = std::adjacent_difference(first1_, last1_, first2_, binaryOp); if (first1_ != first1) { Iterator1 tmp = first1_; --tmp; *first2_ = binaryOp(*first1_, *tmp); } return result; } }; template <typename Iterator1, typename Iterator2, typename BinaryFunction> MPTLAdjacentDifferenceBinary<Iterator1, Iterator2, BinaryFunction> adjacent_difference(Iterator1 first, Iterator1 last, Iterator2 result, BinaryFunction binaryOp) { return MPTLAdjacentDifferenceBinary<Iterator1, Iterator2, BinaryFunction>(first, last, result, binaryOp); } } // namespace mptl#endif /* _NUMERIC_ALGO */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -