Verified Commit cb08cd83 authored by Camil Staps's avatar Camil Staps 🚀

Remove findIndex; make elemAt total

parent ad958a66
Pipeline #8164 passed with stage
in 1 minute and 55 seconds
...@@ -201,24 +201,15 @@ alter :: !((Maybe a) -> Maybe a) !k !(Map k a) -> Map k a | < k ...@@ -201,24 +201,15 @@ alter :: !((Maybe a) -> Maybe a) !k !(Map k a) -> Map k a | < k
/** /**
* Get the index of a key in a Map so that it can be retrieved with * Get the index of a key in a Map so that it can be retrieved with
* {{`elemAt`}}. This gives a runtime error when the key does not exist. * {{`elemAt`}}.
* {{`getIndex`}} does the same, but uses a Maybe instead.
*/
findIndex :: !k !(Map k a) -> Int | < k
/**
* Get the index of a key in a Map so that it can be retrieved with
* {{`elemAt`}}. {{`findIndex`}} does the same with a runtime error instead of
* Maybe.
*/ */
getIndex :: !k !(Map k a) -> Maybe Int | < k getIndex :: !k !(Map k a) -> Maybe Int | < k
/** /**
* Get the entry at a certain index. This gives a runtime error when the index * Get the entry at a certain index. To get an index for a certain key, see
* is out of range. To get an index for a certain key, see {{`findIndex`}} and
* {{`getIndex`}}. * {{`getIndex`}}.
*/ */
elemAt :: !Int !(Map k a) -> (!k, !a) elemAt :: !Int !(Map k a) -> Maybe (!k, !a)
findMin :: !(Map k a) -> (!k, !a) findMin :: !(Map k a) -> (!k, !a)
......
...@@ -406,27 +406,6 @@ alter f k (Bin sx kx x l r) = case lexOrd k kx of ...@@ -406,27 +406,6 @@ alter f k (Bin sx kx x l r) = case lexOrd k kx of
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// Indexing // Indexing
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
// | /O(log n)/. Return the /index/ of a key, which is its zero-based index in
// the sequence sorted by keys. The index is a number from /0/ up to, but not
// including, the 'mapSize' of the map. Calls 'abort` when the key is not
// a 'member` of the map.
//
// > findIndex 2 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map
// > findIndex 3 (fromList [(5,"a"), (3,"b")]) == 0
// > findIndex 5 (fromList [(5,"a"), (3,"b")]) == 1
// > findIndex 6 (fromList [(5,"a"), (3,"b")]) Error: element is not in the map
// See Note: Type of local 'go' function
findIndex :: !k !(Map k a) -> Int | < k
findIndex k m = go 0 k m
where
go :: !Int !k !(Map k a) -> Int | < k
go _ _ Tip = abort "Map.findIndex: element is not in the map"
go idx k (Bin _ kx _ l r) = case lexOrd k kx of
LT -> go idx k l
GT -> go (idx + mapSize l + 1) k r
EQ -> idx + mapSize l
// | /O(log n)/. Lookup the /index/ of a key, which is its zero-based index in // | /O(log n)/. Lookup the /index/ of a key, which is its zero-based index in
// the sequence sorted by keys. The index is a number from /0/ up to, but not // the sequence sorted by keys. The index is a number from /0/ up to, but not
// including, the 'mapSize' of the map. // including, the 'mapSize' of the map.
...@@ -451,18 +430,18 @@ getIndex k m = go 0 k m ...@@ -451,18 +430,18 @@ getIndex k m = go 0 k m
// index in the sequence sorted by keys. If the /index/ is out of range (less // 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. // than zero, greater or equal to 'mapSize' of the map), 'abort` is called.
// //
// > elemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b") // > elemAt 0 (fromList [(5,"a"), (3,"b")]) == Just (3,"b")
// > elemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a") // > elemAt 1 (fromList [(5,"a"), (3,"b")]) == Just (5, "a")
// > elemAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range // > elemAt 2 (fromList [(5,"a"), (3,"b")]) == Nothing
elemAt :: !Int !(Map k a) -> (!k, !a) elemAt :: !Int !(Map k a) -> Maybe (!k, !a)
elemAt _ Tip = abort "Map.elemAt: index out of range" elemAt _ Tip = Nothing
elemAt i (Bin _ kx x l r) elemAt i (Bin _ kx x l r)
#! mapSizeL = mapSize l #! mapSizeL = mapSize l
= case lexOrd i mapSizeL of = case lexOrd i mapSizeL of
LT -> elemAt i l LT -> elemAt i l
GT -> elemAt (i - mapSizeL - 1) r GT -> elemAt (i - mapSizeL - 1) r
EQ -> (kx,x) EQ -> Just (kx,x)
// | /O(log n)/. Update the element at /index/, i.e. by its zero-based index in // | /O(log n)/. Update the element at /index/, i.e. by its zero-based index in
// the sequence sorted by keys. If the /index/ is out of range (less than zero, // the sequence sorted by keys. If the /index/ is out of range (less than zero,
......
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