From 0111bc3c0848d411c169696b6d2176dea40e9417 Mon Sep 17 00:00:00 2001 From: Ryan Scott Date: Sun, 7 Jan 2018 18:25:26 -0500 Subject: [PATCH] Add Semigroup intances for CritBit and Set --- Data/CritBit/Set.hs | 9 +++++++++ Data/CritBit/Tree.hs | 20 ++++++++++++++------ critbit.cabal | 2 +- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/Data/CritBit/Set.hs b/Data/CritBit/Set.hs index 0039925..cb0508b 100644 --- a/Data/CritBit/Set.hs +++ b/Data/CritBit/Set.hs @@ -1,3 +1,4 @@ +{-# LANGUAGE CPP #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- | @@ -90,6 +91,9 @@ import Data.CritBit.Types.Internal (CritBit(..), Set(..), CritBitKey, Node(..)) import Data.Foldable (Foldable, foldMap) import Data.Maybe (isJust) import Data.Monoid (Monoid(..)) +#if MIN_VERSION_base(4,9,0) +import Data.Semigroup (Semigroup(..)) +#endif import Prelude hiding (null, filter, map, foldl, foldr) import qualified Data.CritBit.Tree as T import qualified Data.List as List @@ -97,6 +101,11 @@ import qualified Data.List as List instance (Show a) => Show (Set a) where show s = "fromList " ++ show (toList s) +#if MIN_VERSION_base(4,9,0) +instance CritBitKey k => Semigroup (Set k) where + (<>) = union +#endif + instance CritBitKey k => Monoid (Set k) where mempty = empty mappend = union diff --git a/Data/CritBit/Tree.hs b/Data/CritBit/Tree.hs index 6206fdd..a3e6e3e 100644 --- a/Data/CritBit/Tree.hs +++ b/Data/CritBit/Tree.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE BangPatterns, RecordWildCards, ScopedTypeVariables #-} +{-# LANGUAGE CPP, BangPatterns, RecordWildCards, ScopedTypeVariables #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- | @@ -153,21 +153,31 @@ import Data.CritBit.Core import Data.CritBit.Types.Internal import Data.Maybe (fromMaybe) import Data.Monoid (Monoid(..)) +#if MIN_VERSION_base(4,9,0) +import Data.Semigroup (Semigroup(..)) +#endif import Data.Traversable (Traversable(traverse)) import Prelude hiding (foldl, foldr, lookup, null, map, filter) import qualified Data.Array as A import qualified Data.Foldable as Foldable import qualified Data.List as List +#if MIN_VERSION_base(4,9,0) +instance CritBitKey k => Semigroup (CritBit k v) where + (<>) = union +#endif + instance CritBitKey k => Monoid (CritBit k v) where mempty = empty +#if !(MIN_VERSION_base(4,11,0)) mappend = union +#endif mconcat = unions instance CritBitKey k => Traversable (CritBit k) where traverse f m = traverseWithKey (\_ v -> f v) m -infixl 9 !, \\ +infixl 9 !, \\ -- Comment needed here to avoid CPP bug -- | /O(k)/. Find the value at a key. -- Calls 'error' when the element can not be found. @@ -1247,8 +1257,7 @@ deleteMax m = updateMaxWithKey (\_ _ -> Nothing) m -- > deleteFindMin Error: can not return the minimal element of an empty map deleteFindMin :: CritBit k v -> ((k, v), CritBit k v) deleteFindMin = fromMaybe (error msg) . minViewWithKey - where msg = "CritBit.deleteFindMin: cannot return the minimal \ - \element of an empty map" + where msg = "CritBit.deleteFindMin: cannot return the minimal element of an empty map" {-# INLINABLE deleteFindMin #-} -- | /O(k)/. Delete and find the maximal element. @@ -1257,8 +1266,7 @@ deleteFindMin = fromMaybe (error msg) . minViewWithKey -- > deleteFindMax Error: can not return the maximal element of an empty map deleteFindMax :: CritBit k v -> ((k, v), CritBit k v) deleteFindMax = fromMaybe (error msg) . maxViewWithKey - where msg = "CritBit.deleteFindMax: cannot return the minimal \ - \element of an empty map" + where msg = "CritBit.deleteFindMax: cannot return the minimal element of an empty map" {-# INLINABLE deleteFindMax #-} -- | /O(k')/. Retrieves the value associated with minimal key of the diff --git a/critbit.cabal b/critbit.cabal index 54603e9..637794c 100644 --- a/critbit.cabal +++ b/critbit.cabal @@ -96,7 +96,7 @@ benchmark benchmarks critbit, criterion >= 0.8, deepseq, - hashable < 1.2, + hashable < 1.3, mtl, mwc-random, text,