diff options
Diffstat (limited to 'packages/base/src/Data/Packed/Vector.hs')
-rw-r--r-- | packages/base/src/Data/Packed/Vector.hs | 125 |
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 | |||
18 | module 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 | |||
29 | import Data.Packed.Internal.Vector | ||
30 | import Foreign.Storable | ||
31 | |||
32 | ------------------------------------------------------------------- | ||
33 | |||
34 | #ifdef BINARY | ||
35 | |||
36 | import Data.Binary | ||
37 | import Control.Monad(replicateM) | ||
38 | |||
39 | import Data.ByteString.Internal as BS | ||
40 | import Foreign.ForeignPtr(castForeignPtr) | ||
41 | import Data.Vector.Storable.Internal(updPtr) | ||
42 | import Foreign.Ptr(plusPtr) | ||
43 | |||
44 | |||
45 | -- a 64K cache, with a Double taking 13 bytes in Bytestring, | ||
46 | -- implies a chunk size of 5041 | ||
47 | chunk :: Int | ||
48 | chunk = 5000 | ||
49 | |||
50 | chunks :: Int -> [Int] | ||
51 | chunks 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 | |||
55 | putVector v = mapM_ put $! toList v | ||
56 | |||
57 | getVector d = do | ||
58 | xs <- replicateM d get | ||
59 | return $! fromList xs | ||
60 | |||
61 | -------------------------------------------------------------------------------- | ||
62 | |||
63 | toByteString :: Storable t => Vector t -> ByteString | ||
64 | toByteString 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 | |||
70 | fromByteString :: Storable t => ByteString -> Vector t | ||
71 | fromByteString (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 | |||
79 | instance (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 | ||
104 | 4 |> [0.0,1.0,2.0,3.0]@ | ||
105 | |||
106 | -} | ||
107 | buildVector :: Storable a => Int -> (Int -> a) -> Vector a | ||
108 | buildVector len f = | ||
109 | fromList $ map f [0 .. (len - 1)] | ||
110 | |||
111 | |||
112 | -- | zip for Vectors | ||
113 | zipVector :: (Storable a, Storable b, Storable (a,b)) => Vector a -> Vector b -> Vector (a,b) | ||
114 | zipVector = zipVectorWith (,) | ||
115 | |||
116 | -- | unzip for Vectors | ||
117 | unzipVector :: (Storable a, Storable b, Storable (a,b)) => Vector (a,b) -> (Vector a,Vector b) | ||
118 | unzipVector = unzipVectorWith id | ||
119 | |||
120 | ------------------------------------------------------------------- | ||
121 | |||
122 | {-# DEPRECATED join "use vjoin or Data.Vector.concat" #-} | ||
123 | join :: Storable t => [Vector t] -> Vector t | ||
124 | join = vjoin | ||
125 | |||