⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 non_mutating_algo.h

📁 mptl和omptl一样
💻 H
📖 第 1 页 / 共 2 页
字号:
// 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   non_mutating_algo.h/// \brief  Impl閙entation multithread des algorithmes standard de la STL./// \author Didier Baertschiger/// \date   07-11-2005////// #ifndef _NON_MUTATING_ALGO#define _NON_MUTATING_ALGO 1#include <algorithm>#include <iterator>#include <utility>namespace mptl{    /// \brief  Version multithread de l'algorithm for_each.    ///    /// Cette impl閙entation de l'algorithm for_each est g閚閞ique et ne    /// permet pas d'utiliser la valeur de retour.\n    /// Pour utiliser la valeur de retour, il faut 閏rire sa propre version    /// de cette classe en sp閏ifiant une politique de r閐uction.    template <typename Iterator, typename UnaryFunction>    class MPTLForEach    {    private:        Iterator first1, last1;        UnaryFunction f;        public:        typedef Iterator iterator1;        typedef TwoIterators nIterators;            MPTLForEach(Iterator first1_, Iterator last1_, UnaryFunction f_)            : first1(first1_), last1(last1_), f(f_) { }            Iterator getFirst1() { return first1; }        Iterator getLast1() { return last1; }            void applyAlgorithm(Iterator first, Iterator last)        {            std::for_each(first, last, f);        }    };    template <typename Iterator, typename UnaryFunction>    MPTLForEach<Iterator, UnaryFunction> for_each(Iterator first, Iterator last,                                                  UnaryFunction f)    {        return MPTLForEach<Iterator, UnaryFunction>(first, last, f);    }            /// \brief  Version multithread de l'algorithm \em find().    template <typename Iterator, typename EqualityComparable>    class MPTLFind    {    private:        Iterator first1, last1;        const EqualityComparable& value;            public:        typedef Iterator iterator1;        typedef Iterator resultType;        typedef FindPolicy reduction;        typedef TwoIterators nIterators;                MPTLFind(Iterator first1_, Iterator last1_,                 const EqualityComparable& value_)            : first1(first1_), last1(last1_), value(value_) { }                Iterator getFirst1() { return first1; }        Iterator getLast1() { return last1; }                resultType applyAlgorithm(Iterator first, Iterator last)        {            Iterator it = std::find(first, last, value);            if (it == last) return last1;            return it;        }    };        template <typename Iterator, typename EqualityComparable>    MPTLFind<Iterator, EqualityComparable> find(Iterator first,                                                Iterator last,                                                const EqualityComparable& value)    {        return MPTLFind<Iterator, EqualityComparable>(first, last, value);    }            template <typename Iterator, typename Predicate>    class MPTLFindIf    {    private:        Iterator first1, last1;        Predicate pred;            public:        typedef Iterator iterator1;        typedef Iterator resultType;        typedef FindPolicy reduction;        typedef TwoIterators nIterators;                MPTLFindIf(Iterator first1_, Iterator last1_,                   Predicate pred_)            : first1(first1_), last1(last1_), pred(pred_) { }                Iterator getFirst1() { return first1; }        Iterator getLast1() { return last1; }                resultType applyAlgorithm(Iterator first, Iterator last)        {            Iterator it = std::find_if(first, last, pred);            if (it == last) return last1;            return it;        }    };        template <typename Iterator, typename Predicate>    MPTLFindIf<Iterator, Predicate> find_if(Iterator first,                                            Iterator last,                                            Predicate pred)    {        return MPTLFindIf<Iterator, Predicate>(first, last, pred);    }            template <typename Iterator>    class MPTLAdjacentFind    {    private:        Iterator first1, last1;            public:        typedef Iterator iterator1;        typedef Iterator resultType;        typedef FindPolicy reduction;        typedef TwoIterators nIterators;                MPTLAdjacentFind(Iterator first1_, Iterator last1_)            : first1(first1_), last1(last1_) { }                Iterator getFirst1() { return first1; }        Iterator getLast1() { return last1; }                resultType applyAlgorithm(Iterator first, Iterator last)        {            if (last != last1) ++last;            Iterator it = std::adjacent_find(first, last);            if (it == last) return last1;            return it;        }    };        template <typename Iterator>    MPTLAdjacentFind<Iterator> adjacent_find(Iterator first, Iterator last)    {        return MPTLAdjacentFind<Iterator>(first, last);    }            template <typename Iterator, typename BinaryPredicate>    class MPTLAdjacentFindPred    {    private:        Iterator first1, last1;        BinaryPredicate binaryPred;            public:        typedef Iterator iterator1;        typedef Iterator resultType;        typedef FindPolicy reduction;        typedef TwoIterators nIterators;                MPTLAdjacentFindPred(Iterator first1_, Iterator last1_,                             BinaryPredicate binaryPred_)            : first1(first1_), last1(last1_), binaryPred(binaryPred_) { }                Iterator getFirst1() { return first1; }        Iterator getLast1() { return last1; }                resultType applyAlgorithm(Iterator first, Iterator last)        {            if (last != last1) ++last;            Iterator it = std::adjacent_find(first, last, binaryPred);            if (it == last) return last1;            return it;        }    };        template <typename Iterator, typename BinaryPredicate>    MPTLAdjacentFindPred<Iterator, BinaryPredicate> adjacent_find(Iterator first,                                                                  Iterator last,                                                                  BinaryPredicate binary_pred)    {        return MPTLAdjacentFindPred<Iterator, BinaryPredicate>(first, last, binary_pred);    }            template <typename Iterator1, typename Iterator2>    class MPTLFindFirstOf    {    private:        Iterator1 first1, last1;        Iterator2 first2, last2;            public:        typedef Iterator1 iterator1;        typedef Iterator1 resultType;        typedef FindPolicy reduction;        typedef TwoIterators nIterators;                MPTLFindFirstOf(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)        {            Iterator1 it = std::find_first_of(first, last,                                              first2, last2);            if (it == last) return last1;            return it;        }    };        template <typename Iterator1, typename Iterator2>    MPTLFindFirstOf<Iterator1, Iterator2> find_first_of(Iterator1 first1,                                                        Iterator1 last1,                                                        Iterator2 first2,                                                        Iterator2 last2)    {        return MPTLFindFirstOf<Iterator1, Iterator2>(first1, last1,                                                     first2, last2);    }            template <typename Iterator1, typename Iterator2,    typename BinaryPredicate>    class MPTLFindFirstOfPred    {    private:        Iterator1 first1, last1;        Iterator2 first2, last2;        BinaryPredicate comp;            public:        typedef Iterator1 iterator1;        typedef Iterator1 resultType;        typedef FindPolicy reduction;        typedef TwoIterators nIterators;                MPTLFindFirstOfPred(Iterator1 first1_, Iterator1 last1_,                            Iterator2 first2_, Iterator2 last2_,                            BinaryPredicate comp_)            : first1(first1_), last1(last1_), first2(first2_), last2(last2_),              comp(comp_) { }                Iterator1 getFirst1() { return first1; }        Iterator1 getLast1() { return last1; }                resultType applyAlgorithm(Iterator1 first, Iterator1 last)        {            Iterator1 it = std::find_first_of(first, last,                                              first2, last2,                                              comp);            if (it == last) return last1;            return it;        }    };        template <typename Iterator1, typename Iterator2,              typename BinaryPredicate>    MPTLFindFirstOfPred<Iterator1, Iterator2,                         BinaryPredicate> find_first_of(Iterator1 first1,                                                       Iterator1 last1,                                                       Iterator2 first2,                                                       Iterator2 last2,                                                       BinaryPredicate comp)    {        return MPTLFindFirstOfPred<Iterator1, Iterator2,                                    BinaryPredicate>(first1, last1,                                                    first2, last2,                                                    comp);    }            template <typename Iterator, typename EqualityComparable>    class MPTLCount    {    private:        Iterator first1, last1;        const EqualityComparable& value;            public:        typedef Iterator iterator1;        typedef typename std::iterator_traits<Iterator>::difference_type resultType;        typedef AccumulationPolicy<std::plus<resultType> > reduction;        typedef TwoIterators nIterators;                MPTLCount(Iterator first1_, Iterator last1_,                   const EqualityComparable& value_)            : first1(first1_), last1(last1_), value(value_) { }                Iterator getFirst1() { return first1; }        Iterator getLast1() { return last1; }                resultType applyAlgorithm(Iterator first, Iterator last)        {            return std::count(first, last, value);        }    };        template <typename Iterator, typename EqualityComparable>    MPTLCount<Iterator, EqualityComparable> count(Iterator first, Iterator last,                                                  const EqualityComparable& value)    {        return MPTLCount<Iterator, EqualityComparable>(first, last, value);    }            template <typename Iterator, typename Predicate>    class MPTLCountIf    {    private:        Iterator first1, last1;        Predicate pred;            public:        typedef Iterator iterator1;        typedef typename std::iterator_traits<Iterator>::difference_type resultType;        typedef AccumulationPolicy<std::plus<resultType> > reduction;        typedef TwoIterators nIterators;                MPTLCountIf(Iterator first1_, Iterator last1_, Predicate pred_)            : first1(first1_), last1(last1_), pred(pred_) { }                Iterator getFirst1() { return first1; }        Iterator getLast1() { return last1; }                resultType applyAlgorithm(Iterator first, Iterator last)        {            return std::count_if(first, last, pred);        }    };

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -