Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc. bitset extensions and bug fixes #11

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2473882
Added #dup, #each_set, and #empty?
ericboesch Aug 9, 2013
104e627
More examples
ericboesch Aug 12, 2013
6300326
Added #==, #reverse, and #select_bits. Use more macros for speed.
ericboesch Aug 15, 2013
2f37c07
Use ITS macro to avoid malfunction when len==0
ericboesch Aug 27, 2013
56b93f9
Version update
ericboesch Aug 27, 2013
f0756b4
rdoctask obsolete
ericboesch Feb 22, 2016
b66f067
Quick mention for #dup, clone, Marshal.dump, Marshal.load, #pack, and…
ericboesch Feb 23, 2016
54f1bba
Updated gemspec for 0.3
ericboesch Feb 23, 2016
e5fd0ea
Updated version for 0.3
ericboesch Feb 23, 2016
23e64df
replace #cardinality that fails for size multiple of 64 with #not tha…
ericboesch Feb 23, 2016
eff354b
Replace obsolete .should with expect; added new tests
ericboesch Feb 23, 2016
c95e984
Add Bitset#pack, Bitset.unpack
ericboesch Feb 23, 2016
daed124
added #union_mutable, #intersect_mutable, #difference_mutable, #xor_m…
gabrielformica Mar 30, 2017
fc7f410
Merge pull request #1 from gabrielformica/master
ericboesch Apr 5, 2017
d409b0c
Tyler passed maintenance to Eric; added tests and docs for Gabriel's …
ericboesch May 15, 2017
08cd076
Updated gemspec
ericboesch May 16, 2017
15c5698
Document #values_at and #to_a aliases, plus #to_binary_array
ericboesch May 26, 2017
1287f03
Merging Brendon McLean's additions: allow passing an array to #set an…
ericboesch May 26, 2017
796a6ba
Fix warnings, most of them bogus C-1990 ones.
ericboesch May 26, 2017
6f829da
Added additional credits
ericboesch May 26, 2017
c549d0d
Version bump
ericboesch May 26, 2017
6b78b03
rebuilt
ericboesch May 26, 2017
dd2d6fa
Instead of relying on gcc builtins directly, use Evan Nemerson's port…
ericboesch May 26, 2017
f011a55
Send output to bitset/bitset directory to avoid conflict with bitset.rb
ericboesch May 26, 2017
0f9c76b
File was formerly inaccessible
ericboesch May 26, 2017
2cfe63e
Added some of Brendan McLean's tests, plus #pack and .unpack
ericboesch May 26, 2017
722289d
File was formerly inaccessible
ericboesch May 26, 2017
111e6ae
Version bump to 1.1.0
ericboesch Jun 20, 2017
2bb286e
Added [index, [len]] as optional arguments to #each_set
ericboesch Jan 6, 2018
92c8315
Added initialize by array and #inspect
ericboesch May 1, 2018
1b16230
Regenerate gemspec for version 1.2.0
ericboesch May 1, 2018
e338bd0
YARD docs correction
ericboesch May 1, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 98 additions & 14 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Usually you want to do this:
But if you want the latest patches or want to work on it yourself, you may want
to do this:

git clone git://github.com/tyler/bitset.git
git clone git://github.com/ericboesch/bitset.git
cd bitset
rake build
gem install pkg/bitset-<version>.gem
Expand All @@ -35,53 +35,137 @@ We can also create a bitset based on a string of ones and zeros.
>> Bitset.from_s('00010001')
=> 00010001

or from an array. Falsey values (false and nil) are converted to
zeroes; all other values, including 0 and "", are converted to ones.

>> Bitset.new [false, nil, 3, 0]
=> 0011

To input an array of ones and zeroes:

>> Bitset.new([0,1,1,0].map(&:positive?))
=> 0110

Obviously you can also set and clear bits...

>> bitset = Bitset.new(8)
=> 00000000

>> bitset[3] = true
=> 00010000

>> bitset[3] = false
=> 00000000

>> bitset.set(1, 3, 5, 7)
=> 01010101

>> bitset.clear(1, 5)
=> 00010001

Arrays of Integers can also be passed to #clear and #set (c/o brendon9x).

The point of a bitset is to be, effectively, an array of single bits. It should
support basic set and bitwise operations. So, let's look at a few of those.

>> a = Bitset.from_s('00001111')
=> 00001111

>> b = Bitset.from_s('01010101')
=> 01010101

>> a & b
=> 00000101

>> a | b
=> 01011111

>> b - a
=> 01010000

>> a ^ b
=> 01011010

>> ~a
=> 11110000

>> a.hamming(b)
=> 4

>> a.cardinality
=> 4

>> a.reverse
=> 11110000

# Tell whether all of the given bit numbers are set
>> a.set? 6
=> true

# Return a new Bitset composed of bits #1, #3, #5, #4, and #1
# again. Unlike Array#values_at, this function currently only
# accepts an array of Fixnums as its argument.
>> a.values_at [1,3,5,4,1]
=> 00110

# Tell whether all of the given bit numbers are clear
>> a.clear? 1,3,5
=> false

# Tell whether all bits are clear
>> a.empty?
=> false

# Pass all bits to the block
>> b.each { |v| puts v }
=> false
true
false
...

# Pass the positions of all set bits to the block
>> b.each_set { |bit| puts bit }
=> 1
3
5
7

# Return an array of the positions of all set bits
>> b.each_set # AKA b.to_a
=> [1, 3, 5, 7]

# b.each_set(index) == b.each_set[index], but faster.
>> b.each_set(-3) # Negative index wraps around.
=> 3

# b.each_set(index, len) == b.each_set[index, len], but faster.
>> b.each_set(2,2) # Block is also allowed
=> [5,7]


# The following methods modify a Bitset in place very quickly:
>> a.intersect!(b) # like a &= b
>> a.union!(b) # like a |= b
>> a.difference!(b) # like a -= b
>> a.xor!(b) # like a ^= b
>> a.reset! # Zeroes all bits

# Above, "like" does not mean "identical to." a |= b creates a new
# Bitset object. a.union!(b) changes an existing object which
# affects all variables that point to the same object.

# Attempting to apply bitwise binary operators or their in-place
# equivalents between bitsets of different sizes will raise an
# ArgumentError.

>> b.to_binary_array
=> [0, 1, 0, 1, 0, 1, 0, 1]

# b.dup and b.clone are also available.

# Marshal.dump and Marshal.load are also supported. If you want to
# save a few bytes and don't need Marshal.load to work, you can
# use #pack and Bitset.unpack instead.

Contributing
------------
Expand All @@ -98,4 +182,4 @@ License
See LICENSE.txt.


### Thanks for using Bitset!
### Thanks for using Bitset!
12 changes: 7 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ task :default => :spec
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = "bitset"
gem.homepage = "http://github.com/tyler/bitset"
gem.homepage = "http://github.com/ericboesch/bitset"
gem.license = "MIT"
gem.summary = 'Bitset implementation.'
gem.description = 'A fast C-based Bitset. It supports the standard set operations as well as operations you may expect on bit arrays. (popcount, for instance)'
gem.email = "[email protected]"
gem.description = 'A fast C-based Bitset. It supports the standard set operations as well as operations you may expect on bit arrays,such as popcount.'
gem.email = "[email protected]"
gem.authors = ["Tyler McMullen"]
# Other significant contributions from Eric Boesch, Gabriel Formica, and Brendon McLean.

end
Jeweler::RubygemsDotOrgTasks.new

require 'rake/rdoctask'
Rake::RDocTask.new do |rdoc|
require 'rdoc/task'
RDoc::Task.new do |rdoc|
version = File.exist?('VERSION') ? File.read('VERSION') : ""

rdoc.rdoc_dir = 'rdoc'
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
1.2.0
48 changes: 20 additions & 28 deletions bitset.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,40 @@
# DO NOT EDIT THIS FILE DIRECTLY
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
# -*- encoding: utf-8 -*-
# stub: bitset 1.2.0 ruby lib
# stub: ext/bitset/extconf.rb

Gem::Specification.new do |s|
s.name = %q{bitset}
s.version = "0.1.0"
s.name = "bitset".freeze
s.version = "1.2.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Tyler McMullen"]
s.date = %q{2011-03-03}
s.description = %q{A fast C-based Bitset. It supports the standard set operations as well as operations you may expect on bit arrays. (popcount, for instance)}
s.email = %q{[email protected]}
s.extensions = ["ext/bitset/extconf.rb"]
s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
s.require_paths = ["lib".freeze]
s.authors = ["Tyler McMullen".freeze]
s.date = "2018-05-01"
s.description = "A fast C-based Bitset. It supports the standard set operations as well as operations you may expect on bit arrays,such as popcount.".freeze
s.email = "[email protected]".freeze
s.extensions = ["ext/bitset/extconf.rb".freeze]
s.extra_rdoc_files = [
"LICENSE.txt",
"README.rdoc"
"README.markdown"
]
s.files = [
"LICENSE.txt",
"README.rdoc",
"README.markdown",
"Rakefile",
"VERSION",
"bitset.gemspec",
"ext/bitset/bitset.c",
"ext/bitset/builtin.h",
"ext/bitset/exact-int.h",
"ext/bitset/extconf.rb",
"lib/bitset.rb",
"spec/bitset_spec.rb"
]
s.homepage = %q{http://github.com/tyler/bitset}
s.licenses = ["MIT"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.7}
s.summary = %q{Bitset implementation.}
s.test_files = [
"spec/bitset_spec.rb"
]

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 3

if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
else
end
else
end
s.homepage = "http://github.com/ericboesch/bitset".freeze
s.licenses = ["MIT".freeze]
s.rubygems_version = "2.6.14".freeze
s.summary = "Bitset implementation.".freeze
end

Loading