bitarray 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6fcf6acdc1a14446a50e3cca4c8f96da1f36ab10
4
- data.tar.gz: dc2cc9af844024134c930ab9678be08731458a7a
2
+ SHA256:
3
+ metadata.gz: 5a056ce9816063f7e51c6fce69fb88c4644336236545613d4224977a01c50df8
4
+ data.tar.gz: f9f05b2a142f7cea4f529f9ebfbf9e053f635bb75d4ba44a10bdb6a2864c3f0d
5
5
  SHA512:
6
- metadata.gz: 3ed10001a59707f9837e62727cf3fbd338c7d5c14af834862d04338800a69639a178a824d0ab629883451f89cdda006d27d9c8116b5751fc351dd17c33878d6a
7
- data.tar.gz: 446232b243b040be2e7627b042ff5dc03db7c8ebd13ce4060ac2400299954d940c9aa97349a906a73170f5a1f401e3f7b5505d0d1f680c71781f114f1fa5d94e
6
+ metadata.gz: 7373fc1fc97187412eee8e854839961b7647f880afbffa8d4f0e261830d214edc4626763a100dcd71a8ce4dd05c2892522594d63c6d21f95be7bc413afc26c87
7
+ data.tar.gz: f884ad42cba8ce15fa2c3810427f510028db459f14080ca9bda02f823b35d27e00769e941cb5232fb6e0220ddc494d55e00843e5186d4d96a41e5b250754cfc5
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
- # BitArray: A simple bit-array/bitfield library in pure Ruby
1
+ # BitArray: Pure Ruby bit-array/bitfield library
2
2
 
3
- A simple, pure-Ruby 'bit field' object. Works well for Bloom filters (the use case for which I originally wrote it).
3
+ A simple, pure-Ruby 'bit field' object. Works well for Bloom filters (the use case for which I originally wrote it, although there are numerous good libraries for that task now).
4
4
 
5
- Originally written in 2007 and left without significant update until 2017, it has now been updated to work within a typical, modern Ruby environment while maintaining the same API.
5
+ Despite its age, BitArray has been updated to work within a typical, modern Ruby environment, but is only 'mildly' maintained.
6
6
 
7
7
  ## Installation
8
8
 
9
9
  ```ruby
10
- gem install bitarray
10
+ bundle add bitarray
11
11
  ```
12
12
 
13
13
  ## Examples
@@ -63,6 +63,7 @@ ba.to_s # "0000111111110000"
63
63
 
64
64
 
65
65
  ## History
66
+ - 1.3 in 2022 (cleanups and a minor perf tweak)
66
67
  - 1.2 in 2018 (Added option to skip reverse the bits for each byte by @dalibor)
67
68
  - 1.1 in 2018 (fixed a significant bug)
68
69
  - 1.0 in 2017 (updated for modern Ruby, more efficient storage, and 10th birthday)
@@ -77,8 +78,8 @@ ba.to_s # "0000111111110000"
77
78
 
78
79
  Thanks to Michael Slade for encouraging me to update this library on its 10th birthday and for suggesting finally using String's getbyte and setbyte methods now that we're all on 1.9+ compatible implementations.
79
80
 
80
- Further thanks to @tdeo, @JoshuaSP, @dalibor and @m1lt0n for pull requests.
81
+ Further thanks to @tdeo, @JoshuaSP, @dalibor, @yegct and @m1lt0n for pull requests.
81
82
 
82
83
  ## License
83
84
 
84
- MIT licensed. Copyright 2007-2018 Peter Cooper.
85
+ MIT licensed. Copyright 2007-2022 Peter Cooper.
@@ -3,7 +3,7 @@ class BitArray
3
3
  attr_reader :field
4
4
  include Enumerable
5
5
 
6
- VERSION = "1.2.0"
6
+ VERSION = "1.3.0"
7
7
  ELEMENT_WIDTH = 32
8
8
 
9
9
  def initialize(size, field = nil)
@@ -26,7 +26,8 @@ class BitArray
26
26
  end
27
27
 
28
28
  # Iterate over each bit
29
- def each(&block)
29
+ def each
30
+ return to_enum unless block_given?
30
31
  @size.times { |position| yield self[position] }
31
32
  end
32
33
 
@@ -34,10 +35,17 @@ class BitArray
34
35
  def to_s
35
36
  @field.collect{|ea| ("%0#{ELEMENT_WIDTH}b" % ea).reverse}.join[0..@size-1]
36
37
  end
38
+
39
+ # Iterate over each byte
40
+ def each_byte
41
+ return to_enum(:each_byte) unless block_given?
42
+ @field.each { |byte| yield byte }
43
+ end
37
44
 
38
45
  # Returns the total number of bits that are set
39
- # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
46
+ # Use Brian Kernighan's way, see
47
+ # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
40
48
  def total_set
41
- @field.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
49
+ @field.each_byte.inject(0) { |a, byte| (a += 1; byte &= byte - 1) while byte > 0 ; a }
42
50
  end
43
51
  end
data/lib/bitarray.rb CHANGED
@@ -3,7 +3,7 @@ class BitArray
3
3
  attr_reader :field
4
4
  include Enumerable
5
5
 
6
- VERSION = "1.2.0"
6
+ VERSION = "1.3.0"
7
7
 
8
8
  def initialize(size, field = nil, reverse_byte: true)
9
9
  @size = size
@@ -26,7 +26,8 @@ class BitArray
26
26
  end
27
27
 
28
28
  # Iterate over each bit
29
- def each(&block)
29
+ def each
30
+ return to_enum(:each) unless block_given?
30
31
  @size.times { |position| yield self[position] }
31
32
  end
32
33
 
@@ -38,11 +39,18 @@ class BitArray
38
39
  @field.bytes.collect { |ea| ("%08b" % ea) }.join[0, @size]
39
40
  end
40
41
  end
42
+
43
+ # Iterates over each byte
44
+ def each_byte
45
+ return to_enum(:each_byte) unless block_given?
46
+ @field.bytes.each{ |byte| yield byte }
47
+ end
41
48
 
42
49
  # Returns the total number of bits that are set
43
- # (The technique used here is about 6 times faster than using each or inject direct on the bitfield)
50
+ # Use Brian Kernighan's way, see
51
+ # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
44
52
  def total_set
45
- @field.bytes.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
53
+ @field.each_byte.inject(0) { |a, byte| (a += 1; byte &= byte - 1) while byte > 0 ; a }
46
54
  end
47
55
 
48
56
  def byte_position(position)
@@ -1,5 +1,5 @@
1
1
  require "minitest/autorun"
2
- require "bitarray"
2
+ require_relative "../lib/bitarray"
3
3
 
4
4
  class TestBitArray < Minitest::Test
5
5
  def setup
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitarray
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Cooper
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-09-13 00:00:00.000000000 Z
11
+ date: 2022-04-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -47,7 +47,6 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
49
  - Gemfile
50
- - Gemfile.lock
51
50
  - README.md
52
51
  - Rakefile
53
52
  - bitarray.gemspec
@@ -58,7 +57,7 @@ files:
58
57
  homepage: https://github.com/peterc/bitarray
59
58
  licenses: []
60
59
  metadata: {}
61
- post_install_message:
60
+ post_install_message:
62
61
  rdoc_options: []
63
62
  require_paths:
64
63
  - lib
@@ -73,9 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
72
  - !ruby/object:Gem::Version
74
73
  version: '0'
75
74
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 2.6.10
78
- signing_key:
75
+ rubygems_version: 3.2.22
76
+ signing_key:
79
77
  specification_version: 4
80
78
  summary: A simple, pure Ruby bit-array / bitfield implementation.
81
79
  test_files:
data/Gemfile.lock DELETED
@@ -1,21 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- bitarray (1.2.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- minitest (5.10.1)
10
- rake (12.0.0)
11
-
12
- PLATFORMS
13
- ruby
14
-
15
- DEPENDENCIES
16
- bitarray!
17
- minitest
18
- rake
19
-
20
- BUNDLED WITH
21
- 1.16.3