Optimized map function that creates weight vector and index only once if the weight function does not involve the mapped argument.