bitarray 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a056ce9816063f7e51c6fce69fb88c4644336236545613d4224977a01c50df8
4
- data.tar.gz: f9f05b2a142f7cea4f529f9ebfbf9e053f635bb75d4ba44a10bdb6a2864c3f0d
3
+ metadata.gz: 8dfa5accb0957e88fd6dca488250f0f22495f1ce5acdd3de74477d644adc7f95
4
+ data.tar.gz: aa10a826fd23bd077346993395ea47a0be789c4c98d5a492b15d9e70943746f3
5
5
  SHA512:
6
- metadata.gz: 7373fc1fc97187412eee8e854839961b7647f880afbffa8d4f0e261830d214edc4626763a100dcd71a8ce4dd05c2892522594d63c6d21f95be7bc413afc26c87
7
- data.tar.gz: f884ad42cba8ce15fa2c3810427f510028db459f14080ca9bda02f823b35d27e00769e941cb5232fb6e0220ddc494d55e00843e5186d4d96a41e5b250754cfc5
6
+ metadata.gz: 63cb034bc5b88a49eeebc8b056c50486959f9a359c1ad45513e14ec66f269962607a4d4829af7bfbde7499db6e9b1bf818d9d7bcce21579feffe59582b455c45
7
+ data.tar.gz: b47a38d9f6a7c9472cac1182c9fe3b6bef6c577812ce9310061ec0d81e3bcba2317fdc951458485b2446ee0ce37b79327cd828c70da31a6f205d6e099f56c46b
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2007-2024 Peter Cooper
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
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, although there are numerous good libraries for that task now).
3
+ A simple, pure-Ruby 'bit field' object.
4
4
 
5
- Despite its age, BitArray has been updated to work within a typical, modern Ruby environment, but is only 'mildly' maintained.
5
+ Originally built to help power a bloom filter, although there are other higher level libraries for that task now (https://github.com/igrigorik/bloomfilter-rb is a popular one.)
6
+
7
+ BitArray has changed little over the years, but has been maintained to work within a typical, modern Ruby environment and works with Ruby 2.1+ through Ruby 3.x.
6
8
 
7
9
  ## Installation
8
10
 
@@ -61,8 +63,9 @@ ba = BitArray.new(16, ["0000111111110000"].pack('B*'), reverse_byte: false)
61
63
  ba.to_s # "0000111111110000"
62
64
  ```
63
65
 
64
-
65
66
  ## History
67
+ - 1.3.2 in 2026 (minor cleanup, faster total_set)
68
+ - 1.3.1 in 2024 (no changes other than adding license to gemspec)
66
69
  - 1.3 in 2022 (cleanups and a minor perf tweak)
67
70
  - 1.2 in 2018 (Added option to skip reverse the bits for each byte by @dalibor)
68
71
  - 1.1 in 2018 (fixed a significant bug)
@@ -82,4 +85,4 @@ Further thanks to @tdeo, @JoshuaSP, @dalibor, @yegct and @m1lt0n for pull reques
82
85
 
83
86
  ## License
84
87
 
85
- MIT licensed. Copyright 2007-2022 Peter Cooper.
88
+ MIT licensed. Copyright 2007-2026 Peter Cooper.
data/bitarray.gemspec CHANGED
@@ -11,11 +11,12 @@ Gem::Specification.new do |s|
11
11
  s.homepage = "https://github.com/peterc/bitarray"
12
12
  s.summary = %q{A simple, pure Ruby bit-array / bitfield implementation.}
13
13
  s.description = %q{A simple, pure Ruby bit-array / bitfield implementation.}
14
+ s.license = "MIT"
14
15
 
15
16
  s.files = `git ls-files`.split("\n")
16
- s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
17
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
18
  s.require_paths = ["lib"]
19
+ s.required_ruby_version = ">= 2.1"
19
20
 
20
21
  s.add_development_dependency "rake"
21
22
  s.add_development_dependency "minitest"
@@ -0,0 +1,56 @@
1
+ class BitArray
2
+ attr_reader :field, :reverse_byte, :size
3
+ include Enumerable
4
+
5
+ VERSION = "1.3.2"
6
+
7
+ def initialize(size, field = nil, reverse_byte: true)
8
+ @size = size
9
+ @field = field || "\0" * (size / 8 + 1)
10
+ @reverse_byte = reverse_byte
11
+ end
12
+
13
+ # Set a bit (1/0)
14
+ def []=(position, value)
15
+ if value == 1
16
+ @field.setbyte(position >> 3, @field.getbyte(position >> 3) | (1 << (byte_position(position) % 8)))
17
+ else
18
+ @field.setbyte(position >> 3, @field.getbyte(position >> 3) & ~(1 << (byte_position(position) % 8)))
19
+ end
20
+ end
21
+
22
+ # Read a bit (1/0)
23
+ def [](position)
24
+ (@field.getbyte(position >> 3) & (1 << (byte_position(position) % 8))) > 0 ? 1 : 0
25
+ end
26
+
27
+ # Iterate over each bit
28
+ def each
29
+ return to_enum(:each) unless block_given?
30
+ @size.times { |position| yield self[position] }
31
+ end
32
+
33
+ # Returns the field as a string like "0101010100111100," etc.
34
+ def to_s
35
+ if @reverse_byte
36
+ @field.bytes.collect { |ea| ("%08b" % ea).reverse }.join[0, @size]
37
+ else
38
+ @field.bytes.collect { |ea| ("%08b" % ea) }.join[0, @size]
39
+ end
40
+ end
41
+
42
+ # Iterates over each byte
43
+ def each_byte
44
+ return to_enum(:each_byte) unless block_given?
45
+ @field.bytes.each{ |byte| yield byte }
46
+ end
47
+
48
+ # Returns the total number of bits that are set
49
+ def total_set
50
+ @field.unpack1('B*').count('1')
51
+ end
52
+
53
+ private def byte_position(position)
54
+ @reverse_byte ? position : 7 - position
55
+ end
56
+ end
data/lib/bitarray.rb CHANGED
@@ -1,59 +1 @@
1
- class BitArray
2
- attr_reader :size
3
- attr_reader :field
4
- include Enumerable
5
-
6
- VERSION = "1.3.0"
7
-
8
- def initialize(size, field = nil, reverse_byte: true)
9
- @size = size
10
- @field = field || "\0" * (size / 8 + 1)
11
- @reverse_byte = reverse_byte
12
- end
13
-
14
- # Set a bit (1/0)
15
- def []=(position, value)
16
- if value == 1
17
- @field.setbyte(position >> 3, @field.getbyte(position >> 3) | (1 << (byte_position(position) % 8)))
18
- else
19
- @field.setbyte(position >> 3, @field.getbyte(position >> 3) & ~(1 << (byte_position(position) % 8)))
20
- end
21
- end
22
-
23
- # Read a bit (1/0)
24
- def [](position)
25
- (@field.getbyte(position >> 3) & (1 << (byte_position(position) % 8))) > 0 ? 1 : 0
26
- end
27
-
28
- # Iterate over each bit
29
- def each
30
- return to_enum(:each) unless block_given?
31
- @size.times { |position| yield self[position] }
32
- end
33
-
34
- # Returns the field as a string like "0101010100111100," etc.
35
- def to_s
36
- if @reverse_byte
37
- @field.bytes.collect { |ea| ("%08b" % ea).reverse }.join[0, @size]
38
- else
39
- @field.bytes.collect { |ea| ("%08b" % ea) }.join[0, @size]
40
- end
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
48
-
49
- # Returns the total number of bits that are set
50
- # Use Brian Kernighan's way, see
51
- # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
52
- def total_set
53
- @field.each_byte.inject(0) { |a, byte| (a += 1; byte &= byte - 1) while byte > 0 ; a }
54
- end
55
-
56
- def byte_position(position)
57
- @reverse_byte ? position : 7 - position
58
- end
59
- end
1
+ require_relative "bitarray/bit_array"
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitarray
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Cooper
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-04-04 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rake
@@ -47,17 +46,18 @@ extra_rdoc_files: []
47
46
  files:
48
47
  - ".gitignore"
49
48
  - Gemfile
49
+ - LICENSE
50
50
  - README.md
51
51
  - Rakefile
52
52
  - bitarray.gemspec
53
- - lib/bitarray-array.rb
54
53
  - lib/bitarray.rb
54
+ - lib/bitarray/bit_array.rb
55
55
  - memory-test.rb
56
- - test/test_bitarray.rb
56
+ - test/test_bit_array.rb
57
57
  homepage: https://github.com/peterc/bitarray
58
- licenses: []
58
+ licenses:
59
+ - MIT
59
60
  metadata: {}
60
- post_install_message:
61
61
  rdoc_options: []
62
62
  require_paths:
63
63
  - lib
@@ -65,16 +65,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: '2.1'
69
69
  required_rubygems_version: !ruby/object:Gem::Requirement
70
70
  requirements:
71
71
  - - ">="
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []
75
- rubygems_version: 3.2.22
76
- signing_key:
75
+ rubygems_version: 3.6.9
77
76
  specification_version: 4
78
77
  summary: A simple, pure Ruby bit-array / bitfield implementation.
79
- test_files:
80
- - test/test_bitarray.rb
78
+ test_files: []
@@ -1,51 +0,0 @@
1
- class BitArray
2
- attr_reader :size
3
- attr_reader :field
4
- include Enumerable
5
-
6
- VERSION = "1.3.0"
7
- ELEMENT_WIDTH = 32
8
-
9
- def initialize(size, field = nil)
10
- @size = size
11
- @field = field || Array.new(((size - 1) / ELEMENT_WIDTH) + 1, 0)
12
- end
13
-
14
- # Set a bit (1/0)
15
- def []=(position, value)
16
- if value == 1
17
- @field[position / ELEMENT_WIDTH] |= 1 << (position % ELEMENT_WIDTH)
18
- elsif (@field[position / ELEMENT_WIDTH]) & (1 << (position % ELEMENT_WIDTH)) != 0
19
- @field[position / ELEMENT_WIDTH] ^= 1 << (position % ELEMENT_WIDTH)
20
- end
21
- end
22
-
23
- # Read a bit (1/0)
24
- def [](position)
25
- @field[position / ELEMENT_WIDTH] & 1 << (position % ELEMENT_WIDTH) > 0 ? 1 : 0
26
- end
27
-
28
- # Iterate over each bit
29
- def each
30
- return to_enum unless block_given?
31
- @size.times { |position| yield self[position] }
32
- end
33
-
34
- # Returns the field as a string like "0101010100111100," etc.
35
- def to_s
36
- @field.collect{|ea| ("%0#{ELEMENT_WIDTH}b" % ea).reverse}.join[0..@size-1]
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
44
-
45
- # Returns the total number of bits that are set
46
- # Use Brian Kernighan's way, see
47
- # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
48
- def total_set
49
- @field.each_byte.inject(0) { |a, byte| (a += 1; byte &= byte - 1) while byte > 0 ; a }
50
- end
51
- end
File without changes