summaryrefslogtreecommitdiff
path: root/src/Storage.hs
blob: a97a5fc4e0c13f29e11cffd74eb45036ebe07af4 (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
module Storage(
    Store,

    createStore,
    setPref,
    checkPref
    ) where




import qualified Control.Monad.Primitive as Prim
import qualified Data.Vector.Unboxed.Mutable as Vec
import qualified Data.Int as Ints




data Store = Store { pointer      :: Vec.MVector Prim.RealWorld Ints.Int8
                   , sizeOfBallot :: Int}




createStore :: Int -> Int -> IO Store
createStore numberOfEntries ballotSize = do
    v <- Vec.new (numberOfEntries * ballotSize)
    return (Store v ballotSize)




setPref :: Store -> Int -> Int -> Int -> IO ()
setPref prefStore ballot position rank = do
    let place = (ballot - 1) * (sizeOfBallot prefStore) + (position - 1)
    Vec.write (pointer prefStore) place (fromIntegral rank)




checkPref :: Store -> Int -> Int -> Int -> IO Bool
checkPref prefStore ballot position rank = do
    let place = (ballot - 1) * (sizeOfBallot prefStore) + (position - 1)
    value <- Vec.read (pointer prefStore) place
    return (value == (fromIntegral rank))