- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有一个简单的数据类型,例如:
data Cell = Open | Blocked
UArray Int Cell
.是否有捷径可寻?我可以以某种方式重用
UArray Int Bool
的定义吗? ?
最佳答案
This answer解释了为什么 Vectors 比 Arrays 更好,所以我将为您提供未装箱向量的答案。
我确实尝试导出 MArray
和 IArray
Cell
的实例基于Bool
实例,但 Bool
实例相当复杂;它至少和手动导出 Unbox
一样难看。向量的例子。与向量不同,您也不能只推导出 Storable
并使用 Storable
数组:您仍然需要 Marray
和 IArray
实例。似乎还没有一个很好的 TH 解决方案,因此出于这些原因,您最好也使用向量。
有几种方法可以做到这一点,有些方法比其他方法更痛苦。
Unbox
短得多实例-XTemplateHaskell
{-# LANGUAGE TemplateHaskell, MultiParamTypeClasses, TypeFamilies #-}
import Data.Vector.Unboxed
import Data.Vector.Unboxed.Deriving
import qualified Data.Vector.Generic
import qualified Data.Vector.Generic.Mutable
data Cell = Open | Blocked deriving (Show)
derivingUnbox "Cell"
[t| Cell -> Bool |]
[| \ x -> case x of
Open -> True
Blocked -> False |]
[| \ x -> case x of
True -> Open
False -> Blocked |]
main = print $ show $ singleton Open
Unbox
, M.MVector
, 和 V.Vector
实例,加上两个数据实例{-# LANGUAGE TypeFamilies, MultiParamTypeClasses #-}
import qualified Data.Vector.Generic as V
import qualified Data.Vector.Generic.Mutable as M
import qualified Data.Vector.Unboxed as U
import Control.Monad
data Cell = Open | Blocked deriving (Show)
data instance U.MVector s Cell = MV_Cell (U.MVector s Cell)
data instance U.Vector Cell = V_Cell (U.Vector Cell)
instance U.Unbox Cell
{- purloined and tweaked from code in `vector`
package that defines types as unboxed -}
instance M.MVector U.MVector Cell where
{-# INLINE basicLength #-}
{-# INLINE basicUnsafeSlice #-}
{-# INLINE basicOverlaps #-}
{-# INLINE basicUnsafeNew #-}
{-# INLINE basicUnsafeReplicate #-}
{-# INLINE basicUnsafeRead #-}
{-# INLINE basicUnsafeWrite #-}
{-# INLINE basicClear #-}
{-# INLINE basicSet #-}
{-# INLINE basicUnsafeCopy #-}
{-# INLINE basicUnsafeGrow #-}
basicLength (MV_Cell v) = M.basicLength v
basicUnsafeSlice i n (MV_Cell v) = MV_Cell $ M.basicUnsafeSlice i n v
basicOverlaps (MV_Cell v1) (MV_Cell v2) = M.basicOverlaps v1 v2
basicUnsafeNew n = MV_Cell `liftM` M.basicUnsafeNew n
basicUnsafeReplicate n x = MV_Cell `liftM` M.basicUnsafeReplicate n x
basicUnsafeRead (MV_Cell v) i = M.basicUnsafeRead v i
basicUnsafeWrite (MV_Cell v) i x = M.basicUnsafeWrite v i x
basicClear (MV_Cell v) = M.basicClear v
basicSet (MV_Cell v) x = M.basicSet v x
basicUnsafeCopy (MV_Cell v1) (MV_Cell v2) = M.basicUnsafeCopy v1 v2
basicUnsafeMove (MV_Cell v1) (MV_Cell v2) = M.basicUnsafeMove v1 v2
basicUnsafeGrow (MV_Cell v) n = MV_Cell `liftM` M.basicUnsafeGrow v n
instance V.Vector U.Vector Cell where
{-# INLINE basicUnsafeFreeze #-}
{-# INLINE basicUnsafeThaw #-}
{-# INLINE basicLength #-}
{-# INLINE basicUnsafeSlice #-}
{-# INLINE basicUnsafeIndexM #-}
{-# INLINE elemseq #-}
basicUnsafeFreeze (MV_Cell v) = V_Cell `liftM` V.basicUnsafeFreeze v
basicUnsafeThaw (V_Cell v) = MV_Cell `liftM` V.basicUnsafeThaw v
basicLength (V_Cell v) = V.basicLength v
basicUnsafeSlice i n (V_Cell v) = V_Cell $ V.basicUnsafeSlice i n v
basicUnsafeIndexM (V_Cell v) i = V.basicUnsafeIndexM v i
basicUnsafeCopy (MV_Cell mv) (V_Cell v) = V.basicUnsafeCopy mv v
elemseq _ = seq
main = print $ show $ U.singleton Open
Storable
实例和使用Data.Vector.Storable
反而。Storable
的 SO 问题时,向量,有人会不可避免地问你为什么不使用 Unboxed
向量,尽管似乎没有人知道为什么 Unboxed
载体更好。{-# LANGUAGE ScopedTypeVariables #-}
import Control.Monad
import Data.Vector.Storable
import Foreign.Storable
import GHC.Ptr
import GHC.Int
-- defined in HsBaseConfig.h as
-- #define HTYPE_INT Int32
type HTYPE_INT = Int32
data Cell = Open | Blocked deriving (Show)
instance Storable Cell where
sizeOf _ = sizeOf (undefined::HTYPE_INT)
alignment _ = alignment (undefined::HTYPE_INT)
peekElemOff p i = liftM (\x -> case x of
(0::HTYPE_INT) -> Blocked
otherwise -> Open) $ peekElemOff (castPtr p) i
pokeElemOff p i x = pokeElemOff (castPtr p) i $ case x of
Blocked -> 0
Open -> (1 :: HTYPE_INT)
main = print $ show $ singleton Open
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
import Data.Vector.Storable as S
import Foreign.Storable
newtype Cell = IsOpen Bool deriving (Show)
main = print $ show $ S.singleton (Foo True)
newtype
拆箱实例newtype
,但为了完整起见,我将其包括在内。Unboxed
仇恨者的向量{-# LANGUAGE GeneralizedNewtypeDeriving,
StandaloneDeriving,
MultiParamTypeClasses #-}
import Data.Vector.Generic as V
import Data.Vector.Generic.Mutable as M
import Data.Vector.Unboxed as U
newtype Cell = IsOpen Bool deriving (Unbox, Show)
deriving instance V.Vector U.Vector Cell
deriving instance M.MVector U.MVector Cell
main = print $ show $ U.singleton (IsOpen True)
关于arrays - 创建 UArray 的自定义实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21896924/
假设我有一个简单的数据类型,例如: data Cell = Open | Blocked 我想使用 UArray Int Cell .是否有捷径可寻?我可以以某种方式重用 UArray Int Boo
我正在尝试为我的数据类型实现 Show 类型类 data Heap a = Heap {invariant :: a -> a -> Ordering ,arr
我正在寻找一种方法来拥有 Enum a => UArray a(这对我来说很有意义,因为我们可以简单地将枚举映射到 Int 并通过 toEnum 和 fromEnum) 到目前为止,我试图从 Data
我正在尝试使用 UArray Int (Complex Double)。此签名很好,但是当我尝试使用 (!) 访问特定索引时,我收到错误消息。这是我尝试使用的代码: test :: UArray In
我正在尝试使用 UArray Int (Complex Double)。此签名很好,但是当我尝试使用 (!) 访问特定索引时,我收到错误消息。这是我尝试使用的代码: test :: UArray In
我正在使用 Network.Pcap 进行一些网络捕获( pcap ) 并计划使用 Net.PacketParsing 进行一些检查( network-house )。为此,看起来我必须将我的数据包解
我是一名优秀的程序员,十分优秀!