Initial commit

This commit is contained in:
iko 2022-04-13 19:49:30 +03:00
commit 59a52b2e24
Signed by untrusted user: iko
GPG Key ID: 82C257048D1026F2
6 changed files with 115 additions and 0 deletions

21
LICENSE Normal file
View 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
View 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
View 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
View 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

View 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
View File

@ -0,0 +1,4 @@
resolver: lts-18.28
packages:
- .