1
1
mirror of https://github.com/github/semantic.git synced 2024-12-21 13:51:44 +03:00

Define a function to lookup a branch in a Table.

This commit is contained in:
Rob Rix 2017-08-31 23:09:08 -04:00
parent d742af2c28
commit 8bbf7e0c5e

View File

@ -3,6 +3,7 @@ module Data.Syntax.Assignment.Table.Array
, singleton , singleton
, fromListWith , fromListWith
, toList , toList
, lookup
) where ) where
import Control.Arrow ((&&&)) import Control.Arrow ((&&&))
@ -11,6 +12,7 @@ import Data.Functor.Classes
import Data.List.NonEmpty (NonEmpty(..)) import Data.List.NonEmpty (NonEmpty(..))
import Data.Semigroup (Max(..), Min(..), sconcat) import Data.Semigroup (Max(..), Min(..), sconcat)
import GHC.Stack import GHC.Stack
import Prelude hiding (lookup)
data Table i a = Table { tableAddresses :: [i], tableBranches :: Array i (Maybe a) } data Table i a = Table { tableAddresses :: [i], tableBranches :: Array i (Maybe a) }
deriving (Foldable, Functor, Traversable) deriving (Foldable, Functor, Traversable)
@ -29,5 +31,11 @@ toList :: Ix i => Table i a -> [(i, a)]
toList Table{..} = tableAddresses >>= \ addr -> maybe [] (pure . (,) addr) (tableBranches ! addr) toList Table{..} = tableAddresses >>= \ addr -> maybe [] (pure . (,) addr) (tableBranches ! addr)
lookup :: Ix i => i -> Table i a -> Maybe a
lookup i Table{..}
| bounds tableBranches `inRange` i = tableBranches ! i
| otherwise = Nothing
instance (Ix i, Show i) => Show1 (Table i) where instance (Ix i, Show i) => Show1 (Table i) where
liftShowsPrec spA slA d t = showsBinaryWith showsPrec (const (liftShowList spA slA)) "Table" d (tableAddresses t) (toList t) liftShowsPrec spA slA d t = showsBinaryWith showsPrec (const (liftShowList spA slA)) "Table" d (tableAddresses t) (toList t)