implement calculating r-nearest references