Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Do you have a <em>really</em> good reason to use a list here at all? At first glance, it looks like a <code>std::vector</code> will be a better choice. You probably also don't want a container of pointers, just a container of objects.</p> <p>You can also do the job quite a bit more neatly a standard algorithm:</p> <pre><code>typedef std::vector&lt;pfx_t&gt; pfx_list_t; int eval_one_pkt(pfx_list_t const &amp;cfg, unsigned int ip_addr) { auto pos = std::find_if(cfg.begin(), cfg.end(), [ip_addr](pfx_t const &amp;p) { return ip_addr &gt;= p.begin &amp;&amp; ip_addr &lt;= p.end; }); if (pos != cfg.end()) { ++(pos-&gt;count); return 1; } return 0; } </code></pre> <p>If I were doing it, however, I'd probably turn that into generic algorithm instead:</p> <pre><code>template &lt;class InIter&gt; int eval_one_pkt(InIter b, InIter e, unsigned int ip_addr) { auto pos = std::find_if(b, e, [ip_addr](pfx_t const &amp;p) { return ip_addr &gt;= p.begin &amp;&amp; ip_addr &lt;= p.end; }); if (pos != cfg.end()) { ++(pos-&gt;count); return 1; } return 0; } </code></pre> <p>Though unrelated to C vs. C++, for a possible slight further optimization on the range check you might want to try something like this:</p> <pre><code>return ((unsigned)(ip_addr-p.begin) &lt;= (p.end-p.begin)); </code></pre> <p>With a modern compiler with optimization enabled, I'd expect the template to be expanded inline entirely at the point of use, so there probably wouldn't be any function calls involved at all.</p>
    singulars
    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.
    3. 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