title: Algorithm to unify contiguous chunks in a collection of chunks
body: <p>I'm creating a pre-allocator with dynamic memory chunk size, and I need to unify contiguous memory chunks.</p>
  
  <pre><code>struct Chunk // Chunk of memory
  {
      Ptr begin, end; // [begin, end) range
  }
  
  struct PreAlloc
  {
      std::vector&lt;Chunk&gt; chunks; // I need to unify contiguous chunks here
      ...
  }
  </code></pre>
  
  <p>I tried <a href="http://pastie.org/8260375" rel="noreferrer">a naive solution</a>, that, after sorting the chunks based on their <code>begin</code>,  basically did a pass through the vector checking if the next chunk's <code>begin</code> was equal to the current chunk's <code>end</code>. I'm sure it could be improved.</p>
  
  <p>Is there a good algorithm to <em>unify contiguous ranges</em>?</p>
  
  <p>Information:</p>
  
  <ul>
  <li><strong>Chunks can never "overlap".</strong></li>
  <li><strong>Chunks can have any size greater than 0.</strong></li>
  <li><strong>Performance is the most important factor.</strong></li>
  </ul>