bitarray 1.3.0 → 1.3.1
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 +4 -4
 - data/LICENSE +21 -0
 - data/README.md +6 -4
 - data/bitarray.gemspec +1 -0
 - data/lib/bitarray/bit_array.rb +58 -0
 - data/lib/bitarray.rb +1 -59
 - metadata +9 -7
 - data/lib/bitarray-array.rb +0 -51
 - /data/test/{test_bitarray.rb → test_bit_array.rb} +0 -0
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            SHA256:
         
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 5f87879a065d489418062f6fb66b5fdf3e9b07360e60491ed7a9908a2c0ac59f
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: eec43c2f9a2589e0d3ca49888229eb54cbf37195f5ea5d2ecd237faca24753a9
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 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,8 +1,10 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            # BitArray: Pure Ruby bit-array/bitfield library
         
     | 
| 
       2 
2 
     | 
    
         | 
| 
       3 
     | 
    
         
            -
            A simple, pure-Ruby 'bit field' object. 
     | 
| 
      
 3 
     | 
    
         
            +
            A simple, pure-Ruby 'bit field' object.
         
     | 
| 
       4 
4 
     | 
    
         | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
      
 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 
     | 
    
         | 
| 
         @@ -61,8 +63,8 @@ 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)
         
     | 
| 
       66 
68 
     | 
    
         
             
            - 1.3 in 2022 (cleanups and a minor perf tweak)
         
     | 
| 
       67 
69 
     | 
    
         
             
            - 1.2 in 2018 (Added option to skip reverse the bits for each byte by @dalibor)
         
     | 
| 
       68 
70 
     | 
    
         
             
            - 1.1 in 2018 (fixed a significant bug)
         
     | 
| 
         @@ -82,4 +84,4 @@ Further thanks to @tdeo, @JoshuaSP, @dalibor, @yegct and @m1lt0n for pull reques 
     | 
|
| 
       82 
84 
     | 
    
         | 
| 
       83 
85 
     | 
    
         
             
            ## License
         
     | 
| 
       84 
86 
     | 
    
         | 
| 
       85 
     | 
    
         
            -
            MIT licensed. Copyright 2007- 
     | 
| 
      
 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,59 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
             
     | 
| 
       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,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: bitarray
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 1.3. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.3.1
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Peter Cooper
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire:
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date:  
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2024-02-11 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: rake
         
     | 
| 
         @@ -47,15 +47,17 @@ extra_rdoc_files: [] 
     | 
|
| 
       47 
47 
     | 
    
         
             
            files:
         
     | 
| 
       48 
48 
     | 
    
         
             
            - ".gitignore"
         
     | 
| 
       49 
49 
     | 
    
         
             
            - Gemfile
         
     | 
| 
      
 50 
     | 
    
         
            +
            - LICENSE
         
     | 
| 
       50 
51 
     | 
    
         
             
            - README.md
         
     | 
| 
       51 
52 
     | 
    
         
             
            - Rakefile
         
     | 
| 
       52 
53 
     | 
    
         
             
            - bitarray.gemspec
         
     | 
| 
       53 
     | 
    
         
            -
            - lib/bitarray-array.rb
         
     | 
| 
       54 
54 
     | 
    
         
             
            - lib/bitarray.rb
         
     | 
| 
      
 55 
     | 
    
         
            +
            - lib/bitarray/bit_array.rb
         
     | 
| 
       55 
56 
     | 
    
         
             
            - memory-test.rb
         
     | 
| 
       56 
     | 
    
         
            -
            - test/ 
     | 
| 
      
 57 
     | 
    
         
            +
            - test/test_bit_array.rb
         
     | 
| 
       57 
58 
     | 
    
         
             
            homepage: https://github.com/peterc/bitarray
         
     | 
| 
       58 
     | 
    
         
            -
            licenses: 
     | 
| 
      
 59 
     | 
    
         
            +
            licenses:
         
     | 
| 
      
 60 
     | 
    
         
            +
            - MIT
         
     | 
| 
       59 
61 
     | 
    
         
             
            metadata: {}
         
     | 
| 
       60 
62 
     | 
    
         
             
            post_install_message:
         
     | 
| 
       61 
63 
     | 
    
         
             
            rdoc_options: []
         
     | 
| 
         @@ -72,9 +74,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       72 
74 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       73 
75 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       74 
76 
     | 
    
         
             
            requirements: []
         
     | 
| 
       75 
     | 
    
         
            -
            rubygems_version: 3. 
     | 
| 
      
 77 
     | 
    
         
            +
            rubygems_version: 3.5.3
         
     | 
| 
       76 
78 
     | 
    
         
             
            signing_key:
         
     | 
| 
       77 
79 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       78 
80 
     | 
    
         
             
            summary: A simple, pure Ruby bit-array / bitfield implementation.
         
     | 
| 
       79 
81 
     | 
    
         
             
            test_files:
         
     | 
| 
       80 
     | 
    
         
            -
            - test/ 
     | 
| 
      
 82 
     | 
    
         
            +
            - test/test_bit_array.rb
         
     | 
    
        data/lib/bitarray-array.rb
    DELETED
    
    | 
         @@ -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
         
     |