All Projects → Bodigrim → Bitvec

Bodigrim / Bitvec

Licence: other
Bit vectors: 8x less memory, up to 1000x faster than Vector Bool

Programming Languages

haskell
3896 projects

Labels

Projects that are alternatives of or similar to Bitvec

bitmap
Bitmap Data Structure In Golang
Stars: ✭ 27 (-41.3%)
Mutual labels:  bitmap
Pixelate
Simple Android library to pixelate images or certain areas of an image.
Stars: ✭ 602 (+1208.7%)
Mutual labels:  bitmap
Doramon
常见工具汇总:一键式生成整个前后端工具,单机高性能幂等工具,zookeeper客户端工具,分布式全局id生成器,一致性哈希工具,Bitmap工具,布隆过滤器参数生成器,Yaml和properties互转工具等等
Stars: ✭ 24 (-47.83%)
Mutual labels:  bitmap
Screenshott
[Android Library] Take a screenshot of your view layout , programmatically!
Stars: ✭ 311 (+576.09%)
Mutual labels:  bitmap
Ployfun
LowPoly image processing./导入图片生成Low Poly风格图片的app
Stars: ✭ 507 (+1002.17%)
Mutual labels:  bitmap
Devutils
🔥 ( 持续更新,目前含 160+ 工具类 ) DevUtils 是一个 Android 工具库,主要根据不同功能模块,封装快捷使用的工具类及 API 方法调用。该项目尽可能的便于开发人员,快捷、高效开发安全可靠的项目。
Stars: ✭ 680 (+1378.26%)
Mutual labels:  bitmap
TakingImageOfAView
An example on how to take screenshot of a particular view
Stars: ✭ 15 (-67.39%)
Mutual labels:  bitmap
Libbmp
A simple Bitmap (BMP) library.
Stars: ✭ 30 (-34.78%)
Mutual labels:  bitmap
Glidebitmappool
Glide Bitmap Pool is a memory management library for reusing the bitmap memory
Stars: ✭ 544 (+1082.61%)
Mutual labels:  bitmap
Zpix Pixel Font
Zpix (最像素) is a pixel font supporting English, Traditional Chinese, Simplified Chinese and Japanese.
Stars: ✭ 916 (+1891.3%)
Mutual labels:  bitmap
Magicalcamera
A library to take picture easy, transform your data in different format and save photos in your device
Stars: ✭ 327 (+610.87%)
Mutual labels:  bitmap
Gaussianblur
An easy and fast library to apply gaussian blur filter on any images. 🎩
Stars: ✭ 473 (+928.26%)
Mutual labels:  bitmap
Instacapture
Android library to capture screenshot from your app
Stars: ✭ 681 (+1380.43%)
Mutual labels:  bitmap
Gostl
Data structure and algorithm library for go, designed to provide functions similar to C++ STL
Stars: ✭ 254 (+452.17%)
Mutual labels:  bitmap
Php Bitmap
Bitmap representation with bitwise operations
Stars: ✭ 7 (-84.78%)
Mutual labels:  bitmap
zigimg
Zig library for reading and writing different image formats
Stars: ✭ 112 (+143.48%)
Mutual labels:  bitmap
Quickshot
Capture images of any View, SurfaceView or Bitmap from your Android app in: .jpg .png or .nomedia with simple oneliner codes.
Stars: ✭ 663 (+1341.3%)
Mutual labels:  bitmap
Pencil
Pencil2D is an easy, intuitive tool to make 2D hand-drawn animations. Pencil2D is open source and cross-platform.
Stars: ✭ 968 (+2004.35%)
Mutual labels:  bitmap
Cal Bonus
竞足奖金计算算法
Stars: ✭ 11 (-76.09%)
Mutual labels:  bitmap
Scientifica
tall, condensed, bitmap font for geeks.
Stars: ✭ 821 (+1684.78%)
Mutual labels:  bitmap

bitvec GitHub Build Status Travis Build Status Hackage Hackage CI Stackage LTS Stackage Nightly Coverage Status

A newtype over Bool with a better Vector instance: 8x less memory, up to 1000x faster.

The vector package represents unboxed arrays of Bool spending 1 byte (8 bits) per boolean. This library provides a newtype wrapper Bit and a custom instance of unboxed Vector, which packs bits densely, achieving 8x less memory footprint. The performance stays mostly the same; the most significant degradation happens for random writes (up to 10% slower). On the other hand, for certain bulk bit operations Vector Bit is up to 1000x faster than Vector Bool.

Thread safety

  • Data.Bit is faster, but writes and flips are thread-unsafe. This is because naive updates are not atomic: read the whole word from memory, then modify a bit, then write the whole word back.
  • Data.Bit.ThreadSafe is slower (up to 20%), but writes and flips are thread-safe.

Quick start

Consider the following (very naive) implementation of the sieve of Eratosthenes. It returns a vector with True at prime indices and False at composite indices.

import Control.Monad
import Control.Monad.ST
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as MU

eratosthenes :: U.Vector Bool
eratosthenes = runST $ do
  let len = 100
  sieve <- MU.replicate len True
  MU.write sieve 0 False
  MU.write sieve 1 False
  forM_ [2 .. floor (sqrt (fromIntegral len))] $ \p -> do
    isPrime <- MU.read sieve p
    when isPrime $
      forM_ [2 * p, 3 * p .. len - 1] $ \i ->
        MU.write sieve i False
  U.unsafeFreeze sieve

We can switch from Bool to Bit just by adding newtype constructors:

import Data.Bit

import Control.Monad
import Control.Monad.ST
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector.Unboxed.Mutable as MU

eratosthenes :: U.Vector Bit
eratosthenes = runST $ do
  let len = 100
  sieve <- MU.replicate len (Bit True)
  MU.write sieve 0 (Bit False)
  MU.write sieve 1 (Bit False)
  forM_ [2 .. floor (sqrt (fromIntegral len))] $ \p -> do
    Bit isPrime <- MU.read sieve p
    when isPrime $
      forM_ [2 * p, 3 * p .. len - 1] $ \i ->
        MU.write sieve i (Bit False)
  U.unsafeFreeze sieve

Bit-based implementation requires 8x less memory to store the vector. For large sizes it allows to crunch more data in RAM without swapping. For smaller arrays it helps to fit into CPU caches.

> listBits eratosthenes
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

There are several high-level helpers, digesting bits in bulk, which makes them up to 64x faster than respective counterparts for Vector Bool. One can query population count (popcount) of a vector (giving us the prime-counting function):

> countBits eratosthenes
25

And vice versa, query an address of the n-th set bit (which corresponds to the n-th prime number here):

> nthBitIndex (Bit True) 10 eratosthenes
Just 29

One may notice that the order of the inner traversal by i does not matter and get tempted to run it in several parallel threads. In this case it is vital to switch from Data.Bit to Data.Bit.ThreadSafe, because the former is thread-unsafe with regards to writes. There is a moderate performance penalty (up to 20%) for using the thread-safe interface.

Sets

Bit vectors can be used as a blazingly fast representation of sets as long as their elements are Enumeratable and sufficiently dense, leaving IntSet far behind.

For example, consider three possible representations of a set of Word16:

  • As an IntSet with a readily available union function.
  • As a 64k-long unboxed Vector Bool, implementing union as zipWith (||).
  • As a 64k-long unboxed Vector Bit, implementing union as zipBits (.|.).

When flag libgmp is enabled, according to our benchmarks (see bench folder) the union of Vector Bit evaluates 24x-36x faster than the union of not-too-sparse IntSet and stunningly outperforms Vector Bool 500x-1000x.

Binary polynomials

Binary polynomials are polynomials with coefficients modulo 2. Their applications include coding theory and cryptography. While one can successfully implement them with poly package, operating on UPoly Bit, this package provides even faster arithmetic routines exposed via F2Poly data type and its instances.

> :set -XBinaryLiterals
> -- (1 + x) (1 + x + x^2) = 1 + x^3 (mod 2)
> 0b11 * 0b111 :: F2Poly
F2Poly {unF2Poly = [1,0,0,1]}

Use fromInteger / toInteger to convert binary polynomials from Integer to F2Poly and back.

Package flags

Similar packages

  • bv and bv-little do not offer mutable vectors.

  • array is memory-efficient for Bool, but lacks a handy Vector interface and is not thread-safe.

Additional resources

  • Bit vectors without compromises, Haskell Love, 31.07.2020: slides, video.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].