Итерация в последовательную позицию в boost :: multi_index по индексу size_t? - PullRequest
0 голосов
/ 05 мая 2011

Я только начал использовать boost multi_index, и до сих пор у меня есть контейнер, состоящий из последовательного типа и составных ключей, неупорядоченных хэшей.

То, что я хочу сделать, это получить доступ к секвенированному контейнеру, как если бы это был std :: vector или std :: list. Под этим я подразумеваю использование size_t index.

Вот мой код ниже:

// tags
struct linear { };
struct semantic { };
struct semantic_and_index { };

// typedefs for multi_index
typedef boost::multi_index::sequenced< 
    boost::multi_index::tag<linear>
> LinearIndex;

typedef boost::multi_index::composite_key<
    CVertexElement,
    boost::multi_index::const_mem_fun<CVertexElement, Buffer::VertexSemantic, &CVertexElement::GetVertexSemantic>,
    boost::multi_index::const_mem_fun<CVertexElement, UInt, &CVertexElement::GetSemanticIndex> 
> CompositeSemanticIndex;

typedef boost::multi_index::hashed_non_unique<
    boost::multi_index::tag<semantic_and_index>,
    CompositeSemanticIndex
> SemanticAndIDIndex;

typedef boost::multi_index::hashed_non_unique<
    boost::multi_index::tag<semantic>,
    boost::multi_index::const_mem_fun<CVertexElement, Buffer::VertexSemantic, &CVertexElement::GetVertexSemantic> 
> SemanticIndex;

class CVertexFormat
{
public:
    typedef boost::multi_index::multi_index_container <
        CVertexElementPtr,
        boost::multi_index::indexed_by <
            LinearIndex,
            SemanticAndIDIndex,
            SemanticIndex
        > // index_by
    > ElementContainer; 

      // etc...

protected:
    ElementContainer m_elements;
};

Я имею в виду функцию:

// this function fails! :(
CVertexElementPtr CVertexFormat::GetElement(size_t index) const 
{
    //sequenced_index.
    typedef ElementContainer::index<linear>::type element_by_linear;
    const element_by_linear& index_type = m_elements.get<linear>();

    if ( index < index_type.size() )
    {
        auto itor = index_type.begin();
        for (UInt i = 0; i < index_type.size(); ++i)
            ++itor;

        const CVertexElementPtr& pElement = (*itor);        
        return pElement;
    }
    else
    {
        assert(!"Invalid index called for GetElement");
        return CVertexElementPtr();
    }
}

Изображение ошибки в vs2010 (посмотрите на окно часов справа):

Снимок экрана в высоком разрешении

1 Ответ

0 голосов
/ 06 мая 2011

Проблема была решена путем изменения функции CVertexElementPtr CVertexFormat::GetElement(size_t index) const.

Правильная функция:

CVertexElementPtr CVertexFormat::GetElement(size_t index) const
{
    //sequenced_index.
    typedef ElementContainer::index<linear>::type element_by_linear;
    const element_by_linear& index_type = m_elements.get<linear>();

    size_t size = index_type.size();
    if ( index < size )
    {
        auto itor = index_type.begin();
        for (UInt i = 0; i < index; ++i)
            ++itor;

        const CVertexElementPtr& pElement = (*itor);        
        return pElement;
    }
    else
    {
        assert( false && ieS("Invalid index called for GetElement") );
        return CVertexElementPtr();
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...