9

Boost::range を使用して、NumPy と Matlab で利用可能な「派手なインデックス作成」に似たものを実現したいと考えています。具体的には、別のコンテナーの要素をインデックスとして使用して、あるインデックス可能なコンテナーの特定の要素を選択したいと思います。たとえば、Python では次のことができます。

>>> squares = numpy.arange(10)**2  # Step 1 - setup squares
>>> indices = numpy.array([1,3,4]) # Step 2 - setup indices
>>> squares[indices]               # Step 3 - fancy indexing
array([ 1, 9, 16])

C++ で boost::range を使用すると、上記のコードは次のようになると思います。

#include "sampled.hpp"
#include <boost/assign/std/vector.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/range/adaptors.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/counting_range.hpp>
#include <iostream>
#include <vector>

using namespace boost;
using namespace boost::adaptors;
using namespace boost::assign;
using namespace boost::lambda;
using namespace std;

int main(int argc, char** argv)
{
    // Step 1 - setup squares
    vector<int> squares;
    push_back(
        squares,
        counting_range(1,11) | transformed(ret<int>(_1 * _1))
    );

    // Step 2 - setup indices
    vector<size_t> indices;
    indices += 1,3,4;

    // Step 3 - fancy indexing
    for_each(
        squares | sampled(indices),
        cout << _1 << constant(" ")
    );

    return 0;
}

「indexed」という名前は既存のレンジ アダプター (boost::adaptor::indexed) で既に使用されているため、上記のコードではまだ実装されていないインデックス アダプターを「sampled」と呼んでいます。

そのようなアダプターがブーストのどこかにすでに存在するか、または共有しても構わないと思っている実装があるかどうかを誰かが知っていますか? 最初に「sampled_iterator」(iterator_adaptor を使用)、次に「sampled_range」(iterator_range を使用) を記述して、自分で実装しようとしましたが、かなりトリッキーだと思います。

4

1 に答える 1

2

わかりましたので、boost::permutation_iterator を範囲アダプターの基礎として使用して、何とか機能させることができました。permutation_iterator が使用されているため、問題のコードの抜粋で言及されているように、範囲アダプターを「サンプリング」ではなく「置換」と呼びました。したがって、numpy コードの C++ バージョンは次のようになります。

// Step 3
for_each(
    squares | permuted(indices),
    cout << _1 << constant(" ")
);

「順列」のコードは次のとおりです。

#pragma once

#include <boost/range/adaptor/argument_fwd.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/iterator/permutation_iterator.hpp>

template <class IndexContainer, class ElementRange>
class permuted_range :
    public boost::iterator_range<
        boost::permutation_iterator<
            typename boost::range_iterator<ElementRange>::type,
            typename IndexContainer::iterator> >
{
private:
    typedef boost::iterator_range<
        boost::permutation_iterator<
        typename boost::range_iterator<ElementRange>::type,
        typename IndexContainer::iterator> > base;

public:
    permuted_range(IndexContainer& i, ElementRange& r) :
        base(
            boost::make_permutation_iterator(boost::begin(r), i.begin()),
            boost::make_permutation_iterator(boost::begin(r), i.end()))
    { }
};

template <class IndexContainer>
struct permuted_holder : boost::range_detail::holder<IndexContainer>
{
    permuted_holder(IndexContainer i) :
        boost::range_detail::holder<IndexContainer>(i)
    { }
};

template <class ElementRange, class IndexContainer>
inline permuted_range<IndexContainer, ElementRange> operator| (
    ElementRange& r,
    permuted_holder<IndexContainer> i)
{
    return permuted_range<IndexContainer, ElementRange>(i.val, r);
}

template <class ElementRange, class IndexContainer>
inline permuted_range<IndexContainer, const ElementRange> operator| (
    const ElementRange& r,
    permuted_holder<IndexContainer> i)
{
    return permuted_range<IndexContainer, const ElementRange>(i.val, r);
}

static boost::range_detail::forwarder<permuted_holder> permuted =
    boost::range_detail::forwarder<permuted_holder>();

改善できる点はいくつかあると思います。そして、permutation_iterator はこれのかなり効率的な基礎のように見えますが、おそらくより良い代替手段があります。何かご意見は?

于 2011-08-26T21:54:20.360 に答える