void Combined_neighborhood::find_neighbors( const Geovalue& center ) {
  neighbors_.clear();
  center_ = center;
  neigh_filter_->clear();

  /* this version was not good
  first_->find_neighbors( center );
  if( first_->size() < max_size_ ) {
    second_->max_size( max_size_ - first_->size() );
    second_->find_neighbors( center );
  }
  
  neighbors_.resize( first_->size() + second_->size() );
  iterator end = std::copy( first_->begin(), first_->end(), neighbors_.begin() );
  std::copy( second_->begin(), second_->end(), end );
  */

  // ask both neighborhoods to search neighbors
  first_->find_neighbors( center );
  second_->find_neighbors( center );
  std::sort(second_->begin(), second_->end(),Geovalue_covariance_comparator(center.location(), *cov_));
  

  std::vector<Geovalue> neighbors_temp;
  neighbors_temp.resize( first_->size() + second_->size() );
  neighbors_.reserve( first_->size() + second_->size() );
  // select those that are closest to "center" from both neighborhoods
  if( cov_ )
    std::merge( first_->begin(), first_->end(), 
                second_->begin(), second_->end(), 
                neighbors_temp.begin(), 
                Geovalue_covariance_comparator(center.location(), *cov_)  );
  else
    std::merge( first_->begin(), first_->end(), 
                second_->begin(), second_->end(), 
                neighbors_temp.begin(), Geovalue_comparator(center.location() )  );

  Neighborhood::iterator it_unique = 
    std::unique(neighbors_temp.begin(), neighbors_temp.end(),Geovalue_location_comparator() );
 // neighbors_temp.erase( it_unique, neighbors_temp.end() );

  Neighborhood::iterator it_neigh = neighbors_temp.begin();
  for( ; it_neigh != it_unique; ++it_neigh ) {
    if(neigh_filter_->is_admissible(*it_neigh, center)) {
      neighbors_.push_back( *it_neigh );
    }
  }


  if( neighbors_.size() > max_size_ )
    neighbors_.erase( neighbors_.begin() + max_size_, neighbors_.end() );
}
void Combined_neighborhood::find_neighbors( const Geovalue& center ) {
  neighbors_.clear();
  center_ = center;

  /* this version was not good
  first_->find_neighbors( center );
  if( first_->size() < max_size_ ) {
    second_->max_size( max_size_ - first_->size() );
    second_->find_neighbors( center );
  }
  
  neighbors_.resize( first_->size() + second_->size() );
  iterator end = std::copy( first_->begin(), first_->end(), neighbors_.begin() );
  std::copy( second_->begin(), second_->end(), end );
  */

  // ask both neighborhoods to search neighbors
  first_->find_neighbors( center );
  second_->find_neighbors( center );

  neighbors_.resize( first_->size() + second_->size() );
  // select those that are closest to "center" from both neighborhoods
  if( cov_ )
    std::merge( first_->begin(), first_->end(), 
                second_->begin(), second_->end(), 
                neighbors_.begin(), 
                Geovalue_covariance_comparator(center.location(), *cov_)  );
  else
    std::merge( first_->begin(), first_->end(), 
                second_->begin(), second_->end(), 
                neighbors_.begin(), Geovalue_comparator(center.location() )  );

  if( neighbors_.size() > max_size_ )
    neighbors_.erase( neighbors_.begin() + max_size_, neighbors_.end() );
}
void Combined_neighborhood_dedup::find_neighbors( const Geovalue& center ){
  neighbors_.clear();
  center_ = center;

  // ask both neighborhoods to search neighbors
  first_->find_neighbors( center );
  second_->find_neighbors( center );
 
  // remove the colocated neighbors
  iterator end_1st = first_->end();
  iterator end_2nd = second_->end();
  if(!first_->is_empty() && !second_->is_empty()) {
    if(override_first_ ) {
      for(iterator it= second_->begin(); it!=second_->end(); ++it ){
        iterator new_last = std::remove_if(first_->begin(),end_1st,
                                  Location_comparator(it->location()) );
        end_1st = new_last;
      }
      
    } else {
      for(iterator it= first_->begin(); it!=first_->end(); ++it ){
        iterator new_last = std::remove_if(second_->begin(),end_2nd,
                                  Location_comparator(it->location()) );
        end_2nd = new_last;
      }
    }
  }

  //neighbors_.resize( first_->size() + second_->size() );
  neighbors_.resize( std::distance(first_->begin(),end_1st) 
                      + std::distance(second_->begin(),end_2nd) );
  // select those that are closest to "center" from both neighborhoods
  if( cov_ )
    std::merge( first_->begin(), end_1st, 
                second_->begin(), end_2nd, 
                neighbors_.begin(), 
                Geovalue_covariance_comparator(center.location(), *cov_)  );
  else
    std::merge( first_->begin(), end_1st, 
                second_->begin(), end_2nd, 
                neighbors_.begin(), Geovalue_comparator(center.location() )  );


  if( neighbors_.size() > max_size_ )
    neighbors_.erase( neighbors_.begin() + max_size_, neighbors_.end() );
}