summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreyMulik <>2021-02-23 06:19:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2021-02-23 06:19:00 (GMT)
commit82954b16a166d7af2272a7a6137dbda0b8045c0a (patch)
treeaec2c6bb20ef082662e3521c4bdc197c6ab9a4be
version 0.2HEAD0.2master
-rw-r--r--LICENSE30
-rw-r--r--Setup.hs2
-rw-r--r--sdp4unordered.cabal50
-rw-r--r--src/SDP/HashMap/Lazy.hs90
-rw-r--r--src/SDP/HashMap/Strict.hs90
-rw-r--r--src/SDP/HashSet.hs95
6 files changed, 357 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..819ce9a
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,30 @@
+Copyright Andrey Mulik (c) 2020
+
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the following
+ disclaimer in the documentation and/or other materials provided
+ with the distribution.
+
+ * Neither the name of Andrey Mulik nor the names of other
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..9a994af
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/sdp4unordered.cabal b/sdp4unordered.cabal
new file mode 100644
index 0000000..f2521f0
--- /dev/null
+++ b/sdp4unordered.cabal
@@ -0,0 +1,50 @@
+name: sdp4unordered
+version: 0.2
+category: Data Structures
+
+synopsis: SDP classes for unordered containers
+description: Implementation of SDP core classes for unordered containers
+
+author: Andrey Mulik
+maintainer: work.a.mulik@gmail.com
+bug-reports: https://github.com/andreymulik/sdp4unordered/issues
+
+copyright: 2020 Andrey Mulik
+license-file: LICENSE
+license: BSD3
+
+build-type: Simple
+cabal-version: >=1.10
+
+source-repository head
+ type: git
+ location: https://github.com/andreymulik/sdp4unordered
+
+--- _ _____ ______ ______ ___ ______ __ __ ---
+--- | | |_ _|| ___ \| ___ \ / _ \ | ___ \\ \ / / ---
+--- | | | | | |_/ /| |_/ // /_\ \| |_/ / \ V / ---
+--- | | | | | ___ \| / | _ || / \ / ---
+--- | |____ _| |_ | |_/ /| |\ \ | | | || |\ \ | | ---
+--- \_____/ \___/ \____/ \_| \_|\_| |_/\_| \_| \_/ ---
+
+Library
+ default-language: Haskell2010
+ hs-source-dirs: src
+
+ build-depends:
+ base >= 4.12 && < 5,
+ sdp >= 0.2 && < 1,
+ sdp-hashable >= 0.2 && < 1,
+ unordered-containers >= 0.2 && < 0.3
+
+ ghc-options: -Wall -Wno-orphans -Wcompat
+
+ exposed-modules:
+ SDP.HashMap.Strict
+ SDP.HashMap.Lazy
+ SDP.HashSet
+
+
+
+
+
diff --git a/src/SDP/HashMap/Lazy.hs b/src/SDP/HashMap/Lazy.hs
new file mode 100644
index 0000000..27a07e9
--- /dev/null
+++ b/src/SDP/HashMap/Lazy.hs
@@ -0,0 +1,90 @@
+{-# LANGUAGE Safe, MultiParamTypeClasses, FlexibleInstances #-}
+
+{- |
+ Module : SDP.HashMap.Lazy
+ Copyright : (c) Andrey Mulik 2020
+ License : BSD-style
+ Maintainer : work.a.mulik@gmail.com
+ Portability : portable
+
+ @SDP.HashMap.Lazy@ provides 'HashMap' - lazy unordered associative array
+ with 'Hashable' keys.
+-}
+module SDP.HashMap.Lazy
+(
+ -- * Exports
+ module SDP.Hashable,
+ module SDP.Linear,
+ module SDP.Map,
+
+ -- * Hash map
+ HashMap, LHashMap
+)
+where
+
+import Prelude ()
+import SDP.SafePrelude
+import SDP.Hashable
+import SDP.Linear
+import SDP.Map
+
+import qualified Data.HashMap.Lazy as H
+import Data.HashMap.Lazy ( HashMap )
+
+import Data.Maybe
+
+import Control.Exception.SDP
+
+default ()
+
+--------------------------------------------------------------------------------
+
+-- | 'HashMap' alias, may reduce ambiguity.
+type LHashMap = HashMap
+
+--------------------------------------------------------------------------------
+
+instance Nullable (HashMap k e)
+ where
+ isNull = null
+ lzero = H.empty
+
+instance (Index k) => Estimate (HashMap k e)
+ where
+ (<==>) = on (<=>) length
+ (.<=.) = on (<=) length
+ (.>=.) = on (>=) length
+ (.>.) = on (>) length
+ (.<.) = on (<) length
+
+ (<.=>) = (<=>) . length
+ (.>=) = (>=) . length
+ (.<=) = (<=) . length
+ (.>) = (>) . length
+ (.<) = (<) . length
+
+instance (Eq k, Hashable k) => Map (HashMap k e) k e
+ where
+ toMap' = const toMap
+ toMap = H.fromList
+ assocs = H.toList
+
+ kfoldl = H.foldlWithKey' . flip
+ kfoldr = H.foldrWithKey
+
+ filter' = H.filterWithKey
+ member' = H.member
+ insert' = H.insert
+ delete' = H.delete
+
+ -- | Throws 'IndexException' instead 'error' call.
+ (!) = fromMaybe (undEx "(!) {HashMap k e}") ... (!?)
+ (//) = toMap ... (++) . assocs
+ (!?) = flip H.lookup
+ keys = H.keys
+
+--------------------------------------------------------------------------------
+
+undEx :: String -> a
+undEx = throw . UndefinedValue . showString "in SDP.HashMap.Lazy."
+
diff --git a/src/SDP/HashMap/Strict.hs b/src/SDP/HashMap/Strict.hs
new file mode 100644
index 0000000..2c88834
--- /dev/null
+++ b/src/SDP/HashMap/Strict.hs
@@ -0,0 +1,90 @@
+{-# LANGUAGE Safe, MultiParamTypeClasses, FlexibleInstances #-}
+
+{- |
+ Module : SDP.HashMap.Strict
+ Copyright : (c) Andrey Mulik 2020
+ License : BSD-style
+ Maintainer : work.a.mulik@gmail.com
+ Portability : portable
+
+ @SDP.HashMap.Strict@ provides 'HashMap' - strict unordered associative array
+ with 'Hashable' keys.
+-}
+module SDP.HashMap.Strict
+(
+ -- * Exports
+ module SDP.Hashable,
+ module SDP.Linear,
+ module SDP.Map,
+
+ -- * Hash map
+ HashMap, SHashMap
+)
+where
+
+import Prelude ()
+import SDP.SafePrelude
+import SDP.Hashable
+import SDP.Linear
+import SDP.Map
+
+import qualified Data.HashMap.Strict as H
+import Data.HashMap.Strict ( HashMap )
+
+import Data.Maybe
+
+import Control.Exception.SDP
+
+default ()
+
+--------------------------------------------------------------------------------
+
+-- | 'HashMap' alias, may reduce ambiguity.
+type SHashMap = HashMap
+
+--------------------------------------------------------------------------------
+
+instance Nullable (HashMap k e)
+ where
+ isNull = null
+ lzero = H.empty
+
+instance (Index k) => Estimate (HashMap k e)
+ where
+ (<==>) = on (<=>) length
+ (.<=.) = on (<=) length
+ (.>=.) = on (>=) length
+ (.>.) = on (>) length
+ (.<.) = on (<) length
+
+ (<.=>) = (<=>) . length
+ (.>=) = (>=) . length
+ (.<=) = (<=) . length
+ (.>) = (>) . length
+ (.<) = (<) . length
+
+instance (Eq k, Hashable k) => Map (HashMap k e) k e
+ where
+ toMap' = const toMap
+ toMap = H.fromList
+ assocs = H.toList
+
+ kfoldl = H.foldlWithKey' . flip
+ kfoldr = H.foldrWithKey
+
+ filter' = H.filterWithKey
+ member' = H.member
+ insert' = H.insert
+ delete' = H.delete
+
+ -- | Throws 'IndexException' instead 'error' call.
+ (!) = fromMaybe (undEx "(!) {HashMap k e}") ... (!?)
+ (//) = toMap ... (++) . assocs
+ (!?) = flip H.lookup
+ keys = H.keys
+
+--------------------------------------------------------------------------------
+
+undEx :: String -> a
+undEx = throw . UndefinedValue . showString "in SDP.HashMap.Strict."
+
diff --git a/src/SDP/HashSet.hs b/src/SDP/HashSet.hs
new file mode 100644
index 0000000..9b8da9c
--- /dev/null
+++ b/src/SDP/HashSet.hs
@@ -0,0 +1,95 @@
+{-# LANGUAGE Safe, MultiParamTypeClasses, FlexibleInstances #-}
+
+{- |
+ Module : SDP.HashSet
+ Copyright : (c) Andrey Mulik 2020
+ License : BSD-style
+ Maintainer : work.a.mulik@gmail.com
+ Portability : portable
+
+ @SDP.HashSet@ provides 'HashSet' - unordered set with 'Hashable' keys.
+-}
+module SDP.HashSet
+(
+ -- * Exports
+ module SDP.Hashable,
+ module SDP.Linear,
+ module SDP.Set,
+
+ -- * Hash set
+ HashSet
+)
+where
+
+import Prelude ()
+import SDP.SafePrelude
+import SDP.Hashable
+import SDP.Linear
+import SDP.Set
+
+import qualified Data.HashSet as H
+import Data.HashSet ( HashSet )
+
+default ()
+
+--------------------------------------------------------------------------------
+
+{- Nullable, Estimate and Bordered instances. -}
+
+instance Nullable (HashSet e)
+ where
+ isNull = null
+ lzero = H.empty
+
+instance Estimate (HashSet e)
+ where
+ (<==>) = on (<=>) sizeOf
+ (.<=.) = on (<=) sizeOf
+ (.>=.) = on (>=) sizeOf
+ (.>.) = on (>) sizeOf
+ (.<.) = on (<) sizeOf
+
+ (<.=>) = (<=>) . sizeOf
+ (.>=) = (>=) . sizeOf
+ (.<=) = (<=) . sizeOf
+ (.>) = (>) . sizeOf
+ (.<) = (<) . sizeOf
+
+instance Bordered (HashSet e) Int
+ where
+ sizeOf = length
+
+ upper xs = length xs - 1
+ lower _ = 0
+
+--------------------------------------------------------------------------------
+
+instance (Eq e, Hashable e) => Set (HashSet e) e
+ where
+ set = id -- always correct
+ member = H.member
+ insert = H.insert
+ delete = H.delete
+
+ unions = foldr1 (\/)
+ symdiffs = foldr1 (\^/)
+ differences = foldr1 (/\)
+ intersections = foldr1 (\\)
+
+ (/\) = H.intersection
+ (\\) = H.difference
+ (\/) = H.union
+
+ xs \^/ ys = (xs \/ ys) \\ (xs /\ ys)
+ xs \+/ ys = isNull (ys \\ xs)
+ xs /?\ ys = isNull (xs /\ ys)
+ xs \?/ ys = not (xs /?\ ys)
+
+ lookupLT e = lookupLT e . H.toList
+ lookupGT e = lookupGT e . H.toList
+ lookupLE e = lookupLE e . H.toList
+ lookupGE e = lookupGE e . H.toList
+
+
+
+