author | nemo |
Tue, 17 Aug 2010 19:43:17 -0400 | |
changeset 3742 | 8461f0cef2e6 |
parent 3741 | 73246d25dfe1 |
child 3747 | 76a197793b62 |
permissions | -rw-r--r-- |
3425 | 1 |
module Store( |
2 |
ElemIndex(), |
|
3 |
MStore(), |
|
4 |
IStore(), |
|
5 |
newStore, |
|
6 |
addElem, |
|
7 |
removeElem, |
|
8 |
readElem, |
|
9 |
writeElem, |
|
10 |
modifyElem, |
|
3741
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
11 |
elemExists, |
3425 | 12 |
firstIndex, |
3501 | 13 |
indicesM, |
3425 | 14 |
withIStore, |
15 |
withIStore2, |
|
16 |
(!), |
|
17 |
indices |
|
18 |
) where |
|
19 |
||
20 |
import qualified Data.Array.IArray as IA |
|
21 |
import qualified Data.Array.IO as IOA |
|
22 |
import qualified Data.IntSet as IntSet |
|
23 |
import Data.IORef |
|
24 |
import Control.Monad |
|
25 |
||
26 |
||
27 |
newtype ElemIndex = ElemIndex Int |
|
3566 | 28 |
deriving (Eq, Show, Read, Ord) |
3425 | 29 |
newtype MStore e = MStore (IORef (IntSet.IntSet, IntSet.IntSet, IOA.IOArray Int e)) |
30 |
newtype IStore e = IStore (IntSet.IntSet, IA.Array Int e) |
|
31 |
||
32 |
||
33 |
firstIndex :: ElemIndex |
|
34 |
firstIndex = ElemIndex 0 |
|
35 |
||
36 |
-- MStore code |
|
37 |
initialSize :: Int |
|
38 |
initialSize = 10 |
|
39 |
||
40 |
||
41 |
growFunc :: Int -> Int |
|
42 |
growFunc a = a * 3 `div` 2 |
|
43 |
||
44 |
||
45 |
newStore :: IO (MStore e) |
|
46 |
newStore = do |
|
47 |
newar <- IOA.newArray_ (0, initialSize - 1) |
|
48 |
new <- newIORef (IntSet.empty, IntSet.fromAscList [0..initialSize - 1], newar) |
|
49 |
return (MStore new) |
|
50 |
||
51 |
||
52 |
growStore :: MStore e -> IO () |
|
53 |
growStore (MStore ref) = do |
|
54 |
(busyElems, freeElems, arr) <- readIORef ref |
|
55 |
(_, m') <- IOA.getBounds arr |
|
56 |
let newM' = growFunc (m' + 1) - 1 |
|
57 |
newArr <- IOA.newArray_ (0, newM') |
|
58 |
sequence_ [IOA.readArray arr i >>= IOA.writeArray newArr i | i <- [0..m']] |
|
59 |
writeIORef ref (busyElems, freeElems `IntSet.union` (IntSet.fromAscList [m'+1..newM']), newArr) |
|
60 |
||
61 |
||
62 |
growIfNeeded :: MStore e -> IO () |
|
63 |
growIfNeeded m@(MStore ref) = do |
|
64 |
(_, freeElems, _) <- readIORef ref |
|
65 |
when (IntSet.null freeElems) $ growStore m |
|
66 |
||
67 |
||
68 |
addElem :: MStore e -> e -> IO ElemIndex |
|
69 |
addElem m@(MStore ref) element = do |
|
70 |
growIfNeeded m |
|
71 |
(busyElems, freeElems, arr) <- readIORef ref |
|
72 |
let (n, freeElems') = IntSet.deleteFindMin freeElems |
|
73 |
IOA.writeArray arr n element |
|
74 |
writeIORef ref (IntSet.insert n busyElems, freeElems', arr) |
|
75 |
return $ ElemIndex n |
|
76 |
||
77 |
||
78 |
removeElem :: MStore e -> ElemIndex -> IO () |
|
79 |
removeElem (MStore ref) (ElemIndex n) = do |
|
80 |
(busyElems, freeElems, arr) <- readIORef ref |
|
3673
45778b16b224
Some comments on the reason of the bug, leave bug not fixed yet
unc0rr
parents:
3671
diff
changeset
|
81 |
IOA.writeArray arr n (error $ "Store: no element " ++ show n) |
3425 | 82 |
writeIORef ref (IntSet.delete n busyElems, IntSet.insert n freeElems, arr) |
83 |
||
84 |
||
85 |
readElem :: MStore e -> ElemIndex -> IO e |
|
86 |
readElem (MStore ref) (ElemIndex n) = readIORef ref >>= \(_, _, arr) -> IOA.readArray arr n |
|
87 |
||
88 |
||
89 |
writeElem :: MStore e -> ElemIndex -> e -> IO () |
|
90 |
writeElem (MStore ref) (ElemIndex n) el = readIORef ref >>= \(_, _, arr) -> IOA.writeArray arr n el |
|
91 |
||
92 |
||
93 |
modifyElem :: MStore e -> (e -> e) -> ElemIndex -> IO () |
|
94 |
modifyElem (MStore ref) f (ElemIndex n) = do |
|
95 |
(_, _, arr) <- readIORef ref |
|
96 |
IOA.readArray arr n >>= (IOA.writeArray arr n) . f |
|
97 |
||
3741
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
98 |
elemExists :: MStore e -> ElemIndex -> IO Bool |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
99 |
elemExists (MStore ref) (ElemIndex n) = do |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
100 |
(_, free, _) <- readIORef ref |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
101 |
return $ n `IntSet.notMember` free |
3425 | 102 |
|
3501 | 103 |
indicesM :: MStore e -> IO [ElemIndex] |
104 |
indicesM (MStore ref) = do |
|
105 |
(busy, _, _) <- readIORef ref |
|
106 |
return $ map ElemIndex $ IntSet.toList busy |
|
107 |
||
108 |
||
3741
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
109 |
-- A way to see MStore elements in pure code via IStore |
3425 | 110 |
m2i :: MStore e -> IO (IStore e) |
111 |
m2i (MStore ref) = do |
|
3741
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
112 |
(a, _, c') <- readIORef ref |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
113 |
c <- IOA.unsafeFreeze c' |
3425 | 114 |
return $ IStore (a, c) |
115 |
||
3741
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
116 |
i2m :: (MStore e) -> IStore e -> IO () |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
117 |
i2m (MStore ref) (IStore (_, arr)) = do |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
118 |
(b, e, _) <- readIORef ref |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
119 |
a <- IOA.unsafeThaw arr |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
120 |
writeIORef ref (b, e, a) |
3501 | 121 |
|
3425 | 122 |
withIStore :: MStore e -> (IStore e -> a) -> IO a |
3741
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
123 |
withIStore m f = do |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
124 |
i <- m2i m |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
125 |
let res = f i |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
126 |
res `seq` i2m m i |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
127 |
return res |
3425 | 128 |
|
129 |
||
130 |
withIStore2 :: MStore e1 -> MStore e2 -> (IStore e1 -> IStore e2 -> a) -> IO a |
|
131 |
withIStore2 m1 m2 f = do |
|
132 |
i1 <- m2i m1 |
|
133 |
i2 <- m2i m2 |
|
3741
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
134 |
let res = f i1 i2 |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
135 |
res `seq` i2m m1 i1 |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
136 |
i2m m2 i2 |
73246d25dfe1
Add some more strictness, use unsafeThaw and unsafeFreeze functions which work at O(1)
unc0rr
parents:
3673
diff
changeset
|
137 |
return res |
3425 | 138 |
|
139 |
||
140 |
-- IStore code |
|
141 |
(!) :: IStore e -> ElemIndex -> e |
|
142 |
(!) (IStore (_, arr)) (ElemIndex i) = (IA.!) arr i |
|
143 |
||
144 |
indices :: IStore e -> [ElemIndex] |
|
145 |
indices (IStore (busy, _)) = map ElemIndex $ IntSet.toList busy |