1

我想用两个sequenced视图构建一个多索引,我只能从一个视图中删除值。

在代码中:

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>

#include <algorithm>
#include <iterator>
#include <vector>

using namespace boost::multi_index;

typedef multi_index_container<
  int,
  indexed_by<
    sequenced<>,
    sequenced<> 
    > 
  > container;

int main()
{
  container c;
  std::vector<int> 
    // complete
    data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 
    // only uneven
    uneven_data;
  std::copy_if(begin(data), end(data), 
               std::back_inserter(uneven_data), 
               [](int i) { return i % 2 != 0; });

  container cont;
  std::copy(begin(data), end(data), std::back_inserter(cont));

  auto& idx0 = cont.get<0>();
  auto& idx1 = cont.get<1>();
  // remove all uneven from idx1
  idx1.remove_if([](int i) { return i % 2 == 0; });

  // behavior I would like to be true, but now both idx0 and idx1 are
  // equal to uneven_data
  assert(std::equal(begin(data), end(data), begin(idx0)));
  assert(std::equal(begin(uneven_data), end(uneven_data), begin(idx1)));

  return 0;
}

这样的事情可能boost::multi_index吗?

4

2 回答 2

2

不,在 boost multi_index_container 中,索引作为相同值的单独视图耦合在一起。这是 boost multi_index_containers 的重要保证。

“Boost.MultiIndex 允许指定 multi_index_containers 由一个或多个索引组成,这些索引具有与同一元素集合的不同接口”(boost multi_index 教程)。

使用两个不同的序列 <> 索引,您可以以两种不同的方式重新排列顺序,但它们都必须包含所有值。

如果您希望根据布尔谓词查看元素的子集(例如:值是奇数),请考虑使用boost::filter_iterator。这不会尝试擦除容器中的值,而是在迭代期间跳过它们。

或者,可以通过排序获得选择/过滤,使用带有适当键提取器的 multi_index ordered_non_unique索引。

例如,基于键提取器的 ordered_non_unique 索引:[](int i) { return i % 2; }会将奇数和偶数值放在不同的组中,因此可以使用equal_range获得所有奇数值的迭代器范围。

于 2013-04-20T02:08:28.537 回答
0

正如我上面提到的,一种使用boost intrusive的更灵活(非基于谓词)的方法。使用两个或多个列表挂钩,您可以使一个对象同时参与两个或多个序列。

#include <boost/intrusive/list.hpp>
#include <boost/foreach.hpp>

#include <algorithm>
#include <memory>

#include <iostream>
namespace intr = boost::intrusive;
template<class Value>
struct Sequence2
{
    typedef intr::link_mode< intr::auto_unlink > AutoUnlinkMode;
    typedef intr::list_member_hook< AutoUnlinkMode > SeqHook;

    struct Node
    {
        Node( const Value& i ) : value(i) {}

        operator Value&()
        {
            return value;
        }

        Value value;

        SeqHook mainHook;
        SeqHook hook0;
        SeqHook hook1;
    };

    typedef intr::member_hook< Node, SeqHook,&Node::mainHook > UsingMainHook;
    typedef intr::member_hook< Node, SeqHook,&Node::hook0 > UsingSeqHook0;
    typedef intr::member_hook< Node, SeqHook,&Node::hook1 > UsingSeqHook1;

    typedef intr::constant_time_size<false> NonConstantTimeSized;

    typedef intr::list< Node, UsingMainHook, NonConstantTimeSized > NodesList;
    typedef intr::list< Node, UsingSeqHook0, NonConstantTimeSized > Sequence0;
    typedef intr::list< Node, UsingSeqHook1, NonConstantTimeSized > Sequence1;

    NodesList nodes;
    Sequence0 seq0;
    Sequence1 seq1;

    typename NodesList::iterator insert( const Value& item )
    {
        Node* node = new Node(item);

        nodes.push_back( *node );

        typename NodesList::iterator iter = nodes.end(); iter--;
        seq0.push_back( *node );
        seq1.push_back( *node );
        return iter;
    }

    typename Sequence0::iterator iterator0( typename NodesList::iterator iter )
    {
        return seq0.iterator_to( *iter );
    }
    typename Sequence1::iterator iterator1( typename NodesList::iterator iter )
    {
        return seq1.iterator_to( *iter );
    }

    //! Erase from both sequences
    void erase( typename NodesList::iterator at )
    {
        nodes.erase_and_dispose( at, std::default_delete<Node>() );   
    }
    //! Erase from sequence 0
    void erase( typename Sequence0::iterator at )
    {
        Node& n = *at;

        assert( n.hook0.is_linked() );
        if( ! n.hook1.is_linked() )
        {
            seq0.erase_and_dispose( at, std::default_delete<Node>() );   
        }
        else
        {
            seq0.erase(at);
        }
    }
    //! Erase from sequence 1
    void erase( typename Sequence1::iterator at )
    {
        Node& n = *at;

        assert( n.hook1.is_linked() );
        if( ! n.hook0.is_linked() )
        {
            seq1.erase_and_dispose( at, std::default_delete<Node>() );   
        }
        else
        {
            seq1.erase(at);
        }
    }

    ~Sequence2()
    {
        nodes.clear_and_dispose( std::default_delete<Node>() );
    }
};

template< class T >
void show( Sequence2<T>& mseq, const std::string& comment )
{
    std::cout << comment << "\nseq 0:\t";
    BOOST_FOREACH( T& i, mseq.seq0 )
    {
        std::cout << i << " ";
    }
    std::cout << "\nseq 1:\t";
    BOOST_FOREACH( T& i, mseq.seq1 )
    {
        std::cout << i << " ";
    }
    std::cout << "\n\n";
}

int main(void)
{
    Sequence2< std::string > mseq;

    mseq.insert( "." );
    auto iterX = mseq.insert("X");
    auto iterY = mseq.insert("Y");
    auto iterZ = mseq.insert("Z");
    mseq.insert(".");
    mseq.insert(".");

    show(mseq, "start" );

    mseq.seq0.reverse();
    show(mseq, "after reverse seq0");    

    mseq.erase( mseq.iterator0(iterY) );
    show(mseq, "after erase Y in seq0");

    // Update a value in both sequences
    std::string& v = *iterZ;
    v = "z";
    show(mseq, "after modify Z in both");


    mseq.erase( iterX );
    show(mseq, "after erase X in both");

    mseq.erase( iterY );
    show(mseq, "after erase Y in both");

    return 0;
}

产生:

start
seq 0:  . X Y Z . . 
seq 1:  . X Y Z . . 

after reverse seq0
seq 0:  . . Z Y X . 
seq 1:  . X Y Z . . 

after erase Y in seq0
seq 0:  . . Z X . 
seq 1:  . X Y Z . . 

after modify Z in both
seq 0:  . . z X . 
seq 1:  . X Y z . . 

after erase X in both
seq 0:  . . z . 
seq 1:  . Y z . . 

after erase Y in both
seq 0:  . . z . 
seq 1:  . z . . 
于 2013-04-20T14:56:52.980 回答