summaryrefslogtreecommitdiff
path: root/psq-wrap
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2019-09-28 13:43:29 -0400
committerJoe Crayne <joe@jerkface.net>2020-01-01 19:27:53 -0500
commit11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch)
tree5716463275c2d3e902889db619908ded2a73971c /psq-wrap
parentadd2c76bced51fde5e9917e7449ef52be70faf87 (diff)
Factor out some new libraries
word64-map: Data.Word64Map network-addr: Network.Address tox-crypto: Crypto.Tox lifted-concurrent: Control.Concurrent.Lifted.Instrument Control.Concurrent.Async.Lifted.Instrument psq-wrap: Data.Wrapper.PSQInt Data.Wrapper.PSQ minmax-psq: Data.MinMaxPSQ tasks: Control.Concurrent.Tasks kad: Network.Kademlia Network.Kademlia.Bootstrap Network.Kademlia.Routing Network.Kademlia.CommonAPI Network.Kademlia.Persistence Network.Kademlia.Search
Diffstat (limited to 'psq-wrap')
-rw-r--r--psq-wrap/CHANGELOG.md5
-rw-r--r--psq-wrap/LICENSE30
-rw-r--r--psq-wrap/Setup.hs2
-rw-r--r--psq-wrap/psq-wrap.cabal30
-rw-r--r--psq-wrap/src/Data/Wrapper/PSQ.hs91
-rw-r--r--psq-wrap/src/Data/Wrapper/PSQInt.hs53
6 files changed, 211 insertions, 0 deletions
diff --git a/psq-wrap/CHANGELOG.md b/psq-wrap/CHANGELOG.md
new file mode 100644
index 00000000..a2e49e7a
--- /dev/null
+++ b/psq-wrap/CHANGELOG.md
@@ -0,0 +1,5 @@
1# Revision history for psq-wrap
2
3## 0.1.0.0 -- YYYY-mm-dd
4
5* First version. Released on an unsuspecting world.
diff --git a/psq-wrap/LICENSE b/psq-wrap/LICENSE
new file mode 100644
index 00000000..e8eaef49
--- /dev/null
+++ b/psq-wrap/LICENSE
@@ -0,0 +1,30 @@
1Copyright (c) 2019, James Crayne
2
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15
16 * Neither the name of James Crayne nor the names of other
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
19
20THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/psq-wrap/Setup.hs b/psq-wrap/Setup.hs
new file mode 100644
index 00000000..9a994af6
--- /dev/null
+++ b/psq-wrap/Setup.hs
@@ -0,0 +1,2 @@
1import Distribution.Simple
2main = defaultMain
diff --git a/psq-wrap/psq-wrap.cabal b/psq-wrap/psq-wrap.cabal
new file mode 100644
index 00000000..77a7901b
--- /dev/null
+++ b/psq-wrap/psq-wrap.cabal
@@ -0,0 +1,30 @@
1-- Initial psq-wrap.cabal generated by cabal init. For further
2-- documentation, see http://haskell.org/cabal/users-guide/
3
4name: psq-wrap
5version: 0.1.0.0
6-- synopsis:
7-- description:
8license: BSD3
9license-file: LICENSE
10author: James Crayne
11maintainer: jim.crayne@gmail.com
12-- copyright:
13-- category:
14build-type: Simple
15extra-source-files: CHANGELOG.md
16cabal-version: >=1.10
17
18library
19 exposed-modules:
20 Data.Wrapper.PSQInt
21 , Data.Wrapper.PSQ
22 -- other-modules:
23 other-extensions: PatternSynonyms, CPP, ConstraintKinds
24 build-depends:
25 base
26 , time
27 , hashable
28 , psqueues
29 hs-source-dirs: src
30 default-language: Haskell2010
diff --git a/psq-wrap/src/Data/Wrapper/PSQ.hs b/psq-wrap/src/Data/Wrapper/PSQ.hs
new file mode 100644
index 00000000..4fdeec67
--- /dev/null
+++ b/psq-wrap/src/Data/Wrapper/PSQ.hs
@@ -0,0 +1,91 @@
1{-# LANGUAGE PatternSynonyms #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE ConstraintKinds #-}
4module Data.Wrapper.PSQ
5#if 0
6 ( module Data.Wrapper.PSQ , module Data.PSQueue ) where
7
8import Data.PSQueue hiding (foldr, foldl)
9import qualified Data.PSQueue as PSQueue
10
11type PSQKey k = (Ord k)
12
13-- | Wrapper over PSQueue-style foldr to implement a psqueues-style interface.
14fold' :: (Ord k, Ord p) => (k -> p -> () -> a -> a) -> a -> PSQ k p -> a
15fold' f a q = PSQueue.foldr f' a q
16 where
17 f' (k :-> prio) x = f k prio () x
18
19#else
20 ( module Data.Wrapper.PSQ , module HashPSQ ) where
21
22-- import Data.OrdPSQ as OrdPSQ hiding (insert, map, singleton, minView)
23-- import qualified Data.OrdPSQ as OrdPSQ
24
25import Data.Hashable
26import qualified Data.HashPSQ as Q
27 ;import Data.HashPSQ as HashPSQ hiding (insert, map, minView,
28 singleton)
29import Data.Time.Clock.POSIX (POSIXTime)
30
31-- type PSQ' k p v = HashPSQ k p v
32type PSQ' = HashPSQ
33type PSQ k p = PSQ' k p ()
34
35type Binding' k p v = (k,p,v)
36type Binding k p = Binding' k p ()
37
38type PSQKey k = (Hashable k, Ord k)
39
40pattern (:->) :: k -> p -> Binding k p
41pattern k :-> p <- (k,p,_) where k :-> p = (k,p,())
42
43-- I tried defining (::->) :: (k,v) -> p -> Binding' k p v
44-- but no luck...
45pattern Binding :: k -> v -> p -> Binding' k p v
46pattern Binding k v p <- (k,p,v) where Binding k v p = (k,p,v)
47
48key :: (k,p,v) -> k
49key (k,p,v) = k
50{-# INLINE key #-}
51
52prio :: (k,p,v) -> p
53prio (k,p,v) = p
54{-# INLINE prio #-}
55
56insert :: (PSQKey k, Ord p) => k -> p -> PSQ k p -> PSQ k p
57insert k p q = Q.insert k p () q
58{-# INLINE insert #-}
59
60insert' :: (PSQKey k, Ord p) => k -> v -> p -> PSQ' k p v -> PSQ' k p v
61insert' k v p q = Q.insert k p v q
62{-# INLINE insert' #-}
63
64insertWith :: (PSQKey k, Ord p) => (p -> p -> p) -> k -> p -> PSQ k p -> PSQ k p
65insertWith f k p0 q = snd $ Q.alter f' k q
66 where
67 f' (Just (p,())) = ((),Just (f p0 p, ()))
68 f' Nothing = ((),Just (p0,()))
69{-# INLINE insertWith #-}
70
71singleton :: (PSQKey k, Ord p) => k -> p -> PSQ k p
72singleton k p = Q.singleton k p ()
73{-# INLINE singleton #-}
74
75singleton' :: (PSQKey k, Ord p) => k -> v -> p -> PSQ' k p v
76singleton' k v p = Q.singleton k p v
77{-# INLINE singleton' #-}
78
79
80minView :: (PSQKey k, Ord p) => PSQ' k p v -> Maybe (Binding' k p v, PSQ' k p v)
81minView q = fmap (\(k,p,v,q') -> (Binding k v p, q')) $ Q.minView q
82{-# INLINE minView #-}
83
84
85-- | Utility to convert a 'POSIXTime' delta into microseconds suitable for
86-- passing to 'threadDelay'.
87toMicroseconds :: POSIXTime -> Int
88toMicroseconds = round . (* 1000) . (* 1000)
89
90
91#endif
diff --git a/psq-wrap/src/Data/Wrapper/PSQInt.hs b/psq-wrap/src/Data/Wrapper/PSQInt.hs
new file mode 100644
index 00000000..5badb8b2
--- /dev/null
+++ b/psq-wrap/src/Data/Wrapper/PSQInt.hs
@@ -0,0 +1,53 @@
1{-# LANGUAGE PatternSynonyms #-}
2{-# LANGUAGE CPP #-}
3{-# LANGUAGE ConstraintKinds #-}
4module Data.Wrapper.PSQInt
5#if 0
6 ( module Data.Wrapper.PSQInt , module Data.PSQueue ) where
7
8import Data.PSQueue hiding (foldr, foldl, PSQ)
9import qualified Data.PSQueue as PSQueue
10
11type PSQ p = PSQueue.PSQ Int p
12
13-- | Wrapper over PSQueue-style foldr to implement a psqueues-style interface.
14fold' :: (Ord p) => (Int -> p -> () -> a -> a) -> a -> PSQ p -> a
15fold' f a q = PSQueue.foldr f' a q
16 where
17 f' (k :-> prio) x = f k prio () x
18
19#else
20 ( module Data.Wrapper.PSQInt
21 , module IntPSQ
22 , module Data.Wrapper.PSQ
23 ) where
24
25import Data.Wrapper.PSQ (Binding, pattern (:->), key, prio, toMicroseconds)
26
27import Data.IntPSQ as IntPSQ hiding (insert, map, singleton, minView)
28import qualified Data.IntPSQ as Q
29
30type PSQ p = IntPSQ p ()
31
32type PSQKey = ()
33
34insert :: (Ord p) => Int -> p -> PSQ p -> PSQ p
35insert k p q = Q.insert k p () q
36{-# INLINE insert #-}
37
38insertWith :: (Ord p) => (p -> p -> p) -> Int -> p -> PSQ p -> PSQ p
39insertWith f k p0 q = snd $ Q.alter f' k q
40 where
41 f' (Just (p,())) = ((),Just (f p0 p, ()))
42 f' Nothing = ((),Nothing)
43{-# INLINE insertWith #-}
44
45singleton :: (Ord p) => Int -> p -> PSQ p
46singleton k p = Q.singleton k p ()
47{-# INLINE singleton #-}
48
49minView :: (Ord p) => PSQ p -> Maybe (Binding Int p, PSQ p)
50minView q = fmap (\(k,p,(),q') -> (k :-> p, q')) $ Q.minView q
51{-# INLINE minView #-}
52
53#endif