Git Product home page Git Product logo

bitset's Introduction

bitset Build Status

A bit set is a compact data structure, which maintains a set of members from a type that can be enumerated (i. e. has an Enum instance). Current implementation is abstract with respect to conatiner type, so any numeric type with Bits instance can be used as a container.

Here's a usage example for a dynamic bit set, which uses a tweaked version of Integer for storing bits:

import Data.BitSet (BitSet, (\\))
import qualified Data.BitSet as BitSet

data Color = Red | Green | Blue deriving (Show, Enum)

main :: IO ()
main = print $ bs \\ BitSet.singleton Blue where
  bs :: BitSet Color
  bs = BitSet.fromList [Red, Green, Blue]

The API is exactly the same for a static bitset, based on native Word. Here's an example from [hen] hen library, which uses Data.BitSet to store Xen domain status flags:

import qualified Data.BitSet.Word as BS

data DomainFlag = Dying
                | Crashed
                | Shutdown
                | Paused
                | Blocked
                | Running
                | HVM
                | Debugged
    deriving (Enum, Show)

isAlive :: DomainFlag -> Bool
isAlive = not . BS.null . BS.intersect (BS.fromList [Crashed, Shutdown])

Benchmarks

To run benchmarks, configure cabal with benchmarks and build:

$ cabal-dev install-deps --enable-benchmarks && cabal-dev build
$ ./dist/build/bitset-benchmarks/bitset-benchmarks -o dist/bench.html

bitset's People

Contributors

knsd avatar superbobry avatar treeowl avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

bitset's Issues

Update for GHC 8.2

The library currently does not work on GHC 8.2 which has been out for couple of months now. Please update the dependencies and the code to make it work on GHC 8.2.

Building requires compiler change on OS X 10.9

The default C compiler on newer versions of OS X is clang rather than gcc. Using clang, the bitset installation fails as it can't find the "./gmp.h" include. For now, using cabal install bitset --with-gcc=$MY_GCC works, but it would be nice if the build worked with clang as well. Thanks!

segfault with Data.BitSet.Dynamic

with ghc-7.6.3, bitset-1.4.7 (current from hackage), on x86_64 fedora, in ghci I get this:

import Data.BitSet.Dynamic
fromList [11,63]

Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package bitset-1.4.7 ... linking ... done.
fromList [3,7,8,10,11,14,15,17,20,21,22,25,28,29,30,33,37,40,41,42,43,44,45,46,63]
ghc: internal error: evacuate: strange closure type 1103044792
    (GHC version 7.6.3 for x86_64_unknown_linux)

import Data.BitSet.Dynamic
fromList [1,64]

Loading package array-0.4.0.1 ... linking ... done.
Loading package deepseq-1.3.0.1 ... linking ... done.
Loading package bitset-1.4.7 ... linking ... done.
fromList [3,5,8,9,13,15,18,19,20,36,37,38,39,40,41,42,43,44,45,46,64]
Segmentation fault (core dumped)

it seems to work ok with Data.BitSet.Generic; so it would depend on a difference between Integer and FasterInteger?

Can't cabal install bitset-1.4.8 on Windows with GHC-7.8.4

The error is typical enough:

Configuring bitset-1.4.8...
setup.exe: Missing dependency on a foreign library:
* Missing C library: gmp-10
This problem can usually be solved by installing the system package that
provides this library (you may need the "-dev" version). If the library is
already installed but in a non-standard location then you can use the flags
--extra-include-dirs= and --extra-lib-dirs= to specify where it is.
cabal: Error: some packages failed to install:
bitset-1.4.8 failed during the configure step. The exception was:
ExitFailure 1

However, the problem persists even with the appropriate mingw developer libraries installed, where libgmp.dll.a, gmp.h and libgmp-10.dll are all on the library (via cabal --extra-lib-dirs), include (via cabal --extra-include-dirs) and binary (PATH) paths, the error still occurs.

Any thoughts on the issue? Is cabal perhaps failing to identify one or more files by name?

I'm not even sure how cabal could find the files based on a single Extra-libraries clause without a pkg-config.

Bitset without size field

Would it make sense to have an alternative GBitset which is just a newtype of the container field? I tried thinking through the various costs and benefits, but couldn't really decide:

  • adding, removing, union, and intersection would be faster
  • querying size size slower
  • Exactly matches C bitsets for FFI usage (how I often like to use bitsets)
    • if it fits in a register, less marshaling needed across the FFI boundary? I read some stuff, but decided I still didn't fully understand GHC's inner workings well enough to predict an answer.

Even if the benefits are slim, the current GBitset could easily be implemented in terms of this GBitSet, so the code bloat would be minimal.

If you all would be interested in this, or at least benchmarking it, I would be happy to make and submit a pull request.

GHCi failure when using BitSet

Prelude Data.BitSet> insert 42 empty
lookupSymbol failed in relocateSection (RELOC_GOT)
dist/build/GHC/Integer/GMP/PrimExt.o: unknown symbol `_integer_cmm_clearBitIntegerzh'

Doesn't build with GHC 7.10

Sadly, relaxing package upper bounds doesn't just work:

$ ghc --version
The Glorious Glasgow Haskell Compilation System, version 7.10.1

$ cabal install bitset==1.4.8 --allow-newer
Configuring bitset-1.4.8...
Building bitset-1.4.8...
Preprocessing library bitset-1.4.8...

src/GHC/Integer/GMP/TypeExt.hs:16:8:
    Could not find module ‘GHC.Integer.GMP.Prim’
    Use -v to see a list of the files searched for.
Failed to install bitset-1.4.8

Document Enum choice restriction

If fromEnum can produce negative numbers (e.g., if you try to make a BitSet Int), then everything breaks badly (GMP exceptions). As far as I can tell, this is not documented.

Speed up 'toList' and 'fromList'

I think the bottleneck is in 'clearBit' and 'setBit' function. One straightforward way to speed them up is provide a custom 'bit' implementation for 'FasterInteger'.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.