<commit_before>{-# LANGUAGE TemplateHaskell, MagicHash, TypeSynonymInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module      : Berp.Base.Hash
-- Copyright   : (c) 2010 <NAME>
-- License     : BSD-style
-- Maintainer  : <EMAIL>
-- Stability   : experimental
-- Portability : ghc
--
-- Hashing functions.
--
-----------------------------------------------------------------------------

module Berp.Base.Hash (Hash (..), Hashed, hashedStr) where

import Berp.Base.Mangle (mangle)
import Language.Haskell.TH
import Data.HashTable as HT (hashString)
import GHC.Integer (hashInteger)
import GHC.Types (Int (..))

type Hashed a = (Int, a)

class Hash a where
   hash :: a -> Int

instance Hash String where
   hash = fromIntegral . HT.hashString

instance Hash Integer where
   hash i = I# (hashInteger i)

hashedStr :: String -> Q Exp
hashedStr str = [| (n, mangle str) |]
   where
   n :: Int
   n = hash str<commit_msg>Used a more portable hash function for Integer.<commit_after>{-# LANGUAGE TemplateHaskell, MagicHash, TypeSynonymInstances #-}

-----------------------------------------------------------------------------
-- |
-- Module      : Berp.Base.Hash
-- Copyright   : (c) 2010 <NAME>
-- License     : BSD-style
-- Maintainer  : <EMAIL>
-- Stability   : experimental
-- Portability : ghc
--
-- Hashing functions.
--
-----------------------------------------------------------------------------

module Berp.Base.Hash (Hash (..), Hashed, hashedStr) where

import Berp.Base.Mangle (mangle)
import Language.Haskell.TH
import Data.HashTable as HT (hashString)

type Hashed a = (Int, a)

class Hash a where
   hash :: a -> Int

instance Hash String where
   hash = fromIntegral . HT.hashString

instance Hash Integer where
   -- hash i = I# (hashInteger i)
   hash = hashInteger

hashedStr :: String -> Q Exp
hashedStr str = [| (n, mangle str) |]
   where
   n :: Int
   n = hash str

-- XXX May need to reconsider this. We'll want something which is fast.
hashInteger :: Integer -> Int
hashInteger i = fromInteger (i `mod` (toInteger (maxBound :: Int)))
{'fraction_non_alphanumeric': 0.2539081004263382, 'fraction_numerical': 0.0037896731406916154, 'mean_word_length': 1.7973509933774834, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, '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': '7352750', 'n_tokens_mistral': 621, 'n_tokens_neox': 603, 'n_words': 197}