title: Data structure to build and lookup set of integer ranges
body: <p>I have a set of <code>uint32</code> integers, there may be millions of items in the set. 50-70% of them are consecutive, but in input stream they appear in unpredictable order.</p>
  
  <p>I need to:</p>
  
  <ol>
  <li><p>Compress this set into ranges to achieve space efficient representation. Already implemented this using trivial algorithm, since ranges computed only once speed is not important here. After this transformation number of resulting ranges is typically within 5 000-10 000, many of them are single-item, of course.</p></li>
  <li><p>Test membership of some integer, information about specific range in the set is not required. This one must be very fast -- O(1). Was thinking about <a href="http://en.wikipedia.org/wiki/Perfect_hash_function" rel="noreferrer">minimal perfect hash functions</a>, but they do not play well with ranges. <a href="http://en.wikipedia.org/wiki/Bit_array" rel="noreferrer">Bitsets</a> are very space inefficient. Other structures, like binary trees, has complexity of O(log n), worst thing with them that implementation make many conditional jumps and processor can not predict them well giving poor performance.</p></li>
  </ol>
  
  <p>Is there any data structure or algorithm specialized in integer ranges to solve this task?</p>