diff options
Diffstat (limited to 'src/Data/Bitfield.hs')
-rw-r--r-- | src/Data/Bitfield.hs | 20 |
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 | ||
129 | singleton :: PieceIx -> PieceCount -> Bitfield | ||
130 | singleton 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. |
129 | adjustSize :: PieceCount -> Bitfield -> Bitfield | 134 | adjustSize :: PieceCount -> Bitfield -> Bitfield |
@@ -152,6 +157,19 @@ totalCount = bfSize | |||
152 | completeness :: Bitfield -> Ratio PieceCount | 157 | completeness :: Bitfield -> Ratio PieceCount |
153 | completeness b = haveCount b % totalCount b | 158 | completeness b = haveCount b % totalCount b |
154 | 159 | ||
160 | inRange :: PieceIx -> Bitfield -> Bool | ||
161 | inRange ix bf @ Bitfield {..} = 0 <= ix && ix < bfSize | ||
162 | |||
163 | member :: PieceIx -> Bitfield -> Bool | ||
164 | member ix bf @ Bitfield {..} | ||
165 | | ix `inRange` bf = ix `S.member` bfSet | ||
166 | | otherwise = False | ||
167 | |||
168 | notMember :: PieceIx -> Bitfield -> Bool | ||
169 | notMember 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. |
156 | findMin :: Bitfield -> Maybe PieceIx | 174 | findMin :: Bitfield -> Maybe PieceIx |
157 | findMin Bitfield {..} | 175 | findMin Bitfield {..} |