summaryrefslogtreecommitdiff
path: root/libraries/base/GHC/ArrayArray.hs
blob: 199bac3a15947690721fda89496a4cd2952c3f7b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneKindSignatures #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UnliftedNewtypes #-}

-----------------------------------------------------------------------------
-- |
-- Module      :  GHC.ArrayArray
-- License     :  see libraries/base/LICENSE
--
-- Maintainer  :  cvs-ghc@haskell.org
-- Stability   :  internal
-- Portability :  non-portable (GHC Extensions)
--
-- Legacy interface for arrays of arrays.
-- Deprecated, because the 'Array#' type can now store arrays directly.
-- Consider simply using 'Array#' instead of 'ArrayArray#'.
--
-- Use GHC.Exts instead of importing this module directly.
--
----------------------------------------------------------------------------

module GHC.ArrayArray
  ( ArrayArray#(..), MutableArrayArray#(..)
  , newArrayArray#
  , unsafeFreezeArrayArray#
  , sizeofArrayArray#
  , sizeofMutableArrayArray#
  , indexByteArrayArray#
  , indexArrayArrayArray#
  , readByteArrayArray#
  , readMutableByteArrayArray#
  , readArrayArrayArray#
  , readMutableArrayArrayArray#
  , writeByteArrayArray#
  , writeMutableByteArrayArray#
  , writeArrayArrayArray#
  , writeMutableArrayArrayArray#
  , copyArrayArray#
  , copyMutableArrayArray#
  , sameArrayArray#
  , sameMutableArrayArray#
  )
  where

import GHC.Prim
import GHC.Types ( Type, UnliftedType, isTrue# )
import Unsafe.Coerce ( unsafeCoerce, unsafeCoerceUnlifted )
default ()

{- **********************************************************************
*                                                                       *
*                 Arrays of arrays (legacy interface)                    *
*                                                                       *
********************************************************************** -}

type ArrayArray# :: UnliftedType
newtype ArrayArray# = ArrayArray# (Array# ByteArray#)

type MutableArrayArray# :: Type -> UnliftedType
newtype MutableArrayArray# s = MutableArrayArray# (MutableArray# s ByteArray#)

-- | Create a new mutable array of arrays with the specified number of elements,
--   in the specified state thread, with each element recursively referring to the
--   newly created array.
newArrayArray# :: Int# -> State# s -> (# State# s, MutableArrayArray# s #)
newArrayArray# sz s1 =
  -- Create a placeholder ByteArray to initialise the underlying MutableArray#.
  case newByteArray# 0# s1 of
    (# s2, placeholder #) ->
      -- Create a new MutableArray# holding the placeholder ByteArray# value.
      case newArray# sz (unsafeCoerceUnlifted placeholder) s2 of
        (# s3, arr #) ->
          -- Now update the MutableArray# so that the elements refer back
          -- to the mutable array itself.
          case write_array_to_array arr 0# s3 of
            s4 -> (# s4, MutableArrayArray# (unsafeCoerceUnlifted arr) #)

  where
    write_array_to_array :: MutableArray# s ByteArray# -> Int# -> State# s -> State# s
    write_array_to_array _ i s
      | isTrue# (i >=# sz)
      = s
    write_array_to_array arr i s
      = case writeArray# arr i (unsafeCoerceUnlifted arr) s of
          s' -> write_array_to_array arr (i +# 1#) s'

-- | Make a mutable array of arrays immutable, without copying.
unsafeFreezeArrayArray# :: MutableArrayArray# s -> State# s -> (# State# s, ArrayArray# #)
unsafeFreezeArrayArray# = unsafeCoerce unsafeFreezeArray#

-- | Return the number of elements in the array.
sizeofArrayArray# :: ArrayArray# -> Int#
sizeofArrayArray# = unsafeCoerce sizeofArray#

-- | Return the number of elements in the array.
sizeofMutableArrayArray# :: MutableArrayArray# s -> Int#
sizeofMutableArrayArray# = unsafeCoerce sizeofMutableArray#

indexByteArrayArray# :: ArrayArray# -> Int# -> ByteArray#
indexByteArrayArray# = unsafeCoerce indexArray#

indexArrayArrayArray# :: ArrayArray# -> Int# -> ArrayArray#
indexArrayArrayArray# = unsafeCoerce indexArray#

readByteArrayArray# :: MutableArrayArray# s -> Int# -> State# s -> (# State# s, ByteArray# #)
readByteArrayArray# = unsafeCoerce readArray#

readMutableByteArrayArray# :: MutableArrayArray# s -> Int# -> State# s -> (# State# s, MutableByteArray# s #)
readMutableByteArrayArray# = unsafeCoerce readArray#

readArrayArrayArray# :: MutableArrayArray# s -> Int# -> State# s -> (# State# s, ArrayArray# #)
readArrayArrayArray# = unsafeCoerce readArray#

readMutableArrayArrayArray# :: MutableArrayArray# s -> Int# -> State# s -> (# State# s, MutableArrayArray# s #)
readMutableArrayArrayArray# = unsafeCoerce readArray#

writeByteArrayArray# :: MutableArrayArray# s -> Int# -> ByteArray# -> State# s -> State# s
writeByteArrayArray# = unsafeCoerce writeArray#

writeMutableByteArrayArray# :: MutableArrayArray# s -> Int# -> MutableByteArray# s -> State# s -> State# s
writeMutableByteArrayArray# = unsafeCoerce writeArray#

writeArrayArrayArray# :: MutableArrayArray# s -> Int# -> ArrayArray# -> State# s -> State# s
writeArrayArrayArray# = unsafeCoerce writeArray#

writeMutableArrayArrayArray# :: MutableArrayArray# s -> Int# -> MutableArrayArray# s -> State# s -> State# s
writeMutableArrayArrayArray# = unsafeCoerce writeArray#

-- | Copy a range of the 'ArrayArray#' to the specified region in the 'MutableArrayArray#'.
--   Both arrays must fully contain the specified ranges, but this is not checked.
--   The two arrays must not be the same array in different states, but this is not checked either.
copyArrayArray# :: ArrayArray# -> Int# -> MutableArrayArray# s -> Int# -> Int# -> State# s -> State# s
copyArrayArray# = unsafeCoerce copyArray#

-- | Copy a range of the first MutableArrayArray# to the specified region in the second
--   MutableArrayArray#.
--   Both arrays must fully contain the specified ranges, but this is not checked.
--   The regions are allowed to overlap, although this is only possible when the same
--   array is provided as both the source and the destination.
copyMutableArrayArray# :: MutableArrayArray# s -> Int# -> MutableArrayArray# s -> Int# -> Int# -> State# s -> State# s
copyMutableArrayArray# = unsafeCoerce copyMutableArray#

-- | Compare the underlying pointers of two arrays of arrays.
sameArrayArray# :: ArrayArray# -> ArrayArray# -> Int#
sameArrayArray# (ArrayArray# arr1) (ArrayArray# arr2) = reallyUnsafePtrEquality# arr1 arr2

-- | Compare the underlying pointers of two mutable arrays of arrays.
sameMutableArrayArray# :: MutableArrayArray# s -> MutableArrayArray# s -> Int#
sameMutableArrayArray# (MutableArrayArray# marr1) (MutableArrayArray# marr2 ) = reallyUnsafePtrEquality# marr1 marr2