Sha256: 49ced3a8ed4b2e446bef1d206389f23fd3dc1354fe310fdab083e16d266ad1f8

Contents?: true

Size: 634 Bytes

Versions: 1

Compression:

Stored size: 634 Bytes

Contents

module BitsCount
  module Primitive

    UNPACK_PATTERN = "B*"

    class << self

      def population_count_int32(i)
        i = i - ((i >> 1) & 0x55555555)
        i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
        i = (i + (i >> 4)) & 0x0F0F0F0F
        i = i + (i >> 8)
        i = i + (i >> 16)
        i & 0x0000003F
      end


      def population_count_str(s)
        s.unpack(UNPACK_PATTERN).first.count(?1)
      end

      def population_count_map(i)
        WORDBITS[i&0xFFFF] + WORDBITS[i>>16]
      end

    end

    WORDBITS = Hash[0.upto(2**16-1).map{|i| [i, population_count_int32(i)] }].freeze
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bits_count-0.0.1 lib/bits_count/primitive.rb