-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathDynamic.hs
More file actions
240 lines (189 loc) · 6.52 KB
/
Dynamic.hs
File metadata and controls
240 lines (189 loc) · 6.52 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
#include <bitset.h>
-----------------------------------------------------------------------------
-- |
-- Module : Data.BitSet.Dynamic
-- Copyright : (c) Sergei Lebedev, Aleksey Kladov, Fedor Gogolev 2013
-- Based on Data.BitSet (c) Denis Bueno 2008-2009
-- License : MIT
-- Maintainer : superbobry@gmail.com
-- Stability : experimental
-- Portability : GHC
--
-- A space-efficient implementation of set data structure for enumerated
-- data types.
--
-- /Note/: Read below the synopsis for important notes on the use of
-- this module.
--
-- This module is intended to be imported @qualified@, to avoid name
-- clashes with "Prelude" functions, e.g.
--
-- > import Data.BitSet.Dynamic (BitSet)
-- > import qualified Data.BitSet.Dynamic as BS
--
-- The implementation uses 'Integer' as underlying container, thus it
-- grows automatically when more elements are inserted into the bit set.
module Data.BitSet.Dynamic
(
-- * Bit set type
FasterInteger(FasterInteger)
, BitSet
-- * Operators
, (\\)
-- * Construction
, empty
, singleton
, insert
, delete
-- * Query
, null
, size
, member
, notMember
, isSubsetOf
, isProperSubsetOf
-- * Combine
, union
, difference
, intersection
-- * Transformations
, map
-- * Folds
, foldl'
, foldr
-- * Filter
, filter
-- * Lists
, toList
, fromList
) where
import Prelude hiding (null, map, filter, foldr)
import Data.Bits (Bits(..))
import GHC.Base (Int(..))
import Control.DeepSeq (NFData(..))
import GHC.Integer.GMP.TypeExt (popCountInteger, testBitInteger,
setBitInteger, clearBitInteger)
import qualified Data.BitSet.Generic as GS
-- | A wrapper around 'Integer' which provides faster bit-level operations.
newtype FasterInteger = FasterInteger { unFI :: Integer }
deriving (Read, Show, Eq, Ord, Enum, Integral, Num, Real, NFData)
instance Bits FasterInteger where
FasterInteger x .&. FasterInteger y = FasterInteger $ x .&. y
{-# INLINE (.&.) #-}
FasterInteger x .|. FasterInteger y = FasterInteger $ x .|. y
{-# INLINE (.|.) #-}
FasterInteger x `xor` FasterInteger y = FasterInteger $ x `xor` y
{-# INLINE xor #-}
complement = FasterInteger . complement . unFI
{-# INLINE complement #-}
shift (FasterInteger x) = FasterInteger . shift x
{-# INLINE shift #-}
rotate (FasterInteger x) = FasterInteger . rotate x
{-# INLINE rotate #-}
bit = FasterInteger . bit
{-# INLINE bit #-}
testBit (FasterInteger x) (I# i) = testBitInteger x i
{-# SPECIALIZE INLINE testBit :: FasterInteger -> Int -> Bool #-}
setBit (FasterInteger x) (I# i) = FasterInteger $ setBitInteger x i
{-# SPECIALIZE INLINE setBit :: FasterInteger -> Int -> FasterInteger #-}
clearBit (FasterInteger x) (I# i) = FasterInteger $ clearBitInteger x i
{-# SPECIALIZE INLINE clearBit :: FasterInteger -> Int -> FasterInteger #-}
popCount (FasterInteger x) = I# (popCountInteger x)
{-# SPECIALIZE INLINE popCount :: FasterInteger -> Int #-}
isSigned = isSigned . unFI
{-# INLINE isSigned #-}
bitSize _ = error "bitSize: FasterInteger does not support bitSize."
#if MIN_VERSION_base(4,7,0)
bitSizeMaybe _ = Nothing
{-# INLINE bitSizeMaybe #-}
#endif
type BitSet = GS.BitSet FasterInteger
-- | /O(1)/. Is the bit set empty?
null :: BitSet a -> Bool
null = GS.null
{-# INLINE null #-}
-- | /O(1)/. The number of elements in the bit set.
size :: BitSet a -> Int
size = GS.size
{-# INLINE size #-}
-- | /O(1)/. Ask whether the item is in the bit set.
member :: Enum a => a -> BitSet a -> Bool
member = GS.member
{-# INLINE member #-}
-- | /O(1)/. Ask whether the item is in the bit set.
notMember :: Enum a => a -> BitSet a -> Bool
notMember = GS.notMember
{-# INLINE notMember #-}
-- | /O(max(n, m))/. Is this a subset? (@s1 isSubsetOf s2@) tells whether
-- @s1@ is a subset of @s2@.
isSubsetOf :: BitSet a -> BitSet a -> Bool
isSubsetOf = GS.isSubsetOf
{-# INLINE isSubsetOf #-}
-- | /O(max(n, m)/. Is this a proper subset? (ie. a subset but not equal).
isProperSubsetOf :: BitSet a -> BitSet a -> Bool
isProperSubsetOf = GS.isProperSubsetOf
{-# INLINE isProperSubsetOf #-}
-- | The empty bit set.
empty :: Enum a => BitSet a
empty = GS.empty
{-# INLINE empty #-}
-- | O(1). Create a singleton set.
singleton :: Enum a => a -> BitSet a
singleton = GS.singleton
{-# INLINE singleton #-}
-- | /O(1)/. Insert an item into the bit set.
insert :: Enum a => a -> BitSet a -> BitSet a
insert = GS.insert
{-# INLINE insert #-}
-- | /O(1)/. Delete an item from the bit set.
delete :: Enum a => a -> BitSet a -> BitSet a
delete = GS.delete
{-# INLINE delete #-}
-- | /O(max(m, n))/. The union of two bit sets.
union :: BitSet a -> BitSet a -> BitSet a
union = GS.union
{-# INLINE union #-}
-- | /O(1)/. Difference of two bit sets.
difference :: BitSet a -> BitSet a -> BitSet a
difference = GS.difference
{-# INLINE difference #-}
-- | /O(1)/. See `difference'.
(\\) :: BitSet a -> BitSet a -> BitSet a
(\\) = difference
-- | /O(1)/. The intersection of two bit sets.
intersection :: BitSet a -> BitSet a -> BitSet a
intersection = GS.intersection
{-# INLINE intersection #-}
-- | /O(n)/ Transform this bit set by applying a function to every value.
-- Resulting bit set may be smaller then the original.
map :: (Enum a, Enum b) => (a -> b) -> BitSet a -> BitSet b
map = GS.map
{-# INLINE map #-}
-- | /O(n)/ Reduce this bit set by applying a binary function to all
-- elements, using the given starting value. Each application of the
-- operator is evaluated before before using the result in the next
-- application. This function is strict in the starting value.
foldl' :: Enum a => (b -> a -> b) -> b -> BitSet a -> b
foldl' = GS.foldl'
{-# INLINE foldl' #-}
-- | /O(n)/ Reduce this bit set by applying a binary function to all
-- elements, using the given starting value.
foldr :: Enum a => (a -> b -> b) -> b -> BitSet a -> b
foldr = GS.foldr
{-# INLINE foldr #-}
-- | /O(n)/ Filter this bit set by retaining only elements satisfying a
-- predicate.
filter :: Enum a => (a -> Bool) -> BitSet a -> BitSet a
filter = GS.filter
{-# INLINE filter #-}
-- | /O(n)/. Convert the bit set set to a list of elements.
toList :: Enum a => BitSet a -> [a]
toList = GS.toList
{-# INLINE toList #-}
-- | /O(n)/. Make a bit set from a list of elements.
fromList :: Enum a => [a] -> BitSet a
fromList = GS.fromList
{-# INLINE fromList #-}