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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
module Data.Map where
import Prelude hiding (filter,foldl,foldr,null,map,take,drop,splitAt,elems,lookup,(!))
import qualified Data.OldList as List
import qualified Data.Set as Set
import Data.Eq
import Data.Functor
import qualified Data.Foldable
import Text.Show
data Map k a = Map [(k,a)]
empty = Map []
singleton k x = fromList [(k,x)]
fromList kxs = Map $ nubBy (\kx1 kx2 -> fst kx1 == fst kx2) kxs
fromAscList kxs = fromList kxs
fromDescList kxs = fromList kxs
fromDistinctAscList kxs = fromList kxs
fromDistinctDescList kxs = fromList kxs
insert k x m = Map $ (k,x):kxs where Map kxs = delete k m
delete k m@(Map xs) = Map $ List.filter (\kx -> fst kx /= k) xs
member k m = List.elem k $ toList m
notMember k m = not $ member k m
null (Map []) = True
null _ = False
size (Map kxs) = length kxs
isSubmapOf m1 m2 = Set.isSubsetOf (keySet m1) (keySet m2)
isProperSubmapOf m1 m2 = Set.isProperSubsetOf (keySet m1) (keySet m2)
--disjoint s1 s2 = size (intersection s1 s2) == 0
--union (Set xs) (Set ys) = Set (xs ++ List.filter (`notElem` xs) ys)
--unions = List.foldl union empty
--difference (Set xs) (Set ys) = Set $ List.filter (`notElem` ys) xs
-- infixl 9 \\
-- s1 \\ s2 = s1 difference s2
--intersection (Set xs) (Set ys) = Set $ List.filter (`elem` ys) xs
--cartesionProduct (Set xs) (Set ys) = Set [(x,y) | x <- xs, y <- ys]
--disjointUnion xs ys = map Left xs `union` map Right ys
filter pred xs = Set.Set $ List.filter (\(_,x) -> pred x) xs
-- takeWhileAntitone
-- dropWhileAntitone
-- spanAntitone
-- partition
-- split
-- splitMember
-- splitRoot
-- lookupIndex
-- findIndex
-- lookupIndex
-- findIndex
-- elemAt
-- deleteAt
-- take n (Set xs) =
-- drop
-- splitAt
map f (Map xs) = Map $ List.map (\(k,x) -> (k,f x)) xs
mapWithKey f (Map xs) = Map $ List.map (\(k,x) -> (k,f k x)) xs
-- mapMonotonic
foldr f z = List.foldr f z . elems
foldl f z = List.foldl f z . elems
--foldr'
--foldl'
--fold
--lookupMin
--lookupMax
--findMin
--findMax
--deleteMin
--deleteMax
--deleteFindMin
--deleteFindMax
--maxView
-- minView
lookup :: Eq k => k -> Map k v -> Maybe v
lookup k' (Map kxs) = go kxs where
go [] = Nothing
go ((k,x):kxs) | k' == k = Just x
| otherwise = go kxs
infixl 9 !
(!) :: Eq a => Map a b -> a -> b
m ! k = case lookup k m of Just x -> x
Nothing -> error "Error: element not in the map"
elems m = List.map snd $ toAscList m
keys m = List.map fst $ toAscList m
assocs = toAscList
keySet m = Set.fromList $ keys m
toList (Map xs) = xs
toAscList (Map xs) = List.sortOn snd xs
toDescList = reverse . toAscList
instance (Eq k, Eq v) => Eq (Map k v) where
Map xs == Map ys = xs == ys
instance Functor (Map k) where
fmap = map
instance Foldable (Map k) where
toList (Map xs) = List.map snd xs
instance (Show k, Show v) => Show (Map k v) where
show (Map xs) = "Map " ++ show xs
|