summaryrefslogtreecommitdiff
path: root/src/Storage.hs
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2017-01-08 23:49:21 +1100
committerJed Barber <jjbarber@y7mail.com>2017-01-08 23:49:21 +1100
commit82cb1c4265c0c4a55fcd3fec9bcaec6647d11030 (patch)
tree07f226d863fee627f20fd5a1713290b4c2379ff0 /src/Storage.hs
parent1652e49e17e4f4dead4bd23694a2b99a06048023 (diff)
Should be operating at acceptable speed/memusage rates now
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))
+
+