Control.Monad.Memo.Array (original) (raw)
Contents
Description
ArrayCache - mutable-array-based ([IO](/packages/archive/base/4.6.0.1/doc/html/System-IO.html#t:IO)
and [ST](/packages/archive/base/4.6.0.1/doc/html/Control-Monad-ST-Safe.html#t:ST)
hosted) [MonadCache](Control-Monad-Memo-Class.html#t:MonadCache)
Very fast memoization cache. Unfortunatelly it cannot suit every case (see limitations), but if you can use it, please do: it is generally an order of magnitude faster than [Map](Data.html#t:Map)
-based [Memo](Control-Monad-Trans-Memo-Map.html#t:Memo)
, especially unboxed version - try to use it whenever you can.
Limitations: Since [MArray](/packages/archive/array/0.4.0.1/doc/html/Data-Array-Base.html#t:MArray)
is used as [MonadCache](Control-Monad-Memo-Class.html#t:MonadCache)
the key range must be known beforehand and the array is allocated before the first call. It is therefore most suitable for the cases when the distribution of possible key values is within reasonable range and is rather dense (the best case: all values withing some range will be used). If this is the case then [MArray](/packages/archive/array/0.4.0.1/doc/html/Data-Array-MArray.html#t:MArray)
has O(1) for both lookup and update operations. In addition unboxed [UArrayCache](Control-Monad-Memo-Array.html#t:UArrayCache)
can only store unboxed types (but it does it very efficiently).
Synopsis
- type family Array m :: * -> * -> *
- type ArrayCache k e m = Cache (Array m) k e m
- class MaybeLike e v => ArrayMemo v e | v -> e
- evalArrayMemo :: (Ix k, MArray (Array m) e m, ArrayMemo v e) => ArrayCache k e m a -> (k, k) -> m a
- runArrayMemo :: (Ix k, MArray (Array m) e m, ArrayMemo v e) => ArrayCache k e m a -> (k, k) -> m (a, Array m k e)
- type family UArray m :: * -> * -> *
- type UArrayCache k e m = Cache (UArray m) k e m
- class MaybeLike e v => UArrayMemo v e | v -> e
- evalUArrayMemo :: (Ix k, MArray (UArray m) e m, UArrayMemo v e) => UArrayCache k e m a -> (k, k) -> m a
- runUArrayMemo :: (Ix k, MArray (UArray m) e m, UArrayMemo v e) => UArrayCache k e m a -> (k, k) -> m (a, UArray m k e)
- newtype Container arr = Container {
- toArray :: arr
}
- toArray :: arr
- type Cache arr k e = ReaderCache (Container (arr k e))
- genericEvalArrayMemo :: (Ix k, MaybeLike e v, MArray arr e m) => Cache arr k e m a -> (k, k) -> m a
- genericRunArrayMemo :: (Ix k, MaybeLike e v, MArray arr e m) => Cache arr k e m a -> (k, k) -> m (a, arr k e)
ArrayCache for boxed types
type family Array m :: * -> * -> *Source
A family of boxed arrays
class MaybeLike e v => ArrayMemo v e | v -> eSource
This is just to be able to infer the type of the [ArrayCache](Control-Monad-Memo-Array.html#t:ArrayCache)
element
Type families could be used instead but due to the bug in 7.4.* we cannot use them here
evalArrayMemoSource
Evaluate computation using boxed array
Key range should cover all possible keys used in computation otherwise not in range error is generated by array
runArrayMemoSource
Arguments
:: (Ix k, MArray (Array m) e m, ArrayMemo v e) | |
---|---|
=> ArrayCache k e m a | memoized computation to be evaluated |
-> (k, k) | array key range |
-> m (a, Array m k e) | computation result and final array cache |
Evaluate computation and the final content of array cache using boxed array
Key range should cover all possible keys used in computation otherwise not in range error is generated by array
ArrayCache for unboxed types
type family UArray m :: * -> * -> *Source
A family of unboxed arrays
class MaybeLike e v => UArrayMemo v e | v -> eSource
This is just to be able to infer the type of the [UArrayCache](Control-Monad-Memo-Array.html#t:UArrayCache)
element
Type families could be used instead but due to the bug in 7.4.* we cannot use them here
evalUArrayMemoSource
Evaluate computation using unboxed array
Key range should cover all possible keys used in computation otherwise not in range error is generated by array
runUArrayMemoSource
Evaluate computation and the final content of array cache using unboxed array
Key range should cover all possible keys used in computation otherwise not in range error is generated by array