Project: s41467-025-60902-2 | Function: shareNeighbor (cpp)

system: You are an expert C++ programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow C++ best practices and maintain consistent code style.
Return **only** the complete C++ function code.

user: Complete this C++ function:

**DESCRIPTION:** Calculates shared neighbor counts between UMI clusters for graph-based clustering of reads with similar molecular identifiers

**KEY TERMS:** {
  "index": "UMI cluster identifiers",
  "neighbor": "adjacency list of UMI graph",
  "count": "read counts per UMI",
  "share": "shared neighbor counts",
  "intersection": "common neighbors between clusters",
  "selectSum": "sum counts for selected UMIs"
}

**CONTEXT CODE:** #include "umi_cluster.h"

int selectSum(std::map<std::string,int> count,std::vector<std::string> id){
  int len = id.size(),sum = 0;
  for(int i = 0;i < len;i++){
    sum+=count[id[i]];
  }
  return(sum);
}

// [[Rcpp::export]]
// MASKED FUNCTION: Calculates shared neighbor counts between UMI clusters
#include <Rcpp.h>
#include <unordered_set>
#include <vector>
#include <string>
#include <algorithm>

// [[Rcpp::export]]
Rcpp::DataFrame shareNeighbor(std::vector<std::string> index, Rcpp::List neighbor, Rcpp::NumericVector count) {
    size_t n = index.size();
    std::vector<int> results(n * n, 0);

    // Convert List to vector of unordered_sets for efficient lookup
    std::vector<std::unordered_set<std::string>> neighbors(n);
    for (size_t i = 0; i < n; ++i) {
        Rcpp::StringVector cluster = neighbor(i);
        neighbors[i].insert(cluster.begin(), cluster.end());
    }

    // Convert NumericVector to map for efficient count lookup
    std::unordered_map<std::string, int> count_map;
    Rcpp::StringVector count_names = count.attr("names");
    for (size_t i = 0; i < count.size(); ++i) {
        count_map[std::string(count_names[i])] = count[i];
    }

    // Calculate shared neighbor counts
    for (size_t i = 0; i < n; ++i) {
        for (size_t j = i; j < n; ++j) {
            std::vector<std::string> intersection;
            std::set_intersection(neighbors[i].begin(), neighbors[i].end(),
                                  neighbors[j].begin(), neighbors[j].end(),
                                  std::back_inserter(intersection));

            int shared_count = 0;
            for (const auto& umi : intersection) {
                if (count_map.find(umi) != count_map.end()) {
                    shared_count += count_map[umi];
                }
            }

            results[i * n + j] = shared_count;
            results[j * n + i] = shared_count;  // Matrix is symmetric
        }
    }

    Rcpp::IntegerVector shared_results(results.begin(), results.end());
    return Rcpp::DataFrame::create(Rcpp::Named("UMI1") = Rcpp::rep(Rcpp::wrap(index), each = n),
                                   Rcpp::Named("UMI2") = Rcpp::rep(Rcpp::wrap(index), times = n),
                                   Rcpp::Named("SharedCount") = shared_results);
}



Complete the function implementation replacing "throw std::runtime_error("Not implemented");".