Debian Codegraph @

Language/Haskell/TH/Desugar/OMap/Strict.hs

raw
1 {-# LANGUAGE ScopedTypeVariables #-}
2 {-# LANGUAGE TypeApplications #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : Language.Haskell.TH.Desugar.OMap
7 -- Copyright : (C) 2016-2018 Daniel Wagner, 2019 Ryan Scott
8 -- License : BSD-style (see LICENSE)
9 -- Maintainer : Ryan Scott
10 -- Stability : experimental
11 -- Portability : non-portable
12 --
13 -- An 'OMap' behaves much like a 'M.Map', with all the same asymptotics, but
14 -- also remembers the order that keys were inserted.
15 --
16 -- This module offers a simplified version of the "Data.Map.Ordered.Strict" API
17 -- that assumes left-biased indices everywhere and uses a different 'Semigroup'
18 -- instance (the one in this module uses @('<>') = 'union'@) and 'Monoid'
19 -- instance (the one in this module uses @'mappend' = 'union'@).
20 --
21 ----------------------------------------------------------------------------
22 module Language.Haskell.TH.Desugar.OMap.Strict
23 ( OMap(..)
24 -- * Trivial maps
25 , empty, singleton
26 -- * Insertion
27 , insertPre, insertPost, union, unionWithKey
28 -- * Deletion
29 , delete, filterWithKey, (\\), intersection, intersectionWithKey
30 -- * Query
31 , null, size, member, notMember, lookup
32 -- * Indexing
33 , Index, lookupIndex, lookupAt
34 -- * List conversions
35 , fromList, assocs, toAscList
36 -- * 'M.Map' conversion
37 , toMap
38 ) where
39
40 import Data.Coerce
41 import qualified Data.Map.Strict as M (Map)
42 import Data.Map.Ordered.Strict (Index)
43 import qualified Data.Map.Ordered.Strict as OM
44 import Language.Haskell.TH.Desugar.OMap (OMap(..))
45 import Prelude hiding (filter, lookup, null)
46
47 empty :: forall k v. OMap k v
48 empty = coerce (OM.empty @k @v)
49
50 singleton :: k -> v -> OMap k v
51 singleton k v = coerce (OM.singleton (k, v))
52
53 -- | The value's index will be lower than the indices of the values in the
54 -- 'OSet'.
55 insertPre :: Ord k => k -> v -> OMap k v -> OMap k v
56 insertPre k v = coerce ((k, v) OM.|<)
57
58 -- | The value's index will be higher than the indices of the values in the
59 -- 'OSet'.
60 insertPost :: Ord k => OMap k v -> k -> v -> OMap k v
61 insertPost m k v = coerce (coerce m OM.|> (k, v))
62
63 union :: forall k v. Ord k => OMap k v -> OMap k v -> OMap k v
64 union = coerce ((OM.|<>) @k @v)
65
66 unionWithKey :: Ord k => (k -> v -> v -> v) -> OMap k v -> OMap k v -> OMap k v
67 unionWithKey f = coerce (OM.unionWithL f)
68
69 delete :: forall k v. Ord k => k -> OMap k v -> OMap k v
70 delete = coerce (OM.delete @k @v)
71
72 filterWithKey :: Ord k => (k -> v -> Bool) -> OMap k v -> OMap k v
73 filterWithKey f = coerce (OM.filter f)
74
75 (\\) :: forall k v v'. Ord k => OMap k v -> OMap k v' -> OMap k v
76 (\\) = coerce ((OM.\\) @k @v @v')
77
78 intersection :: forall k v v'. Ord k => OMap k v -> OMap k v' -> OMap k v
79 intersection = coerce ((OM.|/\) @k @v @v')
80
81 intersectionWithKey :: Ord k => (k -> v -> v' -> v'') -> OMap k v -> OMap k v' -> OMap k v''
82 intersectionWithKey f = coerce (OM.intersectionWith f)
83
84 null :: forall k v. OMap k v -> Bool
85 null = coerce (OM.null @k @v)
86
87 size :: forall k v. OMap k v -> Int
88 size = coerce (OM.size @k @v)
89
90 member :: forall k v. Ord k => k -> OMap k v -> Bool
91 member = coerce (OM.member @k @v)
92
93 notMember :: forall k v. Ord k => k -> OMap k v -> Bool
94 notMember = coerce (OM.notMember @k @v)
95
96 lookup :: forall k v. Ord k => k -> OMap k v -> Maybe v
97 lookup = coerce (OM.lookup @k @v)
98
99 lookupIndex :: forall k v. Ord k => k -> OMap k v -> Maybe Index
100 lookupIndex = coerce (OM.findIndex @k @v)
101
102 lookupAt :: forall k v. Index -> OMap k v -> Maybe (k, v)
103 lookupAt i m = OM.elemAt @k @v (coerce m) i
104
105 fromList :: Ord k => [(k, v)] -> OMap k v
106 fromList l = coerce (OM.fromList l)
107
108 assocs :: forall k v. OMap k v -> [(k, v)]
109 assocs = coerce (OM.assocs @k @v)
110
111 toAscList :: forall k v. OMap k v -> [(k, v)]
112 toAscList = coerce (OM.toAscList @k @v)
113
114 toMap :: forall k v. OMap k v -> M.Map k v
115 toMap = coerce (OM.toMap @k @v)