apple / swift-algorithms

Commonly used sequence and collection algorithms for Swift
Apache License 2.0
5.97k stars 443 forks source link

Improve performance of Product2.distance(from:to:) #40

Closed timvermeulen closed 3 years ago

timvermeulen commented 3 years ago

This PR gets rid of any unnecessary computations in Product2.distance(from:to:). In particular, it now only does a single pass over base2 in the worst case (instead of 3), and for non-random-access collections it no longer does more index calculations than the default implementation of distance(from:to:).

Checklist

natecook1000 commented 3 years ago

Looks great! And thanks for including the illustrative comments 🖼️