私たちは自明toEnum
とfromEnum
バックInt
に列挙型をマッピングすることができて私には理にかなっている(Enum a => UArray a
を持っている方法を探しています«インスタンス(列挙型aは、有界)=> IArray UArray»
{-# LANGUAGE MagicHash, UnboxedTuples #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleInstances #-}
module UArrays where
import Data.Array.Base
import Data.Array.ST
import Data.Array.Unboxed
import GHC.Base -- (Int(I#), Int#(..))
import GHC.Prim -- (indexIntArray#, readIntArray#, writeIntArray#)
import GHC.ST (ST(..), runST)
import Unsafe.Coerce
instance (Enum a, Bounded a) => IArray UArray a where
{-# INLINE bounds #-}
bounds (UArray l u _ _) = (l, u)
{-# INLINE numElements #-}
numElements (UArray _ _ n _) = n
{-# INLINE unsafeArray #-}
unsafeArray lu ies = runST (unsafeArrayUArray lu ies minBound)
{-# INLINE unsafeAt #-}
unsafeAt (UArray _ _ _ arr#) (I# i#) =
I# $ fromEnum (indexIntArray# arr# i#)
{-# INLINE unsafeReplace #-}
unsafeReplace arr ies = runST (unsafeReplaceUArray arr ies)
{-# INLINE unsafeAccum #-}
unsafeAccum f arr ies = runST (unsafeAccumUArray f arr ies)
{-# INLINE unsafeAccumArray #-}
unsafeAccumArray f initialValue lu ies =
runST (unsafeAccumArrayUArray f initialValue lu ies)
-- data STUArray s i e = STUArray !i !i !Int (GHC.Prim.MutableByteArray# s)
instance (Enum a, Bounded a) => MArray (STUArray s) a (ST s) where
{-# INLINE getBounds #-}
getBounds (STUArray l u _ _) = return (l, u)
{-# INLINE getNumElements #-}
getNumElements (STUArray _ _ n _) = return n
{-# INLINE unsafeNewArray_ #-}
unsafeNewArray_ (l, u) = unsafeNewArraySTUArray_ (l, u) wORD_SCALE
{-# INLINE newArray_ #-}
newArray_ arrBounds = newArray arrBounds minBound
{-# INLINE unsafeRead #-}
-- unsafeRead :: GHC.Arr.Ix i => a i e -> Int -> m e
unsafeRead (STUArray _ _ _ marr#) (I# i#) =
ST $ \ s1# ->
case readIntArray# marr# i# s1# of
(# s2#, e# #) -> (# s2#, I# (toEnum e#) #)
{-# INLINE unsafeWrite #-}
-- unsafeWrite :: GHC.Arr.Ix i => a i e -> Int -> e -> m()
unsafeWrite (STUArray _ _ _ marr#) (I# i#) (I# e#) =
ST $ \ s1# ->
case writeIntArray# marr# (unsafeCoerce i#) (I# $ fromEnum e#) s1# of
s2# -> (# s2#,() #)
しかし、もちろん、それはコンパイルされません:
)は、これまでのところ私はtoEnum
SとあちこちでfromEnum
秒数をData.Array.BaseからUArray Int
のコードを盗み、密輸しようとしました
[2 of 4] Compiling UArrays (UArrays.hs, interpreted)
UArrays.hs:27:14:
Couldn't match expected type `Int#' with actual type `Int'
In the return type of a call of `fromEnum'
In the second argument of `($)', namely
`fromEnum (indexIntArray# arr# i#)'
In the expression: I# $ fromEnum (indexIntArray# arr# i#)
UArrays.hs:52:45:
Couldn't match expected type `Int' with actual type `Int#'
In the first argument of `toEnum', namely `e#'
In the first argument of `I#', namely `(toEnum e#)'
In the expression: I# (toEnum e#)
UArrays.hs:57:57:
Couldn't match expected type `Int#' with actual type `Int'
In the return type of a call of `fromEnum'
In the second argument of `($)', namely `fromEnum e#'
In the third argument of `writeIntArray#', namely
`(I# $ fromEnum e#)'
Failed, modules loaded: Utils.
もGHC.*
には魔法unboxInt :: Int -> Int#
が存在しない、とI#
上のパターンマッチングはInt
を生じないが、代わりにInt#
、まだ何とかUArray Int
が存在し、Int#
Sに作用します。
また、making an UArray for newtypesについて投稿を見つけましたが、unsafeCoerce
に依存しているため適用されていないようです。私はそれを試みたが、それはすべてのコンストラクタがBlue
だった面白いlistArray (0, 54) $ cycle [Red, Yellow, Green]
を作った。
間違っていますか?
更新:
それが機能するようになりました、ここではソースコードは次のとおりです。
- UArrays.hs:http://hpaste.org/56728
- BenchmarkUArray.hs:http://hpaste.org/56729
- のMakefile:http://hpaste.org/56727
私はこれはとても良いアイデアだとは思わない。 'fromEnum'と' toEnum'はすべてのインスタンスでは必要ではありません。そのため、いくつかの型ではボックス化された配列よりも遅くなることがあります。 – leftaroundabout
@leftaround私の(愚かで人工的な)ケースでは、 'data Color = Red | 100000の値の配列を格納して処理するために、時間と空間の両方の要件を70%削減しました。イエロー|グリーン|ブルー ' –