primitive-slice (empty) → 0.1.0.0
raw patch · 5 files changed
+144/−0 lines, 5 filesdep +basedep +primitivedep +primitive-unliftedsetup-changed
Dependencies added: base, primitive, primitive-unlifted
Files
- CHANGELOG.md +5/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- primitive-slice.cabal +28/−0
- src/Data/Primitive/Slice.hs +79/−0
+ CHANGELOG.md view
@@ -0,0 +1,5 @@+# Revision history for primitive-sliced++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2019, Andrew Martin++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 Andrew Martin 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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ primitive-slice.cabal view
@@ -0,0 +1,28 @@+cabal-version: 2.2+name: primitive-slice+version: 0.1.0.0+synopsis: Slices of primitive arrays+description:+ The `vector` library provides types for slicing into many+ types of arrays. However, it does not include slices of+ `SmallArray` or `UnliftedArray`. This library provides types+ for working with such slices.+homepage: https://github.com/haskell-primitive/primitive-slice+bug-reports: https://github.com/haskell-primitive/primitive-slice/issues+license: BSD-3-Clause+license-file: LICENSE+author: Andrew Martin+maintainer: andrew.thaddeus@gmail.com+copyright: 2019 Andrew Martin+category: Data+extra-source-files: CHANGELOG.md++library+ exposed-modules: Data.Primitive.Slice+ build-depends:+ , base >=4.12.0.0 && <5+ , primitive >=0.7 && <0.8+ , primitive-unlifted >=0.1 && <0.2+ hs-source-dirs: src+ ghc-options: -O2 -Wall+ default-language: Haskell2010
+ src/Data/Primitive/Slice.hs view
@@ -0,0 +1,79 @@+{-# language BangPatterns #-}+{-# language DuplicateRecordFields #-}+{-# language RankNTypes #-}+{-# language TypeFamilies #-}++module Data.Primitive.Slice+ ( -- * Types+ UnliftedVector(..)+ , MutableUnliftedVector(..)+ , SmallVector(..)+ , SmallMutableVector(..)+ -- * Conversion+ , unslicedUnliftedVector+ , unslicedSmallVector+ ) where++import Prelude hiding (length)++import Data.Primitive (SmallArray,SmallMutableArray)+import Data.Primitive.Unlifted.Array (UnliftedArray,MutableUnliftedArray)+import Data.Primitive.Unlifted.Class (PrimUnlifted)+import GHC.Exts (IsList)++import qualified GHC.Exts as Exts+import qualified Data.Primitive as PM+import qualified Data.Primitive.Unlifted.Array as PM++data UnliftedVector a = UnliftedVector+ { array :: !(UnliftedArray a)+ , offset :: !Int+ , length :: !Int+ }++instance PrimUnlifted a => IsList (UnliftedVector a) where+ type Item (UnliftedVector a) = a+ fromList = unslicedUnliftedVector . Exts.fromList+ fromListN n = unslicedUnliftedVector . Exts.fromListN n+ toList = foldrUnliftedVector (:) []++data MutableUnliftedVector s a = MutableUnliftedVector+ { array :: !(MutableUnliftedArray s a)+ , offset :: !Int+ , length :: !Int+ }++data SmallVector a = SmallVector+ { array :: !(SmallArray a)+ , offset :: !Int+ , length :: !Int+ }++data SmallMutableVector s a = SmallMutableVector+ { array :: !(SmallMutableArray s a)+ , offset :: !Int+ , length :: !Int+ }++unslicedUnliftedVector :: UnliftedArray a -> UnliftedVector a+unslicedUnliftedVector x = UnliftedVector+ { array = x+ , offset = 0+ , length = PM.sizeofUnliftedArray x+ }++unslicedSmallVector :: SmallArray a -> SmallVector a+unslicedSmallVector x = SmallVector+ { array = x+ , offset = 0+ , length = PM.sizeofSmallArray x+ }++foldrUnliftedVector :: forall a b. PrimUnlifted a => (a -> b -> b) -> b -> UnliftedVector a -> b+{-# INLINE foldrUnliftedVector #-}+foldrUnliftedVector f z (UnliftedVector arr off0 len) = go off0+ where+ !end = len + off0+ go !i+ | end > i = f (PM.indexUnliftedArray arr i) (go (i+1))+ | otherwise = z