Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Solution 2 (maintaining a separate index for each serotype can be done with a little metaprogramming:</p> <pre><code>#include &lt;boost/mpl/push_back.hpp&gt; #include &lt;boost/mpl/range_c.hpp&gt; #include &lt;boost/mpl/fold.hpp&gt; #include &lt;boost/mpl/for_each.hpp&gt; #include &lt;boost/mpl/vector.hpp&gt; #include &lt;boost/multi_index_container.hpp&gt; #include &lt;boost/multi_index/ordered_index.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; using namespace boost::multi_index; struct host { static const int NUM_SEROTYPES=8; bool infections[NUM_SEROTYPES]; }; typedef boost::shared_ptr&lt;host&gt; host_ptr; template&lt;typename Num&gt; struct infection_extractor { typedef bool result_type; result_type&amp; operator()(const host_ptr&amp; p)const { return p-&gt;infections[Num::value]; } }; typedef boost::mpl::fold&lt; boost::mpl::range_c&lt;int,0,host::NUM_SEROTYPES&gt;, boost::mpl::vector&lt;&gt;, boost::mpl::push_back&lt; boost::mpl::_1, ordered_non_unique&lt;infection_extractor&lt;boost::mpl::_2&gt; &gt; &gt; &gt;::type index_list_type; typedef multi_index_container&lt; host_ptr, index_list_type &gt; multi_t; template&lt;int Num&gt; std::size_t infected_with(const multi_t&amp; m) { return m.get&lt;Num&gt;().count(true); }; typedef std::size_t (*infected_with_type)(const multi_t&amp;); struct infected_with_table_assigner { infected_with_table_assigner(infected_with_type* table):table(table) { boost::mpl::for_each&lt; boost::mpl::range_c&lt;int,0,host::NUM_SEROTYPES&gt; &gt;(*this); } template&lt;typename Num&gt; void operator()(Num)const{table[Num::value]=&amp;infected_with&lt;Num::value&gt;;} infected_with_type* table; }; std::size_t infected_with(const multi_t&amp; m,int n) { static infected_with_type table[host::NUM_SEROTYPES]; static infected_with_table_assigner assigner(table); return table[n](m); } #include &lt;iostream&gt; int main() { multi_t m; host h; for(int i=0;i&lt;200;++i){ for(int j=0;j&lt;host::NUM_SEROTYPES;++j)h.infections[j]=(i&amp;(1&lt;&lt;j))?true:false; m.insert(host_ptr(new host(h))); } for(int n=0;n&lt;host::NUM_SEROTYPES;++n) { std::cout&lt;&lt;"infected with serotype "&lt;&lt;n&lt;&lt;": " &lt;&lt;infected_with(m,n)&lt;&lt;std::endl; } } </code></pre> <p>But take into account that maintaining 8~10 indices has a memory and insertion time penalty. A simpler solution is to just maintain one <a href="http://www.boost.org/libs/multi_index/doc/tutorial/indices.html#rnd_indices" rel="nofollow noreferrer">random access index</a> and sort this appropriately (with a custom comparer suited to the particular serotype of interest in each moment) before querying.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload