module Data.Text.PgpWordlist.Internal.Word8Bimap (
Word8Bimap
, unsafeConstruct
, lookupL
, lookupR
) where
import Data.Map (Map)
import qualified Data.Map as M
import Data.Vector (Vector, (!))
import qualified Data.Vector as V
import Data.Tuple
import Data.Word
data Word8Bimap a = Bimap (Vector a) (Map a Word8)
unsafeConstruct :: Ord a => [(Word8, a)] -> Word8Bimap a
unsafeConstruct assocs = Bimap vec inverseMap
where
vec = V.fromList (map snd assocs)
inverseMap = M.fromList (map swap assocs)
lookupL :: Word8Bimap a -> Word8 -> a
lookupL (Bimap l _) i = l ! fromIntegral i
lookupR :: Ord a => Word8Bimap a -> a -> Maybe Word8
lookupR (Bimap _ r) i = M.lookup i r