refactor distancefunction
Use template to optimize one dimension data structure, and use template to replace virtual distance calculation function.