module Data.HMemDb.TableVarId (TableVarId(TableVarId), idToVar, varToId) where
import Control.Compose (Id(Id), unId)
import Data.Function (on)
import Data.HMemDb.MapTVar (MS, readTVarMap)
import Data.HMemDb.References (Ref(Ref, refContent, refIndex))
import Data.HMemDb.Tables (Table(Table), tabContent)
import Data.HMemDb.TableVars (TableVar, TableVarS(TableVar))
newtype TableVarId a = TableVarId {unTVId :: Integer}
instance Eq (TableVarId a) where (==) = (==) `on` unTVId
instance Ord (TableVarId a) where compare = compare `on` unTVId
varToId :: TableVar a -> TableVarId a
varToId (TableVar _ iref _) = TableVarId $ refIndex $ unId iref
idToVar :: Table a -> TableVarId a -> MS (TableVar a)
idToVar (Table b pt) tvId =
do let i = unTVId tvId
tv <- readTVarMap (tabContent pt) i
return $ TableVar b (Id Ref {refContent = tv, refIndex = i}) pt