bitarray 1.2.0 → 1.3.1

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: 5f87879a065d489418062f6fb66b5fdf3e9b07360e60491ed7a9908a2c0ac59f
4
+ data.tar.gz: eec43c2f9a2589e0d3ca49888229eb54cbf37195f5ea5d2ecd237faca24753a9
5
5
  SHA512:
6
- metadata.gz: 3ed10001a59707f9837e62727cf3fbd338c7d5c14af834862d04338800a69639a178a824d0ab629883451f89cdda006d27d9c8116b5751fc351dd17c33878d6a
7
- data.tar.gz: 446232b243b040be2e7627b042ff5dc03db7c8ebd13ce4060ac2400299954d940c9aa97349a906a73170f5a1f401e3f7b5505d0d1f680c71781f114f1fa5d94e
6
+ metadata.gz: 83149918dc115df376c8cf01432d0573b26524aac7fdbfac64d229ac70d881f64e60cbbcf15ec9a978acb60c9bfbef62b253165d8f22e5e621c3b374b9389a9a
7
+ data.tar.gz: 54c18727b4264b934ef16529b6098ae221f9d10b74a1b39f0ed3d825c42120c8bf79cd95433192f72729b340e17e77cdf1738c9463c75b21bfaf4b57007bea94
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,13 +1,15 @@
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.
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
+ 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, as of February 2024, is confirmed to work with both Ruby 3.0.1 and Ruby 3.3.0.
6
8
 
7
9
  ## Installation
8
10
 
9
11
  ```ruby
10
- gem install bitarray
12
+ bundle add bitarray
11
13
  ```
12
14
 
13
15
  ## Examples
@@ -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.1 in 2024 (no changes other than adding license to gemspec)
68
+ - 1.3 in 2022 (cleanups and a minor perf tweak)
66
69
  - 1.2 in 2018 (Added option to skip reverse the bits for each byte by @dalibor)
67
70
  - 1.1 in 2018 (fixed a significant bug)
68
71
  - 1.0 in 2017 (updated for modern Ruby, more efficient storage, and 10th birthday)
@@ -77,8 +80,8 @@ ba.to_s # "0000111111110000"
77
80
 
78
81
  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
82
 
80
- Further thanks to @tdeo, @JoshuaSP, @dalibor and @m1lt0n for pull requests.
83
+ Further thanks to @tdeo, @JoshuaSP, @dalibor, @yegct and @m1lt0n for pull requests.
81
84
 
82
85
  ## License
83
86
 
84
- MIT licensed. Copyright 2007-2018 Peter Cooper.
87
+ MIT licensed. Copyright 2007-2024 Peter Cooper.
data/bitarray.gemspec CHANGED
@@ -11,6 +11,7 @@ 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
17
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
@@ -0,0 +1,58 @@
1
+ class BitArray
2
+ attr_reader :field, :reverse_byte, :size
3
+ include Enumerable
4
+
5
+ VERSION = "1.3.1"
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
+ # Use Brian Kernighan's way, see
50
+ # https://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetKernighan
51
+ def total_set
52
+ @field.each_byte.inject(0) { |a, byte| (a += 1; byte &= byte - 1) while byte > 0 ; a }
53
+ end
54
+
55
+ private def byte_position(position)
56
+ @reverse_byte ? position : 7 - position
57
+ end
58
+ end
data/lib/bitarray.rb CHANGED
@@ -1,51 +1 @@
1
- class BitArray
2
- attr_reader :size
3
- attr_reader :field
4
- include Enumerable
5
-
6
- VERSION = "1.2.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(&block)
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
- # 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)
44
- def total_set
45
- @field.bytes.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
46
- end
47
-
48
- def byte_position(position)
49
- @reverse_byte ? position : 7 - position
50
- end
51
- end
1
+ require_relative "bitarray/bit_array"
@@ -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.1
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: 2024-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -47,18 +47,19 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - ".gitignore"
49
49
  - Gemfile
50
- - Gemfile.lock
50
+ - LICENSE
51
51
  - README.md
52
52
  - Rakefile
53
53
  - bitarray.gemspec
54
- - lib/bitarray-array.rb
55
54
  - lib/bitarray.rb
55
+ - lib/bitarray/bit_array.rb
56
56
  - memory-test.rb
57
- - test/test_bitarray.rb
57
+ - test/test_bit_array.rb
58
58
  homepage: https://github.com/peterc/bitarray
59
- licenses: []
59
+ licenses:
60
+ - MIT
60
61
  metadata: {}
61
- post_install_message:
62
+ post_install_message:
62
63
  rdoc_options: []
63
64
  require_paths:
64
65
  - lib
@@ -73,10 +74,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
73
74
  - !ruby/object:Gem::Version
74
75
  version: '0'
75
76
  requirements: []
76
- rubyforge_project:
77
- rubygems_version: 2.6.10
78
- signing_key:
77
+ rubygems_version: 3.5.3
78
+ signing_key:
79
79
  specification_version: 4
80
80
  summary: A simple, pure Ruby bit-array / bitfield implementation.
81
81
  test_files:
82
- - test/test_bitarray.rb
82
+ - test/test_bit_array.rb
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
@@ -1,43 +0,0 @@
1
- class BitArray
2
- attr_reader :size
3
- attr_reader :field
4
- include Enumerable
5
-
6
- VERSION = "1.2.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(&block)
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
- @field.collect{|ea| ("%0#{ELEMENT_WIDTH}b" % ea).reverse}.join[0..@size-1]
36
- end
37
-
38
- # 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)
40
- def total_set
41
- @field.inject(0) { |a, byte| a += byte & 1 and byte >>= 1 until byte == 0; a }
42
- end
43
- end