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

    createStore,
    setPref,
    checkPref
    ) where




import Data.Int ( Int8 )
import Preferences ( Preference )
import qualified Data.Vector.Unboxed.Mutable as Vec




data Store = Store
    { getPointer    :: Vec.IOVector Int8
    , getBallotSize :: Int }




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




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




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