Bersaelor / KDTree

Swift implementation of a k-dimensional binary space partitioning tree.
MIT License
155 stars 27 forks source link

Limiting Protocol definition #58

Open bshafiee opened 1 year ago

bshafiee commented 1 year ago

Hi, Thanks for creating this awesome library. Would it be Possible to extend the protocol to support any KDTreePoint implemented type? What I mean is that, currently the protocol expect the same type for distance comparision:

public protocol KDTreePoint: Equatable {
    ...
    func squaredDistance(to otherPoint: **Self**) -> Double
}

This is okay but very limiting, imagine my nodes have this structure:

class Node {
   point2d: CGPoint
   otherData...
}

if I create my tree with Node, I am forced always to query it with a Node as well, but if you would have relaxed the Protocol to:

public protocol KDTreePoint: Equatable {
    ...
    func squaredDistance(to otherPoint: **KDTreePoint**) -> Double
}

then I could extend both Node and CGPoint and do my queries with a tiny CGPoint (albeit handling the type check in the distance function etc.). I know this is a convenience but figured mention it. Thank you!

Bersaelor commented 1 year ago

hello @bshafiee,

i didn't expect a heterogenous tree structure with different types being compared would be a common use case. Potentially this could cause a lot of confusion.

Why don't you handle it on the client side?

I.e. you could have

struct MetaNode: KDTreePoint
    point: Either<CGPoint, OtherPoint>

    func squaredDistance(to otherPoint: Self) -> Double {
         switch self { 
             ...
    }

Assuming the common definition many people use for Either<A, B>