Repository navigation
Expand file tree
/
Copy pathIntMap.hs
More file actions
46 lines (36 loc) · 1.79 KB
/
Copy pathIntMap.hs
File metadata and controls
46 lines (36 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
module IntMap where
import Data.Maybe
data IntMap a = IntMap {val :: Maybe a, l :: IntMap a, r :: IntMap a}
empty :: IntMap a
empty = IntMap Nothing empty empty
put :: Integral b => IntMap a -> b -> a -> IntMap a
put map 0 y = map {val = Just y}
put map n y = if n `mod` 2 == 1 then map {r = put (r map) (n `div` 2) y}
else map {l = put (l map) (n `div` 2) y}
maybeGet :: Integral b => IntMap a -> b -> Maybe a
maybeGet map 0 = val map
maybeGet map n = if n `mod` 2 == 1 then maybeGet (r map) (n `div` 2)
else maybeGet (l map) (n `div` 2)
get :: Integral b => IntMap a -> b -> a
get (IntMap Nothing _ _) 0 = error "empty index"
get map 0 = fromJust $ val map
get map n = if n `mod` 2 == 1 then get (r map) (n `div` 2)
else get (l map) (n `div` 2)
toList :: Integral b => b -> b -> a -> IntMap a -> [a]
toList from to def map | from > to = []
| otherwise = (f' def $ maybeGet map from) :
toList (from + 1) to def map
where f' def Nothing = def
f' _ (Just x) = x
{-
IntMap - data structure to storage (a type) values by not negative Int / Integer keys
empty :: IntMap of (a type)
put :: curr IntMap -> index -> new value -> new IntMap
maybeGet :: curr IntMap -> index -> Nothing - value wasn't inserted
Just x - value was inserted and it's x
get :: curr IntMap -> index -> value - if value wasn't inserted will throw error
toList :: left bound -> right bound -> default value -> curr IntMap -> [values]
form list of values with keys in [left, right]. if it's no such value then use default
toList work in O((right - left) * log(right)) time
other operations in O(log(key))
-}