flattened2d テンプレート・クラス

概要

複数のコンテナーの 1 つのコンテナーのフラットなビューを提供するアダプター。

構文

    template<typename Container>
    class flattened2;
     
    template <typename Container>
    flattened2d<Container> flatten2d(const Container &c); 
     
    template <typename Container>
    flattened2d<Container> flatten2d(
        const Container &c, 
        const typename Container::const_iterator b, 
        const typename Container::const_iterator e); 

ヘッダー

#include "tbb/enumerable_thread_specific.h"

説明

flattened2d は、複数のコンテナーの 1 つのコンテナーのフラットなビューを提供します。begin() から end() の反復で、内部コンテナーの要素をすべて確認します。要素がコンテナーである enumerable_thread_specific を走査するときに便利です。

flatten2d ユーティリティー関数は、コンテナーから flattened2d オブジェクトを作成します。

次のコードは、flatten2d および flattened2d を使用する単純な例を示しています。各スレッドは、スレッド・ローカル・ベクトルで K により均等に分割可能な i の値を集めます。メインで、すべてのローカルベクトルの全要素を単純に調べることができるように、結果は flattened2d を使用して出力されます。

    
             #include <iostream>
             #include <utility>
             #include <vector>
              
             #include "tbb/task_scheduler_init.h"
             #include "tbb/enumerable_thread_specific.h"
             #include "tbb/parallel_for.h"
             #include "tbb/blocked_range.h"
              
             using namespace tbb;
              
             // VecType はスレッドごとに別の std::vector<int> を含む
             typedef enumerable_thread_specific< std::vector<int> > VecType;
             VecType MyVectors; 
             int K = 1000000;
              
             struct Func {
                 void operator()(const blocked_range<int>& r) const {
                     VecType::reference v = MyVectors.local();
                     for (int i=r.begin(); i!=r.end(); ++i) 
                         if( i%k==0 ) 
                             v.push_back(i);
                 } 
             };
              
             int main() {
                 parallel_for(blocked_range<int>(0, 100000000), 
                              Func());
              
                 flattened2d<VecType> flat_view = flatten2d( MyVectors );
                 for( flattened2d<VecType>::const_iterator 
                      i = flat_view.begin(); i != flat_view.end(); ++i) 
                     cout << *i << endl;
                 return 0;
             }
              

メンバー

namespace tbb {
            
                template<typename Container>
                class flattened2d {
             
                public:
                    // 基本型
                    typedef implementation-dependent size_type;
                    typedef implementation-dependent difference_type;
                    typedef implementation-dependent allocator_type;
                    typedef implementation-dependent value_type;
                    typedef implementation-dependent reference;
                    typedef implementation-dependent const_reference;
                    typedef implementation-dependent pointer;
                    typedef implementation-dependent const_pointer;
             
                    typedef implementation-dependent iterator;
                    typedef implementation-dependent const_iterator;
             
                    flattened2d( const Container& c );
             
                    flattened2d( const Container& c, 
                                 typename Container::const_iterator first,
                                typename Container::const_iterator last );
             
                    iterator begin();
                    iterator end();
                    const_iterator begin() const;
                    const_iterator end() const;
             
                    size_type size() const;
                };
             
                template <typename Container>
                flattened2d<Container> flatten2d(const Container &c);
             
                template <typename Container>
                flattened2d<Container> flatten2d(
                    const Container &c, 
                    const typename Container::const_iterator first, 
                    const typename Container::const_iterator last);
            }