summaryrefslogtreecommitdiff
path: root/packages/base/src/Data/Packed/Vector.hs
diff options
context:
space:
mode:
Diffstat (limited to 'packages/base/src/Data/Packed/Vector.hs')
-rw-r--r--packages/base/src/Data/Packed/Vector.hs125
1 files changed, 0 insertions, 125 deletions
diff --git a/packages/base/src/Data/Packed/Vector.hs b/packages/base/src/Data/Packed/Vector.hs
deleted file mode 100644
index 2104f52..0000000
--- a/packages/base/src/Data/Packed/Vector.hs
+++ /dev/null
@@ -1,125 +0,0 @@
1{-# LANGUAGE FlexibleContexts #-}
2{-# LANGUAGE CPP #-}
3-----------------------------------------------------------------------------
4-- |
5-- Module : Data.Packed.Vector
6-- Copyright : (c) Alberto Ruiz 2007-10
7-- License : BSD3
8-- Maintainer : Alberto Ruiz
9-- Stability : provisional
10--
11-- 1D arrays suitable for numeric computations using external libraries.
12--
13-- This module provides basic functions for manipulation of structure.
14--
15-----------------------------------------------------------------------------
16{-# OPTIONS_HADDOCK hide #-}
17
18module Data.Packed.Vector (
19 Vector,
20 fromList, (|>), toList, buildVector,
21 dim, (@>),
22 subVector, takesV, vjoin, join,
23 mapVector, mapVectorWithIndex, zipVector, zipVectorWith, unzipVector, unzipVectorWith,
24 mapVectorM, mapVectorM_, mapVectorWithIndexM, mapVectorWithIndexM_,
25 foldLoop, foldVector, foldVectorG, foldVectorWithIndex,
26 toByteString, fromByteString
27) where
28
29import Data.Packed.Internal.Vector
30import Foreign.Storable
31
32-------------------------------------------------------------------
33
34#ifdef BINARY
35
36import Data.Binary
37import Control.Monad(replicateM)
38
39import Data.ByteString.Internal as BS
40import Foreign.ForeignPtr(castForeignPtr)
41import Data.Vector.Storable.Internal(updPtr)
42import Foreign.Ptr(plusPtr)
43
44
45-- a 64K cache, with a Double taking 13 bytes in Bytestring,
46-- implies a chunk size of 5041
47chunk :: Int
48chunk = 5000
49
50chunks :: Int -> [Int]
51chunks d = let c = d `div` chunk
52 m = d `mod` chunk
53 in if m /= 0 then reverse (m:(replicate c chunk)) else (replicate c chunk)
54
55putVector v = mapM_ put $! toList v
56
57getVector d = do
58 xs <- replicateM d get
59 return $! fromList xs
60
61--------------------------------------------------------------------------------
62
63toByteString :: Storable t => Vector t -> ByteString
64toByteString v = BS.PS (castForeignPtr fp) (sz*o) (sz * dim v)
65 where
66 (fp,o,_n) = unsafeToForeignPtr v
67 sz = sizeOf (v@>0)
68
69
70fromByteString :: Storable t => ByteString -> Vector t
71fromByteString (BS.PS fp o n) = r
72 where
73 r = unsafeFromForeignPtr (castForeignPtr (updPtr (`plusPtr` o) fp)) 0 n'
74 n' = n `div` sz
75 sz = sizeOf (r@>0)
76
77--------------------------------------------------------------------------------
78
79instance (Binary a, Storable a) => Binary (Vector a) where
80
81 put v = do
82 let d = dim v
83 put d
84 mapM_ putVector $! takesV (chunks d) v
85
86 -- put = put . v2bs
87
88 get = do
89 d <- get
90 vs <- mapM getVector $ chunks d
91 return $! vjoin vs
92
93 -- get = fmap bs2v get
94
95#endif
96
97
98-------------------------------------------------------------------
99
100{- | creates a Vector of the specified length using the supplied function to
101 to map the index to the value at that index.
102
103@> buildVector 4 fromIntegral
1044 |> [0.0,1.0,2.0,3.0]@
105
106-}
107buildVector :: Storable a => Int -> (Int -> a) -> Vector a
108buildVector len f =
109 fromList $ map f [0 .. (len - 1)]
110
111
112-- | zip for Vectors
113zipVector :: (Storable a, Storable b, Storable (a,b)) => Vector a -> Vector b -> Vector (a,b)
114zipVector = zipVectorWith (,)
115
116-- | unzip for Vectors
117unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b)
118unzipVector = unzipVectorWith id
119
120-------------------------------------------------------------------
121
122{-# DEPRECATED join "use vjoin or Data.Vector.concat" #-}
123join :: Storable t => [Vector t] -> Vector t
124join = vjoin
125