summaryrefslogtreecommitdiff
path: root/src/Storage.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Storage.hs')
-rw-r--r--src/Storage.hs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/Storage.hs b/src/Storage.hs
new file mode 100644
index 0000000..a97a5fc
--- /dev/null
+++ b/src/Storage.hs
@@ -0,0 +1,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))
+
+