diff options
Diffstat (limited to 'lib/Data/Packed/Internal/Vector.hs')
-rw-r--r-- | lib/Data/Packed/Internal/Vector.hs | 6 |
1 files changed, 4 insertions, 2 deletions
diff --git a/lib/Data/Packed/Internal/Vector.hs b/lib/Data/Packed/Internal/Vector.hs index 76bd4d1..082e09d 100644 --- a/lib/Data/Packed/Internal/Vector.hs +++ b/lib/Data/Packed/Internal/Vector.hs | |||
@@ -80,6 +80,8 @@ fromList l = unsafePerformIO $ do | |||
80 | f // vec v // check "fromList" [] | 80 | f // vec v // check "fromList" [] |
81 | return v | 81 | return v |
82 | 82 | ||
83 | safeRead v f = unsafePerformIO $ withForeignPtr (fptr v) $ const $ f (ptr v) | ||
84 | |||
83 | {- | extracts the Vector elements to a list | 85 | {- | extracts the Vector elements to a list |
84 | 86 | ||
85 | @> toList (linspace 5 (1,10)) | 87 | @> toList (linspace 5 (1,10)) |
@@ -87,7 +89,7 @@ fromList l = unsafePerformIO $ do | |||
87 | 89 | ||
88 | -} | 90 | -} |
89 | toList :: Storable a => Vector a -> [a] | 91 | toList :: Storable a => Vector a -> [a] |
90 | toList v = unsafePerformIO $ peekArray (dim v) (ptr v) | 92 | toList v = safeRead v $ peekArray (dim v) |
91 | 93 | ||
92 | -- | an alternative to 'fromList' with explicit dimension, used also in the instances for Show (Vector a). | 94 | -- | an alternative to 'fromList' with explicit dimension, used also in the instances for Show (Vector a). |
93 | (|>) :: (Storable a) => Int -> [a] -> Vector a | 95 | (|>) :: (Storable a) => Int -> [a] -> Vector a |
@@ -96,7 +98,7 @@ n |> l = if length l == n then fromList l else error "|> with wrong size" | |||
96 | 98 | ||
97 | -- | access to Vector elements without range checking | 99 | -- | access to Vector elements without range checking |
98 | at' :: Storable a => Vector a -> Int -> a | 100 | at' :: Storable a => Vector a -> Int -> a |
99 | at' v n = unsafePerformIO $ peekElemOff (ptr v) n | 101 | at' v n = safeRead v $ flip peekElemOff n |
100 | 102 | ||
101 | -- | access to Vector elements with range checking. | 103 | -- | access to Vector elements with range checking. |
102 | at :: Storable a => Vector a -> Int -> a | 104 | at :: Storable a => Vector a -> Int -> a |