summaryrefslogtreecommitdiff
path: root/ghc/compiler/utils/BitSet.lhs
blob: a108136af3708c20a6e4e233010f5ef5a23cbcda (plain)
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
%
% (c) The GRASP Project, Glasgow University, 1994-1998
%
\section[BitSet]{An implementation of very small sets}

Bit sets are a fast implementation of sets of integers ranging from 0
to one less than the number of bits in a machine word (typically 31).
If any element exceeds the maximum value for a particular machine
architecture, the results of these operations are undefined.  You have
been warned.  If you put any safety checks in this code, I will have
to kill you.

Note: the Yale Haskell implementation won't provide a full 32 bits.
However, if you can handle the performance loss, you could change to
Integer and get virtually unlimited sets.

\begin{code}

module BitSet (
	BitSet,		-- abstract type
	mkBS, listBS, emptyBS, unitBS,
	unionBS, minusBS, intBS
    ) where

#include "HsVersions.h"

#ifdef __GLASGOW_HASKELL__
import GLAEXTS
-- nothing to import
#elif defined(__YALE_HASKELL__)
{-hide import from mkdependHS-}
import
	LogOpPrims
#else
{-hide import from mkdependHS-}
import
	Word
#endif

#ifdef __GLASGOW_HASKELL__

data BitSet = MkBS Word#

emptyBS :: BitSet
emptyBS = MkBS (int2Word# 0#)

mkBS :: [Int] -> BitSet
mkBS xs = foldr (unionBS . unitBS) emptyBS xs

unitBS :: Int -> BitSet
unitBS x = case x of
#if __GLASGOW_HASKELL__ >= 503
    I# i# -> MkBS ((int2Word# 1#) `uncheckedShiftL#` i#)
#else
    I# i# -> MkBS ((int2Word# 1#) `shiftL#` i#)
#endif

unionBS :: BitSet -> BitSet -> BitSet
unionBS (MkBS x#) (MkBS y#) = MkBS (x# `or#` y#)

minusBS :: BitSet -> BitSet -> BitSet
minusBS (MkBS x#) (MkBS y#) = MkBS (x# `and#` (not# y#))

#if 0
-- not used in GHC
isEmptyBS :: BitSet -> Bool
isEmptyBS (MkBS s#)
  = case word2Int# s# of
    	0# -> True
    	_  -> False

intersectBS :: BitSet -> BitSet -> BitSet
intersectBS (MkBS x#) (MkBS y#) = MkBS (x# `and#` y#)

elementBS :: Int -> BitSet -> Bool
elementBS x (MkBS s#) = case x of
    I# i# -> case word2Int# (((int2Word# 1#) `shiftL#` i#) `and#` s#) of
    	    	    0# -> False
    	    	    _  -> True
#endif

listBS :: BitSet -> [Int]
listBS s = listify s 0
    where listify (MkBS s#) n =
    	    case word2Int# s# of
    	        0# -> []
    	        _  -> let s' = (MkBS (s# `shiftr` 1#))
    	    	    	  more = listify s' (n + 1)
    	    	      in case word2Int# (s# `and#` (int2Word# 1#)) of
    	    	    	  0# -> more
    	    	    	  _  -> n : more
#if __GLASGOW_HASKELL__ >= 503
	  shiftr x y = uncheckedShiftRL# x y
#else
	  shiftr x y = shiftRL# x y
#endif

-- intBS is a bit naughty.
intBS :: BitSet -> Int
intBS (MkBS w#) = I# (word2Int# w#)

#elif defined(__YALE_HASKELL__)

data BitSet = MkBS Int

emptyBS :: BitSet
emptyBS = MkBS 0

mkBS :: [Int] -> BitSet
mkBS xs = foldr (unionBS . unitBS) emptyBS xs

unitBS :: Int -> BitSet
unitBS x = MkBS (1 `ashInt` x)

unionBS :: BitSet -> BitSet -> BitSet
unionBS (MkBS x) (MkBS y) = MkBS (x `logiorInt` y)

#if 0
-- not used in GHC
isEmptyBS :: BitSet -> Bool
isEmptyBS (MkBS s)
  = case s of
    	0 -> True
    	_ -> False

intersectBS :: BitSet -> BitSet -> BitSet
intersectBS (MkBS x) (MkBS y) = MkBS (x `logandInt` y)

elementBS :: Int -> BitSet -> Bool
elementBS x (MkBS s)
  = case logbitpInt x s of
    	0 -> False
    	_ -> True
#endif

minusBS :: BitSet -> BitSet -> BitSet
minusBS (MkBS x) (MkBS y) = MkBS (x `logandc2Int` y)

-- rewritten to avoid right shifts (which would give nonsense on negative
-- values.
listBS :: BitSet -> [Int]
listBS (MkBS s) = listify s 0 1
    where listify s n m =
    	    case s of
    	        0 -> []
    	        _ -> let n' = n+1; m' = m+m in
		     case logbitpInt s m of
		     0 -> listify s n' m'
		     _ -> n : listify (s `logandc2Int` m) n' m'

#else	/* HBC, perhaps? */

data BitSet = MkBS Word

emptyBS :: BitSet
emptyBS = MkBS 0

mkBS :: [Int] -> BitSet
mkBS xs = foldr (unionBS . unitBS) emptyBS xs

unitBS :: Int -> BitSet
unitBS x = MkBS (1 `bitLsh` x)

unionBS :: BitSet -> BitSet -> BitSet
unionBS (MkBS x) (MkBS y) = MkBS (x `bitOr` y)

#if 0
-- not used in GHC
isEmptyBS :: BitSet -> Bool
isEmptyBS (MkBS s)
  = case s of
    	0 -> True
    	_ -> False

intersectBS :: BitSet -> BitSet -> BitSet
intersectBS (MkBS x) (MkBS y) = MkBS (x `bitAnd` y)

elementBS :: Int -> BitSet -> Bool
elementBS x (MkBS s)
  = case (1 `bitLsh` x) `bitAnd` s of
    	0 -> False
    	_ -> True
#endif

minusBS :: BitSet -> BitSet -> BitSet
minusBS (MkBS x) (MkBS y) = MkBS (x `bitAnd` (bitCompl y))

listBS :: BitSet -> [Int]
listBS (MkBS s) = listify s 0
    where listify s n =
    	    case s of
    	        0 -> []
    	        _ -> let s' = s `bitRsh` 1
    	    	         more = listify s' (n + 1)
    	    	     in case (s `bitAnd` 1) of
    	    	    	    0 -> more
    	    	    	    _ -> n : more

#endif

\end{code}