git.iko.soy/typeable/ordered-containers
on master help sign in
Merge pull request #17 from RyanGlScott/toMap-toSet

Implement toMap and toSet (#16)

commit1c5995361a96b7df06246dbab71cbbc3cd6955c4 authorDaniel Wagner <me@dmwit.com> dateFri 5 Jul 2019, 09:19 (7 years ago) parent14d0b5c0f74a5a
5 files changed+24−1
ChangeLog.md+5 Data/Map/Ordered.hs+2 Data/Map/Ordered/Internal.hs+6 Data/Map/Ordered/Strict.hs+3−1 Data/Set/Ordered.hs+8
ChangeLog.md+5
@@ -1,5 +1,10 @@
11# Revision history for ordered-containers
22
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+
38## 0.2.1 -- 2019-03-25
49
510* Compatibility fixes from Ryan Scott (thanks!)
Data/Map/Ordered.hs+2
@@ -32,6 +32,8 @@ module Data.Map.Ordered
3232 , Index, findIndex, elemAt
3333 -- * List conversions
3434 , fromList, assocs, toAscList
35+ -- * 'M.Map' conversion
36+ , toMap
3537 ) where
3638
3739import qualified Data.Map as M ()
Data/Map/Ordered/Internal.hs+6
@@ -242,3 +242,9 @@ assocs (OMap _ kvs) = map snd $ M.toAscList kvs
242242-- | Return key-value pairs in order of increasing key.
243243toAscList :: OMap k v -> [(k, v)]
244244toAscList (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
3232 , Index, findIndex, elemAt
3333 -- * List conversions
3434 , fromList, assocs, toAscList
35+ -- * 'M.Map' conversion
36+ , toMap
3537 ) where
3638
3739import Data.Foldable (foldl')
3840import qualified Data.Map.Strict as M
3941import Data.Map.Ordered.Internal
4042 ( 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 )
4244import Data.Map.Util
4345import Prelude hiding (filter, lookup, null)
4446
Data/Set/Ordered.hs+8
@@ -28,6 +28,8 @@ module Data.Set.Ordered
2828 , Index, findIndex, elemAt
2929 -- * List conversions
3030 , fromList, toAscList
31+ -- * 'Set' conversion
32+ , toSet
3133 ) where
3234
3335import Control.Monad (guard)
@@ -216,3 +218,9 @@ elemAt o@(OSet ts vs) i = do
216218-- insertion order.)
217219toAscList :: OSet a -> [a]
218220toAscList 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