Amit Prakash Ambasta
Consider the following code snippet:
[...]
struct LinkList;
struct LinkList { size_t index; LinkList* parent; LinkList(size_t index, LinkList* parent) : index(index), parent(parent) {} };
struct llindex {};
typedef ordered_unique< tag<llindex>, member
LinkListIndex; typedef boost::multi_index_container< LinkList, indexed_by<LinkListIndex> IndexedLinkList;
[...]
int main() { IndexedLinkList ill; auto &index_ll = ill.get<0>();
ill.insert(LinkList{0, nullptr}); ill.insert(LinkList{1, (LinkList*)(&(*index_ll.find(0)))}); ill.insert(LinkList{2, (LinkList*)(&(*index_ll.find(0)))}); ill.insert(LinkList{3, (LinkList*)(&(*index_ll.find(2)))});
show(ill); return 0; }
Would (LinkList*)(&(*index__ll.find(value))) be the recommended way to fetch the pointer to parent?
Depends on the context. If you're inserting the elements in sequential
ordering (i.e. the newly inserted element is linked to the last one),
then this is faster as you don't do any lookup:
auto it=ill.insert(LinkList{0, nullptr}).first;
it=ill.insert(LinkList{1, (LinkList*)(&*it)}).first;
it=ill.insert(LinkList{2, (LinkList*)(&*it)}).first;
it=ill.insert(LinkList{3, (LinkList*)(&*it)}).first;
Maybe you can provide some more info on the purpose of the structure
you're buliding here? Boost.MultiIndex might help further. For
instance, this is a list-like structure quicky searchable by
index:
typedef boost::multi_index_container<
node,
indexed_by<
sequenced<>,
ordered_unique