git.iko.soy/typeable/ordered-containers
on master help sign in
Merge pull request #17 from RyanGlScott/toMap-toSet
1c59953Daniel Wagner
ChangeLog.md+5
@@ -1,5 +1,10 @@
1# Revision history for ordered-containers
1# Revision history for ordered-containers
2
2
3+## next -- ????-??-??
4+
5+* Add `toMap` and `toSet`, which support efficient conversions from
6+ `OMap`s/`OSet`s to `Map`s/`Set`s, respectively.
7+
3## 0.2.1 -- 2019-03-25
8## 0.2.1 -- 2019-03-25
4
9
5* Compatibility fixes from Ryan Scott (thanks!)
10* Compatibility fixes from Ryan Scott (thanks!)
Data/Map/Ordered.hs+2
@@ -32,6 +32,8 @@ module Data.Map.Ordered
32 , Index, findIndex, elemAt
32 , Index, findIndex, elemAt
33 -- * List conversions
33 -- * List conversions
34 , fromList, assocs, toAscList
34 , fromList, assocs, toAscList
35+ -- * 'M.Map' conversion
36+ , toMap
35 ) where
37 ) where
36
38
37import qualified Data.Map as M ()
39import qualified Data.Map as M ()
Data/Map/Ordered/Internal.hs+6
@@ -242,3 +242,9 @@ assocs (OMap _ kvs) = map snd $ M.toAscList kvs
242-- | Return key-value pairs in order of increasing key.
242-- | Return key-value pairs in order of increasing key.
243toAscList :: OMap k v -> [(k, v)]
243toAscList :: OMap k v -> [(k, v)]
244toAscList (OMap tvs kvs) = map (\(k, (t, v)) -> (k, v)) $ M.toAscList tvs
244toAscList (OMap tvs kvs) = map (\(k, (t, v)) -> (k, v)) $ M.toAscList tvs
245+
246+-- | Convert an 'OMap' to a 'Map'.
247+--
248+-- /O(n)/, where /n/ is the size of the 'OMap'.
249+toMap :: OMap k v -> Map k v
250+toMap (OMap tvs _) = fmap snd tvs
Data/Map/Ordered/Strict.hs+3−1
@@ -32,13 +32,15 @@ module Data.Map.Ordered.Strict
32 , Index, findIndex, elemAt
32 , Index, findIndex, elemAt
33 -- * List conversions
33 -- * List conversions
34 , fromList, assocs, toAscList
34 , fromList, assocs, toAscList
35+ -- * 'M.Map' conversion
36+ , toMap
35 ) where
37 ) where
36
38
37import Data.Foldable (foldl')
39import Data.Foldable (foldl')
38import qualified Data.Map.Strict as M
40import qualified Data.Map.Strict as M
39import Data.Map.Ordered.Internal
41import Data.Map.Ordered.Internal
40 ( OMap(..), empty, (<>|), (|<>), delete, filter, (\\), (|/\), (/\|), null, size
42 ( OMap(..), empty, (<>|), (|<>), delete, filter, (\\), (|/\), (/\|), null, size
41− , member, notMember, lookup, findIndex, elemAt, assocs, toAscList, fromTV )
43+ , member, notMember, lookup, findIndex, elemAt, assocs, toAscList, fromTV, toMap )
42import Data.Map.Util
44import Data.Map.Util
43import Prelude hiding (filter, lookup, null)
45import Prelude hiding (filter, lookup, null)
44
46
Data/Set/Ordered.hs+8
@@ -28,6 +28,8 @@ module Data.Set.Ordered
28 , Index, findIndex, elemAt
28 , Index, findIndex, elemAt
29 -- * List conversions
29 -- * List conversions
30 , fromList, toAscList
30 , fromList, toAscList
31+ -- * 'Set' conversion
32+ , toSet
31 ) where
33 ) where
32
34
33import Control.Monad (guard)
35import Control.Monad (guard)
@@ -216,3 +218,9 @@ elemAt o@(OSet ts vs) i = do
216-- insertion order.)
218-- insertion order.)
217toAscList :: OSet a -> [a]
219toAscList :: OSet a -> [a]
218toAscList o@(OSet ts _) = fmap fst (M.toAscList ts)
220toAscList o@(OSet ts _) = fmap fst (M.toAscList ts)
221+
222+-- | Convert an 'OSet' to a 'Set'.
223+--
224+-- /O(n)/, where /n/ is the size of the 'OSet'.
225+toSet :: OSet a -> Set a
226+toSet (OSet ts _) = M.keysSet ts