         nvbio::make_transform_iterator( mems, mem_read_id_functor() ),
         n_mems ) - nvbio::make_transform_iterator( mems, mem_read_id_functor() ) );
 
+    typedef nvbio::vector_wrapper<chain*> chain_vector_type;
+    const uint32 MAX_CHAINS = 2048;     // need to handle overflow in multiple passes...
+
+    // keep a priority queue of the chains organized by the reference coordinate of their leftmost seed
+    chain chain_queue_storage[MAX_CHAINS];
+    nvbio::priority_queue<chain, chain_vector_type, chain_compare> chain_queue( chain_vector_type( 0u, chain_queue_storage ) );
+
     // process the seeds in order
-    //nvbio::cuda::ldg_pointer<mem_state::mem_type> mems_ldg( mems );
     for (uint32 i = mem_begin; i < mem_end; ++i)
     {
         const mem_state::mem_type seed = mems[ mems_index[i] ];
 
         // insert seed
+        if (chain_queue.empty())
+        {
+            chain_queue.push( chain(seed) );
+        }
+        else
+        {
+            chain& chn = chain_queue.top();
+
+            if (chn.merge( seed ) == false)
+                chain_queue.push( chain(seed) );
+        }
     }
 }
 

        loc.begin() + n_mems,
        mem->mems_index.begin() );
}

// build chains for the current pipeline::chunk of reads
__global__


#include <nvbio/basic/numbers.h>
#include <nvbio/basic/algorithms.h>
#include <nvbio/basic/priority_queue.h>
#include <nvbio/basic/transform_iterator.h>
#include <nvbio/basic/vector_wrapper.h>
#include <nvbio/basic/cuda/ldg.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/transform_iterator.h>
