<commit_before>-- | A usage-table is sort of a bottom-up symbol table, describing how
-- (and if) a variable is used.
module Futhark.EnablingOpts.UsageTable
  ( UsageTable
  , empty
  , contains
  , without
  , lookup
  , used
  , usedAsPredicate
  , usages
  , predicateUsage
  , Usages
  , isPredicate
  )
  where

import Prelude hiding (lookup)

import qualified Data.HashMap.Lazy as HM
import qualified Data.HashSet as HS
import qualified Data.Set as S

import Futhark.InternalRep

type UsageTable = HM.HashMap VName Usages

empty :: UsageTable
empty = HM.empty

contains :: UsageTable -> HS.HashSet VName -> Bool
contains table names = not $ HS.null $ names `HS.intersection` keys
  where keys = HS.fromList $ HM.keys table

without :: UsageTable -> HS.HashSet VName -> UsageTable
without table names = table `HM.difference` usages names

lookup :: VName -> UsageTable -> Maybe Usages
lookup = HM.lookup

lookupPred :: (Usages -> Bool) -> VName -> UsageTable -> Bool
lookupPred f name = maybe False f . lookup name

used :: VName -> UsageTable -> Bool
used = lookupPred $ const True

usedAsPredicate :: VName -> UsageTable -> Bool
usedAsPredicate = lookupPred isPredicate

usages :: HS.HashSet VName -> UsageTable
usages names = HM.fromList [ (name, S.empty) | name <- HS.toList names ]

predicateUsage :: VName -> UsageTable
predicateUsage name = HM.singleton name $ S.singleton Predicate

type Usages = S.Set Usage

data Usage = Predicate
             deriving (Eq, Ord, Show)

isPredicate :: Usages -> Bool
isPredicate = S.member Predicate
<commit_msg>Improve performance of UsageTable.contains.<commit_after>-- | A usage-table is sort of a bottom-up symbol table, describing how
-- (and if) a variable is used.
module Futhark.EnablingOpts.UsageTable
  ( UsageTable
  , empty
  , contains
  , without
  , lookup
  , used
  , usedAsPredicate
  , usages
  , predicateUsage
  , Usages
  , isPredicate
  )
  where

import Prelude hiding (lookup, any)

import Data.Foldable
import qualified Data.HashMap.Lazy as HM
import qualified Data.HashSet as HS
import qualified Data.Set as S

import Futhark.InternalRep

type UsageTable = HM.HashMap VName Usages

empty :: UsageTable
empty = HM.empty

contains :: UsageTable -> HS.HashSet VName -> Bool
contains table = any (`HM.member` table)

without :: UsageTable -> HS.HashSet VName -> UsageTable
without table names = table `HM.difference` usages names

lookup :: VName -> UsageTable -> Maybe Usages
lookup = HM.lookup

lookupPred :: (Usages -> Bool) -> VName -> UsageTable -> Bool
lookupPred f name = maybe False f . lookup name

used :: VName -> UsageTable -> Bool
used = lookupPred $ const True

usedAsPredicate :: VName -> UsageTable -> Bool
usedAsPredicate = lookupPred isPredicate

usages :: HS.HashSet VName -> UsageTable
usages names = HM.fromList [ (name, S.empty) | name <- HS.toList names ]

predicateUsage :: VName -> UsageTable
predicateUsage name = HM.singleton name $ S.singleton Predicate

type Usages = S.Set Usage

data Usage = Predicate
             deriving (Eq, Ord, Show)

isPredicate :: Usages -> Bool
isPredicate = S.member Predicate

{'fraction_non_alphanumeric': 0.09304571796522859, 'fraction_numerical': 0.0, 'mean_word_length': 3.630402384500745, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7376609', 'n_tokens_mistral': 1015, 'n_tokens_neox': 943, 'n_words': 330}