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)