module Biobase.Types.Names.Internal where
import Data.IORef (newIORef,IORef,readIORef,atomicWriteIORef,atomicModifyIORef')
import Data.Text (Text)
import System.IO.Unsafe (unsafePerformIO,unsafeDupablePerformIO)
import Data.Bijection.HashMap
import Data.Bijection.Vector
speciesNameBimap :: IORef (Bimap (HashMap Text Int) (Vector Text))
speciesNameBimap = unsafePerformIO $ newIORef empty
{-# NoInline speciesNameBimap #-}
speciesNameBimapAdd :: Text -> Int
speciesNameBimapAdd k = unsafeDupablePerformIO $ atomicModifyIORef' speciesNameBimap $ \m ->
case lookupL m k of Just i -> (m,i)
Nothing -> let s = size m
in (insert m (k,s) , s)
{-# Inline speciesNameBimapAdd #-}
speciesNameBimapLookupInt :: Int -> Text
speciesNameBimapLookupInt r = seq r . unsafeDupablePerformIO $ atomicModifyIORef' speciesNameBimap $ \m ->
case lookupR m r of Just l -> (m,l)
Nothing -> error "speciesNameBimapLookupInt: totality assumption invalidated"
{-# Inline speciesNameBimapLookupInt #-}