Portable generic KDTree implementation