/
Util.hs
85 lines (69 loc) · 1.72 KB
/
Util.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
{-# LANGUAGE BangPatterns #-}
module GHC.Vacuum.Util (
module GHC.Vacuum.Util
) where
import Data.List
import Data.Char
import Data.Bits
import Data.Array.IArray hiding ((!))
import qualified Data.Array.IArray as A
hash :: String -> Int
hash [] = 0
hash s = go 0 (fmap ord s)
where go !h [] = h
go !h (n:ns) =
let a = (h `shiftL` 4)
b = a + n
c = b .&. 0xf0000000
!d = case c==0 of
False -> let !e = c `shiftR` 24
in b `xor` e
True -> b
!e = complement c
!f = d .&. e
in go f ns
{-
unsigned long
elfhash(const char *s)
{
unsigned long h=0, g;
while (*s){
h = (h << 4) + *s++;
if((g = h & 0xf0000000))
h ^= g >> 24;
h &= ~g;
}
return h;
}
-}
------------------------------------------------
{-
look :: HValue -> [(HValue, a)] -> Maybe a
look _ [] = Nothing
look hval ((x,i):xs)
| hval .==. x = Just i
| otherwise = look hval xs
(.==.) :: HValue -> HValue -> Bool
a .==. b = a `seq` b `seq`
(0 /= I# (reallyUnsafePtrEquality# a b))
lookLazy :: HValue -> [(HValue, a)] -> Maybe a
lookLazy _ [] = Nothing
lookLazy hval ((x,i):xs)
| hval =.= x = Just i
| otherwise = lookLazy hval xs
(=.=) :: HValue -> HValue -> Bool
a =.= b = (0 /= I# (reallyUnsafePtrEquality# a b))
dumpArray :: Array Int a -> [a]
dumpArray a = let (m,n) = bounds a
in fmap (a A.!) [m..n]
-}
mapfst f = \(a,b) -> (f a,b)
mapsnd f = \(a,b) -> (a,f b)
f *** g = \(a, b) -> (f a, g b)
{-
p2i :: Ptr a -> Int
i2p :: Int -> Ptr a
p2i (Ptr a#) = I# (addr2Int# a#)
i2p (I# n#) = Ptr (int2Addr# n#)
-}
------------------------------------------------