Boost C++ Libraries: Ticket #5641: Multiple keys in same index for same value in multi_index https://svn.boost.org/trac10/ticket/5641 <p> I have a collection of data objects analogous to this one: </p> <div class="wiki-code"><div class="code"><pre><span class="k">struct</span> <span class="n">foo</span> <span class="p">{</span> <span class="n">bar</span> <span class="n">b</span><span class="p">;</span> <span class="n">std</span><span class="o">::</span><span class="n">set</span><span class="o">&lt;</span><span class="n">baz</span><span class="o">&gt;</span> <span class="n">s</span><span class="p">;</span> <span class="n">quux</span> <span class="n">q</span><span class="p">;</span> <span class="p">};</span> </pre></div></div><p> which I want to be able to efficiently search either by b, or any element of s (q is extra information). The natural way to do that would be some sort of multi_index-like container, where I have a hash-map of bar to foo (with one key per value), and another hash-map of baz to foo (with multiple keys mapping to the same foo). </p> <p> I may be mistaken, but I don't believe the second map can be set up (efficiently, in its obvious implementation) in the current multi_index. However, if it is allowable to have different sizes for different views of the same collection / different numbers of keys of the same index mapping to the same value, it would perhaps be a useful capability. </p> <p> Without looking at the Boost sources, this would probably involve generalizing key extractors to support multiple keys from the same value, with syntax looking something like the following: </p> <div class="wiki-code"><div class="code"><pre><span class="k">typedef</span> <span class="n">multi_index_container</span><span class="o">&lt;</span> <span class="n">foo</span><span class="p">,</span> <span class="n">indexed_by</span><span class="o">&lt;</span> <span class="n">hashed_unique</span><span class="o">&lt;</span> <span class="n">member</span><span class="o">&lt;</span><span class="n">foo</span><span class="p">,</span> <span class="n">bar</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">foo</span><span class="o">::</span><span class="n">b</span><span class="o">&gt;</span> <span class="o">&gt;</span><span class="p">,</span> <span class="n">hashed_unique</span><span class="o">&lt;</span> <span class="n">member_collection</span><span class="o">&lt;</span><span class="n">foo</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">set</span><span class="o">&lt;</span><span class="n">baz</span><span class="o">&gt;::</span><span class="n">iterator</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">foo</span><span class="o">::</span><span class="n">s</span><span class="p">.</span><span class="n">begin</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">foo</span><span class="o">::</span><span class="n">s</span><span class="p">.</span><span class="n">end</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="o">&gt;</span> <span class="n">my_map</span><span class="p">;</span> </pre></div></div> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5641 Trac 1.4.3 Joaquín M López Muñoz Tue, 05 Jul 2011 14:08:21 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/5641#comment:1 https://svn.boost.org/trac10/ticket/5641#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> Hi Aaron, </p> <p> The idea you propose is in direct contradiction with many basic assumptions about the kind of data structures Boost.<a class="missing wiki">MultiIndex</a> is designed to generate: for one, all indices have the same size(), meaning that a particular element appears once and exactly once in each index. Breaking these basic assumption subvert the whole internal infrastucture of the library, and it is not easy to see how this could be done in a conceptually sound manner. In other words, I don't plan to augment Boost.<a class="missing wiki">MultiIndex</a> the way you propose. </p> <p> You can approximate your scenario with some manual work: instead of storing plain foos use some kind of proxy like </p> <pre class="wiki">struct foo_proxy { boost::shared_ptr&lt;foo&gt; p; boost::function&lt;const baz&amp;(const foo&amp;)&gt; key; }; </pre><p> where p points to the actual element and key is a key extractor settable at run time. Then, for each element x insert as many proxys as elements has its s member, setting for each the appropriate key extractor returning the i-th element of x.s. A collateral effect is that indices other than the used for s will contain duplicates as well. </p> Ticket