elmcraft / core-extra

Utility functions for an improved experience with elm/core
https://package.elm-lang.org/packages/elmcraft/core-extra/latest/
Other
22 stars 10 forks source link

Add Array.Extra.bisectRightWith #23

Open gampleman opened 10 months ago

gampleman commented 10 months ago

bisectRightWith : (a -> a -> Order) -> a -> Array a -> Int

Given an ordering function, a needle element and a sorted array haystack, it finds an insertion point that would maintain the sorted order of the array. The returned insertion point I partitions the array into two halves so that all v <= x for v in Array.slice 0 i array for the left side and all v > x for v in Array.slice i (Array length array) array) for the right side.

Runs in O(log(n)) time.

Motivating use case

Allows you to turn an array into an ad hoc binary search tree. Personally, I've used this function to compute histograms for instance, but it is useful to speed up any alogirthm that needs to maintain a sorted array while inserting stuff into it.

Family

As the name suggests, there are a number of functions that work essentially similarly, but with small variations:

And of course potentially the grid of their combinations:

order left right centre
with bisectLeftWith / bisectLeftWithWithin bisectRightWith / bisectRightWithWithin -
by bisectLeftBy / bisectLeftByWithin bisectRightBy / bisectRightByWithin bisectCenterBy / bisectCenterByWithin
comparable bisectLeft / bisectLeftWithin bisectRight / bisectRightWithin bisectCenter / bisectCenterWithin

Which if any of these to add is an open question. Suddenly adding all 16 certainly feels like massive overkill. Personally I would start with bisectRightWith and perhaps bisectLeftWith and leave the rest for now.

Rationale

A naive implementation looks something like this:

bisectRightWith : (a -> a -> Order) -> a -> Array a  -> Int
bisectRightWith compare item array =
    case Array.get 0 array of
        Nothing ->
            0

        Just default ->
            let
               gt = 
                    Order.Extra.greaterThanBy compare 

                -- we start by doing a bounds check, so this shouldn't fail
                get index =
                    Array.get index array |> Maybe.withDefault default

                helper lo hi =
                    if lo < hi then
                        let
                            mid =
                               (lo + hi) // 2
                        in
                        if gt (get mid) item then
                            helper lo mid

                        else
                            helper (mid + 1) hi

                    else
                        lo
            in
            helper 0 (Array.length array)

And although this is a fairly standard CS algorithm, there are a few things that make it a bit tricky. Mostly the mandatory Maybe checks are annoying, since (if this is implemented correctly) there are already bounds checks and there is no case where where a Nothing should be seen.