summaryrefslogtreecommitdiff
path: root/src/Data/Bitfield.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Data/Bitfield.hs')
-rw-r--r--src/Data/Bitfield.hs20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/Data/Bitfield.hs b/src/Data/Bitfield.hs
index 3c278691..9c62e183 100644
--- a/src/Data/Bitfield.hs
+++ b/src/Data/Bitfield.hs
@@ -31,12 +31,14 @@ module Data.Bitfield
31 ( PieceIx, PieceCount, Bitfield 31 ( PieceIx, PieceCount, Bitfield
32 32
33 -- * Construction 33 -- * Construction
34 , haveAll, haveNone, have 34 , haveAll, haveNone, have, singleton
35 , adjustSize 35 , adjustSize
36 36
37 -- * Query 37 -- * Query
38 , Data.Bitfield.null 38 , Data.Bitfield.null
39 , haveCount, totalCount, completeness 39 , haveCount, totalCount, completeness
40
41 , member, notMember
40 , findMin, findMax 42 , findMin, findMax
41 43
42 , Frequency, frequencies, rarest 44 , Frequency, frequencies, rarest
@@ -124,6 +126,9 @@ have ix Bitfield {..}
124 | 0 <= ix && ix < bfSize = Bitfield bfSize (S.insert ix bfSet) 126 | 0 <= ix && ix < bfSize = Bitfield bfSize (S.insert ix bfSet)
125 | otherwise = Bitfield bfSize bfSet 127 | otherwise = Bitfield bfSize bfSet
126 128
129singleton :: PieceIx -> PieceCount -> Bitfield
130singleton ix pc = have ix (haveNone pc)
131
127-- | Assign new size to bitfield. FIXME Normally, size should be only 132-- | Assign new size to bitfield. FIXME Normally, size should be only
128-- decreased, otherwise exception raised. 133-- decreased, otherwise exception raised.
129adjustSize :: PieceCount -> Bitfield -> Bitfield 134adjustSize :: PieceCount -> Bitfield -> Bitfield
@@ -152,6 +157,19 @@ totalCount = bfSize
152completeness :: Bitfield -> Ratio PieceCount 157completeness :: Bitfield -> Ratio PieceCount
153completeness b = haveCount b % totalCount b 158completeness b = haveCount b % totalCount b
154 159
160inRange :: PieceIx -> Bitfield -> Bool
161inRange ix bf @ Bitfield {..} = 0 <= ix && ix < bfSize
162
163member :: PieceIx -> Bitfield -> Bool
164member ix bf @ Bitfield {..}
165 | ix `inRange` bf = ix `S.member` bfSet
166 | otherwise = False
167
168notMember :: PieceIx -> Bitfield -> Bool
169notMember ix bf @ Bitfield {..}
170 | ix `inRange` bf = ix `S.notMember` bfSet
171 | otherwise = True
172
155-- | Find first available piece index. 173-- | Find first available piece index.
156findMin :: Bitfield -> Maybe PieceIx 174findMin :: Bitfield -> Maybe PieceIx
157findMin Bitfield {..} 175findMin Bitfield {..}