Skip to content
A fast Bitset class in Swift
Branch: master
Clone or download
Latest commit fd0e5af Sep 25, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Fix build for swift 4.0.2 (#1) Nov 21, 2017
.travis.yml Activating Sep 19, 2018
AUTHORS adding author Sep 15, 2017
Package.swift Fixing up arrayLiteral Sep 25, 2018


Swift 4 compatible Build Status

A bitset class in Swift for fast and concise set operations over integers. Works under both Linux and MacOS. It is engineered to be really fast, on par with portable C/C++ implementations.

It can be orders of magnitude faster than an IndexSet:

$ git clone
$ cd SwiftBitsetBenchmark
$ swift build  -Xcc -march=native  --configuration release

$ $(swift build   --configuration release --show-bin-path)/SwiftBitsetBenchmark
testAddPerformance  10.693318  ms
testIndexSetAddPerformance  231.737616  ms

testCountPerformance  0.617098  ms
testIndexSetCountPerformance  0.007483  ms

testIteratorPerformance  5.503873  ms
testIndexSetIteratorPerformance  234.289692  ms

testIntersectionPerformance  3.157883  ms
testIndexSetIntersectionPerformance  2774.959423  ms


Complete example for library users

Create a directory where you will create your application:

mkdir fun
cd fun
swift package init --type executable

Then edit Package.swift so that it reads something like this:

import PackageDescription

let package = Package(
    name: "fun",
    dependencies: [
   .package(url: "",  from: "0.3.2")
    targets: [
            name: "fun",
            dependencies: ["Bitset"]),

Edit main.swift (in Sources) so that it looks something like this :

import Bitset;

let b1 = Bitset (arrayLiteral: 1,4,10,1000,10000);
for i in b1 {

You can run your example as follows:

swift build  -Xcc -march=native  --configuration release
$(swift build   --configuration release  --show-bin-path)/fun

Code example

import Bitset;

let b1 = Bitset ();
b1.add(10000) // can add one
b1.addMany(1,4,10,1000,10000); // can add many
let b2 = Bitset ();
let bexpected = Bitset (1,3,4,10,1000,10000); // can init with list
print(b2.count() == 6) // print true
print(b2 == bexpected) // print true
print(b1 == bexpected) // print true
for i in b1 {
// will print 1 4 10 1000 10000
b1.remove(4) // can remove values
let d1 = b1 & b2;// intersection
let d2 = b1 | b2;// union
let d3 = b1 - b2;// difference
let d4 = b1 ^ b2;// symmetric difference
b1 &= b2;// inplace intersection
b1 |= b2;// inplace union
b1 -= b2;// inplace difference
b1 ^= b2;// inplace symmetric difference

Usage for contributors

swift build -Xcc -march=native --configuration release
swift test # optional

To dissamble a function...

swift build -Xcc -march=native --configuration release
lldb ./.build/release/
disassemble -n intersectionCount

To benchmark from the command line:

swift test -Xswiftc -Ounchecked -s BitsetTests.BitsetTests/testForEachPerformance

For interactive use:

$ swift build -Xcc -march=native --configuration release
$ swift -I .build/release -L .build/release -lBitsetDynamic
  1> import Bitset
  2> let b1 = Bitset ()
  3> print(b1)

For Xcode users (Mac Only)

$ swift package generate-xcodeproj
generated: ./Bitset.xcodeproj
$ open ./SwiftBitset.xcodeproj


Apache 2.0

You can’t perform that action at this time.