Verified Commit 5c16d875 authored by Camil Staps's avatar Camil Staps 🙂

Add positionOf to Data.Map

parent ad17d96c
Pipeline #8117 passed with stage
in 1 minute and 44 seconds
......@@ -199,6 +199,16 @@ findWithDefault :: !a !k !(Map k a) -> a | < k
alter :: !((Maybe a) -> Maybe a) !k !(Map k a) -> Map k a | < k
/**
* Get the position of a key in a Map so that it can be retrieved with
* {{`elemAt`}}.
*/
positionOf :: !k !(Map k a) -> Maybe Int | < k
/**
* Get the entry at a certain index. This gives a runtime error when the index
* is out of range. To get an index for a certain key, see {{`positionOf`}}.
*/
elemAt :: !Int !(Map k a) -> (!k, !a)
findMin :: !(Map k a) -> (!k, !a)
......
......@@ -447,6 +447,13 @@ getIndex k m = go 0 k m
GT -> go (idx + mapSize l + 1) k r
EQ -> Just (idx + mapSize l)
positionOf :: !k !(Map k a) -> Maybe Int | < k
positionOf k Tip = Nothing
positionOf k (Bin s kx _ l r) = case lexOrd k kx of
LT -> positionOf k l
EQ -> Just (mapSize l)
GT -> ((+) (mapSize l + 1)) <$> positionOf k r
// | /O(log n)/. Retrieve an element by its /index/, i.e. by its zero-based
// index in the sequence sorted by keys. If the /index/ is out of range (less
// than zero, greater or equal to 'mapSize' of the map), 'abort` is called.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment