mirror of
https://github.com/ilyakooo0/MemoTrie-instances.git
synced 2024-10-05 20:27:16 +03:00
Initial commit
This commit is contained in:
commit
59a52b2e24
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 iko
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
30
MemoTrie-instances.cabal
Normal file
30
MemoTrie-instances.cabal
Normal file
@ -0,0 +1,30 @@
|
||||
cabal-version: 1.12
|
||||
|
||||
-- This file has been generated from package.yaml by hpack version 0.34.5.
|
||||
--
|
||||
-- see: https://github.com/sol/hpack
|
||||
|
||||
name: MemoTrie-instances
|
||||
version: 0.0.0.0
|
||||
author: Ilya Kostyuchenko
|
||||
maintainer: Ilya Kostyuchenko
|
||||
license: MIT
|
||||
build-type: Simple
|
||||
|
||||
library
|
||||
exposed-modules:
|
||||
Data.MemoTrie.Instances.Text
|
||||
other-modules:
|
||||
Paths_MemoTrie_instances
|
||||
hs-source-dirs:
|
||||
src
|
||||
default-extensions:
|
||||
TypeFamilies
|
||||
TypeOperators
|
||||
ghc-options: -Weverything -Wno-prepositive-qualified-module -Wno-missing-safe-haskell-mode -Wno-missing-import-lists -Wno-unsafe
|
||||
|
||||
build-depends:
|
||||
MemoTrie
|
||||
, base
|
||||
, text
|
||||
default-language: Haskell2010
|
5
README.md
Normal file
5
README.md
Normal file
@ -0,0 +1,5 @@
|
||||
# MemoTrie-instances
|
||||
|
||||
A package with instances I need for [`MemoTrie`](https://hackage.haskell.org/package/MemoTrie).
|
||||
|
||||
Feel free to add your own instances. It would be nice to have them all in one place.
|
22
package.yaml
Normal file
22
package.yaml
Normal file
@ -0,0 +1,22 @@
|
||||
name: MemoTrie-instances
|
||||
version: 0.0.0.0
|
||||
license: MIT
|
||||
author: Ilya Kostyuchenko
|
||||
|
||||
library:
|
||||
source-dirs: src
|
||||
dependencies:
|
||||
- base
|
||||
- text
|
||||
- MemoTrie
|
||||
|
||||
default-extensions:
|
||||
- TypeFamilies
|
||||
- TypeOperators
|
||||
|
||||
ghc-options: >
|
||||
-Weverything
|
||||
-Wno-prepositive-qualified-module
|
||||
-Wno-missing-safe-haskell-mode
|
||||
-Wno-missing-import-lists
|
||||
-Wno-unsafe
|
33
src/Data/MemoTrie/Instances/Text.hs
Normal file
33
src/Data/MemoTrie/Instances/Text.hs
Normal file
@ -0,0 +1,33 @@
|
||||
{-# OPTIONS_GHC -Wno-orphans #-}
|
||||
|
||||
module Data.MemoTrie.Instances.Text () where
|
||||
|
||||
import Data.Foldable (for_)
|
||||
import Data.MemoTrie
|
||||
import Data.Text.Array
|
||||
import qualified Data.Text.Internal as T
|
||||
import qualified Data.Text.Lazy as TL
|
||||
import Data.Word
|
||||
|
||||
instance HasTrie T.Text where
|
||||
newtype T.Text :->: b = TextTrie ((Int, [Word16]) :->: b)
|
||||
trie f = TextTrie $ trie $ f . textFromList
|
||||
untrie (TextTrie x) = untrie x . textToList
|
||||
enumerate (TextTrie x) = (\(a, b) -> (textFromList a, b)) <$> enumerate x
|
||||
|
||||
instance HasTrie TL.Text where
|
||||
newtype TL.Text :->: b = LazyTextTrie (T.Text :->: b)
|
||||
trie f = LazyTextTrie $ trie $ f . TL.fromStrict
|
||||
untrie (LazyTextTrie x) = untrie x . TL.toStrict
|
||||
enumerate (LazyTextTrie x) = (\(a, b) -> (TL.fromStrict a, b)) <$> enumerate x
|
||||
|
||||
textToList :: T.Text -> (Int, [Word16])
|
||||
textToList (T.Text arr off len) = (len, toList arr off len)
|
||||
|
||||
textFromList :: (Int, [Word16]) -> T.Text
|
||||
textFromList (len, ws) = T.text arr 0 len
|
||||
where
|
||||
arr = run $ do
|
||||
a <- new len
|
||||
for_ (zip [0 ..] ws) $ \(i, w) -> unsafeWrite a i w
|
||||
pure a
|
4
stack.yaml
Normal file
4
stack.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
resolver: lts-18.28
|
||||
|
||||
packages:
|
||||
- .
|
Loading…
Reference in New Issue
Block a user