diff options
author | James Crayne <jim.crayne@gmail.com> | 2019-09-28 13:43:29 -0400 |
---|---|---|
committer | Joe Crayne <joe@jerkface.net> | 2020-01-01 19:27:53 -0500 |
commit | 11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch) | |
tree | 5716463275c2d3e902889db619908ded2a73971c /word64-map | |
parent | add2c76bced51fde5e9917e7449ef52be70faf87 (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 'word64-map')
-rw-r--r-- | word64-map/CHANGELOG.md | 5 | ||||
-rw-r--r-- | word64-map/LICENSE | 30 | ||||
-rw-r--r-- | word64-map/Setup.hs | 2 | ||||
-rw-r--r-- | word64-map/src/Data/Word64Map.hs | 66 | ||||
-rw-r--r-- | word64-map/word64-map.cabal | 26 |
5 files changed, 129 insertions, 0 deletions
diff --git a/word64-map/CHANGELOG.md b/word64-map/CHANGELOG.md new file mode 100644 index 00000000..d325ac08 --- /dev/null +++ b/word64-map/CHANGELOG.md | |||
@@ -0,0 +1,5 @@ | |||
1 | # Revision history for word64-map | ||
2 | |||
3 | ## 0.1.0.0 -- YYYY-mm-dd | ||
4 | |||
5 | * First version. Released on an unsuspecting world. | ||
diff --git a/word64-map/LICENSE b/word64-map/LICENSE new file mode 100644 index 00000000..e8eaef49 --- /dev/null +++ b/word64-map/LICENSE | |||
@@ -0,0 +1,30 @@ | |||
1 | Copyright (c) 2019, James Crayne | ||
2 | |||
3 | All rights reserved. | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without | ||
6 | modification, 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 | |||
20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
diff --git a/word64-map/Setup.hs b/word64-map/Setup.hs new file mode 100644 index 00000000..9a994af6 --- /dev/null +++ b/word64-map/Setup.hs | |||
@@ -0,0 +1,2 @@ | |||
1 | import Distribution.Simple | ||
2 | main = defaultMain | ||
diff --git a/word64-map/src/Data/Word64Map.hs b/word64-map/src/Data/Word64Map.hs new file mode 100644 index 00000000..adc9c27e --- /dev/null +++ b/word64-map/src/Data/Word64Map.hs | |||
@@ -0,0 +1,66 @@ | |||
1 | {-# LANGUAGE RankNTypes #-} | ||
2 | {-# LANGUAGE ScopedTypeVariables #-} | ||
3 | {-# LANGUAGE UnboxedTuples #-} | ||
4 | module Data.Word64Map where | ||
5 | |||
6 | import Data.Bits | ||
7 | import qualified Data.IntMap as IntMap | ||
8 | ;import Data.IntMap (IntMap) | ||
9 | import Data.Monoid | ||
10 | import Data.Typeable | ||
11 | import Data.Word | ||
12 | |||
13 | -- | Since 'Int' may be 32 or 64 bits, this function is provided as a | ||
14 | -- convenience to test if an integral type, such as 'Data.Word.Word64', can be | ||
15 | -- safely transformed into an 'Int' for use with 'IntMap'. | ||
16 | -- | ||
17 | -- Returns 'True' if the proxied type can be losslessly converted to 'Int' using | ||
18 | -- 'fromIntegral'. | ||
19 | fitsInInt :: forall proxy word. (Bounded word, Integral word) => proxy word -> Bool | ||
20 | fitsInInt proxy = (original == casted) | ||
21 | where | ||
22 | original = div maxBound 2 :: word | ||
23 | casted = fromIntegral (fromIntegral original :: Int) :: word | ||
24 | |||
25 | newtype Word64Map a = Word64Map (IntMap (IntMap a)) | ||
26 | |||
27 | size :: Word64Map a -> Int | ||
28 | size (Word64Map m) = getSum $ foldMap (\n -> Sum (IntMap.size n)) m | ||
29 | |||
30 | empty :: Word64Map a | ||
31 | empty = Word64Map IntMap.empty | ||
32 | |||
33 | -- Warning: This function assumes an 'Int' is either 64 or 32 bits. | ||
34 | keyFrom64 :: Word64 -> (# Int,Int #) | ||
35 | keyFrom64 w8 = | ||
36 | if fitsInInt (Proxy :: Proxy Word64) | ||
37 | then (# fromIntegral w8 , 0 #) | ||
38 | else (# fromIntegral (w8 `shiftR` 32), fromIntegral w8 #) | ||
39 | {-# INLINE keyFrom64 #-} | ||
40 | |||
41 | lookup :: Word64 -> Word64Map b -> Maybe b | ||
42 | lookup w8 (Word64Map m) | (# hi,lo #) <- keyFrom64 w8 = do | ||
43 | m' <- IntMap.lookup hi m | ||
44 | IntMap.lookup lo m' | ||
45 | {-# INLINE lookup #-} | ||
46 | |||
47 | insert :: Word64 -> b -> Word64Map b -> Word64Map b | ||
48 | insert w8 b (Word64Map m) | (# hi,lo #) <- keyFrom64 w8 | ||
49 | = Word64Map $ IntMap.alter (Just . maybe (IntMap.singleton lo b) | ||
50 | (IntMap.insert lo b)) | ||
51 | hi | ||
52 | m | ||
53 | {-# INLINE insert #-} | ||
54 | |||
55 | delete :: Word64 -> Word64Map b -> Word64Map b | ||
56 | delete w8 (Word64Map m) | (# hi,lo #) <- keyFrom64 w8 | ||
57 | = Word64Map $ IntMap.alter (maybe Nothing | ||
58 | (\m' -> case IntMap.delete lo m' of | ||
59 | m'' | IntMap.null m'' -> Nothing | ||
60 | m'' -> Just m'')) | ||
61 | hi | ||
62 | m | ||
63 | {-# INLINE delete #-} | ||
64 | |||
65 | |||
66 | |||
diff --git a/word64-map/word64-map.cabal b/word64-map/word64-map.cabal new file mode 100644 index 00000000..938d2e22 --- /dev/null +++ b/word64-map/word64-map.cabal | |||
@@ -0,0 +1,26 @@ | |||
1 | -- Initial word64-map.cabal generated by cabal init. For further | ||
2 | -- documentation, see http://haskell.org/cabal/users-guide/ | ||
3 | |||
4 | name: word64-map | ||
5 | version: 0.1.0.0 | ||
6 | -- synopsis: | ||
7 | -- description: | ||
8 | license: BSD3 | ||
9 | license-file: LICENSE | ||
10 | author: James Crayne | ||
11 | maintainer: jim.crayne@gmail.com | ||
12 | -- copyright: | ||
13 | -- category: | ||
14 | build-type: Simple | ||
15 | extra-source-files: CHANGELOG.md | ||
16 | cabal-version: >=1.10 | ||
17 | |||
18 | library | ||
19 | exposed-modules: Data.Word64Map | ||
20 | -- other-modules: | ||
21 | other-extensions: RankNTypes, ScopedTypeVariables, UnboxedTuples | ||
22 | build-depends: | ||
23 | base | ||
24 | , containers | ||
25 | hs-source-dirs: src | ||
26 | default-language: Haskell2010 | ||