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"
 
// VecType はスレッドごとに別の std::vector<int> を含む
typedef tbb::enumerable_thread_specific< std::vector<int> > VecType;
VecType MyVectors; 
int K = 1000000;
 
struct Func {
    void operator()(const tbb::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() {
    tbb::parallel_for(tbb::blocked_range<int>(0, 100000000), Func());
 
    tbb::flattened2d<VecType> flat_view = tbb::flatten2d( MyVectors );
    for( tbb::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);
}