Commit 3bd03e78 authored by Steffen Michels's avatar Steffen Michels

Merge branch 'add-Data.Map.positionOf' into 'master'

Export indexing functions of Data.Map

See merge request !69
parents 2dfec4e2 f938d7d5
Pipeline #8169 passed with stage
in 1 minute and 54 seconds
......@@ -2,7 +2,9 @@ implementation module Data.Graph
import StdBool
import StdFunc
import StdList
import StdInt
from StdList import ++, filter, hd, isEmpty, isMember, map, reverse, tl,
instance length []
import StdOrdList
import StdTuple
......
......@@ -199,7 +199,28 @@ findWithDefault :: !a !k !(Map k a) -> a | < k
alter :: !((Maybe a) -> Maybe a) !k !(Map k a) -> Map k a | < k
elemAt :: !Int !(Map k a) -> (!k, !a)
/**
* Get the index of a key in a Map so that it can be retrieved with
* {{`elemAt`}}.
*/
getIndex :: !k !(Map k a) -> Maybe Int | < k
/**
* Get the entry at a certain index. To get an index for a certain key, see
* {{`getIndex`}}.
*/
elemAt :: !Int !(Map k a) -> Maybe (!k, !a)
/**
* Update an entry at a certain index. To get an index for a certain key, see
* {{`getIndex`}}.
*
* @param The update function
* @param The index
* @param The map
* @result The new map, or Maybe in case of an index out of range
*/
updateAt :: !(k a -> Maybe a) !Int !(Map k a) -> Maybe (Map k a)
findMin :: !(Map k a) -> (!k, !a)
......
......@@ -6,6 +6,7 @@ from StdBool import &&, ||
from StdFunc import id, flip, o, const
from StdTuple import snd
from StdMisc import abort, undef
import StdString
from GenEq import generic gEq
import qualified StdList as SL
import Data.Maybe, Text.JSON
......@@ -406,27 +407,6 @@ alter f k (Bin sx kx x l r) = case lexOrd k kx of
//////////////////////////////////////////////////////////////////////
// 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
// the sequence sorted by keys. The index is a number from /0/ up to, but not
// including, the 'mapSize' of the map.
......@@ -449,66 +429,46 @@ getIndex k m = go 0 k m
// | /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.
// than zero, greater or equal to 'mapSize' of the map), 'Nothing` is returned.
//
// > elemAt 0 (fromList [(5,"a"), (3,"b")]) == (3,"b")
// > elemAt 1 (fromList [(5,"a"), (3,"b")]) == (5, "a")
// > elemAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
// > elemAt 0 (fromList [(5,"a"), (3,"b")]) == Just (3,"b")
// > elemAt 1 (fromList [(5,"a"), (3,"b")]) == Just (5, "a")
// > elemAt 2 (fromList [(5,"a"), (3,"b")]) == Nothing
elemAt :: !Int !(Map k a) -> (!k, !a)
elemAt _ Tip = abort "Map.elemAt: index out of range"
elemAt :: !Int !(Map k a) -> Maybe (!k, !a)
elemAt _ Tip = Nothing
elemAt i (Bin _ kx x l r)
#! mapSizeL = mapSize l
= case lexOrd i mapSizeL of
LT -> elemAt i l
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
// 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.
//
// > updateAt (\ _ _ -> Just "x") 0 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "x"), (5, "a")]
// > updateAt (\ _ _ -> Just "x") 1 (fromList [(5,"a"), (3,"b")]) == fromList [(3, "b"), (5, "x")]
// > updateAt (\ _ _ -> Just "x") 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
// > updateAt (\ _ _ -> Just "x") (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range
// > updateAt (\_ _ -> Nothing) 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
// > updateAt (\_ _ -> Nothing) 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
// > updateAt (\_ _ -> Nothing) 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
// > updateAt (\_ _ -> Nothing) (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range
updateAt :: !(k a -> Maybe a) !Int !(Map k a) -> Map k a
// greater or equal to 'mapSize' of the map), 'Nothing` is returned.
//
// > updateAt (\ _ _ -> Just "x") 0 (fromList [(5,"a"), (3,"b")]) == Just (fromList [(3, "x"), (5, "a")])
// > updateAt (\ _ _ -> Just "x") 1 (fromList [(5,"a"), (3,"b")]) == Just (fromList [(3, "b"), (5, "x")])
// > updateAt (\ _ _ -> Just "x") 2 (fromList [(5,"a"), (3,"b")]) == Nothing
// > updateAt (\ _ _ -> Just "x") (-1) (fromList [(5,"a"), (3,"b")]) == Nothing
// > updateAt (\_ _ -> Nothing) 0 (fromList [(5,"a"), (3,"b")]) == Just (singleton 5 "a")
// > updateAt (\_ _ -> Nothing) 1 (fromList [(5,"a"), (3,"b")]) == Just (singleton 3 "b")
// > updateAt (\_ _ -> Nothing) 2 (fromList [(5,"a"), (3,"b")]) == Nothing
// > updateAt (\_ _ -> Nothing) (-1) (fromList [(5,"a"), (3,"b")]) == Nothing
updateAt :: !(k a -> Maybe a) !Int !(Map k a) -> Maybe (Map k a)
updateAt f i t =
case t of
Tip = abort "Map.updateAt: index out of range"
Tip = Nothing
Bin sx kx x l r
#! mapSizeL = mapSize l
= case lexOrd i mapSizeL of
LT -> balanceR kx x (updateAt f i l) r
GT -> balanceL kx x l (updateAt f (i-mapSizeL-1) r)
LT -> flip (balanceR kx x) r <$> updateAt f i l
GT -> balanceL kx x l <$> updateAt f (i-mapSizeL-1) r
EQ -> case f kx x of
Just x` -> Bin sx kx x` l r
Nothing -> glue l r
// | /O(log n)/. Delete 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,
// greater or equal to 'mapSize' of the map), 'abort` is called.
//
// > deleteAt 0 (fromList [(5,"a"), (3,"b")]) == singleton 5 "a"
// > deleteAt 1 (fromList [(5,"a"), (3,"b")]) == singleton 3 "b"
// > deleteAt 2 (fromList [(5,"a"), (3,"b")]) Error: index out of range
// > deleteAt (-1) (fromList [(5,"a"), (3,"b")]) Error: index out of range
deleteAt :: !Int !(Map k a) -> Map k a
deleteAt i t =
case t of
Tip = abort "Map.deleteAt: index out of range"
Bin _ kx x l r
#! mapSizeL = mapSize l
= case lexOrd i mapSizeL of
LT -> balanceR kx x (deleteAt i l) r
GT -> balanceL kx x l (deleteAt (i-mapSizeL-1) r)
EQ -> glue l r
Just x` -> Just (Bin sx kx x` l r)
Nothing -> Just (glue l r)
//////////////////////////////////////////////////////////////////////
......
implementation module Internet.HTTP.CGI
import StdFile, StdInt, StdBool, StdArray
import Data.Maybe, Data.Tuple, Internet.HTTP, Text, System.Environment, Data.Map
import Data.Maybe, Data.Tuple, Internet.HTTP, System.Environment, Data.Map
//Http headers which should be polled in the environment
CGI_HEADERS :== [ ("Content-Type","CONTENT_TYPE")
......
......@@ -8,9 +8,12 @@ definition module Text.JSON
* For more info about JSON see: http://www.json.org/
*/
import StdGeneric, Data.Maybe, StdList, StdString
import StdGeneric
from StdFile import class <<<
from StdOverloaded import class fromString, class ==(..)
from StdString import instance == {#Char}
from Data.List import !?
from Data.Maybe import :: Maybe(..)
:: JSONNode = JSONNull
| JSONBool !Bool
......
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