Sha256: 2f3c0556312ca10e470544cebc71fa1c073824dad9815a5bb3fab730ba97cafc

Contents?: true

Size: 1.01 KB

Versions: 8

Compression:

Stored size: 1.01 KB

Contents

# frozen_string_literal: true

module DiverDown
  class Web
    # Use bitflag for a lightweight representation of a list of bound definitions on a URL.
    # Each definition is related with a unique bitflag, and when merging, the bitflag is calculated with `OR` to represent the merged definitions in a lightweight and fast.
    module BitId
      class << self
        # @param ids [Array<Integer>]
        # @return [Integer]
        def ids_to_bit_id(ids)
          ids.inject(0) { _1 | id_to_bit_id(_2) }
        end

        # @param id [Integer]
        # @return [Array<Integer>]
        def bit_id_to_ids(bit_id)
          ids = []
          shift = 0
          while bit_id.positive?
            if (bit_id & 1) == 1
              ids.push(shift + 1)
            end

            bit_id >>= 1
            shift += 1
          end
          ids
        end

        private

        # @param id [Integer]
        # @return [Integer]
        def id_to_bit_id(id)
          1 << (id - 1)
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
diver_down-0.0.1.alpha17 lib/diver_down/web/bit_id.rb
diver_down-0.0.1.alpha16 lib/diver_down/web/bit_id.rb
diver_down-0.0.1.alpha15 lib/diver_down/web/bit_id.rb
diver_down-0.0.1.alpha14 lib/diver_down/web/bit_id.rb
diver_down-0.0.1.alpha13 lib/diver_down/web/bit_id.rb
diver_down-0.0.1.alpha3 lib/diver_down/web/bit_id.rb
diver_down-0.0.1.alpha2 lib/diver_down/web/bit_id.rb
diver_down-0.0.1.alpha1 lib/diver_down/web/bit_id.rb