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 +5 -5
- data/README.md +7 -6
- data/lib/bitarray-array.rb +12 -4
- data/lib/bitarray.rb +12 -4
- data/test/test_bitarray.rb +1 -1
- metadata +6 -8
- data/Gemfile.lock +0 -21
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5a056ce9816063f7e51c6fce69fb88c4644336236545613d4224977a01c50df8
|
4
|
+
data.tar.gz: f9f05b2a142f7cea4f529f9ebfbf9e053f635bb75d4ba44a10bdb6a2864c3f0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7373fc1fc97187412eee8e854839961b7647f880afbffa8d4f0e261830d214edc4626763a100dcd71a8ce4dd05c2892522594d63c6d21f95be7bc413afc26c87
|
7
|
+
data.tar.gz: f884ad42cba8ce15fa2c3810427f510028db459f14080ca9bda02f823b35d27e00769e941cb5232fb6e0220ddc494d55e00843e5186d4d96a41e5b250754cfc5
|
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
# BitArray:
|
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
|
-
|
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
|
-
|
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-
|
85
|
+
MIT licensed. Copyright 2007-2022 Peter Cooper.
|
data/lib/bitarray-array.rb
CHANGED
@@ -3,7 +3,7 @@ class BitArray
|
|
3
3
|
attr_reader :field
|
4
4
|
include Enumerable
|
5
5
|
|
6
|
-
VERSION = "1.
|
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
|
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
|
-
#
|
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
|
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.
|
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
|
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
|
-
#
|
50
|
+
# Use Brian Kernighan's way, see
|
51
|
+
# https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
|
44
52
|
def total_set
|
45
|
-
@field.
|
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)
|
data/test/test_bitarray.rb
CHANGED
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.
|
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:
|
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
|
-
|
77
|
-
|
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